Project

General

Profile

1
<?php
2
/*
3
Has a database object, a codifier object and a cache object ....
4
gets requests for data (from view), codifys them, checks if there are in the cache, if not gets them from db, if yes, gets them from cache
5
places restrictions on them and sends them to model
6
also sets up the view
7

    
8
*/
9
require_once("paths.php");
10
//setting up the logger
11
require_once('./js/log4php/Logger.php');
12
Logger::configure('./js/log4php/log4php.xml');
13
$logger = Logger::getLogger("controller");
14
require_once('MYDB.php');
15
//require_once('error_handler.php');
16
//require_once('help_functions.php');
17

    
18
class Controller {
19

    
20
	private $database;
21
	private $log;
22
	private $data = null;
23
	private $chart;
24
	private $chart2;
25
	private $colors = array();
26
	private $size = 30;
27
 
28
 
29
	private $myqueries = null;
30

    
31
	function __construct($myflag = true){
32

    
33
		// this creates a logger named "Controller"
34
		$this->log = Logger::getLogger(__CLASS__);
35
		$this->database = new MYDB();
36
		
37
		if($myflag) {
38
			$this->database->loadSchema($GLOBALS['schema_file']);
39
		} else {
40
			$this->database->doConnect($GLOBALS['schema_file']);
41
		}
42

    
43
		$this->myqueries = array();
44
		$this->myqueries['mperf1'] = array();
45
		$this->myqueries['mperf1']['q'] = "select funding_lvl1, max(daysfromend) from result_projects, project where result_projects.project=project.id and funding_lvl0='FP7' group by funding_lvl1 order by funding_lvl1";	
46
		$this->myqueries['aperf1'] = array();
47
		$this->myqueries['aperf1']['q'] = "select funding_lvl1, avg(daysfromend) from result_projects, project where result_projects.project=project.id and funding_lvl0='FP7' and daysfromend>=0 group by funding_lvl1 order by funding_lvl1";	
48

    
49
		$this->myqueries['mperf2'] = array();
50
		$this->myqueries['mperf2']['q'] = "select funding_lvl2, max(daysfromend) from result_projects, project where result_projects.project=project.id and funding_lvl0='FP7' group by funding_lvl2 order by funding_lvl2";	
51
		$this->myqueries['aperf2'] = array();
52
		$this->myqueries['aperf2']['q'] = "select funding_lvl2, avg(daysfromend) from result_projects, project where result_projects.project=project.id and funding_lvl0='FP7'  and daysfromend>=0 group by funding_lvl2 order by funding_lvl2";	
53

    
54
		$this->myqueries['maperf1'] = array();
55
		$this->myqueries['maperf1']['q'] = "select funding_lvl1, max(daysfromend), avg(daysfromend) from result_projects, project where result_projects.project=project.id and funding_lvl0='FP7'  and daysfromend>=0 group by funding_lvl1 order by funding_lvl1";	
56

    
57
		$this->myqueries['maperf2'] = array();
58
		$this->myqueries['maperf2']['q'] = "select funding_lvl2, max(daysfromend), avg(daysfromend) from result_projects, project where result_projects.project=project.id and funding_lvl0='FP7'  and daysfromend>=0 group by funding_lvl2 order by funding_lvl2";	
59

    
60
                $this->myqueries['pubsperf1'] = array();
61
                $this->myqueries['pubsperf1']['q'] = "select r.year, count(distinct p.id) as field0, funding_lvl1 from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id where r.year >= 2007 and funding_lvl0='FP7' and r.year < 2015 group by funding_lvl1, r.year order by r.year, funding_lvl1";
62
		$this->myqueries['pubsperf2'] = array();
63

    
64
//		$this->myqueries['pubsperf2']['q'] = "select r.year, count(distinct r.id) as field0, funding_lvl2 from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id where extract(year from date(r.date)) >= 2007 and date <> '' and funding_lvl0='FP7' and funding_lvl2 <> '' and date(r.date) < 2014 group by funding_lvl2, extract(year from date(r.date)) order by  extract(year from date(r.date)), funding_lvl2 ";
65

    
66
$this->myqueries['pubsperf2']['q'] ="  select r.year, count(distinct r.id) as field0, funding_lvl2 from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id where r.year  >= 2007  and funding_lvl0='FP7' and funding_lvl2 <> '' and r.year <  2015 group by funding_lvl2, r.year order by  r.year, funding_lvl2 ";
67
	
68

    
69
$this->myqueries['pubsperf1_double'] = array();
70

    
71
$this->myqueries['pubsperf1_double']['q'] = "select funding_lvl1, count(sq.id), sum((case when access_mode='Open Access' then 1 else 0 end)) as open from (select distinct r.id, access_mode, funding_lvl1 from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id where  funding_lvl0='FP7' and type='publication') as sq group by sq.funding_lvl1 order by funding_lvl1";
72

    
73
		
74

    
75

    
76
$this->myqueries['pubsperf2_double'] = array();
77
		$this->myqueries['pubsperf2_double']['q'] = "select funding_lvl2, count(sq.id), sum((case when access_mode='Open Access' then 1 else 0 end)) as open from (select distinct r.id, access_mode, funding_lvl2 from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id where  funding_lvl0='FP7' and funding_lvl2 <> ''  and type='publication') as sq group by sq.funding_lvl2 order by funding_lvl2";
78

    
79
		$this->myqueries['pubsperf1_total'] = array();
80
		$this->myqueries['pubsperf1_total']['q'] = "select funding_lvl1, count(sq.id) from (select distinct r.id, access_mode, funding_lvl1 from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id where  funding_lvl0='FP7' and type='publication') as sq group by sq.funding_lvl1 order by funding_lvl1";
81

    
82
		$this->myqueries['pubsperf2_total'] = array();
83
		$this->myqueries['pubsperf2_total']['q'] = "select funding_lvl2, count(sq.id) from (select distinct r.id, access_mode, funding_lvl2 from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id where  funding_lvl0='FP7' and funding_lvl2 <> '' and type='publication') as sq group by sq.funding_lvl2 order by funding_lvl2";
84

    
85
		$this->myquerie['allproj'] = array();
86
		$this->myqueries['allproj']['q'] = "select acronym, startdate, enddate, funding_lvl1, funding_lvl2, numpubs, delayedpubs, case when daysforlastpub>0 then daysforlastpub else 0 end as days from project where funding_lvl0='FP7' and numpubs>0 and enddate!='' and cast(enddate as date) < CURRENT_DATE  order by days desc, funding_lvl1, funding_lvl2";
87

    
88
		$this->myqueries['erctime'] = array();
89

    
90
 $this->myqueries['erctime']['q'] = "select r.year, count(r.id)  from result r natural join result_projects rp join project p on rp.project=p.id where type='publication' and r.year >= 2007 and r.year > 0  and funding_lvl2='ERC' and r.year < 2015 group by r.year order by r.year ; ";	
91

    
92

    
93

    
94
/* WT IS NOW A FUNDER */
95
	$this->myqueries['wttime'] = array();
96
	$this->myqueries['wttime']['q'] = " select r.year, count(r.id)  from result r natural join result_projects rp join project p on rp.project=p.id where type='publication' and r.year >= 2007 and r.year >0  and funder='Wellcome Trust' and r.year < 2015 group by r.year order by r.year;";
97
/* 
98
 $this->myqueries['wttime'] = array();
99
        $this->myqueries['wttime']['q'] = " select r.year, count(r.id)  from result r natural join result_projects rp join project p on rp.project=p.id where type='publication' and r.year >= 2007 and r.year >0  and funding_lvl0='WT' and r.year < 2015 group by r.year order by r.year;";
100

    
101
*/
102
		$this->myqueries['projpubs'] = array();
103
		$this->myqueries['projpubs']['q'] = "SELECT project.funding_lvl1 as xfield, count(distinct project.id) as field0 FROM project, project_results, result  WHERE project.project_results=project_results.id and project_results.result=result.id  and (project.funding_lvl0='FP7') and (project.haspubs='yes') and (result.type='publication')  GROUP BY project.funding_lvl1 ORDER BY project.funding_lvl1 LIMIT 30";
104

    
105
		$this->myqueries['projpubsf2'] = array();
106
		$this->myqueries['projpubsf2']['q'] = "SELECT project.funding_lvl2 as xfield, count(distinct project.id) as field0 FROM project, project_results, result  WHERE project.project_results=project_results.id and project_results.result=result.id  and (project.funding_lvl0='FP7') and (project.funding_lvl2 <> '' ) and (project.haspubs='yes') and (result.type='publication')  GROUP BY project.funding_lvl2 ORDER BY project.funding_lvl2 LIMIT 30";
107

    
108
		$this->myqueries['fp7pubsdtsrc']= array();
109
		$this->myqueries['fp7pubsdtsrc']['q'] = "select datasource.type, count(distinct result_projects.id) from result_projects, project, result_datasources, datasource, result where result.id=result_projects.id and result.type='publication' and result_projects.project=project.id and result_datasources.datasource=datasource.id and result_projects.id=result_datasources.id and funding_lvl0='FP7'group by datasource.type";
110

    
111
// ERI - TODO egi timeline has been updated- change it everywhere 
112
$this->myqueries['egiTimeline']= array();
113
$this->myqueries['egiTimeline']['q'] = "select r.year as xfield, count(r.id) as field0 from result r,result_concepts rc, concept con, context ctx, category cat where rc.id=r.id  and  con.id=rc.concept and con.category= cat.id and lower(cat.context)= lower(ctx.name)  and cat.context ='egi' and r.year>0 group by r.year order by r.year asc;";
114

    
115
 $this->myqueries['egiProjects']= array();
116
		$this->myqueries['egiProjects']['q'] = "select c.name as xfield, count(distinct rc.id) as field0 from result_concepts rc join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.id='egi::projects' group by c.name order by c.name;";
117

    
118
$this->myqueries['egiVO']= array();
119
		$this->myqueries['egiVO']['q'] = "select c.name as xfield, count(distinct rc.id) as field0 from result_concepts rc join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.id='egi::virtual' group by c.name order by c.name;";
120
		
121
$this->myqueries['egiOA']= array();
122
$this->myqueries['egiOA']['q'] = "select bestlicense as xfield, count(distinct r.id) as field0 from result r join result_concepts rc on r.id=rc.id join concept c on c.id=rc.concept group 
123
by r.bestlicense";
124

    
125

    
126
//FET queries
127

    
128
$this->myqueries['fetTimeline']= array();
129
 
130
$this->myqueries['fetTimeline']['q'] = "select r.year as xfield, count(distinct r.id) as field0 from result r,result_concepts rc, concept con, category cat where rc.id=r.id  and  con.id =rc.concept and con.category= cat.id and cat.context like 'fet%' and r.year >0 group by r.year order by r.year asc;"; 
131
$this->myqueries['fetFlagships']= array();
132

    
133
                $this->myqueries['fetFlagships']['q'] = "select  c.name as  xfield , count ( distinct r.id) as   field0  from result r,result_concepts rc , project p,concept c ,result_projects rp where rc.id =r.id and r.id=rp.id and rp.project=p.id and c.category ='fet-fp7::flagship' and rc.concept=c.id group by p.id , c.name  order by count ( distinct r.id) desc limit 3;";
134

    
135

    
136

    
137
$this->myqueries['fetProactive']= array();
138

    
139
$this->myqueries['fetProactive']['q'] = "select  c.name as  xfield , count ( distinct r.id) as   field0  from result r,result_concepts rc , project p,concept c ,result_projects rp where rc.id =r.id and r.id=rp.id and rp.project=p.id and c.category ='fet-fp7::proactive' and rc.concept=c.id  group by p.id , c.name  order by count ( distinct r.id) desc limit 5;";
140

    
141

    
142

    
143
$this->myqueries['fetOA']= array();
144
$this->myqueries['fetOA']['q'] = "select bestlicense as xfield, count(distinct r.id) as field0 from result r join result_concepts rc on r.id=rc.id join concept c on c.id=rc.concept join category cat on cat.id=c.category  where cat.id like 'fet%' group by bestlicense ; ";
145

    
146
$this->myqueries['fetOpen']= array();
147

    
148
$this->myqueries['fetOpen']['q'] = "select  c.name as  xfield , count ( distinct r.id) as   field0  from result r,result_concepts rc , project p,concept c ,result_projects rp where rc.id =r.id and r.id=rp.id and rp.project=p.id and c.category ='fet-fp7::open' and rc.concept=c.id  group by p.id , c.name  order by count ( distinct r.id) desc limit 5;";
149

    
150

    
151

    
152

    
153

    
154

    
155
/*TODO added WT pie query - copy to beta and prod*/
156
/*
157
$this->myqueries['wtOA']= array();
158
$this->myqueries['wtOA']['q'] =" SELECT bestlicense as xfield,count(distinct result_projects.id)  as field0   FROM result,result_projects, project WHERE result.result_projects= result_projects.id  AND  result_projects.project = project.id and funding_lvl0 = 'WT' and type='publication'  group by bestlicense;";
159
*/
160

    
161
$this->myqueries['ercOA']= array();
162
$this->myqueries['ercOA']['q'] ="SELECT bestlicense as xfield,count(distinct result_projects.id)  as field0   FROM result,result_projects, project WHERE result.result_projects= result_projects.id  AND  result_projects.project = project.id and funding_lvl2 = 'ERC' and type='publication'  group by bestlicense";
163

    
164

    
165

    
166
/*
167
WT IS NOW A FUNDER !!!
168
*/
169

    
170
$this->myqueries['wtOA']= array();
171
$this->myqueries['wtOA']['q'] =" SELECT bestlicense as xfield,count(distinct result_projects.id)  as field0   FROM result,result_projects, project WHERE result.result_projects= result_projects.id  AND  result_projects.project = project.id and funder = 'Wellcome Trust' and type='publication'  group by bestlicense;";
172

    
173

    
174
/* FCT*/
175
/*
176

    
177
 $this->myqueries['fcttime'] = array();
178

    
179
 $this->myqueries['fcttime']['q'] = "select r.year, count( distinct r.id) from result r natural join result_projects rp join project p on rp.project=p.id where type='publication' and  r.year >= 2007 and r.year < 2015  and funding_lvl0='FCT'  group by r.year order by r.year";
180

    
181

    
182
$this->myqueries['fctOA']= array();
183
$this->myqueries['fctOA']['q'] ="SELECT bestlicense as xfield,count(distinct result_projects.id)  as field0   FROM result,result_projects, project WHERE result.result_projects= result_projects.id  AND  result_projects.project = project.id and funding_lvl0 = 'FCT' and type='publication'  group by bestlicense";
184

    
185

    
186
$this->myqueries['fctfp7time']= array();
187

    
188
$this->myqueries['fctfp7time']['q']="select r.year, count(distinct r.id) from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id  join result_projects rp2 on rp2.id=r.id  join  project p2 on rp2.project=p2.id  where p2.funding_lvl0='FCT' and p.funding_lvl0='FP7'  and r.year >= 2007 and r.year < 2015 group by r.year  order by r.year ;";
189

    
190

    
191
*/
192

    
193

    
194
/* FCT FOR NEW FUNDERS*/
195

    
196

    
197
 $this->myqueries['fcttime'] = array();
198

    
199
 $this->myqueries['fcttime']['q'] = "select r.year, count( distinct r.id) from result r natural join result_projects rp join project p on rp.project=p.id where type='publication' and  r.year >= 2007 and r.year < 2015  and  funder = 'FCT'  group by r.year order by r.year";
200

    
201
$this->myqueries['fctOA']= array();
202
$this->myqueries['fctOA']['q'] ="SELECT bestlicense as xfield,count(distinct result_projects.id)  as field0   FROM result,result_projects, project WHERE result.result_projects= result_projects.id  AND  result_projects.project = project.id and  funder='FCT'  and type='publication'  group by bestlicense";
203

    
204

    
205
$this->myqueries['fctfp7time']= array();
206

    
207
$this->myqueries['fctfp7time']['q']="select r.year, count(distinct r.id) from result r join result_projects rp on r.id=rp.id join project p on rp.project=p.id  join result_projects rp2 on rp2.id=r.id  join  project p2 on rp2.project=p2.id  where p2.funder='FCT' and p.funding_lvl0='FP7'  and r.year >= 2007 and r.year < 2015 group by r.year  order by r.year ;";
208

    
209

    
210
$this->myqueries['dtscrtype']= array();
211

    
212
$this->myqueries['dtscrtype']['q']="select d.type,count ( distinct r.id) from datasource d, result r, result_datasources rd, project p, project_results pr where p.funding_lvl0='FP7' and r.type='publication' and p.id=pr.id and r.id=pr.result and d.id= rd.datasource and r.id=rd.id group by d.type;";
213

    
214

    
215

    
216

    
217

    
218
// FOR DATASOURCE AD PROJCT VIEW PAGES
219

    
220

    
221

    
222
$this->myqueries['dtsrcPubs']['q'] =array();
223
$this->myqueries['dtsrcPubs']['q'] = "select result_classifications.type as xfield, count( distinct result.id) as field0 FROM result  ,result_datasources, datasource,result_classifications where datasource.id='DTSRC_NAME' and result_datasources.datasource=datasource.id and result_datasources.id=result.id and result.id=result_classifications.id GROUP BY result_classifications.type ;" ;
224

    
225
$this->myqueries['dtsrcYear']['q'] = array();
226

    
227
$this->myqueries['dtsrcYear']['q'] = "select result.year as xfield, count( distinct  result.id) as field0 FROM result,result_datasources, datasource  where datasource.id='DTSRC_NAME' and result_datasources.datasource=datasource.id and result_datasources .id=result.id and  result.year >= 1995 and result.year <= 2014  GROUP BY  result.year  order by result.year asc;";
228

    
229
$this->myqueries['projScient']['q'] = array();
230
$this->myqueries['projScient']['q']="select r.year as xfield, count( distinct r.id) as field0 from result r  , result_projects rp, project p  where r.id=rp.id and p.id =rp.project and r.year>=p.start_year and r.year<=p.end_year  and r.type='publication' and  p.id=  'PROJ_TITLE' group by r.year;";
231

    
232

    
233
$this->myqueries['dtsrcPubsFund']['q'] = array();
234
$this->myqueries['dtsrcPubsFund']['q']=" select p.funder as xfield, count( distinct  result.id) as field0 FROM result,result_datasources, datasource, project p, project_results pr  where datasource.id='DTSRC_NAME' and result_datasources.datasource=datasource.id and result_datasources .id=result.id and p.id=pr.id and pr.result=result.id  GROUP BY  p.funder  order by count( distinct  result.id) desc ;";
235
$this->myqueries['dtsrcProjPubs']['q'] = array();
236

    
237
$this->myqueries['dtsrcProjPubs']['q'] =" select p.title as xfield, count( distinct  result.id) as field0 FROM result,result_datasources, datasource, project p, project_results pr  where datasource.id='DTSRC_NAME' and result_datasources.datasource=datasource.id and result_datasources .id=result.id and p.id=pr.id and pr.result=result.id GROUP BY  p.title  order by count( distinct  result.id) desc limit 10; ";
238
$this->myqueries['dtsrcProjData']['q'] = array();
239

    
240
$this->myqueries['dtsrcProjData']['q'] =" select p.title as xfield, count( distinct  result.id) as field0 FROM result,result_datasources, datasource, project p, project_results pr  where datasource.id='DTSRC_NAME' and result_datasources.datasource=datasource.id and result_datasources .id=result.id and p.id=pr.id and pr.result=result.id  and result.type='dataset' GROUP BY  p.title  order by count( distinct  result.id) desc limit 10;";
241

    
242
$this->myqueries['projScient']['q'] = array();
243
$this->myqueries['projScient']['q']="select r.year as xfield, count( distinct r.id) as field0 from result r  , result_projects rp, project p  where r.id=rp.id and p.id =rp.project and r.year>=p.start_year and r.year<=p.end_year  and  p.id=  'PROJ_TITLE' group by r.year;";
244

    
245
$this->myqueries['projOA']['q'] = array();
246
$this->myqueries['projOA']['q'] = "select r.bestlicense  as xfield, count( distinct r.id) as field0 from result r  , result_projects rp, project p  where r.id=rp.id and p.id =rp.project and r.type='publication' and  p.id=  'PROJ_TITLE' group by r.bestlicense ;" ;
247

    
248
$this->myqueries['projPubsRepos']['q'] = array();
249
$this->myqueries['projPubsRepos']['q']="select d.name, count (distinct r.id) from result r, project_results pr, datasource d, datasource_results dr  , project p where r.id=dr.result and d.id=dr.id and p.id=pr.id and r.id=pr.result  and r.type='publication' and p.id='PROJ_TITLE' group by d.name order by count (distinct r.id) desc;";
250

    
251
$this->colors[0] = '#4572A7';
252
		$this->colors[1] = '#AA4643';
253
		$this->colors[2] = '#89A54E';
254

    
255
		$this->types[0] = 'column';
256
		$this->types[1] = 'spline';
257
		$this->types[2] = 'spline';
258

    
259
		/*set up*/
260
		$this->chart = array();
261
		$this->chart['chart'] = array();
262
		//$this->chart['exporting'] = array();
263
		$this->chart['title'] = array();
264
		$this->chart['xAxis'] = array();
265
		$this->chart['xAxis']['labels'] = array();
266
		$this->chart['xAxis']['title'] = array();
267
		$this->chart['xAxis']['categories'] = array();
268
		$this->chart['yAxis'] = array();
269
		$this->chart['tooltip'] = array();
270
		$this->chart['legend'] = array();
271
		$this->chart['series'] = array();
272
		/*more*/
273
//checkign for exporting options
274
		//$this->chart['exporting']['enabled'] = false;
275
		$this->chart['chart']['renderTo'] = 'chart';
276
		$this->chart['chart']['reflow'] = false;
277
		$this->chart['chart']['showAxes'] = true;
278
		$this->chart['chart']['zoomType'] = 'xy';
279
		//$this->chart['chart']['margin'] = 'auto';
280
		//$this->chart['chart']['marginRight'] = 70;
281
		//$this->chart['chart']['marginBottom'] = 70;
282
		//$this->chart['chart']['height'] = 450;
283
		//$this->chart['chart']['spacingBottom'] = 100;
284
		$this->chart['credits']['enabled'] = false;
285
		//$this->chart['credits']['text'] = "from OpenAIRE via HighCharts".date("d / m / Y");
286
		//$this->chart['credits']['href'] = "#";
287
		//$this->chart['credits']['position'] = array();
288
		//$this->chart['credits']['position']['y'] = -2;
289

    
290

    
291
		/*$this->chart['xAxis']['labels']['overflow'] = null;//'justify';
292
		$this->chart['xAxis']['labels']['style'] = array();
293
		$this->chart['xAxis']['labels']['style']['font-size'] = '10px';
294

    
295
		$this->chart['xAxis']['showEmpty'] = true;
296
		$this->chart['xAxis']['labels']['enabled'] = true;
297
		$this->chart['xAxis']['labels']['style'] = array();
298
		$this->chart['xAxis']['labels']['style']['font-size'] = '8px';
299
		//$this->chart['xAxis']['labels']['style']['margin-left'] = '10px';
300
		//$this->chart['xAxis']['labels']['style']['margin-right'] = '10px';*/
301
		$this->chart['xAxis']['startOnTick'] = true;
302
		$this->chart['xAxis']['endOnTick'] = true;
303
		$this->chart['xAxis']['showFirstLabel'] = true;
304
		$this->chart['xAxis']['showLastLabel'] = true;
305

    
306
		$this->chart['tooltip']['percentageDecimals'] = 1;
307
		$this->chart['tooltip']['valueDecimals'] = 1;
308
		$this->chart['legend']['layout'] = 'vertical';
309
		$this->chart['legend']['align'] = 'right';
310
		$this->chart['legend']['verticalAlign'] = 'top';
311
		$this->chart['legend']['floating'] = true;
312
		$this->chart['legend']['borderWidth'] = 0;
313
		$this->chart['legend']['x'] = -10;
314
		$this->chart['legend']['y'] = 50;
315
		$this->chart['legend']['padding'] = 3;
316
		$this->chart['legend']['itemMarginBottom'] = 5;
317

    
318
		//for scatter plots
319
		$this->chart['plotOptions'] = array();
320
		$this->chart['plotOptions']['series'] = array();
321
	        $this->chart['plotOptions']['series']['showCheckbox'] = true;
322
	        $this->chart['plotOptions']['series']['selected'] = true;
323
		$this->chart['plotOptions']['scatter'] = array();
324
		$this->chart['plotOptions']['scatter']['marker'] = array();
325
		$this->chart['plotOptions']['scatter']['marker']['radius'] = 5;
326
		$this->chart['plotOptions']['scatter']['marker']['states'] = array();
327
		$this->chart['plotOptions']['scatter']['marker']['states']['hover'] = array();
328
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['enabled'] = true;
329
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['lineColor'] = 'rgb(100,100,100)';
330
		$this->chart['plotOptions']['scatter']['states'] = array();
331
		$this->chart['plotOptions']['scatter']['states']['hover'] = array();
332
		$this->chart['plotOptions']['scatter']['states']['hover']['marker'] = array();
333
		$this->chart['plotOptions']['scatter']['states']['hover']['marker']['enabled'] = false;
334

    
335
		$this->chart['plotOptions']['area'] = array();
336
                $this->chart['plotOptions']['area']['stacking'] = null;
337

    
338
                $this->chart['plotOptions']['areaspline'] = array();
339
                $this->chart['plotOptions']['areaspline']['stacking'] = null;
340

    
341
		//for pie charts
342
		$this->chart['plotOptions']['pie'] = array();
343
		$this->chart['plotOptions']['pie']['allowPointSelect'] = true;
344
		//$this->chart['plotOptions']['pie']['size'] = '50%';
345
		$this->chart['plotOptions']['pie']['cursor'] = 'pointer';
346
		$this->chart['plotOptions']['pie']['showInLegend'] = true;
347
		$this->chart['plotOptions']['pie']['dataLabels'] = array();
348
		$this->chart['plotOptions']['pie']['dataLabels']['enabled'] = true;
349
		$this->chart['plotOptions']['pie']['dataLabels']['color'] = '#000000';
350
		$this->chart['plotOptions']['pie']['dataLabels']['connectorColor'] = '#000000';
351
		$this->chart['plotOptions']['pie']['dataLabels']['crop'] = false;
352
		$this->chart['plotOptions']['pie']['dataLabels']['distance'] = 10;
353
		//$this->chart['plotOptions']['pie']['dataLabels']['formatter'] = '';
354
		//for column
355
		$this->chart['plotOptions']['column'] = array();
356
		$this->chart['plotOptions']['column']['allowPointSelect'] = true;
357
		$this->chart['plotOptions']['column']['cursor'] = 'pointer';
358
		$this->chart['plotOptions']['column']['showInLegend'] = true;
359
		$this->chart['plotOptions']['column']['grouping'] = true;
360

    
361
		$this->chart['plotOptions']['area'] = array();
362
	}
363

    
364
	function makeQuery($viztype,$persistent) {
365
		if(!isset($_GET['data'])){
366
			$this->log->info("data param not set: ". print_r($_GET,true));
367
			return 'empty';
368
		}
369
		else {
370
			$data = $_GET['data'];
371
                      
372
	
373
                        $this->data = json_decode($data,true);//print_r($this->data);
374
			$this->log->debug("data param decoded: ". print_r($this->data,true));
375
			$cachedData = $this->data;
376
			//unset($cachedData['yaxisheaders']);
377
			//unset($cachedData['fieldsheaders']);
378
			unset($cachedData['title']);
379
			unset($cachedData['subtitle']);
380
			//unset($cachedData['xaxistitle']);
381
			unset($cachedData['theme']);
382
			unset($cachedData['xStyle']);
383
			$this->size = $this->data['size'];		
384
			return @$this->computeChartObject($viztype,$persistent);
385
		}
386
	}
387

    
388
	function computeChartObject($viztype,$persistent){
389
		//$this->log->info("DATA: ".print_r($this->data,true));
390
		if(!isset($this->data['query'])){
391
			$this->log->info("query not set");
392
//TODO here add persistent value??
393
			$this->queryResult = $this->database->getData($this->data,$persistent);
394
		}
395
		else{
396
			 
397

    
398
//TODO added here option for custom queries in project & gatasouces views
399

    
400
$this->log->info("Query set ".$this->data['query']);
401

    
402
if(isset($this->data['dtsrcName']))
403
{$this->log->info("Datasource query".$this->data['dtsrcName']);
404
$dtsrc= $this->myqueries[$this->data['query']]['q'];
405
$dtsrc=str_replace("DTSRC_NAME",$this->data['dtsrcName'],$dtsrc);
406
$this->log->info("Query for datasource ".$dtsrc);
407
$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($dtsrc,"false"));
408
}
409
else if(isset($this->data['projTitle']))
410
{$this->log->info("Project query".$this->data['projTitle']);
411
$proj= $this->myqueries[$this->data['query']]['q'];
412
$proj=str_replace("PROJ_TITLE",$this->data['projTitle'],$proj);
413
$this->log->info("Query for proj ".$proj);
414
$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($proj,"false"));
415

    
416
}
417
else
418
{$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($this->myqueries[$this->data['query']]['q'],true));
419
}
420

    
421
}
422

    
423
		$this->log->info("data from DB: ".print_r($this->queryResult,true));
