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
$this->myqueries['wttime'] = array();
86

    
87
$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;";
88
$this->myqueries['projpubs'] = array();
89

    
90
$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";
91

    
92
		$this->myqueries['projpubsf2'] = array();
93
		$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";
94

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

    
98
// ERI - TODO egi timeline has been updated- change it everywhere 
99
$this->myqueries['egiTimeline']= array();
100
$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;";
101
 $this->myqueries['egiProjects']= array();
102
		$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;";
103

    
104
$this->myqueries['egiVO']= array();
105
		$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;";
106
		
107
$this->myqueries['egiOA']= array();
108
$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 
109
by r.bestlicense";
110

    
111

    
112
//FET queries
113

    
114
$this->myqueries['fetTimeline']= array();
115
 
116
$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;"; 
117
$this->myqueries['fetFlagships']= array();
118

    
119
                $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;";
120

    
121

    
122

    
123
$this->myqueries['fetProactive']= array();
124

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

    
127

    
128

    
129
$this->myqueries['fetOA']= array();
130
$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 ; ";
131

    
132
$this->myqueries['fetOpen']= array();
133

    
134
$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;";
135

    
136

    
137

    
138

    
139

    
140

    
141
/*TODO added WT pie query - copy to beta and prod*/
142

    
143
$this->myqueries['wtOA']= array();
144
$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;";
145

    
146

    
147
$this->myqueries['ercOA']= array();
148
$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";
149

    
150

    
151

    
152
/* FCT*/
153

    
154

    
155
 $this->myqueries['fcttime'] = array();
156

    
157

    
158
 $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";
159

    
160
$this->myqueries['fctOA']= array();
161
$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";
162

    
163
$this->myqueries['fctfp7time']= array();
164
$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 ;";	
165
$this->colors[0] = '#4572A7';
166
		$this->colors[1] = '#AA4643';
167
		$this->colors[2] = '#89A54E';
168

    
169
		$this->types[0] = 'column';
170
		$this->types[1] = 'spline';
171
		$this->types[2] = 'spline';
172

    
173
		/*set up*/
174
		$this->chart = array();
175
		$this->chart['chart'] = array();
176
		//$this->chart['exporting'] = array();
177
		$this->chart['title'] = array();
178
		$this->chart['xAxis'] = array();
179
		$this->chart['xAxis']['labels'] = array();
180
		$this->chart['xAxis']['title'] = array();
181
		$this->chart['xAxis']['categories'] = array();
182
		$this->chart['yAxis'] = array();
183
		$this->chart['tooltip'] = array();
184
		$this->chart['legend'] = array();
185
		$this->chart['series'] = array();
186
		/*more*/
187
		//$this->chart['exporting']['enabled'] = true;
188
		$this->chart['chart']['renderTo'] = 'chart';
189
		$this->chart['chart']['reflow'] = false;
190
		$this->chart['chart']['showAxes'] = true;
191
		$this->chart['chart']['zoomType'] = 'xy';
192
		//$this->chart['chart']['margin'] = 'auto';
193
		//$this->chart['chart']['marginRight'] = 70;
194
		//$this->chart['chart']['marginBottom'] = 70;
195
		//$this->chart['chart']['height'] = 450;
196
		//$this->chart['chart']['spacingBottom'] = 100;
197
		$this->chart['credits']['enabled'] = false;
198
		//$this->chart['credits']['text'] = "from OpenAIRE via HighCharts".date("d / m / Y");
199
		//$this->chart['credits']['href'] = "#";
200
		//$this->chart['credits']['position'] = array();
201
		//$this->chart['credits']['position']['y'] = -2;
202

    
203

    
204
		/*$this->chart['xAxis']['labels']['overflow'] = null;//'justify';
205
		$this->chart['xAxis']['labels']['style'] = array();
206
		$this->chart['xAxis']['labels']['style']['font-size'] = '10px';
207

    
208
		$this->chart['xAxis']['showEmpty'] = true;
209
		$this->chart['xAxis']['labels']['enabled'] = true;
210
		$this->chart['xAxis']['labels']['style'] = array();
211
		$this->chart['xAxis']['labels']['style']['font-size'] = '8px';
212
		//$this->chart['xAxis']['labels']['style']['margin-left'] = '10px';
213
		//$this->chart['xAxis']['labels']['style']['margin-right'] = '10px';*/
214
		$this->chart['xAxis']['startOnTick'] = true;
215
		$this->chart['xAxis']['endOnTick'] = true;
216
		$this->chart['xAxis']['showFirstLabel'] = true;
217
		$this->chart['xAxis']['showLastLabel'] = true;
218

    
219
		$this->chart['tooltip']['percentageDecimals'] = 1;
220
		$this->chart['tooltip']['valueDecimals'] = 1;
221
		$this->chart['legend']['layout'] = 'vertical';
222
		$this->chart['legend']['align'] = 'right';
223
		$this->chart['legend']['verticalAlign'] = 'top';
224
		$this->chart['legend']['floating'] = true;
225
		$this->chart['legend']['borderWidth'] = 0;
226
		$this->chart['legend']['x'] = -10;
227
		$this->chart['legend']['y'] = 50;
228
		$this->chart['legend']['padding'] = 3;
229
		$this->chart['legend']['itemMarginBottom'] = 5;
230

    
231
		//for scatter plots
232
		$this->chart['plotOptions'] = array();
233
		$this->chart['plotOptions']['series'] = array();
234
	        $this->chart['plotOptions']['series']['showCheckbox'] = true;
235
	        $this->chart['plotOptions']['series']['selected'] = true;
236
		$this->chart['plotOptions']['scatter'] = array();
237
		$this->chart['plotOptions']['scatter']['marker'] = array();
238
		$this->chart['plotOptions']['scatter']['marker']['radius'] = 5;
