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 extract(year from date(r.date)), 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 extract(year from date(r.date)) >= 2007 and date <> '' and funding_lvl0='FP7' and date(r.date) < now() group by funding_lvl1,  extract(year from date(r.date)) order by  extract(year from date(r.date)), funding_lvl1";
62

    
63
		$this->myqueries['pubsperf2'] = array();
64
		$this->myqueries['pubsperf2']['q'] = "select extract(year from date(r.date)), 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) < now() group by funding_lvl2,  extract(year from date(r.date)) order by  extract(year from date(r.date)), funding_lvl2";
65

    
66
		$this->myqueries['pubsperf1_double'] = array();
67
		$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";
68

    
69
		$this->myqueries['pubsperf2_double'] = array();
70
		$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";
71

    
72
		$this->myqueries['pubsperf1_total'] = array();
73
		$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";
74

    
75
		$this->myqueries['pubsperf2_total'] = array();
76
		$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";
77

    
78
		$this->myquerie['allproj'] = array();
79
		$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";
80

    
81
		$this->myqueries['erctime'] = array();
82

    
83
 $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 ; ";	
84

    
85

    
86

    
87
/* WT IS NOW A FUNDER */
88
	$this->myqueries['wttime'] = array();
89
	$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;";
90
/* 
91
 $this->myqueries['wttime'] = array();
92
        $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;";
93

    
94
*/
95
		$this->myqueries['projpubs'] = array();
96
		$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";
97

    
98
		$this->myqueries['projpubsf2'] = array();
99
		$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";
100

    
101
		$this->myqueries['fp7pubsdtsrc']= array();
102
		$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";
103

    
104
// ERI - TODO egi timeline has been updated- change it everywhere 
105
$this->myqueries['egiTimeline']= array();
106
$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;";
107

    
108
 $this->myqueries['egiProjects']= array();
109
		$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;";
110

    
111
$this->myqueries['egiVO']= array();
112
		$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;";
113
		
114
$this->myqueries['egiOA']= array();
115
$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 
116
by r.bestlicense";
117

    
118

    
119
//FET queries
120

    
121
$this->myqueries['fetTimeline']= array();
122
 
123
$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;"; 
124
$this->myqueries['fetFlagships']= array();
125

    
126
                $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;";
127

    
128

    
129

    
130
$this->myqueries['fetProactive']= array();
131

    
132
$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;";
133

    
134

    
135

    
136
$this->myqueries['fetOA']= array();
137
$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 ; ";
138

    
139
$this->myqueries['fetOpen']= array();
140

    
141
$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;";
142

    
143

    
144

    
145

    
146

    
147

    
148
/*TODO added WT pie query - copy to beta and prod*/
149
/*
150
$this->myqueries['wtOA']= array();
151
$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;";
152
*/
153

    
154
$this->myqueries['ercOA']= array();
155
$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";
156

    
157

    
158

    
159
/*
160
WT IS NOW A FUNDER !!!
161
*/
162

    
163
$this->myqueries['wtOA']= array();
164
$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;";
165

    
166

    
167
/* FCT*/
168
/*
169

    
170
 $this->myqueries['fcttime'] = array();
171

    
172
 $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";
173

    
174

    
175
$this->myqueries['fctOA']= array();
176
$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";
177

    
178

    
179
$this->myqueries['fctfp7time']= array();
180

    
181
$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 ;";
182

    
183

    
184
*/
185

    
186

    
187
/* FCT FOR NEW FUNDERS*/
188

    
189

    
190
 $this->myqueries['fcttime'] = array();
191

    
192
 $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";
193

    
194
$this->myqueries['fctOA']= array();
195
$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";
196

    
197

    
198
$this->myqueries['fctfp7time']= array();
199

    
200
$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 ;";
201

    
202
	
203

    
204
$this->colors[0] = '#4572A7';
205
		$this->colors[1] = '#AA4643';
206
		$this->colors[2] = '#89A54E';
207

    
208
		$this->types[0] = 'column';
209
		$this->types[1] = 'spline';
210
		$this->types[2] = 'spline';
211

    
212
		/*set up*/
213
		$this->chart = array();
214
		$this->chart['chart'] = array();
215
		//$this->chart['exporting'] = array();
216
		$this->chart['title'] = array();
217
		$this->chart['xAxis'] = array();
218
		$this->chart['xAxis']['labels'] = array();
219
		$this->chart['xAxis']['title'] = array();
220
		$this->chart['xAxis']['categories'] = array();
221
		$this->chart['yAxis'] = array();
222
		$this->chart['tooltip'] = array();
223
		$this->chart['legend'] = array();
224
		$this->chart['series'] = array();
225
		/*more*/
226
//checkign for exporting options
227
		//$this->chart['exporting']['enabled'] = false;
228
		$this->chart['chart']['renderTo'] = 'chart';
229
		$this->chart['chart']['reflow'] = false;
230
		$this->chart['chart']['showAxes'] = true;
231
		$this->chart['chart']['zoomType'] = 'xy';
232
		//$this->chart['chart']['margin'] = 'auto';
233
		//$this->chart['chart']['marginRight'] = 70;
234
		//$this->chart['chart']['marginBottom'] = 70;
235
		//$this->chart['chart']['height'] = 450;
236
		//$this->chart['chart']['spacingBottom'] = 100;
237
		$this->chart['credits']['enabled'] = false;
238
		//$this->chart['credits']['text'] = "from OpenAIRE via HighCharts".date("d / m / Y");
239
		//$this->chart['credits']['href'] = "#";
240
		//$this->chart['credits']['position'] = array();
241
		//$this->chart['credits']['position']['y'] = -2;
242

    
243

    
244
		/*$this->chart['xAxis']['labels']['overflow'] = null;//'justify';
245
		$this->chart['xAxis']['labels']['style'] = array();
246
		$this->chart['xAxis']['labels']['style']['font-size'] = '10px';
247

    
248
		$this->chart['xAxis']['showEmpty'] = true;
249
		$this->chart['xAxis']['labels']['enabled'] = true;
250
		$this->chart['xAxis']['labels']['style'] = array();
251
		$this->chart['xAxis']['labels']['style']['font-size'] = '8px';
252
		//$this->chart['xAxis']['labels']['style']['margin-left'] = '10px';
253
		//$this->chart['xAxis']['labels']['style']['margin-right'] = '10px';*/