424
		if($viztype=="chart"){
425
			if($this->queryResult['type'] == 'scatter'){
426
				$this->createScatterData();
427
			}
428
			else{
429
				$this->createChartData();
430
			}
431

    
432
			if(isset($this->data['in']) && count($this->data['in'])){
433
				$this->chart2 = $this->chart;
434
				for($w=0;$w<count($this->chart['series']);$w++){
435
					//$whichfield = $this->data['in'][$w]['f'];
436
					//$this->chart2 = $this->chart;
437
					$data = $this->chart2['series'][$w]['data'];
438
					for($i=1;$i<count($this->chart2['xAxis']['categories']);$i++){
439
						$data[$i] += $data[$i-1];
440
					}
441
					$this->chart2['series'][$w]['data'] = $data;
442
				}
443
				//return json_encode(array($this->chart,$this->chart2),JSON_NUMERIC_CHECK);
444
				return json_encode(array($this->chart,$this->chart2));
445
			}
446

    
447
			//return json_encode($this->chart,JSON_NUMERIC_CHECK);
448
			return json_encode($this->chart);
449
		}
450
		if($viztype=='table'){
451
			//return json_encode($this->queryResult,JSON_NUMERIC_CHECK);
452
			return json_encode($this->queryResult);
453
		}
454
	}