239
		$this->chart['plotOptions']['scatter']['marker']['states'] = array();
240
		$this->chart['plotOptions']['scatter']['marker']['states']['hover'] = array();
241
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['enabled'] = true;
242
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['lineColor'] = 'rgb(100,100,100)';
243
		$this->chart['plotOptions']['scatter']['states'] = array();
244
		$this->chart['plotOptions']['scatter']['states']['hover'] = array();
245
		$this->chart['plotOptions']['scatter']['states']['hover']['marker'] = array();
246
		$this->chart['plotOptions']['scatter']['states']['hover']['marker']['enabled'] = false;
247

    
248
		$this->chart['plotOptions']['area'] = array();
249
                $this->chart['plotOptions']['area']['stacking'] = null;
250

    
251
                $this->chart['plotOptions']['areaspline'] = array();
252
                $this->chart['plotOptions']['areaspline']['stacking'] = null;
253

    
254
		//for pie charts
255
		$this->chart['plotOptions']['pie'] = array();
256
		$this->chart['plotOptions']['pie']['allowPointSelect'] = true;
257
		//$this->chart['plotOptions']['pie']['size'] = '50%';
258
		$this->chart['plotOptions']['pie']['cursor'] = 'pointer';
259
		$this->chart['plotOptions']['pie']['showInLegend'] = true;
260
		$this->chart['plotOptions']['pie']['dataLabels'] = array();
261
		$this->chart['plotOptions']['pie']['dataLabels']['enabled'] = true;
262
		$this->chart['plotOptions']['pie']['dataLabels']['color'] = '#000000';
263
		$this->chart['plotOptions']['pie']['dataLabels']['connectorColor'] = '#000000';
264
		$this->chart['plotOptions']['pie']['dataLabels']['crop'] = false;
265
		$this->chart['plotOptions']['pie']['dataLabels']['distance'] = 10;
266
		//$this->chart['plotOptions']['pie']['dataLabels']['formatter'] = '';
267
		//for column
268
		$this->chart['plotOptions']['column'] = array();
269
		$this->chart['plotOptions']['column']['allowPointSelect'] = true;
270
		$this->chart['plotOptions']['column']['cursor'] = 'pointer';
271
		$this->chart['plotOptions']['column']['showInLegend'] = true;
272
		$this->chart['plotOptions']['column']['grouping'] = true;
273

    
274
		$this->chart['plotOptions']['area'] = array();
275
	}
276

    
277
	function makeQuery($viztype,$persistent) {
278
		if(!isset($_GET['data'])){
279
			$this->log->info("data param not set: ". print_r($_GET,true));
280
			return 'empty';
281
		}
282
		else {
283
			$data = $_GET['data'];
284
                      
285
	
286
                        $this->data = json_decode($data,true);//print_r($this->data);
287
			$this->log->debug("data param decoded: ". print_r($this->data,true));
288
			$cachedData = $this->data;
289
			//unset($cachedData['yaxisheaders']);
290
			//unset($cachedData['fieldsheaders']);
291
			unset($cachedData['title']);
292
			unset($cachedData['subtitle']);
293
			//unset($cachedData['xaxistitle']);
294
			unset($cachedData['theme']);
295
			unset($cachedData['xStyle']);
296
			$this->size = $this->data['size'];		
297
			return $this->computeChartObject($viztype,$persistent);
298
		}
299
	}
300

    
301
	function computeChartObject($viztype,$persistent){
302
		//$this->log->info("DATA: ".print_r($this->data,true));
303
		if(!isset($this->data['query'])){
304
			$this->log->info("query not set");
305
//TODO here add persistent value??
306
			$this->queryResult = $this->database->getData($this->data,$persistent);
307
		}
308
		else{
309
			$this->log->info("query set");
310
			$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($this->myqueries[$this->data['query']]['q'],true));
311

    
312
		}
313

    
314

    
315
		$this->log->info("data from DB: ".print_r($this->queryResult,true));
316
		if($viztype=="chart"){
317
			if($this->queryResult['type'] == 'scatter'){
318
				$this->createScatterData();
319
			}
320
			else{
321
				$this->createChartData();
322
			}
323

    
324
			if(isset($this->data['in']) && count($this->data['in'])){
325
				$this->chart2 = $this->chart;
326
				for($w=0;$w<count($this->chart['series']);$w++){
327
					//$whichfield = $this->data['in'][$w]['f'];
328
					//$this->chart2 = $this->chart;
329
					$data = $this->chart2['series'][$w]['data'];
330
					for($i=1;$i<count($this->chart2['xAxis']['categories']);$i++){
331
						$data[$i] += $data[$i-1];
332
					}
333
					$this->chart2['series'][$w]['data'] = $data;
334
				}
335
				//return json_encode(array($this->chart,$this->chart2),JSON_NUMERIC_CHECK);
336
				return json_encode(array($this->chart,$this->chart2));
337
			}
338

    
339
			//return json_encode($this->chart,JSON_NUMERIC_CHECK);
340
			return json_encode($this->chart);
341
		}
342
		if($viztype=='table'){
343
			//return json_encode($this->queryResult,JSON_NUMERIC_CHECK);
344
			return json_encode($this->queryResult);
345
		}
346
	}
347

    
348
	function COM_getMeasMetadata(){
349
		if(!isset($_GET['table'])) {
350
			echo 'empty';
351
		}
352
		else{
353
			echo json_encode($this->database->getMeasMetadata($_GET['table']));
354
		}
355
	}