254
		$this->chart['xAxis']['startOnTick'] = true;
255
		$this->chart['xAxis']['endOnTick'] = true;
256
		$this->chart['xAxis']['showFirstLabel'] = true;
257
		$this->chart['xAxis']['showLastLabel'] = true;
258

    
259
		$this->chart['tooltip']['percentageDecimals'] = 1;
260
		$this->chart['tooltip']['valueDecimals'] = 1;
261
		$this->chart['legend']['layout'] = 'vertical';
262
		$this->chart['legend']['align'] = 'right';
263
		$this->chart['legend']['verticalAlign'] = 'top';
264
		$this->chart['legend']['floating'] = true;
265
		$this->chart['legend']['borderWidth'] = 0;
266
		$this->chart['legend']['x'] = -10;
267
		$this->chart['legend']['y'] = 50;
268
		$this->chart['legend']['padding'] = 3;
269
		$this->chart['legend']['itemMarginBottom'] = 5;
270

    
271
		//for scatter plots
272
		$this->chart['plotOptions'] = array();
273
		$this->chart['plotOptions']['series'] = array();
274
	        $this->chart['plotOptions']['series']['showCheckbox'] = true;
275
	        $this->chart['plotOptions']['series']['selected'] = true;
276
		$this->chart['plotOptions']['scatter'] = array();
277
		$this->chart['plotOptions']['scatter']['marker'] = array();
278
		$this->chart['plotOptions']['scatter']['marker']['radius'] = 5;
279
		$this->chart['plotOptions']['scatter']['marker']['states'] = array();
280
		$this->chart['plotOptions']['scatter']['marker']['states']['hover'] = array();
281
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['enabled'] = true;
282
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['lineColor'] = 'rgb(100,100,100)';
283
		$this->chart['plotOptions']['scatter']['states'] = array();
284
		$this->chart['plotOptions']['scatter']['states']['hover'] = array();
285
		$this->chart['plotOptions']['scatter']['states']['hover']['marker'] = array();
286
		$this->chart['plotOptions']['scatter']['states']['hover']['marker']['enabled'] = false;
287

    
288
		$this->chart['plotOptions']['area'] = array();
289
                $this->chart['plotOptions']['area']['stacking'] = null;
290

    
291
                $this->chart['plotOptions']['areaspline'] = array();
292
                $this->chart['plotOptions']['areaspline']['stacking'] = null;
293

    
294
		//for pie charts
295
		$this->chart['plotOptions']['pie'] = array();
296
		$this->chart['plotOptions']['pie']['allowPointSelect'] = true;
297
		//$this->chart['plotOptions']['pie']['size'] = '50%';
298
		$this->chart['plotOptions']['pie']['cursor'] = 'pointer';
299
		$this->chart['plotOptions']['pie']['showInLegend'] = true;
300
		$this->chart['plotOptions']['pie']['dataLabels'] = array();
301
		$this->chart['plotOptions']['pie']['dataLabels']['enabled'] = true;
302
		$this->chart['plotOptions']['pie']['dataLabels']['color'] = '#000000';
303
		$this->chart['plotOptions']['pie']['dataLabels']['connectorColor'] = '#000000';
304
		$this->chart['plotOptions']['pie']['dataLabels']['crop'] = false;
305
		$this->chart['plotOptions']['pie']['dataLabels']['distance'] = 10;
306
		//$this->chart['plotOptions']['pie']['dataLabels']['formatter'] = '';
307
		//for column
308
		$this->chart['plotOptions']['column'] = array();
309
		$this->chart['plotOptions']['column']['allowPointSelect'] = true;
310
		$this->chart['plotOptions']['column']['cursor'] = 'pointer';
311
		$this->chart['plotOptions']['column']['showInLegend'] = true;
312
		$this->chart['plotOptions']['column']['grouping'] = true;
313

    
314
		$this->chart['plotOptions']['area'] = array();
315
	}
316

    
317
	function makeQuery($viztype,$persistent) {
318
		if(!isset($_GET['data'])){
319
			$this->log->info("data param not set: ". print_r($_GET,true));
320
			return 'empty';
321
		}
322
		else {
323
			$data = $_GET['data'];
324
                      
325
	
326
                        $this->data = json_decode($data,true);//print_r($this->data);
327
			$this->log->debug("data param decoded: ". print_r($this->data,true));
328
			$cachedData = $this->data;
329
			//unset($cachedData['yaxisheaders']);
330
			//unset($cachedData['fieldsheaders']);
331
			unset($cachedData['title']);
332
			unset($cachedData['subtitle']);
333
			//unset($cachedData['xaxistitle']);
334
			unset($cachedData['theme']);
335
			unset($cachedData['xStyle']);
336
			$this->size = $this->data['size'];		
337
			return @$this->computeChartObject($viztype,$persistent);
338
		}
339
	}
340

    
341
	function computeChartObject($viztype,$persistent){
342
		//$this->log->info("DATA: ".print_r($this->data,true));
343
		if(!isset($this->data['query'])){
344
			$this->log->info("query not set");
345
//TODO here add persistent value??
346
			$this->queryResult = $this->database->getData($this->data,$persistent);
347
		}
348
		else{
349
			$this->log->info("query set");
350
			$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($this->myqueries[$this->data['query']]['q'],true));
351

    
352
		}
353

    
354

    
355
		$this->log->info("data from DB: ".print_r($this->queryResult,true));