455

    
456
	function COM_getMeasMetadata(){
457
		if(!isset($_GET['table'])) {
458
			echo 'empty';
459
		}
460
		else{
461
			echo json_encode($this->database->getMeasMetadata($_GET['table']));
462
		}
463
	}
464

    
465
	function COM_defaultChart() {
466
		//get the default values for this table
467
		$temp = $this->database->getDefaultData($_GET['table']);
468
		if($temp == 'empty')
469
			echo json_encode("empty");
470
		else{
471
			$this->size = $temp['size'];
472
			//$this->queryResult = $temp['data'];
473
			$this->data = $temp['selectedData'];
474
			//echo json_encode($this->data,JSON_NUMERIC_CHECK);
475
			echo json_encode($this->data);
476
		}
477
		//$this->data['group'] = '';
478
		//$this->data['color'] = '';
479
		//print_r($this->queryResult);return;
480
		/*if($this->data['type'] == 'chart')
481
			$this->createChartData();
482
		else
483
			$this->createScatterData();
484
		$toreturn = array();
485
		$toreturn['chart'] = $this->chart;
486
		$toreturn['selectedData'] = $this->data;
487
		echo json_encode($toreturn,JSON_NUMERIC_CHECK);*/
488
	}
489

    
490
	function defaultChart() {
491
		//get the default values for this table
492
		$temp = $this->database->getDefaultData($_GET['table']);
493
		$this->queryResult = $temp['data'];
494
		$this->data = $temp['selectedData'];
495
		$this->data['group'] = '';
496
		$this->data['color'] = 'no';
497
		//print_r($this->queryResult);return;
498
		if($temp['type'] == 'chart')
499
			$this->createChartData($temp['type']);
500
		else
501
			$this->createScatterData('');
502
		//$this->chart['chart']['type'] = $temp['type'];
503
		//print_r($this->chart);return;
504
		//print_r($this->data);
505
		$toreturn = array();
506
		$toreturn['chart'] = $this->chart;
507
		$toreturn['selectedData'] = $this->data;
508
		//return json_encode($toreturn,JSON_NUMERIC_CHECK);
509
		return json_encode($toreturn);
510
	}