356

    
357
	function COM_defaultChart() {
358
		//get the default values for this table
359
		$temp = $this->database->getDefaultData($_GET['table']);
360
		if($temp == 'empty')
361
			echo json_encode("empty");
362
		else{
363
			$this->size = $temp['size'];
364
			//$this->queryResult = $temp['data'];
365
			$this->data = $temp['selectedData'];
366
			//echo json_encode($this->data,JSON_NUMERIC_CHECK);
367
			echo json_encode($this->data);
368
		}
369
		//$this->data['group'] = '';
370
		//$this->data['color'] = '';
371
		//print_r($this->queryResult);return;
372
		/*if($this->data['type'] == 'chart')
373
			$this->createChartData();
374
		else
375
			$this->createScatterData();
376
		$toreturn = array();
377
		$toreturn['chart'] = $this->chart;
378
		$toreturn['selectedData'] = $this->data;
379
		echo json_encode($toreturn,JSON_NUMERIC_CHECK);*/
380
	}
381

    
382
	function defaultChart() {
383
		//get the default values for this table
384
		$temp = $this->database->getDefaultData($_GET['table']);
385
		$this->queryResult = $temp['data'];
386
		$this->data = $temp['selectedData'];
387
		$this->data['group'] = '';
388
		$this->data['color'] = 'no';
389
		//print_r($this->queryResult);return;
390
		if($temp['type'] == 'chart')
391
			$this->createChartData($temp['type']);
392
		else
393
			$this->createScatterData('');
394
		//$this->chart['chart']['type'] = $temp['type'];
395
		//print_r($this->chart);return;
396
		//print_r($this->data);
397
		$toreturn = array();
398
		$toreturn['chart'] = $this->chart;
399
		$toreturn['selectedData'] = $this->data;
400
		//return json_encode($toreturn,JSON_NUMERIC_CHECK);
401
		return json_encode($toreturn);
402
	}
403

    
404
	function defaultChartSelections() {
405
		//get the default values for this table
406
		$temp = $this->database->getDefaultData($_GET['table']);
407
		$this->queryResult = $temp['data'];
408
		$this->data = $temp['selectedData'];
409

    
410
		//echo json_encode($this->data,JSON_NUMERIC_CHECK);
411
		echo json_encode($this->data);
412
	}
413

    
414
	function COM_getFilterData(){
415
		if(!isset($_GET['table']) || !isset($_GET['field'])) {
416
			echo 'empty';
417
		}
418
		else {
419
			echo $this->computeFilterObject($_GET['table'],$_GET['field'],true);
420
		}
421
	}
422

    
423
	function getFilterData($table, $field){
424
		return $this->computeFilterObject($table,$field,false);
425
	}
426

    
427
	function computeFilterObject($table, $field, $encode){
428
		$flds = explode("-",$field);
429
		if(count($flds)>1){
430
			$ctable = $flds[count($flds)-2];
431
			$field = $flds[count($flds)-1];
432
		}
433
		else {
434
			$ctable = $table;
435
			$field = $field;
436
		}
437
		//call mydb function
438
		if($encode)
439
			//return json_encode($this->database->getFilterData($ctable,$field),JSON_NUMERIC_CHECK);
440
			return json_encode($this->database->getFilterData($ctable,$field));
441
		else
442
			return $this->database->getFilterData($ctable,$field);
443
	}
444

    
445
	function COM_get_schema(){
446
		$facts = $this->database->getFacts();
447
		$dimensions = $this->database->getDimensions();
448
		$response = array();
449
		$menu = array();
450
		if($facts === null) {
451
			$this->log->error("there are no fact tables");
452
			echo "empty";
453
			return;
454
		}//print_r($facts);
455
		foreach($facts as $facttable){
456
			$newfact = array();
457
			$newfact['name'] = $facttable['name'];
458
			if(!isset($facttable['meas'][0]))
459
				$newfact['meas'][] = $facttable['meas'];
460
			else
461
				$newfact['meas'] = $facttable['meas'];
462
			$newfact['dim'] = array();
463
			//gia kathe dim theloume: name, type, data, kai attrib opou to attrib mporei na einai ki ayto dim
464
			//otan pigainoume se bathos stin ierarxia twn diastasewn kratame touw progonous kai an ftasoume se diastasi pou yparxei stous progonous tin agnooume
465
			foreach($facttable['dim'] as $curdim) {
466
				$newdim = array();
467
				$newdim['name'] = $curdim['name'];
468
				$newdim['type'] = $curdim['type'];
469
				//$newdim['data'] = $curdim['data'];//may not need it
470
				if(isset($curdim['view']))
471
					$newdim['view'] = $curdim['view'];
472
				if(isset($curdim['dimtable']))
473
					$newdim['dimtable'] = 'no';
474
				else{
475
					$newdim['dimtable'] = 'yes';
476
					$newdim['attrib'] = array();
477

    
478
					//need to find table $curdim['refer']['table'] from all the dimentions
479
					$tabledim = search($dimensions, 'name', $curdim['name']);
480
					//print_r($tabledim);
481
					for($i=0;$i<count($tabledim);$i++){
482
						if(isset($tabledim[$i]['type']) && $tabledim[$i]['type']=='dimension')
483
							break;
484
					}
485
					$newdim['attrib'] = $this->makeAttrList($tabledim[$i]['attrib'],$dimensions,array($curdim['name']),$facttable['name']);
486
				}
487
				//I am interested in all the attributes of this dimension
488
				array_push($newfact['dim'],$newdim);
489
			}
490
			array_push($menu,$newfact);
491
		}
492
		$response['name'] = $GLOBALS['db_name'];
493
		$response['schema'] = $menu;
494
		echo json_encode($response);
495
	}