356
		if($viztype=="chart"){
357
			if($this->queryResult['type'] == 'scatter'){
358
				$this->createScatterData();
359
			}
360
			else{
361
				$this->createChartData();
362
			}
363

    
364
			if(isset($this->data['in']) && count($this->data['in'])){
365
				$this->chart2 = $this->chart;
366
				for($w=0;$w<count($this->chart['series']);$w++){
367
					//$whichfield = $this->data['in'][$w]['f'];
368
					//$this->chart2 = $this->chart;
369
					$data = $this->chart2['series'][$w]['data'];
370
					for($i=1;$i<count($this->chart2['xAxis']['categories']);$i++){
371
						$data[$i] += $data[$i-1];
372
					}
373
					$this->chart2['series'][$w]['data'] = $data;
374
				}
375
				//return json_encode(array($this->chart,$this->chart2),JSON_NUMERIC_CHECK);
376
				return json_encode(array($this->chart,$this->chart2));
377
			}
378

    
379
			//return json_encode($this->chart,JSON_NUMERIC_CHECK);
380
			return json_encode($this->chart);
381
		}
382
		if($viztype=='table'){
383
			//return json_encode($this->queryResult,JSON_NUMERIC_CHECK);
384
			return json_encode($this->queryResult);
385
		}
386
	}
387

    
388
	function COM_getMeasMetadata(){
389
		if(!isset($_GET['table'])) {
390
			echo 'empty';
391
		}
392
		else{
393
			echo json_encode($this->database->getMeasMetadata($_GET['table']));
394
		}
395
	}
396

    
397
	function COM_defaultChart() {
398
		//get the default values for this table
399
		$temp = $this->database->getDefaultData($_GET['table']);
400
		if($temp == 'empty')
401
			echo json_encode("empty");
402
		else{
403
			$this->size = $temp['size'];
404
			//$this->queryResult = $temp['data'];
405
			$this->data = $temp['selectedData'];
406
			//echo json_encode($this->data,JSON_NUMERIC_CHECK);
407
			echo json_encode($this->data);
408
		}
409
		//$this->data['group'] = '';
410
		//$this->data['color'] = '';
411
		//print_r($this->queryResult);return;
412
		/*if($this->data['type'] == 'chart')
413
			$this->createChartData();
414
		else
415
			$this->createScatterData();
416
		$toreturn = array();
417
		$toreturn['chart'] = $this->chart;
418
		$toreturn['selectedData'] = $this->data;
419
		echo json_encode($toreturn,JSON_NUMERIC_CHECK);*/
420
	}
421

    
422
	function defaultChart() {
423
		//get the default values for this table
424
		$temp = $this->database->getDefaultData($_GET['table']);
425
		$this->queryResult = $temp['data'];
426
		$this->data = $temp['selectedData'];
427
		$this->data['group'] = '';
428
		$this->data['color'] = 'no';
429
		//print_r($this->queryResult);return;
430
		if($temp['type'] == 'chart')
431
			$this->createChartData($temp['type']);
432
		else
433
			$this->createScatterData('');
434
		//$this->chart['chart']['type'] = $temp['type'];
435
		//print_r($this->chart);return;
436
		//print_r($this->data);
437
		$toreturn = array();
438
		$toreturn['chart'] = $this->chart;
439
		$toreturn['selectedData'] = $this->data;
440
		//return json_encode($toreturn,JSON_NUMERIC_CHECK);
441
		return json_encode($toreturn);
442
	}
443

    
444
	function defaultChartSelections() {
445
		//get the default values for this table
446
		$temp = $this->database->getDefaultData($_GET['table']);
447
		$this->queryResult = $temp['data'];
448
		$this->data = $temp['selectedData'];
449

    
450
		//echo json_encode($this->data,JSON_NUMERIC_CHECK);
451
		echo json_encode($this->data);
452
	}
453

    
454
	function COM_getFilterData(){
455
		if(!isset($_GET['table']) || !isset($_GET['field'])) {
456
			echo 'empty';
457
		}
458
		else {
459
			echo $this->computeFilterObject($_GET['table'],$_GET['field'],true);
460
		}
461
	}
462

    
463
	function getFilterData($table, $field){
464
		return $this->computeFilterObject($table,$field,false);
465
	}
466

    
467
	function computeFilterObject($table, $field, $encode){
468
		$flds = explode("-",$field);
469
		if(count($flds)>1){
470
			$ctable = $flds[count($flds)-2];
471
			$field = $flds[count($flds)-1];
472
		}
473
		else {
474
			$ctable = $table;
475
			$field = $field;
476
		}
477
		//call mydb function
478
		if($encode)
479
			//return json_encode($this->database->getFilterData($ctable,$field),JSON_NUMERIC_CHECK);
480
			return json_encode($this->database->getFilterData($ctable,$field));
481
		else
482
			return $this->database->getFilterData($ctable,$field);
483
	}
484

    
485
	function COM_get_schema(){
486
		$facts = $this->database->getFacts();
487
		$dimensions = $this->database->getDimensions();
488
		$response = array();
489
		$menu = array();
490
		if($facts === null) {
491
			$this->log->error("there are no fact tables");
492
			echo "empty";
493
			return;
494
		}//print_r($facts);
495
		foreach($facts as $facttable){
496
			$newfact = array();
497
			$newfact['name'] = $facttable['name'];
498
			if(!isset($facttable['meas'][0]))
499
				$newfact['meas'][] = $facttable['meas'];
500
			else
501
				$newfact['meas'] = $facttable['meas'];
502
			$newfact['dim'] = array();
503
			//gia kathe dim theloume: name, type, data, kai attrib opou to attrib mporei na einai ki ayto dim
504
			//otan pigainoume se bathos stin ierarxia twn diastasewn kratame touw progonous kai an ftasoume se diastasi pou yparxei stous progonous tin agnooume
505
			foreach($facttable['dim'] as $curdim) {
506
				$newdim = array();
507
				$newdim['name'] = $curdim['name'];
508
				$newdim['type'] = $curdim['type'];
509
				//$newdim['data'] = $curdim['data'];//may not need it
510
				if(isset($curdim['view']))
511
					$newdim['view'] = $curdim['view'];
512
				if(isset($curdim['dimtable']))
513
					$newdim['dimtable'] = 'no';
514
				else{
515
					$newdim['dimtable'] = 'yes';
516
					$newdim['attrib'] = array();
517

    
518
					//need to find table $curdim['refer']['table'] from all the dimentions
519
					$tabledim = search($dimensions, 'name', $curdim['name']);
520
					//print_r($tabledim);
521
					for($i=0;$i<count($tabledim);$i++){
522
						if(isset($tabledim[$i]['type']) && $tabledim[$i]['type']=='dimension')
523
							break;
524
					}
525
					$newdim['attrib'] = $this->makeAttrList($tabledim[$i]['attrib'],$dimensions,array($curdim['name']),$facttable['name']);
526
				}
527
				//I am interested in all the attributes of this dimension
528
				array_push($newfact['dim'],$newdim);
529
			}
530
			array_push($menu,$newfact);
531
		}
532
		$response['name'] = $GLOBALS['db_name'];
533
		$response['schema'] = $menu;
534
		echo json_encode($response);
535
	}