511

    
512
	function defaultChartSelections() {
513
		//get the default values for this table
514
		$temp = $this->database->getDefaultData($_GET['table']);
515
		$this->queryResult = $temp['data'];
516
		$this->data = $temp['selectedData'];
517

    
518
		//echo json_encode($this->data,JSON_NUMERIC_CHECK);
519
		echo json_encode($this->data);
520
	}
521

    
522
	function COM_getFilterData(){
523
		if(!isset($_GET['table']) || !isset($_GET['field'])) {
524
			echo 'empty';
525
		}
526
		else {
527
			echo $this->computeFilterObject($_GET['table'],$_GET['field'],true);
528
		}
529
	}
530

    
531
	function getFilterData($table, $field){
532
		return $this->computeFilterObject($table,$field,false);
533
	}
534

    
535
	function computeFilterObject($table, $field, $encode){
536
		$flds = explode("-",$field);
537
		if(count($flds)>1){
538
			$ctable = $flds[count($flds)-2];
539
			$field = $flds[count($flds)-1];
540
		}
541
		else {
542
			$ctable = $table;
543
			$field = $field;
544
		}
545
		//call mydb function
546
		if($encode)
547
			//return json_encode($this->database->getFilterData($ctable,$field),JSON_NUMERIC_CHECK);
548
			return json_encode($this->database->getFilterData($ctable,$field));
549
		else
550
			return $this->database->getFilterData($ctable,$field);
551
	}