496

    
497
	function makeAttrList($attrList, $dimensions, $ancestors,$facttable){
498
		$newList = array();
499
		if(is_array($attrList)) {
500
			foreach($attrList as $attr) {
501
				if($attr['name'] != 'id' && $attr['name']!=$facttable) {
502
					$newAttr = array();
503
					$newAttr['name'] = $attr['name'];
504
					if(isset($newAttr['view']))
505
						$newAttr['view'] = $attr['view'];
506
					$newAttr['type'] = $attr['type'];
507
					if(isset($attr['analysed']) && !in_array($attr['name'],$ancestors)){
508

    
509
						$finddim = search($dimensions, 'name', $attr['name']);
510
						$nextdim = search($finddim,'type','dimension');//print_r($nextdim);
511
						$newnewList = $this->makeAttrList($nextdim[0]['attrib'],$dimensions, array_merge($ancestors,array($attr['name'])),$facttable);
512
						$ancestors[] = $attr['name'];
513
						$newAttr['analysed'] = $attr['name'];
514
						$newAttr['attrib']= $newnewList;
515
					}
516
					else{
517
						$newAttr['attrib']= array();
518
					}
519
					array_push($newList,$newAttr);
520
				}
521
			}
522
		}
523
		return $newList;
524
	}
525

    
526

    
527
/*
528
type = color | group | ''
529
*/
530
	function createScatterData(){
531
		unset($this->chart['xAxis']['categories']);
532
		$this->chart['chart']['type'] = 'bubble';
533
		$xaxisindex = 0;
534
		$yaxisindex = 1;
535
		$left = 0;
536
		$right = 0;
537
		$fields = count($this->data['fields']);
538
		$dimindex = 1+$fields;
539
		if($this->data['group'] == '')
540
			$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['name'];
541
		else
542
			$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['agg'].'('.$this->data['xaxis']['name'].')';
543
		//arxikopoiisi
544
		//posoi yaxis??
545
		$this->chart['yAxis'] = array();
546
		$yaxis = $this->data['fields'][$fields-1]['yaxis'];
547
		$tempy = -1;
548
		for($f=0;$f<count($this->data['fields']);$f++){
549
			if($this->data['fields'][$f]['yaxis']!=$tempy+1){//new y
550
				$tempy++;
551
				$this->chart['yAxis'][$tempy] = array();
552
				$this->chart['yAxis'][$tempy]['labels'] = array();
553
				$this->chart['yAxis'][$tempy]['labels']['enabled'] = true;
554
				$this->chart['yAxis'][$tempy]['labels']['overflow'] = 'justify';
555
				//$this->chart['yAxis'][$tempy]['offset'] = 70;
556
				$this->chart['yAxis'][$tempy]['title'] = array();
557
				if($this->data['yaxisheaders'][$tempy] !=''){
558
					$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['yaxisheaders'][$tempy];
559
				}
560
				else{
561
					if($this->data['fieldsheaders'][$f] !=''){
562
						$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fieldsheaders'][$f];
563
					}
564
					else{
565
						if($this->data['group'] == '')
566
							$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['fld'];
567
						else
568
							$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
569
					}
570
				}
571
				if($right>=$left){
572
					$left++;
573
				}
574
				else{
575
					$right++;
576
					$this->chart['yAxis'][$tempy]['opposite'] = true;
577
				}
578
			}
579
			else if($this->data['yaxisheaders'][$tempy] == ""){//more in the name
580
				if($this->data['fieldsheaders'][$f] !=''){
581
					$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fieldsheaders'][$f];
582
				}
583
				else{
584
					if($this->data['group'] == '')
585
						$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['fld'];
586
					else
587
						$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
588
				}
589
			}
590
		}
591
		$this->chart['series'] = array();
592
		$this->chart['legend']['enabled'] = false;
593
		//an einai group exw ena series
594
		if(($this->data['group']  && $this->data['group'] != '') || $this->data['color'] == ''){//ena series kai diaforetiko onoma gia kathe data
595
			for($f=0;$f<$fields;$f++){
596
				$this->chart['series'][$f] = array();
597
				$this->chart['series'][$f]['data'] = array();
598
				if($this->data['fields'][$f]['yaxis']!=1)
599
					$this->chart['series'][$f]['yAxis'] = $this->data['fields'][$f]['yaxis']-1;
600
			}
601
			//for($line=0;$line<10;$line++){
602
			for($line=0;$line<count($this->queryResult['data']);$line++){
603
				for($f=0;$f<$fields;$f++){
604
					//insert a new data object to the only series
605
					$data = array();
606
					$data['x'] = $this->queryResult['data'][$line][$xaxisindex];
607
					$data['y'] = $this->queryResult['data'][$line][$yaxisindex+$f];
608
					if($this->data['group'] != ''){
609
						$data['name'] = $this->queryResult['data'][$line][$dimindex];
610
						if($this->data['fieldsheaders'][$f]=="")
611
							$this->chart['series'][$f]['name'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
612
						else
613
							$this->chart['series'][$f]['name'] = $this->data['fieldsheaders'][$f];
614
					}
615
					else{
616
						if($this->data['fieldsheaders'][$f]=="")
617
							$this->chart['series'][$f]['name'] = $this->data['fields'][$f]['fld'];
618
						else
619
							$this->chart['series'][$f]['name'] = $this->data['fieldsheaders'][$f];
620
					}
621
					$this->chart['series'][$f]['data'][] = $data;
622
				}
623
			}//print_r($this->chart['series']);
624
		}
625
		//an einai color exw ena series gia kathe timi tou dimindex
626
		else{
627
			$this->chart['legend']['enabled'] = true;
628
			$curseries = '';
629
			$seriescount = 0;
630
			//for($line=0;$line<10;$line++){
631
			for($line=0;$line<count($this->queryResult['data']);$line++){
632
				if($this->queryResult['data'][$line][$dimindex] != $curseries){
633
					$curseries = $this->queryResult['data'][$line][$dimindex];
634
					for($f=0;$f<$fields;$f++){
635
						$this->chart['series'][$seriescount+$f] = array();
636
						if($this->data['fieldsheaders'][$f]!='')
637
							$this->chart['series'][$seriescount+$f]['name'] = $this->data['fieldsheaders'][$f]." for ".$this->queryResult['data'][$line][$dimindex];
638
						else
639
							$this->chart['series'][$seriescount+$f]['name'] = $this->data['fields'][$f]['fld']." for ".$this->queryResult['data'][$line][$dimindex];
640
						$this->chart['series'][$seriescount+$f]['data'] = array();
641
					}
642
					$seriescount += $fields;
643
				}
644
				for($f=0;$f<$fields;$f++){
645
					$this->chart['series'][$seriescount-$fields+$f]['data'][] = array($this->queryResult['data'][$line][$xaxisindex],$this->queryResult['data'][$line][$yaxisindex+$f]);
646
				}
647
			}
648
		}
649

    
650
	}
651

    
652
	function createChartData(){
653
		$this->chart['xAxis']['categories'] = array();
654
		unset($this->chart['chart']['type']);
655
		$this->chart['series'] = array();
656
		$this->chart['yAxis'] = array();
657
		$xaxisindex = 0;
658
		$yaxisindex = 1;
659
		$left = 0;
660
		$right = 0;
661
		//print_r($this->queryResult['data']);
662
		$fields = count($this->data['fields']);
663
		$dimindex = 1+$fields;
664
if(isset($this->data['xaxistitle']))
665
		//if($this->data['xaxistitle']!='')
666
			$this->chart['xAxis']['title']['text'] = $this->data['xaxistitle'];
667
		else{
668
			$xaxis = explode("-",$this->data['xaxis']['name']);
669
			$this->chart['xAxis']['title']['text'] = $xaxis[count($xaxis)-1];
670
		}
671
		$this->chart['legend']['enabled'] = true;
672
		//posoi yaxis??
673
		$yaxis = $this->data['fields'][$fields-1]['yaxis'];
674
		$tempy = -1;
675
		for($f=0;$f<count($this->data['fields']);$f++){
676
			if($this->data['fields'][$f]['yaxis']!=$tempy+1){//new y
677
				$tempy++;
678
				$this->chart['yAxis'][$tempy] = array();
679
				$this->chart['yAxis'][$tempy]['labels'] = array();
680
				$this->chart['yAxis'][$tempy]['labels']['enabled'] = true;
681
				$this->chart['yAxis'][$tempy]['labels']['overflow'] = 'justify';
682
				$this->chart['yAxis'][$tempy]['min'] = 0;
683
				//$this->chart['yAxis'][$tempy]['offset'] = 70;
684
				$this->chart['yAxis'][$tempy]['title'] = array();
685
				if($this->data['yaxisheaders'][$tempy]!=''){
686
					$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['yaxisheaders'][$tempy];
687
					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
688
						$this->data['fieldsheaders'][$f] = $this->data['yaxisheaders'][$tempy];
689
					}
690
				}
691
				else{
692
					if($this->data['fieldsheaders'][$f] !=''){
693
						$this->chart['yAxis'][$tempy]['title']['text']  = $this->data['fieldsheaders'][$f];
694
					}
695
					else {
696
						$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
697
					}
698
				}
699
				if($right>=$left){
700
					$left++;
701
				}
702
				else{
703
					$right++;
704
					$this->chart['yAxis'][$tempy]['opposite'] = true;
705
				}
706
			}
707
			else if($this->data['yaxisheaders'][$tempy]==''){//more in the name
708
				if($this->data['fieldsheaders'][$f] !='')
709
					$this->chart['yAxis'][$tempy]['title']['text']  .= " / ".$this->data['fieldsheaders'][$f];
710
				else
711
					$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
712
			}
713
		}
714

    
715
		if($this->data['group'] != ''){//periptwsi analyze
716
			$this->chartDataGroup();
717
		}
718
		else{//polloi yaxis xwris group
719
			$this->chartDataMultiY();
720
		}
721

    
722
		if(count($this->chart['xAxis']['categories']) > 30){
723
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
724
			//$this->chart['xAxis']['labels']['rotation'] = 90;
725
		}//print_r($this->chart);
726
		else{
727
			$trimflag = false;
728
			for($i=0;$i<count($this->chart['xAxis']['categories']);$i++){
729
				if(is_string($this->chart['xAxis']['categories'][$i]) && strlen($this->chart['xAxis']['categories'][$i])>10){
730
					$trimflag = true;
731
					break;
732
				}
733
			}
734
			if($trimflag){
735
				$this->chart['xAxis']['labels']['formatter'] = 'cond-trim';
736
				//$this->chart['xAxis']['labels']['rotation'] = 90;
737
			}
738
		}
739
	}
