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

    
89

    
90
		$this->myqueries['projpubs'] = array();
91
		$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";
92

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

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

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

    
103
 $this->myqueries['egiProjects']= array();
104
		$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;";
105

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

    
113

    
114
//FET queries
115

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

    
121
                $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;";
122

    
123

    
124

    
125
$this->myqueries['fetProactive']= array();
126

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

    
129

    
130

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

    
134
$this->myqueries['fetOpen']= array();
135

    
136
$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;";
137

    
138

    
139

    
140

    
141

    
142

    
143
/*TODO added WT pie query - copy to beta and prod*/
144

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

    
148

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

    
152

    
153

    
154
/* FCT*/
155

    
156

    
157
 $this->myqueries['fcttime'] = array();
158

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

    
161

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

    
165

    
166
$this->myqueries['fctfp7time']= array();
167

    
168
$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 ;";
169

    
170

    
171
	
172

    
173
$this->colors[0] = '#4572A7';
174
		$this->colors[1] = '#AA4643';
175
		$this->colors[2] = '#89A54E';
176

    
177
		$this->types[0] = 'column';
178
		$this->types[1] = 'spline';
179
		$this->types[2] = 'spline';
180

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

    
211

    
212
		/*$this->chart['xAxis']['labels']['overflow'] = null;//'justify';
213
		$this->chart['xAxis']['labels']['style'] = array();
214
		$this->chart['xAxis']['labels']['style']['font-size'] = '10px';
215

    
216
		$this->chart['xAxis']['showEmpty'] = true;
217
		$this->chart['xAxis']['labels']['enabled'] = true;
218
		$this->chart['xAxis']['labels']['style'] = array();
219
		$this->chart['xAxis']['labels']['style']['font-size'] = '8px';
220
		//$this->chart['xAxis']['labels']['style']['margin-left'] = '10px';
221
		//$this->chart['xAxis']['labels']['style']['margin-right'] = '10px';*/
222
		$this->chart['xAxis']['startOnTick'] = true;
223
		$this->chart['xAxis']['endOnTick'] = true;
224
		$this->chart['xAxis']['showFirstLabel'] = true;
225
		$this->chart['xAxis']['showLastLabel'] = true;
226

    
227
		$this->chart['tooltip']['percentageDecimals'] = 1;
228
		$this->chart['tooltip']['valueDecimals'] = 1;
229
		$this->chart['legend']['layout'] = 'vertical';
230
		$this->chart['legend']['align'] = 'right';
231
		$this->chart['legend']['verticalAlign'] = 'top';
232
		$this->chart['legend']['floating'] = true;
233
		$this->chart['legend']['borderWidth'] = 0;
234
		$this->chart['legend']['x'] = -10;
235
		$this->chart['legend']['y'] = 50;
236
		$this->chart['legend']['padding'] = 3;
237
		$this->chart['legend']['itemMarginBottom'] = 5;
238

    
239
		//for scatter plots
240
		$this->chart['plotOptions'] = array();
241
		$this->chart['plotOptions']['series'] = array();
242
	        $this->chart['plotOptions']['series']['showCheckbox'] = true;
243
	        $this->chart['plotOptions']['series']['selected'] = true;
244
		$this->chart['plotOptions']['scatter'] = array();
245
		$this->chart['plotOptions']['scatter']['marker'] = array();
246
		$this->chart['plotOptions']['scatter']['marker']['radius'] = 5;
247
		$this->chart['plotOptions']['scatter']['marker']['states'] = array();
248
		$this->chart['plotOptions']['scatter']['marker']['states']['hover'] = array();
249
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['enabled'] = true;
250
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['lineColor'] = 'rgb(100,100,100)';
251
		$this->chart['plotOptions']['scatter']['states'] = array();
252
		$this->chart['plotOptions']['scatter']['states']['hover'] = array();
253
		$this->chart['plotOptions']['scatter']['states']['hover']['marker'] = array();
254
		$this->chart['plotOptions']['scatter']['states']['hover']['marker']['enabled'] = false;
255

    
256
		$this->chart['plotOptions']['area'] = array();
257
                $this->chart['plotOptions']['area']['stacking'] = null;
258

    
259
                $this->chart['plotOptions']['areaspline'] = array();
260
                $this->chart['plotOptions']['areaspline']['stacking'] = null;
261

    
262
		//for pie charts