536

    
537
	function makeAttrList($attrList, $dimensions, $ancestors,$facttable){
538
		$newList = array();
539
		if(is_array($attrList)) {
540
			foreach($attrList as $attr) {
541
				if($attr['name'] != 'id' && $attr['name']!=$facttable) {
542
					$newAttr = array();
543
					$newAttr['name'] = $attr['name'];
544
					if(isset($newAttr['view']))
545
						$newAttr['view'] = $attr['view'];
546
					$newAttr['type'] = $attr['type'];
547
					if(isset($attr['analysed']) && !in_array($attr['name'],$ancestors)){
548

    
549
						$finddim = search($dimensions, 'name', $attr['name']);
550
						$nextdim = search($finddim,'type','dimension');//print_r($nextdim);
551
						$newnewList = $this->makeAttrList($nextdim[0]['attrib'],$dimensions, array_merge($ancestors,array($attr['name'])),$facttable);
552
						$ancestors[] = $attr['name'];
553
						$newAttr['analysed'] = $attr['name'];
554
						$newAttr['attrib']= $newnewList;
555
					}
556
					else{
557
						$newAttr['attrib']= array();
558
					}
559
					array_push($newList,$newAttr);
560
				}
561
			}
562
		}
563
		return $newList;
564
	}
565

    
566

    
567
/*
568
type = color | group | ''
569
*/
570
	function createScatterData(){
571
		unset($this->chart['xAxis']['categories']);
572
		$this->chart['chart']['type'] = 'bubble';
573
		$xaxisindex = 0;
574
		$yaxisindex = 1;
575
		$left = 0;
576
		$right = 0;
577
		$fields = count($this->data['fields']);
578
		$dimindex = 1+$fields;
579
		if($this->data['group'] == '')
580
			$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['name'];
581
		else
582
			$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['agg'].'('.$this->data['xaxis']['name'].')';
583
		//arxikopoiisi
584
		//posoi yaxis??
585
		$this->chart['yAxis'] = array();
586
		$yaxis = $this->data['fields'][$fields-1]['yaxis'];
587
		$tempy = -1;
588
		for($f=0;$f<count($this->data['fields']);$f++){
589
			if($this->data['fields'][$f]['yaxis']!=$tempy+1){//new y
590
				$tempy++;
591
				$this->chart['yAxis'][$tempy] = array();
592
				$this->chart['yAxis'][$tempy]['labels'] = array();
593
				$this->chart['yAxis'][$tempy]['labels']['enabled'] = true;
594
				$this->chart['yAxis'][$tempy]['labels']['overflow'] = 'justify';
595
				//$this->chart['yAxis'][$tempy]['offset'] = 70;
596
				$this->chart['yAxis'][$tempy]['title'] = array();
597
				if($this->data['yaxisheaders'][$tempy] !=''){
598
					$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['yaxisheaders'][$tempy];
599
				}
600
				else{
601
					if($this->data['fieldsheaders'][$f] !=''){
602
						$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fieldsheaders'][$f];
603
					}
604
					else{
605
						if($this->data['group'] == '')
606
							$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['fld'];
607
						else
608
							$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
609
					}
610
				}
611
				if($right>=$left){
612
					$left++;
613
				}
614
				else{
615
					$right++;
616
					$this->chart['yAxis'][$tempy]['opposite'] = true;
617
				}
618
			}
619
			else if($this->data['yaxisheaders'][$tempy] == ""){//more in the name
620
				if($this->data['fieldsheaders'][$f] !=''){
621
					$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fieldsheaders'][$f];
622
				}
623
				else{
624
					if($this->data['group'] == '')
625
						$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['fld'];
626
					else
627
						$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
628
				}
629
			}
630
		}
631
		$this->chart['series'] = array();
632
		$this->chart['legend']['enabled'] = false;
633
		//an einai group exw ena series
634
		if(($this->data['group']  && $this->data['group'] != '') || $this->data['color'] == ''){//ena series kai diaforetiko onoma gia kathe data
635
			for($f=0;$f<$fields;$f++){
636
				$this->chart['series'][$f] = array();
637
				$this->chart['series'][$f]['data'] = array();
638
				if($this->data['fields'][$f]['yaxis']!=1)
639
					$this->chart['series'][$f]['yAxis'] = $this->data['fields'][$f]['yaxis']-1;
640
			}
641
			//for($line=0;$line<10;$line++){
642
			for($line=0;$line<count($this->queryResult['data']);$line++){
643
				for($f=0;$f<$fields;$f++){
644
					//insert a new data object to the only series
645
					$data = array();
646
					$data['x'] = $this->queryResult['data'][$line][$xaxisindex];
647
					$data['y'] = $this->queryResult['data'][$line][$yaxisindex+$f];
648
					if($this->data['group'] != ''){
649
						$data['name'] = $this->queryResult['data'][$line][$dimindex];
650
						if($this->data['fieldsheaders'][$f]=="")
651
							$this->chart['series'][$f]['name'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
652
						else
653
							$this->chart['series'][$f]['name'] = $this->data['fieldsheaders'][$f];
654
					}
655
					else{
656
						if($this->data['fieldsheaders'][$f]=="")
657
							$this->chart['series'][$f]['name'] = $this->data['fields'][$f]['fld'];
658
						else
659
							$this->chart['series'][$f]['name'] = $this->data['fieldsheaders'][$f];
660
					}
661
					$this->chart['series'][$f]['data'][] = $data;
662
				}
663
			}//print_r($this->chart['series']);
664
		}
665
		//an einai color exw ena series gia kathe timi tou dimindex
666
		else{
667
			$this->chart['legend']['enabled'] = true;
668
			$curseries = '';
669
			$seriescount = 0;
670
			//for($line=0;$line<10;$line++){
671
			for($line=0;$line<count($this->queryResult['data']);$line++){
672
				if($this->queryResult['data'][$line][$dimindex] != $curseries){
673
					$curseries = $this->queryResult['data'][$line][$dimindex];
674
					for($f=0;$f<$fields;$f++){
675
						$this->chart['series'][$seriescount+$f] = array();
676
						if($this->data['fieldsheaders'][$f]!='')
677
							$this->chart['series'][$seriescount+$f]['name'] = $this->data['fieldsheaders'][$f]." for ".$this->queryResult['data'][$line][$dimindex];
678
						else
679
							$this->chart['series'][$seriescount+$f]['name'] = $this->data['fields'][$f]['fld']." for ".$this->queryResult['data'][$line][$dimindex];
680
						$this->chart['series'][$seriescount+$f]['data'] = array();
681
					}
682
					$seriescount += $fields;
683
				}
684
				for($f=0;$f<$fields;$f++){
685
					$this->chart['series'][$seriescount-$fields+$f]['data'][] = array($this->queryResult['data'][$line][$xaxisindex],$this->queryResult['data'][$line][$yaxisindex+$f]);
686
				}
687
			}
688
		}
689

    
690
	}
691

    
692
	function createChartData(){
693
		$this->chart['xAxis']['categories'] = array();
694
		unset($this->chart['chart']['type']);
695
		$this->chart['series'] = array();
696
		$this->chart['yAxis'] = array();
697
		$xaxisindex = 0;
698
		$yaxisindex = 1;
699
		$left = 0;
700
		$right = 0;
701
		//print_r($this->queryResult['data']);
702
		$fields = count($this->data['fields']);
703
		$dimindex = 1+$fields;
704
if(isset($this->data['xaxistitle']))
705
		//if($this->data['xaxistitle']!='')
706
			$this->chart['xAxis']['title']['text'] = $this->data['xaxistitle'];
707
		else{
708
			$xaxis = explode("-",$this->data['xaxis']['name']);
709
			$this->chart['xAxis']['title']['text'] = $xaxis[count($xaxis)-1];
710
		}
711
		$this->chart['legend']['enabled'] = true;
712
		//posoi yaxis??
713
		$yaxis = $this->data['fields'][$fields-1]['yaxis'];
714
		$tempy = -1;
715
		for($f=0;$f<count($this->data['fields']);$f++){
716
			if($this->data['fields'][$f]['yaxis']!=$tempy+1){//new y
717
				$tempy++;
718
				$this->chart['yAxis'][$tempy] = array();
719
				$this->chart['yAxis'][$tempy]['labels'] = array();
720
				$this->chart['yAxis'][$tempy]['labels']['enabled'] = true;
721
				$this->chart['yAxis'][$tempy]['labels']['overflow'] = 'justify';
722
				$this->chart['yAxis'][$tempy]['min'] = 0;
723
				//$this->chart['yAxis'][$tempy]['offset'] = 70;
724
				$this->chart['yAxis'][$tempy]['title'] = array();
725
				if($this->data['yaxisheaders'][$tempy]!=''){
726
					$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['yaxisheaders'][$tempy];
727
					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
728
						$this->data['fieldsheaders'][$f] = $this->data['yaxisheaders'][$tempy];
729
					}
730
				}
731
				else{
732
					if($this->data['fieldsheaders'][$f] !=''){
733
						$this->chart['yAxis'][$tempy]['title']['text']  = $this->data['fieldsheaders'][$f];
734
					}
735
					else {
736
						$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
737
					}
738
				}
739
				if($right>=$left){
740
					$left++;
741
				}
742
				else{
743
					$right++;
744
					$this->chart['yAxis'][$tempy]['opposite'] = true;
745
				}
746
			}
747
			else if($this->data['yaxisheaders'][$tempy]==''){//more in the name
748
				if($this->data['fieldsheaders'][$f] !='')
749
					$this->chart['yAxis'][$tempy]['title']['text']  .= " / ".$this->data['fieldsheaders'][$f];
750
				else
751
					$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
752
			}
753
		}
754

    
755
		if($this->data['group'] != ''){//periptwsi analyze
756
			$this->chartDataGroup();
757
		}
758
		else{//polloi yaxis xwris group
759
			$this->chartDataMultiY();
760
		}
761

    
762
		if(count($this->chart['xAxis']['categories']) > 30){
763
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
764
			//$this->chart['xAxis']['labels']['rotation'] = 90;
765
		}//print_r($this->chart);
766
		else{
767
			$trimflag = false;
768
			for($i=0;$i<count($this->chart['xAxis']['categories']);$i++){
769
				if(is_string($this->chart['xAxis']['categories'][$i]) && strlen($this->chart['xAxis']['categories'][$i])>10){
770
					$trimflag = true;
771
					break;
772
				}
773
			}
774
			if($trimflag){
775
				$this->chart['xAxis']['labels']['formatter'] = 'cond-trim';
776
				//$this->chart['xAxis']['labels']['rotation'] = 90;
777
			}
778
		}
779
	}