740

    
741
	function chartDataGroup(){
742
		//ena series gia kathe field gia kathe timi tou groupdim
743
		//read data
744
		$res = $this->queryResult['data'];
745
		$seriesindex = array();
746
		$names = array();
747
		$name = '';
748
		$seriescount = 0;
749
		$curseries = '';
750
		$xaxisindex = 0;
751
		$yaxisindex = 1;
752
		$fields = count($this->data['fields']);
753
		$dimindex = 1+$fields;
754

    
755
		($this->size >= count($this->queryResult['data'])) ? $num=count($this->queryResult['data']) : $num=$this->size; 
756
		//$num = $this->size;
757
		$flag = true;
758
		for($line=0;$line<$num;$line++){
759
			//list with x axis categories
760
			if(!in_array($res[$line][$xaxisindex],$this->chart['xAxis']['categories'])){
761
				if(count($this->chart['xAxis']['categories'])<$num){
762
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisindex];
763
					$flag = true;
764
				}
765
				else{
766
					$flag = false;
767
				}
768
			}
769
			else{
770
				$flag = true;
771
			}
772
			if($flag){
773
				//insert new data in currentseries or make new one
774
				if($this->queryResult['data'][$line][$dimindex] != $curseries){
775
					$curseries = $this->queryResult['data'][$line][$dimindex];
776
					for($f=0;$f<$fields;$f++){
777
						if($this->data['fieldsheaders'][$f]!="")
778
							$names[$f] = $this->data['fieldsheaders'][$f] ." for ".$curseries;
779
						else
780
							$names[$f] = $curseries;
781
						if(!isset($seriesindex[$names[$f]])){
782
							$seriesindex[$names[$f]] = array();
783
							$seriesindex[$names[$f]]['name'] = $names[$f];
784
							if($this->data['fields'][$f]['type'] !='area')
785
								$seriesindex[$names[$f]]['type'] = $this->data['fields'][$f]['type'];
786
							else
787
								$this->chart->type='area';
788
							if($this->data['fields'][$f]['yaxis']>1){
789
								$seriesindex[$names[$f]]['yAxis'] = $this->data['fields'][$f]['yaxis']-1;
790
							}
791
							$seriesindex[$names[$f]]['data'] = array();
792
							if($curseries == "UNKNOWN")
793
								$seriesindex[$names[$f]]['visible'] = false;
794

    
795
							if(isset($this->data['fields'][$f]['c']) && $this->data['fields'][$f]['c'] == true)
796
								$seriesindex[$names[$f]]['c'] = true;
797
							else
798
								$seriesindex[$names[$f]]['c'] = false;
799
						}
800
					}
801
					$seriescount += $fields;
802
				}
803
				$index = array_search($this->queryResult['data'][$line][$xaxisindex],$this->chart['xAxis']['categories']);
804
				for($f=0;$f<$fields;$f++){
805
					if($this->data['fieldsheaders'][$f]!="")
806
						$name = $this->data['fieldsheaders'][$f] . " for ".$curseries;
807
					else
808
						$name = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].") for ".$curseries;
809
					$lastindex = count($seriesindex[$name]['data']);
810
					for($t=$lastindex;$t<$index;$t++){
811
						$seriesindex[$name]['data'][$t] = 0;
812
					}
813
					$seriesindex[$name]['data'][$index] = $this->queryResult['data'][$line][$yaxisindex+$f];
814
				}
815
			}