552

    
553
	function COM_get_schema(){
554
		$facts = $this->database->getFacts();
555
		$dimensions = $this->database->getDimensions();
556
		$response = array();
557
		$menu = array();
558
		if($facts === null) {
559
			$this->log->error("there are no fact tables");
560
			echo "empty";
561
			return;
562
		}//print_r($facts);
563
		foreach($facts as $facttable){
564
			$newfact = array();
565
			$newfact['name'] = $facttable['name'];
566
			if(!isset($facttable['meas'][0]))
567
				$newfact['meas'][] = $facttable['meas'];
568
			else
569
				$newfact['meas'] = $facttable['meas'];
570
			$newfact['dim'] = array();
571
			//gia kathe dim theloume: name, type, data, kai attrib opou to attrib mporei na einai ki ayto dim
572
			//otan pigainoume se bathos stin ierarxia twn diastasewn kratame touw progonous kai an ftasoume se diastasi pou yparxei stous progonous tin agnooume
573
			foreach($facttable['dim'] as $curdim) {
574
				$newdim = array();
575
				$newdim['name'] = $curdim['name'];
576
				$newdim['type'] = $curdim['type'];
577
				//$newdim['data'] = $curdim['data'];//may not need it
578
				if(isset($curdim['view']))
579
					$newdim['view'] = $curdim['view'];
580
				if(isset($curdim['dimtable']))
581
					$newdim['dimtable'] = 'no';
582
				else{
583
					$newdim['dimtable'] = 'yes';
584
					$newdim['attrib'] = array();
585

    
586
					//need to find table $curdim['refer']['table'] from all the dimentions
587
					$tabledim = search($dimensions, 'name', $curdim['name']);
588
					//print_r($tabledim);
589
					for($i=0;$i<count($tabledim);$i++){
590
						if(isset($tabledim[$i]['type']) && $tabledim[$i]['type']=='dimension')
591
							break;
592
					}
593
					$newdim['attrib'] = $this->makeAttrList($tabledim[$i]['attrib'],$dimensions,array($curdim['name']),$facttable['name']);
594
				}
595
				//I am interested in all the attributes of this dimension
596
				array_push($newfact['dim'],$newdim);
597
			}
598
			array_push($menu,$newfact);
599
		}
600
		$response['name'] = $GLOBALS['db_name'];
601
		$response['schema'] = $menu;
602
		echo json_encode($response);
603
	}
604

    
605
	function makeAttrList($attrList, $dimensions, $ancestors,$facttable){
606
		$newList = array();
607
		if(is_array($attrList)) {
608
			foreach($attrList as $attr) {
609
				if($attr['name'] != 'id' && $attr['name']!=$facttable) {
610
					$newAttr = array();
611
					$newAttr['name'] = $attr['name'];
612
					if(isset($newAttr['view']))
613
						$newAttr['view'] = $attr['view'];
614
					$newAttr['type'] = $attr['type'];
615
					if(isset($attr['analysed']) && !in_array($attr['name'],$ancestors)){
616

    
617
						$finddim = search($dimensions, 'name', $attr['name']);
618
						$nextdim = search($finddim,'type','dimension');//print_r($nextdim);
619
						$newnewList = $this->makeAttrList($nextdim[0]['attrib'],$dimensions, array_merge($ancestors,array($attr['name'])),$facttable);
620
						$ancestors[] = $attr['name'];
621
						$newAttr['analysed'] = $attr['name'];
622
						$newAttr['attrib']= $newnewList;
623
					}
624
					else{
625
						$newAttr['attrib']= array();
626
					}
627
					array_push($newList,$newAttr);
628
				}
629
			}
630
		}
631
		return $newList;
632
	}