780

    
781
	function chartDataGroup(){
782
		//ena series gia kathe field gia kathe timi tou groupdim
783
		//read data
784
		$res = $this->queryResult['data'];
785
		$seriesindex = array();
786
		$names = array();
787
		$name = '';
788
		$seriescount = 0;
789
		$curseries = '';
790
		$xaxisindex = 0;
791
		$yaxisindex = 1;
792
		$fields = count($this->data['fields']);
793
		$dimindex = 1+$fields;
794

    
795
		($this->size >= count($this->queryResult['data'])) ? $num=count($this->queryResult['data']) : $num=$this->size; 
796
		//$num = $this->size;
797
		$flag = true;
798
		for($line=0;$line<$num;$line++){
799
			//list with x axis categories
800
			if(!in_array($res[$line][$xaxisindex],$this->chart['xAxis']['categories'])){
801
				if(count($this->chart['xAxis']['categories'])<$num){
802
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisindex];
803
					$flag = true;
804
				}
805
				else{
806
					$flag = false;
807
				}
808
			}
809
			else{
810
				$flag = true;
811
			}
812
			if($flag){
813
				//insert new data in currentseries or make new one
814
				if($this->queryResult['data'][$line][$dimindex] != $curseries){
815
					$curseries = $this->queryResult['data'][$line][$dimindex];
816
					for($f=0;$f<$fields;$f++){
817
						if($this->data['fieldsheaders'][$f]!="")
818
							$names[$f] = $this->data['fieldsheaders'][$f] ." for ".$curseries;
819
						else
820
							$names[$f] = $curseries;
821
						if(!isset($seriesindex[$names[$f]])){
822
							$seriesindex[$names[$f]] = array();
823
							$seriesindex[$names[$f]]['name'] = $names[$f];
824
							if($this->data['fields'][$f]['type'] !='area')
825
								$seriesindex[$names[$f]]['type'] = $this->data['fields'][$f]['type'];
826
							else
827
								$this->chart->type='area';
828
							if($this->data['fields'][$f]['yaxis']>1){
829
								$seriesindex[$names[$f]]['yAxis'] = $this->data['fields'][$f]['yaxis']-1;
830
							}
831
							$seriesindex[$names[$f]]['data'] = array();
832
							if($curseries == "UNKNOWN")
833
								$seriesindex[$names[$f]]['visible'] = false;
834

    
835
							if(isset($this->data['fields'][$f]['c']) && $this->data['fields'][$f]['c'] == true)
836
								$seriesindex[$names[$f]]['c'] = true;
837
							else
838
								$seriesindex[$names[$f]]['c'] = false;
839
						}
840
					}
841
					$seriescount += $fields;
842
				}
843
				$index = array_search($this->queryResult['data'][$line][$xaxisindex],$this->chart['xAxis']['categories']);
844
				for($f=0;$f<$fields;$f++){
845
					if($this->data['fieldsheaders'][$f]!="")
846
						$name = $this->data['fieldsheaders'][$f] . " for ".$curseries;
847
					else
848
						$name = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].") for ".$curseries;
849
					$lastindex = count($seriesindex[$name]['data']);
850
					for($t=$lastindex;$t<$index;$t++){
851
						$seriesindex[$name]['data'][$t] = 0;
852
					}
853
					$seriesindex[$name]['data'][$index] = $this->queryResult['data'][$line][$yaxisindex+$f];
854
				}
855
			}