816
		}
817
		foreach($seriesindex as $s){
818
			$this->chart['series'][] = $s;
819
		}
820
	}
821

    
822
	function chartDataMultiY(){
823
		$xaxisindex = 0;
824
		$yaxisindex = 1;
825
		//1 series gia kathe field
826
		for($i=0; $i<count($this->data['fields']);$i++){
827
			$this->chart['series'][] = array();
828
			if($this->data['fieldsheaders'][$i]!='')
829
				$this->chart['series'][$i]['name'] = $this->data['fieldsheaders'][$i];
830
			else
831
				$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['agg'].'('.$this->data['fields'][$i]['fld'].')';
832
			if($this->data['fields'][$i]['type'] != 'area')
833
				$this->chart['series'][$i]['type'] = $this->data['fields'][$i]['type'];
834
			else
835
				$this->chart->type='area';
836
			//$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yAxis'];
837

    
838
			if($this->data['fields'][$i]['yaxis']>1){
839
				$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yaxis']-1;
840
			}
841
			$this->chart['series'][$i]['data'] = array();
842
		}
843
		//read data
844
		$res = $this->queryResult['data'];
845

    
846
		($this->size >= count($this->queryResult['data'])) ? $num=count($this->queryResult['data']) : $num=$this->size; 
847
		//$num = $this->size;
848
		$flag = true;
849
		for($line=0;$line<$num;$line++){
850
			//list with x axis categories
851
			if(!in_array($res[$line][$xaxisindex],$this->chart['xAxis']['categories'])){
852
				if(count($this->chart['xAxis']['categories'])<$num){
853
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisindex];
854
					$flag = true;
855
				}
856
				else{
857
					$flag = false;
858
				}
859
			}
860
			else{
861
				$flag = true;
862
			}
863
			//insert new data in each series
864
			if($flag){
865
				for($s=0;$s<count($this->data['fields']);$s++) {
866
					if($this->data['fields'][$s]['type'] == 'pie'){
867
						$this->chart['series'][$s]['data'][$line]['name'] = $res[$line][$xaxisindex];
868
						$this->chart['series'][$s]['data'][$line]['y'] = $res[$line][$s+$yaxisindex];
869
					}
870
					else {
871
						$this->chart['series'][$s]['data'][] = $res[$line][$s+$yaxisindex];
872
					}
873
				}
874
			}
875
		}
876

    
877
		if(count($this->data['fields']) == 1)
878
			$this->chart['legend']['enabled'] = false;
879
	}
880

    
881
	function COM_performQuery() {
882
	
883
		if(!isset($_GET['query']) || $_GET['query'] == ''){
884
			$this->log->error("no query string");
885
			return;
886
		}
887

    
888
		$query = urldecode($_GET['query']);
889

    
890
		$this->log->info("performing query: ".$query);
891
		$resp = json_encode($this->database->performQuery($query));
892
		//$this->log->info("response: ".$resp);
893
		echo $resp;
894
	}