633

    
634

    
635
/*
636
type = color | group | ''
637
*/
638
	function createScatterData(){
639
		unset($this->chart['xAxis']['categories']);
640
		$this->chart['chart']['type'] = 'bubble';
641
		$xaxisindex = 0;
642
		$yaxisindex = 1;
643
		$left = 0;
644
		$right = 0;
645
		$fields = count($this->data['fields']);
646
		$dimindex = 1+$fields;
647
		if($this->data['group'] == '')
648
			$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['name'];
649
		else
650
			$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['agg'].'('.$this->data['xaxis']['name'].')';
651
		//arxikopoiisi
652
		//posoi yaxis??
653
		$this->chart['yAxis'] = array();
654
		$yaxis = $this->data['fields'][$fields-1]['yaxis'];
655
		$tempy = -1;
656
		for($f=0;$f<count($this->data['fields']);$f++){
657
			if($this->data['fields'][$f]['yaxis']!=$tempy+1){//new y
658
				$tempy++;
659
				$this->chart['yAxis'][$tempy] = array();
660
				$this->chart['yAxis'][$tempy]['labels'] = array();
661
				$this->chart['yAxis'][$tempy]['labels']['enabled'] = true;
662
				$this->chart['yAxis'][$tempy]['labels']['overflow'] = 'justify';
663
				//$this->chart['yAxis'][$tempy]['offset'] = 70;
664
				$this->chart['yAxis'][$tempy]['title'] = array();
665
				if($this->data['yaxisheaders'][$tempy] !=''){
666
					$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['yaxisheaders'][$tempy];
667
				}
668
				else{
669
					if($this->data['fieldsheaders'][$f] !=''){
670
						$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fieldsheaders'][$f];
671
					}
672
					else{
673
						if($this->data['group'] == '')
674
							$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['fld'];
675
						else
676
							$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
677
					}
678
				}
679
				if($right>=$left){
680
					$left++;
681
				}
682
				else{
683
					$right++;
684
					$this->chart['yAxis'][$tempy]['opposite'] = true;
685
				}
686
			}
687
			else if($this->data['yaxisheaders'][$tempy] == ""){//more in the name
688
				if($this->data['fieldsheaders'][$f] !=''){
689
					$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fieldsheaders'][$f];
690
				}
691
				else{
692
					if($this->data['group'] == '')
693
						$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['fld'];
694
					else
695
						$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
696
				}
697
			}
698
		}
699
		$this->chart['series'] = array();
700
		$this->chart['legend']['enabled'] = false;
701
		//an einai group exw ena series
702
		if(($this->data['group']  && $this->data['group'] != '') || $this->data['color'] == ''){//ena series kai diaforetiko onoma gia kathe data
703
			for($f=0;$f<$fields;$f++){
704
				$this->chart['series'][$f] = array();
705
				$this->chart['series'][$f]['data'] = array();
706
				if($this->data['fields'][$f]['yaxis']!=1)
707
					$this->chart['series'][$f]['yAxis'] = $this->data['fields'][$f]['yaxis']-1;
708
			}
709
			//for($line=0;$line<10;$line++){
710
			for($line=0;$line<count($this->queryResult['data']);$line++){
711
				for($f=0;$f<$fields;$f++){
712
					//insert a new data object to the only series
713
					$data = array();
714
					$data['x'] = $this->queryResult['data'][$line][$xaxisindex];
715
					$data['y'] = $this->queryResult['data'][$line][$yaxisindex+$f];
716
					if($this->data['group'] != ''){
717
						$data['name'] = $this->queryResult['data'][$line][$dimindex];
718
						if($this->data['fieldsheaders'][$f]=="")
719
							$this->chart['series'][$f]['name'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
720
						else
721
							$this->chart['series'][$f]['name'] = $this->data['fieldsheaders'][$f];
722
					}
723
					else{
724
						if($this->data['fieldsheaders'][$f]=="")
725
							$this->chart['series'][$f]['name'] = $this->data['fields'][$f]['fld'];
726
						else
727
							$this->chart['series'][$f]['name'] = $this->data['fieldsheaders'][$f];
728
					}
729
					$this->chart['series'][$f]['data'][] = $data;
730
				}
731
			}//print_r($this->chart['series']);
732
		}
733
		//an einai color exw ena series gia kathe timi tou dimindex
734
		else{
735
			$this->chart['legend']['enabled'] = true;
736
			$curseries = '';
737
			$seriescount = 0;
738
			//for($line=0;$line<10;$line++){
739
			for($line=0;$line<count($this->queryResult['data']);$line++){
740
				if($this->queryResult['data'][$line][$dimindex] != $curseries){
741
					$curseries = $this->queryResult['data'][$line][$dimindex];
742
					for($f=0;$f<$fields;$f++){
743
						$this->chart['series'][$seriescount+$f] = array();
744
						if($this->data['fieldsheaders'][$f]!='')
745
							$this->chart['series'][$seriescount+$f]['name'] = $this->data['fieldsheaders'][$f]." for ".$this->queryResult['data'][$line][$dimindex];
746
						else
747
							$this->chart['series'][$seriescount+$f]['name'] = $this->data['fields'][$f]['fld']." for ".$this->queryResult['data'][$line][$dimindex];
748
						$this->chart['series'][$seriescount+$f]['data'] = array();
749
					}
750
					$seriescount += $fields;
751
				}
752
				for($f=0;$f<$fields;$f++){
753
					$this->chart['series'][$seriescount-$fields+$f]['data'][] = array($this->queryResult['data'][$line][$xaxisindex],$this->queryResult['data'][$line][$yaxisindex+$f]);
754
				}
755
			}
756
		}
757

    
758
	}
759

    
760
	function createChartData(){
761
		$this->chart['xAxis']['categories'] = array();
762
		unset($this->chart['chart']['type']);
763
		$this->chart['series'] = array();
764
		$this->chart['yAxis'] = array();
765
		$xaxisindex = 0;
766
		$yaxisindex = 1;
767
		$left = 0;
768
		$right = 0;
769
		//print_r($this->queryResult['data']);
770
		$fields = count($this->data['fields']);
771
		$dimindex = 1+$fields;
772
if(isset($this->data['xaxistitle']))
773
		//if($this->data['xaxistitle']!='')
774
			$this->chart['xAxis']['title']['text'] = $this->data['xaxistitle'];
775
		else{
776
			$xaxis = explode("-",$this->data['xaxis']['name']);
777
			$this->chart['xAxis']['title']['text'] = $xaxis[count($xaxis)-1];
778
		}
779
		$this->chart['legend']['enabled'] = true;
780
		//posoi yaxis??
781
		$yaxis = $this->data['fields'][$fields-1]['yaxis'];
782
		$tempy = -1;
783
		for($f=0;$f<count($this->data['fields']);$f++){
784
			if($this->data['fields'][$f]['yaxis']!=$tempy+1){//new y
785
				$tempy++;
786
				$this->chart['yAxis'][$tempy] = array();
787
				$this->chart['yAxis'][$tempy]['labels'] = array();
788
				$this->chart['yAxis'][$tempy]['labels']['enabled'] = true;
789
				$this->chart['yAxis'][$tempy]['labels']['overflow'] = 'justify';
790
				$this->chart['yAxis'][$tempy]['min'] = 0;
791
				//$this->chart['yAxis'][$tempy]['offset'] = 70;
792
				$this->chart['yAxis'][$tempy]['title'] = array();
793
				if($this->data['yaxisheaders'][$tempy]!=''){
794
					$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['yaxisheaders'][$tempy];
795
					if($this->data['fieldsheaders'][$f] == '' && (($f<count($this->data['fields'])-1 && $this->data['fields'][$f]['yaxis']!=$this->data['fields'][$f+1]['yaxis']) || $f==count($this->data['fields'])-1)){//the fields name is not set and it is the only field in the axis
796
						$this->data['fieldsheaders'][$f] = $this->data['yaxisheaders'][$tempy];
797
					}
798
				}
799
				else{
800
					if($this->data['fieldsheaders'][$f] !=''){
801
						$this->chart['yAxis'][$tempy]['title']['text']  = $this->data['fieldsheaders'][$f];
802
					}
803
					else {
804
						$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
805
					}
806
				}
807
				if($right>=$left){
808
					$left++;
809
				}
810
				else{
811
					$right++;
812
					$this->chart['yAxis'][$tempy]['opposite'] = true;
813
				}
814
			}
815
			else if($this->data['yaxisheaders'][$tempy]==''){//more in the name
816
				if($this->data['fieldsheaders'][$f] !='')
817
					$this->chart['yAxis'][$tempy]['title']['text']  .= " / ".$this->data['fieldsheaders'][$f];
818
				else
819
					$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
820
			}
821
		}
822

    
823
		if($this->data['group'] != ''){//periptwsi analyze
824
			$this->chartDataGroup();
825
		}
826
		else{//polloi yaxis xwris group
827
			$this->chartDataMultiY();
828
		}
829

    
830
		if(count($this->chart['xAxis']['categories']) > 30){
831
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
832
			//$this->chart['xAxis']['labels']['rotation'] = 90;
833
		}//print_r($this->chart);
834
		else{
835
			$trimflag = false;
836
			for($i=0;$i<count($this->chart['xAxis']['categories']);$i++){
837
				if(is_string($this->chart['xAxis']['categories'][$i]) && strlen($this->chart['xAxis']['categories'][$i])>10){
838
					$trimflag = true;
839
					break;
840
				}
841
			}
842
			if($trimflag){
843
				$this->chart['xAxis']['labels']['formatter'] = 'cond-trim';
844
				//$this->chart['xAxis']['labels']['rotation'] = 90;
845
			}
846
		}
847
	}
848

    
849
	function chartDataGroup(){
850
		//ena series gia kathe field gia kathe timi tou groupdim
851
		//read data
852
		$res = $this->queryResult['data'];
853
		$seriesindex = array();
854
		$names = array();
855
		$name = '';
856
		$seriescount = 0;
857
		$curseries = '';
858
		$xaxisindex = 0;
859
		$yaxisindex = 1;
860
		$fields = count($this->data['fields']);
861
		$dimindex = 1+$fields;
862

    
863
		($this->size >= count($this->queryResult['data'])) ? $num=count($this->queryResult['data']) : $num=$this->size; 
864
		//$num = $this->size;
865
		$flag = true;
866
		for($line=0;$line<$num;$line++){
867
			//list with x axis categories
868
			if(!in_array($res[$line][$xaxisindex],$this->chart['xAxis']['categories'])){
869
				if(count($this->chart['xAxis']['categories'])<$num){
870
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisindex];
871
					$flag = true;
872
				}
873
				else{
874
					$flag = false;
875
				}
876
			}
877
			else{
878
				$flag = true;
879
			}
880
			if($flag){
881
				//insert new data in currentseries or make new one
882
				if($this->queryResult['data'][$line][$dimindex] != $curseries){
883
					$curseries = $this->queryResult['data'][$line][$dimindex];
884
					for($f=0;$f<$fields;$f++){
885
						if($this->data['fieldsheaders'][$f]!="")
886
							$names[$f] = $this->data['fieldsheaders'][$f] ." for ".$curseries;
887
						else
888
							$names[$f] = $curseries;
889
						if(!isset($seriesindex[$names[$f]])){
890
							$seriesindex[$names[$f]] = array();
891
							$seriesindex[$names[$f]]['name'] = $names[$f];
892
							if($this->data['fields'][$f]['type'] !='area')
893
								$seriesindex[$names[$f]]['type'] = $this->data['fields'][$f]['type'];
894
							else
895
								$this->chart->type='area';
896
							if($this->data['fields'][$f]['yaxis']>1){
897
								$seriesindex[$names[$f]]['yAxis'] = $this->data['fields'][$f]['yaxis']-1;
898
							}
899
							$seriesindex[$names[$f]]['data'] = array();
900
							if($curseries == "UNKNOWN")
901
								$seriesindex[$names[$f]]['visible'] = false;
902

    
903
							if(isset($this->data['fields'][$f]['c']) && $this->data['fields'][$f]['c'] == true)
904
								$seriesindex[$names[$f]]['c'] = true;
905
							else
906
								$seriesindex[$names[$f]]['c'] = false;
907
						}
908
					}
909
					$seriescount += $fields;
910
				}
911
				$index = array_search($this->queryResult['data'][$line][$xaxisindex],$this->chart['xAxis']['categories']);
912
				for($f=0;$f<$fields;$f++){
913
					if($this->data['fieldsheaders'][$f]!="")
914
						$name = $this->data['fieldsheaders'][$f] . " for ".$curseries;
915
					else
916
						$name = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].") for ".$curseries;
917
					$lastindex = count($seriesindex[$name]['data']);
918
					for($t=$lastindex;$t<$index;$t++){
919
						$seriesindex[$name]['data'][$t] = 0;
920
					}
921
					$seriesindex[$name]['data'][$index] = $this->queryResult['data'][$line][$yaxisindex+$f];
922
				}
923
			}
924
		}