856
		}
857
		foreach($seriesindex as $s){
858
			$this->chart['series'][] = $s;
859
		}
860
	}
861

    
862
	function chartDataMultiY(){
863
		$xaxisindex = 0;
864
		$yaxisindex = 1;
865
		//1 series gia kathe field
866
		for($i=0; $i<count($this->data['fields']);$i++){
867
			$this->chart['series'][] = array();
868
			if($this->data['fieldsheaders'][$i]!='')
869
				$this->chart['series'][$i]['name'] = $this->data['fieldsheaders'][$i];
870
			else
871
				$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['agg'].'('.$this->data['fields'][$i]['fld'].')';
872
			if($this->data['fields'][$i]['type'] != 'area')
873
				$this->chart['series'][$i]['type'] = $this->data['fields'][$i]['type'];
874
			else
875
				$this->chart->type='area';
876
			//$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yAxis'];
877

    
878
			if($this->data['fields'][$i]['yaxis']>1){
879
				$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yaxis']-1;
880
			}
881
			$this->chart['series'][$i]['data'] = array();
882
		}
883
		//read data
884
		$res = $this->queryResult['data'];
885

    
886
		($this->size >= count($this->queryResult['data'])) ? $num=count($this->queryResult['data']) : $num=$this->size; 
887
		//$num = $this->size;
888
		$flag = true;
889
		for($line=0;$line<$num;$line++){
890
			//list with x axis categories
891
			if(!in_array($res[$line][$xaxisindex],$this->chart['xAxis']['categories'])){
892
				if(count($this->chart['xAxis']['categories'])<$num){
893
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisindex];
894
					$flag = true;
895
				}
896
				else{
897
					$flag = false;
898
				}
899
			}
900
			else{
901
				$flag = true;
902
			}
903
			//insert new data in each series
904
			if($flag){
905
				for($s=0;$s<count($this->data['fields']);$s++) {
906
					if($this->data['fields'][$s]['type'] == 'pie'){
907
						$this->chart['series'][$s]['data'][$line]['name'] = $res[$line][$xaxisindex];
908
						$this->chart['series'][$s]['data'][$line]['y'] = $res[$line][$s+$yaxisindex];
909
					}
910
					else {
911
						$this->chart['series'][$s]['data'][] = $res[$line][$s+$yaxisindex];
912
					}
913
				}
914
			}
915
		}
916

    
917
		if(count($this->data['fields']) == 1)
918
			$this->chart['legend']['enabled'] = false;
919
	}
920

    
921
	function COM_performQuery() {
922
	
923
		if(!isset($_GET['query']) || $_GET['query'] == ''){
924
			$this->log->error("no query string");
925
			return;
926
		}
927

    
928

    
929
if(isset($_GET['persistent']))
930
{ $persistent=$_GET['persistent'];
931
            }
932
else
933
{
934
 $persistent="true";
935
}
936

    
937
                $query = urldecode($_GET['query']);
938

    
939
		$this->log->info("performing query: ".$query);
940

    
941
		$resp = json_encode($this->database->performQuery($query, $persistent));		//$this->log->info("response: ".$resp);
942
		echo $resp;
943
	}