263
		$this->chart['plotOptions']['pie'] = array();
264
		$this->chart['plotOptions']['pie']['allowPointSelect'] = true;
265
		//$this->chart['plotOptions']['pie']['size'] = '50%';
266
		$this->chart['plotOptions']['pie']['cursor'] = 'pointer';
267
		$this->chart['plotOptions']['pie']['showInLegend'] = true;
268
		$this->chart['plotOptions']['pie']['dataLabels'] = array();
269
		$this->chart['plotOptions']['pie']['dataLabels']['enabled'] = true;
270
		$this->chart['plotOptions']['pie']['dataLabels']['color'] = '#000000';
271
		$this->chart['plotOptions']['pie']['dataLabels']['connectorColor'] = '#000000';
272
		$this->chart['plotOptions']['pie']['dataLabels']['crop'] = false;
273
		$this->chart['plotOptions']['pie']['dataLabels']['distance'] = 10;
274
		//$this->chart['plotOptions']['pie']['dataLabels']['formatter'] = '';
275
		//for column
276
		$this->chart['plotOptions']['column'] = array();
277
		$this->chart['plotOptions']['column']['allowPointSelect'] = true;
278
		$this->chart['plotOptions']['column']['cursor'] = 'pointer';
279
		$this->chart['plotOptions']['column']['showInLegend'] = true;
280
		$this->chart['plotOptions']['column']['grouping'] = true;
281

    
282
		$this->chart['plotOptions']['area'] = array();
283
	}
284

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

    
309
	function computeChartObject($viztype,$persistent){
310
		//$this->log->info("DATA: ".print_r($this->data,true));
311
		if(!isset($this->data['query'])){
312
			$this->log->info("query not set");
313
//TODO here add persistent value??
314
			$this->queryResult = $this->database->getData($this->data,$persistent);
315
		}
316
		else{
317
			$this->log->info("query set");
318
			$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($this->myqueries[$this->data['query']]['q'],true));
319

    
320
		}
321

    
322

    
323
		$this->log->info("data from DB: ".print_r($this->queryResult,true));
324
		if($viztype=="chart"){
325
			if($this->queryResult['type'] == 'scatter'){
326
				$this->createScatterData();
327
			}
328
			else{
329
				$this->createChartData();
330
			}
331

    
332
			if(isset($this->data['in']) && count($this->data['in'])){
333
				$this->chart2 = $this->chart;
334
				for($w=0;$w<count($this->chart['series']);$w++){
335
					//$whichfield = $this->data['in'][$w]['f'];
336
					//$this->chart2 = $this->chart;
337
					$data = $this->chart2['series'][$w]['data'];
338
					for($i=1;$i<count($this->chart2['xAxis']['categories']);$i++){
339
						$data[$i] += $data[$i-1];
340
					}
341
					$this->chart2['series'][$w]['data'] = $data;
342
				}
343
				//return json_encode(array($this->chart,$this->chart2),JSON_NUMERIC_CHECK);
344
				return json_encode(array($this->chart,$this->chart2));
345
			}
346

    
347
			//return json_encode($this->chart,JSON_NUMERIC_CHECK);
348
			return json_encode($this->chart);
349
		}
350
		if($viztype=='table'){
351
			//return json_encode($this->queryResult,JSON_NUMERIC_CHECK);
352
			return json_encode($this->queryResult);
353
		}
354
	}
355

    
356
	function COM_getMeasMetadata(){
357
		if(!isset($_GET['table'])) {
358
			echo 'empty';
359
		}
360
		else{
361
			echo json_encode($this->database->getMeasMetadata($_GET['table']));
362
		}
363
	}
364

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

    
390
	function defaultChart() {
391
		//get the default values for this table
392
		$temp = $this->database->getDefaultData($_GET['table']);
393
		$this->queryResult = $temp['data'];
394
		$this->data = $temp['selectedData'];
395
		$this->data['group'] = '';
396
		$this->data['color'] = 'no';
397
		//print_r($this->queryResult);return;
398
		if($temp['type'] == 'chart')
399
			$this->createChartData($temp['type']);
400
		else
401
			$this->createScatterData('');
402
		//$this->chart['chart']['type'] = $temp['type'];
403
		//print_r($this->chart);return;
404
		//print_r($this->data);
405
		$toreturn = array();
406
		$toreturn['chart'] = $this->chart;
407
		$toreturn['selectedData'] = $this->data;
408
		//return json_encode($toreturn,JSON_NUMERIC_CHECK);
409
		return json_encode($toreturn);
410
	}