925
		foreach($seriesindex as $s){
926
			$this->chart['series'][] = $s;
927
		}
928
	}
929

    
930
	function chartDataMultiY(){
931
		$xaxisindex = 0;
932
		$yaxisindex = 1;
933
		//1 series gia kathe field
934
		for($i=0; $i<count($this->data['fields']);$i++){
935
			$this->chart['series'][] = array();
936
			if($this->data['fieldsheaders'][$i]!='')
937
				$this->chart['series'][$i]['name'] = $this->data['fieldsheaders'][$i];
938
			else
939
				$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['agg'].'('.$this->data['fields'][$i]['fld'].')';
940
			if($this->data['fields'][$i]['type'] != 'area')
941
				$this->chart['series'][$i]['type'] = $this->data['fields'][$i]['type'];
942
			else
943
				$this->chart->type='area';
944
			//$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yAxis'];
945

    
946
			if($this->data['fields'][$i]['yaxis']>1){
947
				$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yaxis']-1;
948
			}
949
			$this->chart['series'][$i]['data'] = array();
950
		}
951
		//read data
952
		$res = $this->queryResult['data'];
953

    
954
		($this->size >= count($this->queryResult['data'])) ? $num=count($this->queryResult['data']) : $num=$this->size; 
955
		//$num = $this->size;
956
		$flag = true;
957
		for($line=0;$line<$num;$line++){
958
			//list with x axis categories
959
			if(!in_array($res[$line][$xaxisindex],$this->chart['xAxis']['categories'])){
960
				if(count($this->chart['xAxis']['categories'])<$num){
961
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisindex];
962
					$flag = true;
963
				}
964
				else{
965
					$flag = false;
966
				}
967
			}
968
			else{
969
				$flag = true;
970
			}
971
			//insert new data in each series
972
			if($flag){
973
				for($s=0;$s<count($this->data['fields']);$s++) {
974
					if($this->data['fields'][$s]['type'] == 'pie'){
975
						$this->chart['series'][$s]['data'][$line]['name'] = $res[$line][$xaxisindex];
976
						$this->chart['series'][$s]['data'][$line]['y'] = $res[$line][$s+$yaxisindex];
977
					}
978
					else {
979
						$this->chart['series'][$s]['data'][] = $res[$line][$s+$yaxisindex];
980
					}
981
				}
982
			}
983
		}
984

    
985
		if(count($this->data['fields']) == 1)
986
			$this->chart['legend']['enabled'] = false;
987
	}
988

    
989
	function COM_performQuery() {
990
	
991
		if(!isset($_GET['query']) || $_GET['query'] == ''){
992
			$this->log->error("no query string");
993
			return;
994
		}
995

    
996

    
997
if(isset($_GET['persistent']))
998
{ $persistent=$_GET['persistent'];
999
            }
1000
else
1001
{
1002
 $persistent="true";
1003
}
1004

    
1005
                $query = urldecode($_GET['query']);
1006

    
1007
		$this->log->info("performing query: ".$query);
1008

    
1009
		$resp = json_encode($this->database->performQuery($query, $persistent));		//$this->log->info("response: ".$resp);
1010
		echo $resp;
1011
	}
1012

    
1013
	function sortSeries() {
1014
		//exoume mia series gia kathe field
1015
		//prepei na broume ayti ti series kai me basi ayti na kanoume sort kai oles tis alles kai ta categories tou xaxis
1016
		$snum = 0;
1017
		for($snum=0;$snum<$this->data['fields'];$snum++){
1018
			if($this->data['fields'][$snum]['agg']."(".$this->data['fields'][$snum]['fld'].")" == $this->data['sort'])
1019
				break;
1020
		}
1021
		//kanw sort ayto to series diatirontas ta kleidia opws einai kai meta kanw sort tis ypoloipes me basi ti seira kleidiwn aytinis
1022
		if($this->data['order'] && $this->data['order'] == 'd'){
1023
			arsort($this->chart['series'][$snum]['data']);
1024
		}
1025
		else{
1026
			asort($this->chart['series'][$snum]['data']);
1027
		}
1028
		//ypoloipes
1029
		//categories
1030
		$tempordered = array() ;
1031
		foreach (array_keys($this->chart['xAxis']['categories']) as $key) {
1032
			$tempordered[$key] = $this->chart['xAxis']['categories'][$key] ;
1033
		}
1034
		$this->chart['xAxis']['categories'] = $tempordered;
1035
		
1036
		for($i=0;$i<$this->data['fields'];$i++){
1037
			if($i!=$snum){
1038
				unset($tempordered);
1039
				$tempordered = array();
1040
				$myarray = $this->chart['series'][$i]['data'];
1041
				foreach (array_keys($myarray) as $key) {
1042
					$tempordered[$key] = $myarray[$key] ;
1043
				}
1044
				$this->chart['series'][$i]['data'] = $tempordered;
1045
			}
1046
		}
1047
	}
1048

    
1049

    
1050
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1051
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1052
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1053
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1054
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1055
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1056

    
1057
	function COM_makeQuery() {
1058
		if(!isset($_GET['data'])){
1059
			$this->log->info("data param not set: ". print_r($_GET,true));
1060
			echo 'empty';
1061
		}
1062
		else {
1063
			$viztype = $_GET['type'];
1064
			$data = $_GET['data'];
1065
			$this->data = json_decode($data,true);
1066

    
1067
			$cachedData = $this->data;
1068
			unset($cachedData['yaxisheaders']);
1069
			unset($cachedData['fieldsheaders']);
1070
			unset($cachedData['title']);
1071
			unset($cachedData['subtitle']);
1072
			unset($cachedData['xaxistitle']);
1073
			unset($cachedData['theme']);
1074
			unset($cachedData['xStyle']);
1075

    
1076
			$this->size = $this->data['size'];
1077

    
1078
//TODO added persistent  option
1079
if(!isset($_GET['persistent'])){
1080
$persistent='true';}
1081
else {
1082
$persistent=$_GET['persistent'];}
1083

    
1084
  
1085
echo $this->computeChartObject($viztype,$persistent);
1086

    
1087
		}
1088
	}
1089

    
1090
	function COM_read() {
1091
		if(!isset($_GET['facttable']) || !isset($_GET['dimensions']) || !isset($_GET['measures'])) {
1092
			echo "empty";
1093
		}
1094

    
1095
		$dims = explode(';',$_GET['dimensions']);
1096
		$meas = explode(';',$_GET['measures']);
1097
		echo $this->readtest($dims, $meas, $_GET['facttable'],$_GET['aggregation']);
1098
	
1099
	}
1100

    
1101
	function COM_check_fact() {
1102
		if(!isset($_GET['name'])) {
1103
			echo "empty";
1104
		}
1105
		else{
1106
			$res = $this->database->findTableType($_GET['name']);
1107
			if($res == false) echo 'false';
1108
			else echo $res;
1109
		}
1110
	}
1111
	
1112
	function COM_list_of_dim_names() {
1113
		$res = $this->database->getAllDims();
1114
		if($res != false)
1115
			echo json_encode($res);
1116
		else
1117
			echo "false";
1118
	}