944

    
945
	function sortSeries() {
946
		//exoume mia series gia kathe field
947
		//prepei na broume ayti ti series kai me basi ayti na kanoume sort kai oles tis alles kai ta categories tou xaxis
948
		$snum = 0;
949
		for($snum=0;$snum<$this->data['fields'];$snum++){
950
			if($this->data['fields'][$snum]['agg']."(".$this->data['fields'][$snum]['fld'].")" == $this->data['sort'])
951
				break;
952
		}
953
		//kanw sort ayto to series diatirontas ta kleidia opws einai kai meta kanw sort tis ypoloipes me basi ti seira kleidiwn aytinis
954
		if($this->data['order'] && $this->data['order'] == 'd'){
955
			arsort($this->chart['series'][$snum]['data']);
956
		}
957
		else{
958
			asort($this->chart['series'][$snum]['data']);
959
		}
960
		//ypoloipes
961
		//categories
962
		$tempordered = array() ;
963
		foreach (array_keys($this->chart['xAxis']['categories']) as $key) {
964
			$tempordered[$key] = $this->chart['xAxis']['categories'][$key] ;
965
		}
966
		$this->chart['xAxis']['categories'] = $tempordered;
967
		
968
		for($i=0;$i<$this->data['fields'];$i++){
969
			if($i!=$snum){
970
				unset($tempordered);
971
				$tempordered = array();
972
				$myarray = $this->chart['series'][$i]['data'];
973
				foreach (array_keys($myarray) as $key) {
974
					$tempordered[$key] = $myarray[$key] ;
975
				}
976
				$this->chart['series'][$i]['data'] = $tempordered;
977
			}
978
		}
979
	}
980

    
981

    
982
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
983
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
984
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
985
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
986
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
987
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
988

    
989
	function COM_makeQuery() {
990
		if(!isset($_GET['data'])){
991
			$this->log->info("data param not set: ". print_r($_GET,true));
992
			echo 'empty';
993
		}
994
		else {
995
			$viztype = $_GET['type'];
996
			$data = $_GET['data'];
997
			$this->data = json_decode($data,true);
998

    
999
			$cachedData = $this->data;
1000
			unset($cachedData['yaxisheaders']);
1001
			unset($cachedData['fieldsheaders']);
1002
			unset($cachedData['title']);
1003
			unset($cachedData['subtitle']);
1004
			unset($cachedData['xaxistitle']);
1005
			unset($cachedData['theme']);
1006
			unset($cachedData['xStyle']);
1007

    
1008
			$this->size = $this->data['size'];
1009

    
1010
//TODO added persistent  option
1011
if(!isset($_GET['persistent'])){
1012
$persistent='true';}
1013
else {
1014
$persistent=$_GET['persistent'];}
1015

    
1016
  
1017
echo $this->computeChartObject($viztype,$persistent);
1018

    
1019
		}
1020
	}
1021

    
1022
	function COM_read() {
1023
		if(!isset($_GET['facttable']) || !isset($_GET['dimensions']) || !isset($_GET['measures'])) {
1024
			echo "empty";
1025
		}
1026

    
1027
		$dims = explode(';',$_GET['dimensions']);
1028
		$meas = explode(';',$_GET['measures']);
1029
		echo $this->readtest($dims, $meas, $_GET['facttable'],$_GET['aggregation']);
1030
	
1031
	}
1032

    
1033
	function COM_check_fact() {
1034
		if(!isset($_GET['name'])) {
1035
			echo "empty";
1036
		}
1037
		else{
1038
			$res = $this->database->findTableType($_GET['name']);
1039
			if($res == false) echo 'false';
1040
			else echo $res;
1041
		}
1042
	}
1043
	
1044
	function COM_list_of_dim_names() {
1045
		$res = $this->database->getAllDims();
1046
		if($res != false)
1047
			echo json_encode($res);
1048
		else
1049
			echo "false";
1050
	}
1051

    
1052
	function COM_search_warehouse_by_dims() {
1053
		if(!isset($_GET['dims'])) {
1054
			echo 'empty';
1055
		}
1056
		else {
1057
			$res = $this->database->searchWarehousebyDims(explode(';',$_GET['dims']));
1058
			if($res == false) echo "false";
1059
			else echo $res;	
1060
		}
1061
	}
1062

    
1063
	function COM_create_new_fact_table() {
1064
		if(!isset($_GET['table']) || !isset($_GET['dims']) || !isset($_GET['meas'])) {
1065
			echo 'empty';
1066
		} 
1067
		else {
1068
			//set up the list of dims
1069
			$dims = explode(',',$_GET['dims']);
1070
			//set up the list of meas
1071
			$new_meas = rtrim($_GET['meas'],';');
1072
			$meas = explode(';',$new_meas);
1073
			//call the db function
1074
			$res = $this->database->createNewFact($_GET['table'], $dims, $meas);
1075
			if($res == false)
1076
				echo 'false';
1077
			else
1078
				echo 'true';
1079
		}
1080
	}
1081

    
1082
	function COM_list_of_fact_tables() {
1083
		$res = json_encode($this->database->getFactsNames());
1084
		if($res == null)
1085
			echo 'null';
1086
		else
1087
			echo $res;
1088
	}
1089

    
1090
	function COM_list_of_meas() {
1091
		if(!isset($_GET['table'])) {
1092
			echo 'empty';
1093
		} 
1094
		else {
1095
			$res = $this->database->findMeas($this->database->findTable($_GET['table'],'fact'));
1096
			if($res == false)
1097
				echo 'false';
1098
			else
1099
				echo json_encode($res);
1100
		}
1101
	}
1102

    
1103
	function COM_list_of_dims() {
1104
		$res = $this->database->getDims();
1105
		if($res == false)
1106
			echo 'false';
1107
		else
1108
			echo json_encode($res);
1109
	}
1110

    
1111
	function COM_del_meas() {
1112
		if(!isset($_GET['facttable']) || !isset($_GET['name'])) {
1113
			echo "empty";
1114
		}
1115
		else {
1116
			$res = $this->database->delMeas($_GET['facttable'], $_GET['name']);
1117
			if($res == true)
1118
				echo "true";
1119
			else 
1120
				echo "false";
1121
		}
1122
	}