411

    
412
	function defaultChartSelections() {
413
		//get the default values for this table
414
		$temp = $this->database->getDefaultData($_GET['table']);
415
		$this->queryResult = $temp['data'];
416
		$this->data = $temp['selectedData'];
417

    
418
		//echo json_encode($this->data,JSON_NUMERIC_CHECK);
419
		echo json_encode($this->data);
420
	}
421

    
422
	function COM_getFilterData(){
423
		if(!isset($_GET['table']) || !isset($_GET['field'])) {
424
			echo 'empty';
425
		}
426
		else {
427
			echo $this->computeFilterObject($_GET['table'],$_GET['field'],true);
428
		}
429
	}
430

    
431
	function getFilterData($table, $field){
432
		return $this->computeFilterObject($table,$field,false);
433
	}
434

    
435
	function computeFilterObject($table, $field, $encode){
436
		$flds = explode("-",$field);
437
		if(count($flds)>1){
438
			$ctable = $flds[count($flds)-2];
439
			$field = $flds[count($flds)-1];
440
		}
441
		else {
442
			$ctable = $table;
443
			$field = $field;
444
		}
445
		//call mydb function
446
		if($encode)
447
			//return json_encode($this->database->getFilterData($ctable,$field),JSON_NUMERIC_CHECK);
448
			return json_encode($this->database->getFilterData($ctable,$field));
449
		else
450
			return $this->database->getFilterData($ctable,$field);
451
	}
452

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

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

    
505
	function makeAttrList($attrList, $dimensions, $ancestors,$facttable){
506
		$newList = array();
507
		if(is_array($attrList)) {
508
			foreach($attrList as $attr) {
509
				if($attr['name'] != 'id' && $attr['name']!=$facttable) {
510
					$newAttr = array();
511
					$newAttr['name'] = $attr['name'];
512
					if(isset($newAttr['view']))
513
						$newAttr['view'] = $attr['view'];
514
					$newAttr['type'] = $attr['type'];
515
					if(isset($attr['analysed']) && !in_array($attr['name'],$ancestors)){
516

    
517
						$finddim = search($dimensions, 'name', $attr['name']);
518
						$nextdim = search($finddim,'type','dimension');//print_r($nextdim);
519
						$newnewList = $this->makeAttrList($nextdim[0]['attrib'],$dimensions, array_merge($ancestors,array($attr['name'])),$facttable);
520
						$ancestors[] = $attr['name'];
521
						$newAttr['analysed'] = $attr['name'];
522
						$newAttr['attrib']= $newnewList;
523
					}
524
					else{
525
						$newAttr['attrib']= array();
526
					}
527
					array_push($newList,$newAttr);
528
				}
529
			}
530
		}
531
		return $newList;
532
	}
533

    
534

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

    
658
	}
659

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

    
723
		if($this->data['group'] != ''){//periptwsi analyze
724
			$this->chartDataGroup();
725
		}
726
		else{//polloi yaxis xwris group
727
			$this->chartDataMultiY();
728
		}
729

    
730
		if(count($this->chart['xAxis']['categories']) > 30){
731
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
732
			//$this->chart['xAxis']['labels']['rotation'] = 90;
733
		}//print_r($this->chart);
734
		else{
735
			$trimflag = false;
736
			for($i=0;$i<count($this->chart['xAxis']['categories']);$i++){
737
				if(is_string($this->chart['xAxis']['categories'][$i]) && strlen($this->chart['xAxis']['categories'][$i])>10){
738
					$trimflag = true;
739
					break;
740
				}
741
			}
742
			if($trimflag){
743
				$this->chart['xAxis']['labels']['formatter'] = 'cond-trim';
744
				//$this->chart['xAxis']['labels']['rotation'] = 90;
745
			}
746
		}
747
	}