895

    
896
	function sortSeries() {
897
		//exoume mia series gia kathe field
898
		//prepei na broume ayti ti series kai me basi ayti na kanoume sort kai oles tis alles kai ta categories tou xaxis
899
		$snum = 0;
900
		for($snum=0;$snum<$this->data['fields'];$snum++){
901
			if($this->data['fields'][$snum]['agg']."(".$this->data['fields'][$snum]['fld'].")" == $this->data['sort'])
902
				break;
903
		}
904
		//kanw sort ayto to series diatirontas ta kleidia opws einai kai meta kanw sort tis ypoloipes me basi ti seira kleidiwn aytinis
905
		if($this->data['order'] && $this->data['order'] == 'd'){
906
			arsort($this->chart['series'][$snum]['data']);
907
		}
908
		else{
909
			asort($this->chart['series'][$snum]['data']);
910
		}
911
		//ypoloipes
912
		//categories
913
		$tempordered = array() ;
914
		foreach (array_keys($this->chart['xAxis']['categories']) as $key) {
915
			$tempordered[$key] = $this->chart['xAxis']['categories'][$key] ;
916
		}
917
		$this->chart['xAxis']['categories'] = $tempordered;
918
		
919
		for($i=0;$i<$this->data['fields'];$i++){
920
			if($i!=$snum){
921
				unset($tempordered);
922
				$tempordered = array();
923
				$myarray = $this->chart['series'][$i]['data'];
924
				foreach (array_keys($myarray) as $key) {
925
					$tempordered[$key] = $myarray[$key] ;
926
				}
927
				$this->chart['series'][$i]['data'] = $tempordered;
928
			}
929
		}
930
	}
931

    
932

    
933
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
934
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
935
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
936
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
937
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
938
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
939

    
940
	function COM_makeQuery() {
941
		if(!isset($_GET['data'])){
942
			$this->log->info("data param not set: ". print_r($_GET,true));
943
			echo 'empty';
944
		}
945
		else {
946
			$viztype = $_GET['type'];
947
			$data = $_GET['data'];
948
			$this->data = json_decode($data,true);
949

    
950
			$cachedData = $this->data;
951
			unset($cachedData['yaxisheaders']);
952
			unset($cachedData['fieldsheaders']);
953
			unset($cachedData['title']);
954
			unset($cachedData['subtitle']);
955
			unset($cachedData['xaxistitle']);
956
			unset($cachedData['theme']);
957
			unset($cachedData['xStyle']);
958

    
959
			$this->size = $this->data['size'];
960

    
961
//TODO added persistent  option
962
                      $this->persistent= $_GET['persistent']; 
963

    
964
			echo $this->computeChartObject($viztype);
965
		}
966
	}
967

    
968
	function COM_read() {
969
		if(!isset($_GET['facttable']) || !isset($_GET['dimensions']) || !isset($_GET['measures'])) {
970
			echo "empty";
971
		}
972

    
973
		$dims = explode(';',$_GET['dimensions']);
974
		$meas = explode(';',$_GET['measures']);
975
		echo $this->readtest($dims, $meas, $_GET['facttable'],$_GET['aggregation']);
976
	
977
	}
978

    
979
	function COM_check_fact() {
980
		if(!isset($_GET['name'])) {
981
			echo "empty";
982
		}
983
		else{
984
			$res = $this->database->findTableType($_GET['name']);
985
			if($res == false) echo 'false';
986
			else echo $res;
987
		}
988
	}
989
	
990
	function COM_list_of_dim_names() {
991
		$res = $this->database->getAllDims();
992
		if($res != false)
993
			echo json_encode($res);
994
		else
995
			echo "false";
996
	}
997

    
998
	function COM_search_warehouse_by_dims() {
999
		if(!isset($_GET['dims'])) {
1000
			echo 'empty';
1001
		}
1002
		else {
1003
			$res = $this->database->searchWarehousebyDims(explode(';',$_GET['dims']));
1004
			if($res == false) echo "false";
1005
			else echo $res;	
1006
		}
1007
	}
1008

    
1009
	function COM_create_new_fact_table() {
1010
		if(!isset($_GET['table']) || !isset($_GET['dims']) || !isset($_GET['meas'])) {
1011
			echo 'empty';
1012
		} 
1013
		else {
1014
			//set up the list of dims
1015
			$dims = explode(',',$_GET['dims']);
1016
			//set up the list of meas
1017
			$new_meas = rtrim($_GET['meas'],';');
1018
			$meas = explode(';',$new_meas);
1019
			//call the db function
1020
			$res = $this->database->createNewFact($_GET['table'], $dims, $meas);
1021
			if($res == false)
1022
				echo 'false';
1023
			else
1024
				echo 'true';
1025
		}
1026
	}
1027

    
1028
	function COM_list_of_fact_tables() {
1029
		$res = json_encode($this->database->getFactsNames());
1030
		if($res == null)
1031
			echo 'null';
1032
		else
1033
			echo $res;
1034
	}
1035

    
1036
	function COM_list_of_meas() {
1037
		if(!isset($_GET['table'])) {
1038
			echo 'empty';
1039
		} 
1040
		else {
1041
			$res = $this->database->findMeas($this->database->findTable($_GET['table'],'fact'));
1042
			if($res == false)
1043
				echo 'false';
1044
			else
1045
				echo json_encode($res);
1046
		}
1047
	}
1048

    
1049
	function COM_list_of_dims() {
1050
		$res = $this->database->getDims();
1051
		if($res == false)
1052
			echo 'false';
1053
		else
1054
			echo json_encode($res);
1055
	}
1056

    
1057
	function COM_del_meas() {
1058
		if(!isset($_GET['facttable']) || !isset($_GET['name'])) {
1059
			echo "empty";
1060
		}
1061
		else {
1062
			$res = $this->database->delMeas($_GET['facttable'], $_GET['name']);
1063
			if($res == true)
1064
				echo "true";
1065
			else 
1066
				echo "false";
1067
		}
1068
	}
1069

    
1070
	function COM_add_meas() {
1071
		if(!isset($_GET['facttable']) || !isset($_GET['name']) || !isset($_GET['formal_name']) || !isset($_GET['type'])) {
1072
			echo "empty";
1073
		}
1074
		else {
1075
			$res = $this->database->addMeas($_GET['facttable'], $_GET['name'], $_GET['formal_name'], $_GET['type']);
1076
			if($res == true)
1077
				echo "true";
1078
			else 
1079
				echo "false";
1080
		}
1081
	}