1123

    
1124
	function COM_add_meas() {
1125
		if(!isset($_GET['facttable']) || !isset($_GET['name']) || !isset($_GET['formal_name']) || !isset($_GET['type'])) {
1126
			echo "empty";
1127
		}
1128
		else {
1129
			$res = $this->database->addMeas($_GET['facttable'], $_GET['name'], $_GET['formal_name'], $_GET['type']);
1130
			if($res == true)
1131
				echo "true";
1132
			else 
1133
				echo "false";
1134
		}
1135
	}
1136

    
1137
	function COM_meas_range_data() {
1138
		if(!isset($_GET['facttable']) || !isset($_GET['measurement'])) {
1139
			echo "empty";
1140
		}
1141
		else {
1142
			$res = $this->database->measRangeData($_GET['facttable'],$_GET['measurement']);
1143
			echo json_encode($res);
1144
		}
1145
	}
1146

    
1147
	function COM_get_dims_fields_list(){
1148
		if(!isset($_GET['table'])) {
1149
			echo "empty";
1150
		}
1151
		else {
1152
			$res = $this->database->getDimsList($_GET['table']);
1153

    
1154
			if($res == false)
1155
				echo "false";
1156
			else if($res == null)
1157
				echo "null";
1158
			else
1159
				echo json_encode($res);
1160
		}
1161
	}
1162

    
1163

    
1164
	function fetchData($table, $dim1, $dim2, $meas, $constraints = null) {
1165
		return $this->database->getJsonData($table, $dim1, $dim2, $meas, $constraints);
1166
	}
1167

    
1168
	function readData($table, $dim1, $dim2, $meas, $constraints = null) {
1169
		return $this->database->readData($table, $dim1, $dim2, $meas, $constraints);
1170
	}
1171

    
1172
	function timeload($start,$end) {
1173
		$this->database->loadTime($start,$end);
1174
	}
1175

    
1176
	function readtest($dimensions,$measures,$table,$aggr) {
1177
		$data = $this->database->readtest($dimensions,$measures,$table,$aggr);
1178
		//echo json_encode($data,JSON_NUMERIC_CHECK);
1179
		echo json_encode($data);
1180
	}
1181
	
1182
	
1183
	/////////////////////////////////////////////////////////////////////////////////////
1184

    
1185
}
1186

    
1187
/*
1188

    
1189
	function createChartDataGroup($givenType) {
1190
		//exoume anagkastika ena mono yaxis
1191
		//ena series gia kathe diaforetiki timi tou tritou pediou
1192
		if($givenType == 'chart'){
1193
			$this->chart['xAxis']['categories'] = array();
1194
			
1195
		}
1196
		else{
1197
			unset($this->chart['xAxis']['categories']);
1198
		}
1199
		$this->chart['series'] = array();
1200
		
1201
		$currentSeries = "";
1202
		$seriesCnt = -1;
1203
		$numOfColumns = count($this->queryResult[0]);
1204
		if(count($this->data['series']) == 0)
1205
			$sNameIndex = -1;
1206
		else
1207
			$sNameIndex = count($this->data['fields']) +1;
1208
			//$sNameIndex = $numOfColumns-1;
1209
		$xaxisIndex = 0;
1210
		$fStartindex = 1;
1211
		$groupindex = 2;
1212
		$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['name'];
1213
		//arxikopoiisi
1214
		$this->chart['yAxis'] = array();
1215
		$this->chart['series'] = array();
1216
		$this->chart['legend']['enabled'] = false;
1217
		//edw tha ektelestei mono mia fora <-------NA TO ALLAKSW
1218
		for($i=0; $i<count($this->data['fields']);$i++){
1219
			//gia kathe field, exoume enan yaxis kai ena series
1220
			$this->chart['yAxis'][] = array();
1221
			$this->chart['yAxis'][$i]['labels'] = array();
1222
			$this->chart['yAxis'][$i]['offset'] = 70;
1223
			//$this->chart['yAxis'][$i]['labels']['formatter'] = 'simple';
1224
			$this->chart['yAxis'][$i]['title'] = array();
1225
			$this->chart['yAxis'][$i]['title']['text'] = $this->data['fields'][$i]['fld'];
1226
			if($i>0){
1227
				$this->chart['yAxis'][$i]['opposite'] = true;
1228
				$this->chart['yAxis'][$i]['gridLineWidth'] = 0;
1229
			}
1230

    
1231
			//$this->chart['series'][] = array();
1232
			//$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['fld'];
1233
			
1234
			//if($i>0) {
1235
			//	$this->chart['series'][$i]['yAxis'] = $i;
1236
			//}
1237
			//$this->chart['series'][$i]['data'] = array();
1238
			//$seriesData[$i] = array();
1239
		}
1240
		
1241
		$res = $this->queryResult['data'];
1242
		//gia kathe diaforetiki timi tou pediou 2, kainourgio category an exoume chart
1243
		//gia kathe diaforetiki timi tou pediou 3, kainourgio series me onoma tin timi tou pediou 3
1244
		//oso eisai sto idio series, apla prosthese ta data
1245
		//an allakseis ftiakse kainourgio
1246
		for($line=0;$line<count($this->queryResult['data']);$line++){
1247
			//list with x axis categories
1248
			if($givenType == 'chart'){
1249
				if(!in_array($res[$line][$xaxisIndex],$this->chart['xAxis']['categories'])){
1250
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisIndex];
1251
				}
1252
			}
1253
			//new series? 
1254
			if($res[$line][$groupindex] != $currentSeries){
1255
				$currentSeries = $res[$line][$groupindex];
1256
				$this->chart['series'][] = array();
1257
				$seriesCnt++;
1258
				$this->chart['series'][$seriesCnt]['name'] = $currentSeries;
1259
				$this->chart['series'][$seriesCnt]['data'] = array();
1260
			}
1261
			$this->chart['series'][$seriesCnt]['data'][] = $res[$line][$fStartindex];
1262
		}
1263

    
1264
		if(count($this->chart['xAxis']['categories']) > 15){
1265
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
1266
			$this->chart['xAxis']['labels']['rotation'] = 45;
1267
		}
1268

    
1269
	}
1270
*/
1271
?>
(10-10/30)