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 extract(year from date(r.date)), count(r.id)  from result r natural join result_projects rp join project p on rp.project=p.id where type='publication' and extract(year from date(r.date)) >= 2007 and date <> ''  and funding_lvl2='ERC' and date(r.date) < now() group by extract(year from date(r.date)) order by extract(year from date(r.date))";
84
		$this->myqueries['wttime'] = array();
85
		$this->myqueries['wttime']['q'] = "select extract(year from date(r.date)), count(r.id)  from result r natural join result_projects rp join project p on rp.project=p.id where type='publication' and extract(year from date(r.date)) >= 2007 and date <> ''  and funding_lvl0='WT' and date(r.date) < now() group by extract(year from date(r.date)) order by extract(year from date(r.date))";
86

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

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

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

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

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

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

    
110

    
111
//FET queries
112

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

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

    
120

    
121

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

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

    
126

    
127

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

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

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

    
135

    
136

    
137

    
138

    
139

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

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

    
145

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

    
149

    
150

    
151
/* FCT*/
152

    
153

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

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

    
158

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

    
162

    
163
$this->myqueries['fctfp7time']= array();
164
$this->myqueries['fctfp7time']['q']="select extract(year from date(r.date))::integer , 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' group by extract(year from date(r.date)) order by extract(year from date(r.date))";
165

    
166
	
167

    
168
$this->colors[0] = '#4572A7';
169
		$this->colors[1] = '#AA4643';
170
		$this->colors[2] = '#89A54E';
171

    
172
		$this->types[0] = 'column';
173
		$this->types[1] = 'spline';
174
		$this->types[2] = 'spline';
175

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

    
206

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

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

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

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

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

    
254
                $this->chart['plotOptions']['areaspline'] = array();
255
                $this->chart['plotOptions']['areaspline']['stacking'] = null;
256

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

    
277
		$this->chart['plotOptions']['area'] = array();
278
	}
279

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

    
300
		
301
			return $this->computeChartObject($viztype);
302
		}
303
	}
304

    
305
	function computeChartObject($viztype){
306
		//$this->log->info("DATA: ".print_r($this->data,true));
307
		if(!isset($this->data['query'])){
308
			$this->log->info("query not set");
309
			$this->queryResult = $this->database->getData($this->data);
310
		}
311
		else{
312
			$this->log->info("query set");
313
			$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($this->myqueries[$this->data['query']]['q']));
314
		}
315

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
527

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

    
651
	}
652

    
653
	function createChartData(){
654
		$this->chart['xAxis']['categories'] = array();
655
		unset($this->chart['chart']['type']);
656
		$this->chart['series'] = array();
657
		$this->chart['yAxis'] = array();
658
		$xaxisindex = 0;
659
		$yaxisindex = 1;
660
		$left = 0;
661
		$right = 0;
662
		//print_r($this->queryResult['data']);
663
		$fields = count($this->data['fields']);
664
		$dimindex = 1+$fields;
665

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
933

    
934
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
935
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
936
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
937
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
938
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
939
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
940

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

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

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

    
962
			echo $this->computeChartObject($viztype);
963
		}
964
	}
965

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1107

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

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

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

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

    
1129
}
1130

    
1131
/*
1132

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

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

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

    
1213
	}
1214
*/
1215
?>
(7-7/28)