1119

    
1120
	function COM_search_warehouse_by_dims() {
1121
		if(!isset($_GET['dims'])) {
1122
			echo 'empty';
1123
		}
1124
		else {
1125
			$res = $this->database->searchWarehousebyDims(explode(';',$_GET['dims']));
1126
			if($res == false) echo "false";
1127
			else echo $res;	
1128
		}
1129
	}
1130

    
1131
	function COM_create_new_fact_table() {
1132
		if(!isset($_GET['table']) || !isset($_GET['dims']) || !isset($_GET['meas'])) {
1133
			echo 'empty';
1134
		} 
1135
		else {
1136
			//set up the list of dims
1137
			$dims = explode(',',$_GET['dims']);
1138
			//set up the list of meas
1139
			$new_meas = rtrim($_GET['meas'],';');
1140
			$meas = explode(';',$new_meas);
1141
			//call the db function
1142
			$res = $this->database->createNewFact($_GET['table'], $dims, $meas);
1143
			if($res == false)
1144
				echo 'false';
1145
			else
1146
				echo 'true';
1147
		}
1148
	}
1149

    
1150
	function COM_list_of_fact_tables() {
1151
		$res = json_encode($this->database->getFactsNames());
1152
		if($res == null)
1153
			echo 'null';
1154
		else
1155
			echo $res;
1156
	}
1157

    
1158
	function COM_list_of_meas() {
1159
		if(!isset($_GET['table'])) {
1160
			echo 'empty';
1161
		} 
1162
		else {
1163
			$res = $this->database->findMeas($this->database->findTable($_GET['table'],'fact'));
1164
			if($res == false)
1165
				echo 'false';
1166
			else
1167
				echo json_encode($res);
1168
		}
1169
	}
1170

    
1171
	function COM_list_of_dims() {
1172
		$res = $this->database->getDims();
1173
		if($res == false)
1174
			echo 'false';
1175
		else
1176
			echo json_encode($res);
1177
	}
1178

    
1179
	function COM_del_meas() {
1180
		if(!isset($_GET['facttable']) || !isset($_GET['name'])) {
1181
			echo "empty";
1182
		}
1183
		else {
1184
			$res = $this->database->delMeas($_GET['facttable'], $_GET['name']);
1185
			if($res == true)
1186
				echo "true";
1187
			else 
1188
				echo "false";
1189
		}
1190
	}
1191

    
1192
	function COM_add_meas() {
1193
		if(!isset($_GET['facttable']) || !isset($_GET['name']) || !isset($_GET['formal_name']) || !isset($_GET['type'])) {
1194
			echo "empty";
1195
		}
1196
		else {
1197
			$res = $this->database->addMeas($_GET['facttable'], $_GET['name'], $_GET['formal_name'], $_GET['type']);
1198
			if($res == true)
1199
				echo "true";
1200
			else 
1201
				echo "false";
1202
		}
1203
	}
1204

    
1205
	function COM_meas_range_data() {
1206
		if(!isset($_GET['facttable']) || !isset($_GET['measurement'])) {
1207
			echo "empty";
1208
		}
1209
		else {
1210
			$res = $this->database->measRangeData($_GET['facttable'],$_GET['measurement']);
1211
			echo json_encode($res);
1212
		}
1213
	}
1214

    
1215
	function COM_get_dims_fields_list(){
1216
		if(!isset($_GET['table'])) {
1217
			echo "empty";
1218
		}
1219
		else {
1220
			$res = $this->database->getDimsList($_GET['table']);
1221

    
1222
			if($res == false)
1223
				echo "false";
1224
			else if($res == null)
1225
				echo "null";
1226
			else
1227
				echo json_encode($res);
1228
		}
1229
	}
1230

    
1231

    
1232
	function fetchData($table, $dim1, $dim2, $meas, $constraints = null) {
1233
		return $this->database->getJsonData($table, $dim1, $dim2, $meas, $constraints);
1234
	}
1235

    
1236
	function readData($table, $dim1, $dim2, $meas, $constraints = null) {
1237
		return $this->database->readData($table, $dim1, $dim2, $meas, $constraints);
1238
	}
1239

    
1240
	function timeload($start,$end) {
1241
		$this->database->loadTime($start,$end);
1242
	}
1243

    
1244
	function readtest($dimensions,$measures,$table,$aggr) {
1245
		$data = $this->database->readtest($dimensions,$measures,$table,$aggr);
1246
		//echo json_encode($data,JSON_NUMERIC_CHECK);
1247
		echo json_encode($data);
1248
	}
1249
	
1250
	
1251
	/////////////////////////////////////////////////////////////////////////////////////
1252

    
1253
}
1254

    
1255
/*
1256

    
1257
	function createChartDataGroup($givenType) {
1258
		//exoume anagkastika ena mono yaxis
1259
		//ena series gia kathe diaforetiki timi tou tritou pediou
1260
		if($givenType == 'chart'){
1261
			$this->chart['xAxis']['categories'] = array();
1262
			
1263
		}
1264
		else{
1265
			unset($this->chart['xAxis']['categories']);
1266
		}
1267
		$this->chart['series'] = array();
1268
		
1269
		$currentSeries = "";
1270
		$seriesCnt = -1;
1271
		$numOfColumns = count($this->queryResult[0]);
1272
		if(count($this->data['series']) == 0)
1273
			$sNameIndex = -1;
1274
		else
1275
			$sNameIndex = count($this->data['fields']) +1;
1276
			//$sNameIndex = $numOfColumns-1;
1277
		$xaxisIndex = 0;
1278
		$fStartindex = 1;
1279
		$groupindex = 2;
1280
		$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['name'];
1281
		//arxikopoiisi
1282
		$this->chart['yAxis'] = array();
1283
		$this->chart['series'] = array();
1284
		$this->chart['legend']['enabled'] = false;
1285
		//edw tha ektelestei mono mia fora <-------NA TO ALLAKSW
1286
		for($i=0; $i<count($this->data['fields']);$i++){
1287
			//gia kathe field, exoume enan yaxis kai ena series
1288
			$this->chart['yAxis'][] = array();
1289
			$this->chart['yAxis'][$i]['labels'] = array();
1290
			$this->chart['yAxis'][$i]['offset'] = 70;
1291
			//$this->chart['yAxis'][$i]['labels']['formatter'] = 'simple';
1292
			$this->chart['yAxis'][$i]['title'] = array();
1293
			$this->chart['yAxis'][$i]['title']['text'] = $this->data['fields'][$i]['fld'];
1294
			if($i>0){
1295
				$this->chart['yAxis'][$i]['opposite'] = true;
1296
				$this->chart['yAxis'][$i]['gridLineWidth'] = 0;
1297
			}
1298

    
1299
			//$this->chart['series'][] = array();
1300
			//$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['fld'];
1301
			
1302
			//if($i>0) {
1303
			//	$this->chart['series'][$i]['yAxis'] = $i;
1304
			//}
1305
			//$this->chart['series'][$i]['data'] = array();
1306
			//$seriesData[$i] = array();
1307
		}
1308
		
1309
		$res = $this->queryResult['data'];
1310
		//gia kathe diaforetiki timi tou pediou 2, kainourgio category an exoume chart
1311
		//gia kathe diaforetiki timi tou pediou 3, kainourgio series me onoma tin timi tou pediou 3
1312
		//oso eisai sto idio series, apla prosthese ta data
1313
		//an allakseis ftiakse kainourgio
1314
		for($line=0;$line<count($this->queryResult['data']);$line++){
1315
			//list with x axis categories
1316
			if($givenType == 'chart'){
1317
				if(!in_array($res[$line][$xaxisIndex],$this->chart['xAxis']['categories'])){
1318
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisIndex];
1319
				}
1320
			}
1321
			//new series? 
1322
			if($res[$line][$groupindex] != $currentSeries){
1323
				$currentSeries = $res[$line][$groupindex];
1324
				$this->chart['series'][] = array();
1325
				$seriesCnt++;
1326
				$this->chart['series'][$seriesCnt]['name'] = $currentSeries;
1327
				$this->chart['series'][$seriesCnt]['data'] = array();
1328
			}
1329
			$this->chart['series'][$seriesCnt]['data'][] = $res[$line][$fStartindex];
1330
		}
1331

    
1332
		if(count($this->chart['xAxis']['categories']) > 15){
1333
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
1334
			$this->chart['xAxis']['labels']['rotation'] = 45;
1335
		}
1336

    
1337
	}
1338
*/
1339
?>
(7-7/28)