748

    
749
	function chartDataGroup(){
750
		//ena series gia kathe field gia kathe timi tou groupdim
751
		//read data
752
		$res = $this->queryResult['data'];
753
		$seriesindex = array();
754
		$names = array();
755
		$name = '';
756
		$seriescount = 0;
757
		$curseries = '';
758
		$xaxisindex = 0;
759
		$yaxisindex = 1;
760
		$fields = count($this->data['fields']);
761
		$dimindex = 1+$fields;
762

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

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

    
830
	function chartDataMultiY(){
831
		$xaxisindex = 0;
832
		$yaxisindex = 1;
833
		//1 series gia kathe field
834
		for($i=0; $i<count($this->data['fields']);$i++){
835
			$this->chart['series'][] = array();
836
			if($this->data['fieldsheaders'][$i]!='')
837
				$this->chart['series'][$i]['name'] = $this->data['fieldsheaders'][$i];
838
			else
839
				$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['agg'].'('.$this->data['fields'][$i]['fld'].')';
840
			if($this->data['fields'][$i]['type'] != 'area')
841
				$this->chart['series'][$i]['type'] = $this->data['fields'][$i]['type'];
842
			else
843
				$this->chart->type='area';
844
			//$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yAxis'];
845

    
846
			if($this->data['fields'][$i]['yaxis']>1){
847
				$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yaxis']-1;
848
			}
849
			$this->chart['series'][$i]['data'] = array();
850
		}
851
		//read data
852
		$res = $this->queryResult['data'];
853

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

    
885
		if(count($this->data['fields']) == 1)
886
			$this->chart['legend']['enabled'] = false;
887
	}
888

    
889
	function COM_performQuery() {
890
	
891
		if(!isset($_GET['query']) || $_GET['query'] == ''){
892
			$this->log->error("no query string");
893
			return;
894
		}
895

    
896
		$query = urldecode($_GET['query']);
897

    
898
		$this->log->info("performing query: ".$query);
899
		$resp = json_encode($this->database->performQuery($query));
900
		//$this->log->info("response: ".$resp);
901
		echo $resp;
902
	}
903

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

    
940

    
941
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
942
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
943
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
944
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
945
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
946
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
947

    
948
	function COM_makeQuery() {
949
		if(!isset($_GET['data'])){
950
			$this->log->info("data param not set: ". print_r($_GET,true));
951
			echo 'empty';
952
		}
953
		else {
954
			$viztype = $_GET['type'];
955
			$data = $_GET['data'];
956
			$this->data = json_decode($data,true);
957

    
958
			$cachedData = $this->data;
959
			unset($cachedData['yaxisheaders']);
960
			unset($cachedData['fieldsheaders']);
961
			unset($cachedData['title']);
962
			unset($cachedData['subtitle']);
963
			unset($cachedData['xaxistitle']);
964
			unset($cachedData['theme']);
965
			unset($cachedData['xStyle']);
966

    
967
			$this->size = $this->data['size'];
968

    
969
//TODO added persistent  option
970
                      $this->persistent= $_GET['persistent']; 
971

    
972
			echo $this->computeChartObject($viztype);
973
		}
974
	}
975

    
976
	function COM_read() {
977
		if(!isset($_GET['facttable']) || !isset($_GET['dimensions']) || !isset($_GET['measures'])) {
978
			echo "empty";
979
		}
980

    
981
		$dims = explode(';',$_GET['dimensions']);
982
		$meas = explode(';',$_GET['measures']);
983
		echo $this->readtest($dims, $meas, $_GET['facttable'],$_GET['aggregation']);
984
	
985
	}
986

    
987
	function COM_check_fact() {
988
		if(!isset($_GET['name'])) {
989
			echo "empty";
990
		}
991
		else{
992
			$res = $this->database->findTableType($_GET['name']);
993
			if($res == false) echo 'false';
994
			else echo $res;
995
		}
996
	}
997
	
998
	function COM_list_of_dim_names() {
999
		$res = $this->database->getAllDims();
1000
		if($res != false)
1001
			echo json_encode($res);
1002
		else
1003
			echo "false";
1004
	}
1005

    
1006
	function COM_search_warehouse_by_dims() {
1007
		if(!isset($_GET['dims'])) {
1008
			echo 'empty';
1009
		}
1010
		else {
1011
			$res = $this->database->searchWarehousebyDims(explode(';',$_GET['dims']));
1012
			if($res == false) echo "false";
1013
			else echo $res;	
1014
		}
1015
	}
1016

    
1017
	function COM_create_new_fact_table() {
1018
		if(!isset($_GET['table']) || !isset($_GET['dims']) || !isset($_GET['meas'])) {
1019
			echo 'empty';
1020
		} 
1021
		else {
1022
			//set up the list of dims
1023
			$dims = explode(',',$_GET['dims']);
1024
			//set up the list of meas
1025
			$new_meas = rtrim($_GET['meas'],';');
1026
			$meas = explode(';',$new_meas);
1027
			//call the db function
1028
			$res = $this->database->createNewFact($_GET['table'], $dims, $meas);
1029
			if($res == false)
1030
				echo 'false';
1031
			else
1032
				echo 'true';
1033
		}
1034
	}
1035

    
1036
	function COM_list_of_fact_tables() {
1037
		$res = json_encode($this->database->getFactsNames());
1038
		if($res == null)
1039
			echo 'null';
1040
		else
1041
			echo $res;
1042
	}
1043

    
1044
	function COM_list_of_meas() {
1045
		if(!isset($_GET['table'])) {
1046
			echo 'empty';
1047
		} 
1048
		else {
1049
			$res = $this->database->findMeas($this->database->findTable($_GET['table'],'fact'));
1050
			if($res == false)
1051
				echo 'false';
1052
			else
1053
				echo json_encode($res);
1054
		}
1055
	}
1056

    
1057
	function COM_list_of_dims() {
1058
		$res = $this->database->getDims();
1059
		if($res == false)
1060
			echo 'false';
1061
		else
1062
			echo json_encode($res);
1063
	}
1064

    
1065
	function COM_del_meas() {
1066
		if(!isset($_GET['facttable']) || !isset($_GET['name'])) {
1067
			echo "empty";
1068
		}
1069
		else {
1070
			$res = $this->database->delMeas($_GET['facttable'], $_GET['name']);
1071
			if($res == true)
1072
				echo "true";
1073
			else 
1074
				echo "false";
1075
		}
1076
	}
1077

    
1078
	function COM_add_meas() {
1079
		if(!isset($_GET['facttable']) || !isset($_GET['name']) || !isset($_GET['formal_name']) || !isset($_GET['type'])) {
1080
			echo "empty";
1081
		}
1082
		else {
1083
			$res = $this->database->addMeas($_GET['facttable'], $_GET['name'], $_GET['formal_name'], $_GET['type']);
1084
			if($res == true)
1085
				echo "true";
1086
			else 
1087
				echo "false";
1088
		}
1089
	}
1090

    
1091
	function COM_meas_range_data() {
1092
		if(!isset($_GET['facttable']) || !isset($_GET['measurement'])) {
1093
			echo "empty";
1094
		}
1095
		else {
1096
			$res = $this->database->measRangeData($_GET['facttable'],$_GET['measurement']);
1097
			echo json_encode($res);
1098
		}
1099
	}
1100

    
1101
	function COM_get_dims_fields_list(){
1102
		if(!isset($_GET['table'])) {
1103
			echo "empty";
1104
		}
1105
		else {
1106
			$res = $this->database->getDimsList($_GET['table']);
1107

    
1108
			if($res == false)
1109
				echo "false";
1110
			else if($res == null)
1111
				echo "null";
1112
			else
1113
				echo json_encode($res);
1114
		}
1115
	}
1116

    
1117

    
1118
	function fetchData($table, $dim1, $dim2, $meas, $constraints = null) {
1119
		return $this->database->getJsonData($table, $dim1, $dim2, $meas, $constraints);
1120
	}
1121

    
1122
	function readData($table, $dim1, $dim2, $meas, $constraints = null) {
1123
		return $this->database->readData($table, $dim1, $dim2, $meas, $constraints);
1124
	}
1125

    
1126
	function timeload($start,$end) {
1127
		$this->database->loadTime($start,$end);
1128
	}
1129

    
1130
	function readtest($dimensions,$measures,$table,$aggr) {
1131
		$data = $this->database->readtest($dimensions,$measures,$table,$aggr);
1132
		//echo json_encode($data,JSON_NUMERIC_CHECK);
1133
		echo json_encode($data);
1134
	}
1135
	
1136
	
1137
	/////////////////////////////////////////////////////////////////////////////////////
1138

    
1139
}
1140

    
1141
/*
1142

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

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

    
1218
		if(count($this->chart['xAxis']['categories']) > 15){
1219
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
1220
			$this->chart['xAxis']['labels']['rotation'] = 45;
1221
		}
1222

    
1223
	}
1224
*/
1225
?>
(7-7/28)