1082

    
1083
	function COM_meas_range_data() {
1084
		if(!isset($_GET['facttable']) || !isset($_GET['measurement'])) {
1085
			echo "empty";
1086
		}
1087
		else {
1088
			$res = $this->database->measRangeData($_GET['facttable'],$_GET['measurement']);
1089
			echo json_encode($res);
1090
		}
1091
	}
1092

    
1093
	function COM_get_dims_fields_list(){
1094
		if(!isset($_GET['table'])) {
1095
			echo "empty";
1096
		}
1097
		else {
1098
			$res = $this->database->getDimsList($_GET['table']);
1099

    
1100
			if($res == false)
1101
				echo "false";
1102
			else if($res == null)
1103
				echo "null";
1104
			else
1105
				echo json_encode($res);
1106
		}
1107
	}
1108

    
1109

    
1110
	function fetchData($table, $dim1, $dim2, $meas, $constraints = null) {
1111
		return $this->database->getJsonData($table, $dim1, $dim2, $meas, $constraints);
1112
	}
1113

    
1114
	function readData($table, $dim1, $dim2, $meas, $constraints = null) {
1115
		return $this->database->readData($table, $dim1, $dim2, $meas, $constraints);
1116
	}
1117

    
1118
	function timeload($start,$end) {
1119
		$this->database->loadTime($start,$end);
1120
	}
1121

    
1122
	function readtest($dimensions,$measures,$table,$aggr) {
1123
		$data = $this->database->readtest($dimensions,$measures,$table,$aggr);
1124
		//echo json_encode($data,JSON_NUMERIC_CHECK);
1125
		echo json_encode($data);
1126
	}
1127
	
1128
	
1129
	/////////////////////////////////////////////////////////////////////////////////////
1130

    
1131
}
1132

    
1133
/*
1134

    
1135
	function createChartDataGroup($givenType) {
1136
		//exoume anagkastika ena mono yaxis
1137
		//ena series gia kathe diaforetiki timi tou tritou pediou
1138
		if($givenType == 'chart'){
1139
			$this->chart['xAxis']['categories'] = array();
1140
			
1141
		}
1142
		else{
1143
			unset($this->chart['xAxis']['categories']);
1144
		}
1145
		$this->chart['series'] = array();
1146
		
1147
		$currentSeries = "";
1148
		$seriesCnt = -1;
1149
		$numOfColumns = count($this->queryResult[0]);
1150
		if(count($this->data['series']) == 0)
1151
			$sNameIndex = -1;
1152
		else
1153
			$sNameIndex = count($this->data['fields']) +1;
1154
			//$sNameIndex = $numOfColumns-1;
1155
		$xaxisIndex = 0;
1156
		$fStartindex = 1;
1157
		$groupindex = 2;
1158
		$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['name'];
1159
		//arxikopoiisi
1160
		$this->chart['yAxis'] = array();
1161
		$this->chart['series'] = array();
1162
		$this->chart['legend']['enabled'] = false;
1163
		//edw tha ektelestei mono mia fora <-------NA TO ALLAKSW
1164
		for($i=0; $i<count($this->data['fields']);$i++){
1165
			//gia kathe field, exoume enan yaxis kai ena series
1166
			$this->chart['yAxis'][] = array();
1167
			$this->chart['yAxis'][$i]['labels'] = array();
1168
			$this->chart['yAxis'][$i]['offset'] = 70;
1169
			//$this->chart['yAxis'][$i]['labels']['formatter'] = 'simple';
1170
			$this->chart['yAxis'][$i]['title'] = array();
1171
			$this->chart['yAxis'][$i]['title']['text'] = $this->data['fields'][$i]['fld'];
1172
			if($i>0){
1173
				$this->chart['yAxis'][$i]['opposite'] = true;
1174
				$this->chart['yAxis'][$i]['gridLineWidth'] = 0;
1175
			}
1176

    
1177
			//$this->chart['series'][] = array();
1178
			//$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['fld'];
1179
			
1180
			//if($i>0) {
1181
			//	$this->chart['series'][$i]['yAxis'] = $i;
1182
			//}
1183
			//$this->chart['series'][$i]['data'] = array();
1184
			//$seriesData[$i] = array();
1185
		}
1186
		
1187
		$res = $this->queryResult['data'];
1188
		//gia kathe diaforetiki timi tou pediou 2, kainourgio category an exoume chart
1189
		//gia kathe diaforetiki timi tou pediou 3, kainourgio series me onoma tin timi tou pediou 3
1190
		//oso eisai sto idio series, apla prosthese ta data
1191
		//an allakseis ftiakse kainourgio
1192
		for($line=0;$line<count($this->queryResult['data']);$line++){
1193
			//list with x axis categories
1194
			if($givenType == 'chart'){
1195
				if(!in_array($res[$line][$xaxisIndex],$this->chart['xAxis']['categories'])){
1196
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisIndex];
1197
				}
1198
			}
1199
			//new series? 
1200
			if($res[$line][$groupindex] != $currentSeries){
1201
				$currentSeries = $res[$line][$groupindex];
1202
				$this->chart['series'][] = array();
1203
				$seriesCnt++;
1204
				$this->chart['series'][$seriesCnt]['name'] = $currentSeries;
1205
				$this->chart['series'][$seriesCnt]['data'] = array();
1206
			}
1207
			$this->chart['series'][$seriesCnt]['data'][] = $res[$line][$fStartindex];
1208
		}
1209

    
1210
		if(count($this->chart['xAxis']['categories']) > 15){
1211
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
1212
			$this->chart['xAxis']['labels']['rotation'] = 45;
1213
		}
1214

    
1215
	}
1216
*/
1217
?>
(9-9/28)