Project

General

Profile

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

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

    
18
class Controller {
19

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

    
31
	function __construct($myflag = true){
32

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

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

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

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

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

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

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

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

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

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

    
73
		
74

    
75

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

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

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

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

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

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

    
92

    
93

    
94
/* WT IS NOW A FUNDER */
95
	$this->myqueries['wttime'] = array();
96

    
97
$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 funder='Wellcome Trust' and r.year <= 2017 group by r.year order by r.year;";
98
/* 
99
 $this->myqueries['wttime'] = array();
100
        $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;";
101

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

    
106
		$this->myqueries['projpubsf2'] = array();
107
		$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";
108

    
109
		$this->myqueries['fp7pubsdtsrc']= array();
110
		$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' and datasource.type!='Publication Catalogue' group by datasource.type";
111

    
112

    
113
// ERI - TODO egi timeline has been updated- change it everywhere 
114
$this->myqueries['egiTimeline']= array();
115
$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>2007 group by r.year order by r.year asc;";
116

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

    
120
$this->myqueries['egiVO']= array();
121
		$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;";
122
		
123
$this->myqueries['egiOA']= array();
124
$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 
125
by r.bestlicense";
126

    
127

    
128
//FET queries
129

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

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

    
137

    
138

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

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

    
143

    
144

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

    
148
$this->myqueries['fetOpen']= array();
149

    
150
$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;";
151

    
152

    
153
/*TODO added WT pie query - copy to beta and prod*/
154
/*
155
$this->myqueries['wtOA']= array();
156
$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;";
157
*/
158

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

    
162

    
163

    
164
/*
165
WT IS NOW A FUNDER !!!
166
*/
167

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

    
171

    
172
/* FCT*/
173
/*
174

    
175
 $this->myqueries['fcttime'] = array();
176

    
177
 $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";
178

    
179

    
180
$this->myqueries['fctOA']= array();
181
$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";
182

    
183

    
184
$this->myqueries['fctfp7time']= array();
185

    
186
$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 ;";
187

    
188

    
189
*/
190

    
191

    
192
/* FCT FOR NEW FUNDERS*/
193

    
194

    
195
 $this->myqueries['fcttime'] = array();
196

    
197
 $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 < 2017  and  funder = 'FCT'  group by r.year order by r.year";
198
$this->myqueries['fctOA']= array();
199
$this->myqueries['fctOA']['q'] ="SELECT bestlicense as xfield,count(distinct result_projects.id)  as field0   FROM result,result_projects, project WHERE result.result_projects= result_projects.id  AND  result_projects.project = project.id and  funder='FCT'  and type='publication'  group by bestlicense";
200

    
201

    
202
$this->myqueries['fctfp7time']= array();
203

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

    
206
$this->myqueries['dtscrtype']= array();
207

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

    
210
// FOR DATASOURCE AD PROJCT VIEW PAGES
211

    
212

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

    
216
$this->myqueries['dtsrcYear']['q'] = array();
217

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

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

    
223

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

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

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

    
233

    
234

    
235
$this->myqueries['dtsrcTimeline']= array();
236

    
237
$this->myqueries['dtsrcTimeline']['q'] = "select  ds.timestamp_month as xfield , ds.numberofviews  as field0 from usagestats.datasourcesstats ds where ds.datasourceid='DTSRC_NAME'  order by to_date(ds.timestamp_month,'MM/YYYY')  limit 10 ; ";
238

    
239
$this->myqueries['dtsrcFund']= array();
240

    
241
$this->myqueries['dtsrcFund']['q'] = "select  p.funder as xfield, sum(ps.numberofviews)  as field0 from usagestats.projectsstats ps, datasource_results dr, result_projects rp , project p where dr.id='DTSRC_NAME' and dr.result=rp.id and rp.project=p.id  and ps.projectid=p.id group by p.funder order by  sum (ps.numberofviews) desc limit 10;";
242

    
243
$this->myqueries['dtsrcAreas']=array();
244
$this->myqueries['dtsrcAreas']['q'] = "select  p.funding_lvl2 as xfield, sum(ps.numberofviews)  as field0 from usagestats.projectsstats ps, datasource_results dr, result_projects rp , project p where dr.id='DTSRC_NAME'  and dr.result=rp.id and rp.project=p.id  and p.funding_lvl2!=' ' and ps.projectid=p.id group by p.funding_lvl2 order by  sum (ps.numberofviews) desc limit 10;";
245

    
246

    
247
$this->myqueries['dtsrcProjT']=array();
248
$this->myqueries['dtsrcProjT']['q'] = "select  r. as xfield, sum(ps.numberofviews)  as field0 from usagestats.projectsstats ps, datasource_results dr, result_projects rp , project p where dr.id='DTSRC_NAME'  and dr.result=rp.id and rp.project=p.id  and p.funding_lvl2!=' ' and ps.projectid=p.id group by p.id order by sum (ps.numberofviews) desc limit 10; ";
249

    
250

    
251

    
252
$this->myqueries['projTimeline']= array();
253
$this->myqueries['projTimeline']['q'] = "select  ps.timestamp_month as xfield , ps.numberofviews  as field0 from usagestats.projectsstats ps where ps.projectid='PROJ_TITLE'  order by to_date(ps.timestamp_month,'MM/YYYY') limit 10 ;";
254

    
255

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

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

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

    
265

    
266
$this->myqueries['orgTimeline']= array();
267
$this->myqueries['orgTimeline']['q'] = "select  os.timestamp_month as xfield , os.numberofviews  as field0 from usagestats.organizationsstats os where os.organizationid='ORG_TITLE'  order by to_date(os.timestamp_month,'MM/YYYY') limit 10 ;";
268

    
269
$this->myqueries['orgProj']= array();
270
$this->myqueries['orgProj']['q'] = "select  p.title  as xfield , sum(ps.numberofviews)  as field0 from usagestats.projectsstats ps , project_organizations por project p where por.organization='ORG_TITLE'  and p.id= ps.projectid  group by p.title, p.id order by sum(ps.numberofviews) limit 10 ;";
271

    
272

    
273
$this->myqueries['resTimeline']= array();
274
$this->myqueries['resTimeline']['q'] = "select  rs.timestamp_month as xfield , rs.numberofviews  as field0 from usagestats.resultsstats rs where rs.resultid='RES_TITLE'  order by to_date(rs.timestamp_month,'MM/YYYY') limit 10 ;";
275

    
276

    
277
//h2020 stuff
278

    
279

    
280
$this->myqueries['h2020Timeline']= array();
281
$this->myqueries['h2020Timeline']['q'] = "select r.year as xfield, count(r.id) as field0 from result r, project_results pr, project p  where pr.id=p.id  and  pr.result=r.id and p.funding_lvl0='H2020' and r.year>0 group by r.year order by r.year asc;";
282

    
283

    
284
$this->myqueries['h2020dtscr']= array();
285
$this->myqueries['h2020dtscr']['q']="select d.type,count ( distinct r.id) from datasource d, result r, result_datasources rd, project p, project_results pr where p.funding_lvl0='H2020' and r.type='publication' and p.id=pr.id and r.id=pr.result and d.id= rd.datasource and r.id=rd.id  and d.type!='Publication Catalogue' group by d.type order by count ( distinct r.id);";
286

    
287

    
288

    
289
$this->colors[0] = '#4572A7';
290
		$this->colors[1] = '#AA4643';
291
		$this->colors[2] = '#89A54E';
292

    
293
		$this->types[0] = 'column';
294
		$this->types[1] = 'spline';
295
		$this->types[2] = 'spline';
296

    
297
		/*set up*/
298
		$this->chart = array();
299
		$this->chart['chart'] = array();
300
		//$this->chart['exporting'] = array();
301
		$this->chart['title'] = array();
302
		$this->chart['xAxis'] = array();
303
		$this->chart['xAxis']['labels'] = array();
304
		$this->chart['xAxis']['title'] = array();
305
		$this->chart['xAxis']['categories'] = array();
306
		$this->chart['yAxis'] = array();
307
		$this->chart['tooltip'] = array();
308
		$this->chart['legend'] = array();
309
		$this->chart['series'] = array();
310
		/*more*/
311
//checkign for exporting options
312
		//$this->chart['exporting']['enabled'] = false;
313
		$this->chart['chart']['renderTo'] = 'chart';
314
		$this->chart['chart']['reflow'] = false;
315
		$this->chart['chart']['showAxes'] = true;
316
		$this->chart['chart']['zoomType'] = 'xy';
317
		//$this->chart['chart']['margin'] = 'auto';
318
		//$this->chart['chart']['marginRight'] = 70;
319
		//$this->chart['chart']['marginBottom'] = 70;
320
		//$this->chart['chart']['height'] = 450;
321
		//$this->chart['chart']['spacingBottom'] = 100;
322
		$this->chart['credits']['enabled'] = false;
323
		//$this->chart['credits']['text'] = "from OpenAIRE via HighCharts".date("d / m / Y");
324
		//$this->chart['credits']['href'] = "#";
325
		//$this->chart['credits']['position'] = array();
326
		//$this->chart['credits']['position']['y'] = -2;
327

    
328

    
329
		/*$this->chart['xAxis']['labels']['overflow'] = null;//'justify';
330
		$this->chart['xAxis']['labels']['style'] = array();
331
		$this->chart['xAxis']['labels']['style']['font-size'] = '10px';
332

    
333
		$this->chart['xAxis']['showEmpty'] = true;
334
		$this->chart['xAxis']['labels']['enabled'] = true;
335
		$this->chart['xAxis']['labels']['style'] = array();
336
		$this->chart['xAxis']['labels']['style']['font-size'] = '8px';
337
		//$this->chart['xAxis']['labels']['style']['margin-left'] = '10px';
338
		//$this->chart['xAxis']['labels']['style']['margin-right'] = '10px';*/
339
		$this->chart['xAxis']['startOnTick'] = true;
340
		$this->chart['xAxis']['endOnTick'] = true;
341
		$this->chart['xAxis']['showFirstLabel'] = true;
342
		$this->chart['xAxis']['showLastLabel'] = true;
343

    
344
		$this->chart['tooltip']['percentageDecimals'] = 1;
345
		$this->chart['tooltip']['valueDecimals'] = 1;
346
		$this->chart['legend']['layout'] = 'vertical';
347
		$this->chart['legend']['align'] = 'right';
348
		$this->chart['legend']['verticalAlign'] = 'top';
349
		$this->chart['legend']['floating'] = true;
350
		$this->chart['legend']['borderWidth'] = 0;
351
		$this->chart['legend']['x'] = -10;
352
		$this->chart['legend']['y'] = 50;
353
		$this->chart['legend']['padding'] = 3;
354
		$this->chart['legend']['itemMarginBottom'] = 5;
355

    
356
		//for scatter plots
357
		$this->chart['plotOptions'] = array();
358
		$this->chart['plotOptions']['series'] = array();
359
	        $this->chart['plotOptions']['series']['showCheckbox'] = true;
360
	        $this->chart['plotOptions']['series']['selected'] = true;
361
		$this->chart['plotOptions']['scatter'] = array();
362
		$this->chart['plotOptions']['scatter']['marker'] = array();
363
		$this->chart['plotOptions']['scatter']['marker']['radius'] = 5;
364
		$this->chart['plotOptions']['scatter']['marker']['states'] = array();
365
		$this->chart['plotOptions']['scatter']['marker']['states']['hover'] = array();
366
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['enabled'] = true;
367
		$this->chart['plotOptions']['scatter']['marker']['states']['hover']['lineColor'] = 'rgb(100,100,100)';
368
		$this->chart['plotOptions']['scatter']['states'] = array();
369
		$this->chart['plotOptions']['scatter']['states']['hover'] = array();
370
		$this->chart['plotOptions']['scatter']['states']['hover']['marker'] = array();
371
		$this->chart['plotOptions']['scatter']['states']['hover']['marker']['enabled'] = false;
372

    
373
		$this->chart['plotOptions']['area'] = array();
374
                $this->chart['plotOptions']['area']['stacking'] = null;
375

    
376
                $this->chart['plotOptions']['areaspline'] = array();
377
                $this->chart['plotOptions']['areaspline']['stacking'] = null;
378

    
379
		//for pie charts
380
		$this->chart['plotOptions']['pie'] = array();
381
		$this->chart['plotOptions']['pie']['allowPointSelect'] = true;
382
		//$this->chart['plotOptions']['pie']['size'] = '50%';
383
		$this->chart['plotOptions']['pie']['cursor'] = 'pointer';
384
		$this->chart['plotOptions']['pie']['showInLegend'] = true;
385
		$this->chart['plotOptions']['pie']['dataLabels'] = array();
386
		$this->chart['plotOptions']['pie']['dataLabels']['enabled'] = true;
387
		$this->chart['plotOptions']['pie']['dataLabels']['color'] = '#000000';
388
		$this->chart['plotOptions']['pie']['dataLabels']['connectorColor'] = '#000000';
389
		$this->chart['plotOptions']['pie']['dataLabels']['crop'] = false;
390
		$this->chart['plotOptions']['pie']['dataLabels']['distance'] = 10;
391
		//$this->chart['plotOptions']['pie']['dataLabels']['formatter'] = '';
392
		//for column
393
		$this->chart['plotOptions']['column'] = array();
394
		$this->chart['plotOptions']['column']['allowPointSelect'] = true;
395
		$this->chart['plotOptions']['column']['cursor'] = 'pointer';
396
		$this->chart['plotOptions']['column']['showInLegend'] = true;
397
		$this->chart['plotOptions']['column']['grouping'] = true;
398

    
399
		$this->chart['plotOptions']['area'] = array();
400
	}
401

    
402
	function makeQuery($viztype,$persistent) {
403
		if(!isset($_GET['data'])){
404
			$this->log->info("data param not set: ". print_r($_GET,true));
405
			return 'empty';
406
		}
407
		else {
408
			$data = $_GET['data'];
409
                      
410
	
411
                        $this->data = json_decode($data,true);//print_r($this->data);
412
			$this->log->debug("data param decoded: ". print_r($this->data,true));
413
			$cachedData = $this->data;
414
			//unset($cachedData['yaxisheaders']);
415
			//unset($cachedData['fieldsheaders']);
416
			unset($cachedData['title']);
417
			unset($cachedData['subtitle']);
418
			//unset($cachedData['xaxistitle']);
419
			unset($cachedData['theme']);
420
			unset($cachedData['xStyle']);
421
			$this->size = $this->data['size'];		
422
			return @$this->computeChartObject($viztype,$persistent);
423
		}
424
	}
425

    
426
	function computeChartObject($viztype,$persistent){
427
		//$this->log->info("DATA: ".print_r($this->data,true));
428
		if(!isset($this->data['query'])){
429
			$this->log->info("query not set");
430
//TODO here add persistent value??
431
			$this->queryResult = $this->database->getData($this->data,$persistent);
432
		}
433
		else{
434
			 
435

    
436
//TODO added here option for custom queries in project & gatasouces views
437

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

    
440
if(isset($this->data['dtsrcName']))
441
{$this->log->info("Datasource query".$this->data['dtsrcName']);
442
$dtsrc= $this->myqueries[$this->data['query']]['q'];
443
$dtsrc=str_replace("DTSRC_NAME",$this->data['dtsrcName'],$dtsrc);
444
$this->log->info("Query for datasource ".$dtsrc);
445
$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($dtsrc,"false"));
446
}
447
else if(isset($this->data['projTitle']))
448
{$this->log->info("Project query".$this->data['projTitle']);
449
$proj= $this->myqueries[$this->data['query']]['q'];
450
$proj=str_replace("PROJ_TITLE",$this->data['projTitle'],$proj);
451
$this->log->info("Query for proj ".$proj);
452
$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($proj,"false"));
453

    
454
}
455
else if(isset($this->data['orgTitle']))
456
{$this->log->info("Organization query".$this->data['orgTitle']);
457
$org= $this->myqueries[$this->data['query']]['q'];
458
$org=str_replace("ORG_TITLE",$this->data['orgTitle'],$org);
459
$this->log->info("Query for org ".$org);
460
$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($org,"false"));
461

    
462
}
463
else if(isset($this->data['resTitle']))
464
{$this->log->info("Result query".$this->data['resTitle']);
465
$res= $this->myqueries[$this->data['query']]['q'];
466
$res=str_replace("RES_TITLE",$this->data['resTitle'],$res);
467
$this->log->info("Query for res ".$res);
468
$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($res,"false"));
469

    
470
}
471
else
472
{$this->queryResult = array("type"=>"chart","data"=>$this->database->performQuery($this->myqueries[$this->data['query']]['q'],true));
473
}
474

    
475
}
476

    
477
		$this->log->info("data from DB: ".print_r($this->queryResult,true));
478
		if($viztype=="chart"){
479
			if($this->queryResult['type'] == 'scatter'){
480
				$this->createScatterData();
481
			}
482
			else{
483
				$this->createChartData();
484
			}
485

    
486
			if(isset($this->data['in']) && count($this->data['in'])){
487
				$this->chart2 = $this->chart;
488
				for($w=0;$w<count($this->chart['series']);$w++){
489
					//$whichfield = $this->data['in'][$w]['f'];
490
					//$this->chart2 = $this->chart;
491
					$data = $this->chart2['series'][$w]['data'];
492
					for($i=1;$i<count($this->chart2['xAxis']['categories']);$i++){
493
						$data[$i] += $data[$i-1];
494
					}
495
					$this->chart2['series'][$w]['data'] = $data;
496
				}
497
				//return json_encode(array($this->chart,$this->chart2),JSON_NUMERIC_CHECK);
498
				return json_encode(array($this->chart,$this->chart2));
499
			}
500

    
501
			//return json_encode($this->chart,JSON_NUMERIC_CHECK);
502
			return json_encode($this->chart);
503
		}
504
		if($viztype=='table'){
505
			//return json_encode($this->queryResult,JSON_NUMERIC_CHECK);
506
			return json_encode($this->queryResult);
507
		}
508
	}
509

    
510
	function COM_getMeasMetadata(){
511
		if(!isset($_GET['table'])) {
512
			echo 'empty';
513
		}
514
		else{
515
			echo json_encode($this->database->getMeasMetadata($_GET['table']));
516
		}
517
	}
518

    
519
	function COM_defaultChart() {
520
		//get the default values for this table
521
		$temp = $this->database->getDefaultData($_GET['table']);
522
		if($temp == 'empty')
523
			echo json_encode("empty");
524
		else{
525
			$this->size = $temp['size'];
526
			//$this->queryResult = $temp['data'];
527
			$this->data = $temp['selectedData'];
528
			//echo json_encode($this->data,JSON_NUMERIC_CHECK);
529
			echo json_encode($this->data);
530
		}
531
		//$this->data['group'] = '';
532
		//$this->data['color'] = '';
533
		//print_r($this->queryResult);return;
534
		/*if($this->data['type'] == 'chart')
535
			$this->createChartData();
536
		else
537
			$this->createScatterData();
538
		$toreturn = array();
539
		$toreturn['chart'] = $this->chart;
540
		$toreturn['selectedData'] = $this->data;
541
		echo json_encode($toreturn,JSON_NUMERIC_CHECK);*/
542
	}
543

    
544
	function defaultChart() {
545
		//get the default values for this table
546
		$temp = $this->database->getDefaultData($_GET['table']);
547
		$this->queryResult = $temp['data'];
548
		$this->data = $temp['selectedData'];
549
		$this->data['group'] = '';
550
		$this->data['color'] = 'no';
551
		//print_r($this->queryResult);return;
552
		if($temp['type'] == 'chart')
553
			$this->createChartData($temp['type']);
554
		else
555
			$this->createScatterData('');
556
		//$this->chart['chart']['type'] = $temp['type'];
557
		//print_r($this->chart);return;
558
		//print_r($this->data);
559
		$toreturn = array();
560
		$toreturn['chart'] = $this->chart;
561
		$toreturn['selectedData'] = $this->data;
562
		//return json_encode($toreturn,JSON_NUMERIC_CHECK);
563
		return json_encode($toreturn);
564
	}
565

    
566
	function defaultChartSelections() {
567
		//get the default values for this table
568
		$temp = $this->database->getDefaultData($_GET['table']);
569
		$this->queryResult = $temp['data'];
570
		$this->data = $temp['selectedData'];
571

    
572
		//echo json_encode($this->data,JSON_NUMERIC_CHECK);
573
		echo json_encode($this->data);
574
	}
575

    
576
	function COM_getFilterData(){
577
		if(!isset($_GET['table']) || !isset($_GET['field'])) {
578
			echo 'empty';
579
		}
580
		else {
581
			echo $this->computeFilterObject($_GET['table'],$_GET['field'],true);
582
		}
583
	}
584

    
585
	function getFilterData($table, $field){
586
		return $this->computeFilterObject($table,$field,false);
587
	}
588

    
589
	function computeFilterObject($table, $field, $encode){
590
		$flds = explode("-",$field);
591
		if(count($flds)>1){
592
			$ctable = $flds[count($flds)-2];
593
			$field = $flds[count($flds)-1];
594
		}
595
		else {
596
			$ctable = $table;
597
			$field = $field;
598
		}
599
		//call mydb function
600
		if($encode)
601
			//return json_encode($this->database->getFilterData($ctable,$field),JSON_NUMERIC_CHECK);
602
			return json_encode($this->database->getFilterData($ctable,$field));
603
		else
604
			return $this->database->getFilterData($ctable,$field);
605
	}
606

    
607
	function COM_get_schema(){
608
		$facts = $this->database->getFacts();
609
		$dimensions = $this->database->getDimensions();
610
		$response = array();
611
		$menu = array();
612
		if($facts === null) {
613
			$this->log->error("there are no fact tables");
614
			echo "empty";
615
			return;
616
		}//print_r($facts);
617
		foreach($facts as $facttable){
618
			$newfact = array();
619
			$newfact['name'] = $facttable['name'];
620
			if(!isset($facttable['meas'][0]))
621
				$newfact['meas'][] = $facttable['meas'];
622
			else
623
				$newfact['meas'] = $facttable['meas'];
624
			$newfact['dim'] = array();
625
			//gia kathe dim theloume: name, type, data, kai attrib opou to attrib mporei na einai ki ayto dim
626
			//otan pigainoume se bathos stin ierarxia twn diastasewn kratame touw progonous kai an ftasoume se diastasi pou yparxei stous progonous tin agnooume
627
			foreach($facttable['dim'] as $curdim) {
628
				$newdim = array();
629
				$newdim['name'] = $curdim['name'];
630
				$newdim['type'] = $curdim['type'];
631
				//$newdim['data'] = $curdim['data'];//may not need it
632
				if(isset($curdim['view']))
633
					$newdim['view'] = $curdim['view'];
634
				if(isset($curdim['dimtable']))
635
					$newdim['dimtable'] = 'no';
636
				else{
637
					$newdim['dimtable'] = 'yes';
638
					$newdim['attrib'] = array();
639

    
640
					//need to find table $curdim['refer']['table'] from all the dimentions
641
					$tabledim = search($dimensions, 'name', $curdim['name']);
642
					//print_r($tabledim);
643
					for($i=0;$i<count($tabledim);$i++){
644
						if(isset($tabledim[$i]['type']) && $tabledim[$i]['type']=='dimension')
645
							break;
646
					}
647
					$newdim['attrib'] = $this->makeAttrList($tabledim[$i]['attrib'],$dimensions,array($curdim['name']),$facttable['name']);
648
				}
649
				//I am interested in all the attributes of this dimension
650
				array_push($newfact['dim'],$newdim);
651
			}
652
			array_push($menu,$newfact);
653
		}
654
		$response['name'] = $GLOBALS['db_name'];
655
		$response['schema'] = $menu;
656
		echo json_encode($response);
657
	}
658

    
659
	function makeAttrList($attrList, $dimensions, $ancestors,$facttable){
660
		$newList = array();
661
		if(is_array($attrList)) {
662
			foreach($attrList as $attr) {
663
				if($attr['name'] != 'id' && $attr['name']!=$facttable) {
664
					$newAttr = array();
665
					$newAttr['name'] = $attr['name'];
666
					if(isset($newAttr['view']))
667
						$newAttr['view'] = $attr['view'];
668
					$newAttr['type'] = $attr['type'];
669
					if(isset($attr['analysed']) && !in_array($attr['name'],$ancestors)){
670

    
671
						$finddim = search($dimensions, 'name', $attr['name']);
672
						$nextdim = search($finddim,'type','dimension');//print_r($nextdim);
673
						$newnewList = $this->makeAttrList($nextdim[0]['attrib'],$dimensions, array_merge($ancestors,array($attr['name'])),$facttable);
674
						$ancestors[] = $attr['name'];
675
						$newAttr['analysed'] = $attr['name'];
676
						$newAttr['attrib']= $newnewList;
677
					}
678
					else{
679
						$newAttr['attrib']= array();
680
					}
681
					array_push($newList,$newAttr);
682
				}
683
			}
684
		}
685
		return $newList;
686
	}
687

    
688

    
689
/*
690
type = color | group | ''
691
*/
692
	function createScatterData(){
693
		unset($this->chart['xAxis']['categories']);
694
		$this->chart['chart']['type'] = 'bubble';
695
		$xaxisindex = 0;
696
		$yaxisindex = 1;
697
		$left = 0;
698
		$right = 0;
699
		$fields = count($this->data['fields']);
700
		$dimindex = 1+$fields;
701
		if($this->data['group'] == '')
702
			$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['name'];
703
		else
704
			$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['agg'].'('.$this->data['xaxis']['name'].')';
705
		//arxikopoiisi
706
		//posoi yaxis??
707
		$this->chart['yAxis'] = array();
708
		$yaxis = $this->data['fields'][$fields-1]['yaxis'];
709
		$tempy = -1;
710
		for($f=0;$f<count($this->data['fields']);$f++){
711
			if($this->data['fields'][$f]['yaxis']!=$tempy+1){//new y
712
				$tempy++;
713
				$this->chart['yAxis'][$tempy] = array();
714
				$this->chart['yAxis'][$tempy]['labels'] = array();
715
				$this->chart['yAxis'][$tempy]['labels']['enabled'] = true;
716
				$this->chart['yAxis'][$tempy]['labels']['overflow'] = 'justify';
717
				//$this->chart['yAxis'][$tempy]['offset'] = 70;
718
				$this->chart['yAxis'][$tempy]['title'] = array();
719
				if($this->data['yaxisheaders'][$tempy] !=''){
720
					$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['yaxisheaders'][$tempy];
721
				}
722
				else{
723
					if($this->data['fieldsheaders'][$f] !=''){
724
						$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fieldsheaders'][$f];
725
					}
726
					else{
727
						if($this->data['group'] == '')
728
							$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['fld'];
729
						else
730
							$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
731
					}
732
				}
733
				if($right>=$left){
734
					$left++;
735
				}
736
				else{
737
					$right++;
738
					$this->chart['yAxis'][$tempy]['opposite'] = true;
739
				}
740
			}
741
			else if($this->data['yaxisheaders'][$tempy] == ""){//more in the name
742
				if($this->data['fieldsheaders'][$f] !=''){
743
					$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fieldsheaders'][$f];
744
				}
745
				else{
746
					if($this->data['group'] == '')
747
						$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['fld'];
748
					else
749
						$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
750
				}
751
			}
752
		}
753
		$this->chart['series'] = array();
754
		$this->chart['legend']['enabled'] = false;
755
		//an einai group exw ena series
756
		if(($this->data['group']  && $this->data['group'] != '') || $this->data['color'] == ''){//ena series kai diaforetiko onoma gia kathe data
757
			for($f=0;$f<$fields;$f++){
758
				$this->chart['series'][$f] = array();
759
				$this->chart['series'][$f]['data'] = array();
760
				if($this->data['fields'][$f]['yaxis']!=1)
761
					$this->chart['series'][$f]['yAxis'] = $this->data['fields'][$f]['yaxis']-1;
762
			}
763
			//for($line=0;$line<10;$line++){
764
			for($line=0;$line<count($this->queryResult['data']);$line++){
765
				for($f=0;$f<$fields;$f++){
766
					//insert a new data object to the only series
767
					$data = array();
768
					$data['x'] = $this->queryResult['data'][$line][$xaxisindex];
769
					$data['y'] = $this->queryResult['data'][$line][$yaxisindex+$f];
770
					if($this->data['group'] != ''){
771
						$data['name'] = $this->queryResult['data'][$line][$dimindex];
772
						if($this->data['fieldsheaders'][$f]=="")
773
							$this->chart['series'][$f]['name'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
774
						else
775
							$this->chart['series'][$f]['name'] = $this->data['fieldsheaders'][$f];
776
					}
777
					else{
778
						if($this->data['fieldsheaders'][$f]=="")
779
							$this->chart['series'][$f]['name'] = $this->data['fields'][$f]['fld'];
780
						else
781
							$this->chart['series'][$f]['name'] = $this->data['fieldsheaders'][$f];
782
					}
783
					$this->chart['series'][$f]['data'][] = $data;
784
				}
785
			}//print_r($this->chart['series']);
786
		}
787
		//an einai color exw ena series gia kathe timi tou dimindex
788
		else{
789
			$this->chart['legend']['enabled'] = true;
790
			$curseries = '';
791
			$seriescount = 0;
792
			//for($line=0;$line<10;$line++){
793
			for($line=0;$line<count($this->queryResult['data']);$line++){
794
				if($this->queryResult['data'][$line][$dimindex] != $curseries){
795
					$curseries = $this->queryResult['data'][$line][$dimindex];
796
					for($f=0;$f<$fields;$f++){
797
						$this->chart['series'][$seriescount+$f] = array();
798
						if($this->data['fieldsheaders'][$f]!='')
799
							$this->chart['series'][$seriescount+$f]['name'] = $this->data['fieldsheaders'][$f]." for ".$this->queryResult['data'][$line][$dimindex];
800
						else
801
							$this->chart['series'][$seriescount+$f]['name'] = $this->data['fields'][$f]['fld']." for ".$this->queryResult['data'][$line][$dimindex];
802
						$this->chart['series'][$seriescount+$f]['data'] = array();
803
					}
804
					$seriescount += $fields;
805
				}
806
				for($f=0;$f<$fields;$f++){
807
					$this->chart['series'][$seriescount-$fields+$f]['data'][] = array($this->queryResult['data'][$line][$xaxisindex],$this->queryResult['data'][$line][$yaxisindex+$f]);
808
				}
809
			}
810
		}
811

    
812
	}
813

    
814
	function createChartData(){
815
		$this->chart['xAxis']['categories'] = array();
816
		unset($this->chart['chart']['type']);
817
		$this->chart['series'] = array();
818
		$this->chart['yAxis'] = array();
819
		$xaxisindex = 0;
820
		$yaxisindex = 1;
821
		$left = 0;
822
		$right = 0;
823
		//print_r($this->queryResult['data']);
824
		$fields = count($this->data['fields']);
825
		$dimindex = 1+$fields;
826
if(isset($this->data['xaxistitle']))
827
		//if($this->data['xaxistitle']!='')
828
			$this->chart['xAxis']['title']['text'] = $this->data['xaxistitle'];
829
		else{
830
			$xaxis = explode("-",$this->data['xaxis']['name']);
831
			$this->chart['xAxis']['title']['text'] = $xaxis[count($xaxis)-1];
832
		}
833
		$this->chart['legend']['enabled'] = true;
834
		//posoi yaxis??
835
		$yaxis = $this->data['fields'][$fields-1]['yaxis'];
836
		$tempy = -1;
837
		for($f=0;$f<count($this->data['fields']);$f++){
838
			if($this->data['fields'][$f]['yaxis']!=$tempy+1){//new y
839
				$tempy++;
840
				$this->chart['yAxis'][$tempy] = array();
841
				$this->chart['yAxis'][$tempy]['labels'] = array();
842
				$this->chart['yAxis'][$tempy]['labels']['enabled'] = true;
843
				$this->chart['yAxis'][$tempy]['labels']['overflow'] = 'justify';
844
				$this->chart['yAxis'][$tempy]['min'] = 0;
845
				//$this->chart['yAxis'][$tempy]['offset'] = 70;
846
				$this->chart['yAxis'][$tempy]['title'] = array();
847
				if($this->data['yaxisheaders'][$tempy]!=''){
848
					$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['yaxisheaders'][$tempy];
849
					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
850
						$this->data['fieldsheaders'][$f] = $this->data['yaxisheaders'][$tempy];
851
					}
852
				}
853
				else{
854
					if($this->data['fieldsheaders'][$f] !=''){
855
						$this->chart['yAxis'][$tempy]['title']['text']  = $this->data['fieldsheaders'][$f];
856
					}
857
					else {
858
						$this->chart['yAxis'][$tempy]['title']['text'] = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
859
					}
860
				}
861
				if($right>=$left){
862
					$left++;
863
				}
864
				else{
865
					$right++;
866
					$this->chart['yAxis'][$tempy]['opposite'] = true;
867
				}
868
			}
869
			else if($this->data['yaxisheaders'][$tempy]==''){//more in the name
870
				if($this->data['fieldsheaders'][$f] !='')
871
					$this->chart['yAxis'][$tempy]['title']['text']  .= " / ".$this->data['fieldsheaders'][$f];
872
				else
873
					$this->chart['yAxis'][$tempy]['title']['text'] .= " / ".$this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].')';
874
			}
875
		}
876

    
877
		if($this->data['group'] != ''){//periptwsi analyze
878
			$this->chartDataGroup();
879
		}
880
		else{//polloi yaxis xwris group
881
			$this->chartDataMultiY();
882
		}
883

    
884
		if(count($this->chart['xAxis']['categories']) > 30){
885
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
886
			//$this->chart['xAxis']['labels']['rotation'] = 90;
887
		}//print_r($this->chart);
888
		else{
889
			$trimflag = false;
890
			for($i=0;$i<count($this->chart['xAxis']['categories']);$i++){
891
				if(is_string($this->chart['xAxis']['categories'][$i]) && strlen($this->chart['xAxis']['categories'][$i])>10){
892
					$trimflag = true;
893
					break;
894
				}
895
			}
896
			if($trimflag){
897
				$this->chart['xAxis']['labels']['formatter'] = 'cond-trim';
898
				//$this->chart['xAxis']['labels']['rotation'] = 90;
899
			}
900
		}
901
	}
902

    
903
	function chartDataGroup(){
904
		//ena series gia kathe field gia kathe timi tou groupdim
905
		//read data
906
		$res = $this->queryResult['data'];
907
		$seriesindex = array();
908
		$names = array();
909
		$name = '';
910
		$seriescount = 0;
911
		$curseries = '';
912
		$xaxisindex = 0;
913
		$yaxisindex = 1;
914
		$fields = count($this->data['fields']);
915
		$dimindex = 1+$fields;
916

    
917
		($this->size >= count($this->queryResult['data'])) ? $num=count($this->queryResult['data']) : $num=$this->size; 
918
		//$num = $this->size;
919
		$flag = true;
920
		for($line=0;$line<$num;$line++){
921
			//list with x axis categories
922
			if(!in_array($res[$line][$xaxisindex],$this->chart['xAxis']['categories'])){
923
				if(count($this->chart['xAxis']['categories'])<$num){
924
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisindex];
925
					$flag = true;
926
				}
927
				else{
928
					$flag = false;
929
				}
930
			}
931
			else{
932
				$flag = true;
933
			}
934
			if($flag){
935
				//insert new data in currentseries or make new one
936
				if($this->queryResult['data'][$line][$dimindex] != $curseries){
937
					$curseries = $this->queryResult['data'][$line][$dimindex];
938
					for($f=0;$f<$fields;$f++){
939
						if($this->data['fieldsheaders'][$f]!="")
940
							$names[$f] = $this->data['fieldsheaders'][$f] ." for ".$curseries;
941
						else
942
							$names[$f] = $curseries;
943
						if(!isset($seriesindex[$names[$f]])){
944
							$seriesindex[$names[$f]] = array();
945
							$seriesindex[$names[$f]]['name'] = $names[$f];
946
							if($this->data['fields'][$f]['type'] !='area')
947
								$seriesindex[$names[$f]]['type'] = $this->data['fields'][$f]['type'];
948
							else
949
								$this->chart->type='area';
950
							if($this->data['fields'][$f]['yaxis']>1){
951
								$seriesindex[$names[$f]]['yAxis'] = $this->data['fields'][$f]['yaxis']-1;
952
							}
953
							$seriesindex[$names[$f]]['data'] = array();
954
							if($curseries == "UNKNOWN")
955
								$seriesindex[$names[$f]]['visible'] = false;
956

    
957
							if(isset($this->data['fields'][$f]['c']) && $this->data['fields'][$f]['c'] == true)
958
								$seriesindex[$names[$f]]['c'] = true;
959
							else
960
								$seriesindex[$names[$f]]['c'] = false;
961
						}
962
					}
963
					$seriescount += $fields;
964
				}
965
				$index = array_search($this->queryResult['data'][$line][$xaxisindex],$this->chart['xAxis']['categories']);
966
				for($f=0;$f<$fields;$f++){
967
					if($this->data['fieldsheaders'][$f]!="")
968
						$name = $this->data['fieldsheaders'][$f] . " for ".$curseries;
969
					else
970
						$name = $this->data['fields'][$f]['agg'].'('.$this->data['fields'][$f]['fld'].") for ".$curseries;
971
					$lastindex = count($seriesindex[$name]['data']);
972
					for($t=$lastindex;$t<$index;$t++){
973
						$seriesindex[$name]['data'][$t] = 0;
974
					}
975
					$seriesindex[$name]['data'][$index] = $this->queryResult['data'][$line][$yaxisindex+$f];
976
				}
977
			}
978
		}
979
		foreach($seriesindex as $s){
980
			$this->chart['series'][] = $s;
981
		}
982
	}
983

    
984
	function chartDataMultiY(){
985
		$xaxisindex = 0;
986
		$yaxisindex = 1;
987
		//1 series gia kathe field
988
		for($i=0; $i<count($this->data['fields']);$i++){
989
			$this->chart['series'][] = array();
990
			if($this->data['fieldsheaders'][$i]!='')
991
				$this->chart['series'][$i]['name'] = $this->data['fieldsheaders'][$i];
992
			else
993
				$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['agg'].'('.$this->data['fields'][$i]['fld'].')';
994
			if($this->data['fields'][$i]['type'] != 'area')
995
				$this->chart['series'][$i]['type'] = $this->data['fields'][$i]['type'];
996
			else
997
				$this->chart->type='area';
998
			//$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yAxis'];
999

    
1000
			if($this->data['fields'][$i]['yaxis']>1){
1001
				$this->chart['series'][$i]['yAxis'] = $this->data['fields'][$i]['yaxis']-1;
1002
			}
1003
			$this->chart['series'][$i]['data'] = array();
1004
		}
1005
		//read data
1006
		$res = $this->queryResult['data'];
1007

    
1008
		($this->size >= count($this->queryResult['data'])) ? $num=count($this->queryResult['data']) : $num=$this->size; 
1009
		//$num = $this->size;
1010
		$flag = true;
1011
		for($line=0;$line<$num;$line++){
1012
			//list with x axis categories
1013
			if(!in_array($res[$line][$xaxisindex],$this->chart['xAxis']['categories'])){
1014
				if(count($this->chart['xAxis']['categories'])<$num){
1015
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisindex];
1016
					$flag = true;
1017
				}
1018
				else{
1019
					$flag = false;
1020
				}
1021
			}
1022
			else{
1023
				$flag = true;
1024
			}
1025
			//insert new data in each series
1026
			if($flag){
1027
				for($s=0;$s<count($this->data['fields']);$s++) {
1028
					if($this->data['fields'][$s]['type'] == 'pie'){
1029
						$this->chart['series'][$s]['data'][$line]['name'] = $res[$line][$xaxisindex];
1030
						$this->chart['series'][$s]['data'][$line]['y'] = $res[$line][$s+$yaxisindex];
1031
					}
1032
					else {
1033
						$this->chart['series'][$s]['data'][] = $res[$line][$s+$yaxisindex];
1034
					}
1035
				}
1036
			}
1037
		}
1038

    
1039
		if(count($this->data['fields']) == 1)
1040
			$this->chart['legend']['enabled'] = false;
1041
	}
1042

    
1043
	function COM_performQuery() {
1044
	
1045
		if(!isset($_GET['query']) || $_GET['query'] == ''){
1046
			$this->log->error("no query string");
1047
			return;
1048
		}
1049

    
1050

    
1051
if(isset($_GET['persistent']))
1052
{ $persistent=$_GET['persistent'];
1053
            }
1054
else
1055
{
1056
 $persistent="true";
1057
}
1058

    
1059
                $query = urldecode($_GET['query']);
1060

    
1061
		$this->log->info("performing query: ".$query);
1062

    
1063
		$resp = json_encode($this->database->performQuery($query, $persistent));		//$this->log->info("response: ".$resp);
1064
		echo $resp;
1065
	}
1066

    
1067
	function sortSeries() {
1068
		//exoume mia series gia kathe field
1069
		//prepei na broume ayti ti series kai me basi ayti na kanoume sort kai oles tis alles kai ta categories tou xaxis
1070
		$snum = 0;
1071
		for($snum=0;$snum<$this->data['fields'];$snum++){
1072
			if($this->data['fields'][$snum]['agg']."(".$this->data['fields'][$snum]['fld'].")" == $this->data['sort'])
1073
				break;
1074
		}
1075
		//kanw sort ayto to series diatirontas ta kleidia opws einai kai meta kanw sort tis ypoloipes me basi ti seira kleidiwn aytinis
1076
		if($this->data['order'] && $this->data['order'] == 'd'){
1077
			arsort($this->chart['series'][$snum]['data']);
1078
		}
1079
		else{
1080
			asort($this->chart['series'][$snum]['data']);
1081
		}
1082
		//ypoloipes
1083
		//categories
1084
		$tempordered = array() ;
1085
		foreach (array_keys($this->chart['xAxis']['categories']) as $key) {
1086
			$tempordered[$key] = $this->chart['xAxis']['categories'][$key] ;
1087
		}
1088
		$this->chart['xAxis']['categories'] = $tempordered;
1089
		
1090
		for($i=0;$i<$this->data['fields'];$i++){
1091
			if($i!=$snum){
1092
				unset($tempordered);
1093
				$tempordered = array();
1094
				$myarray = $this->chart['series'][$i]['data'];
1095
				foreach (array_keys($myarray) as $key) {
1096
					$tempordered[$key] = $myarray[$key] ;
1097
				}
1098
				$this->chart['series'][$i]['data'] = $tempordered;
1099
			}
1100
		}
1101
	}
1102

    
1103

    
1104
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1105
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1106
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1107
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1108
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1109
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1110

    
1111
	function COM_makeQuery() {
1112
		if(!isset($_GET['data'])){
1113
			$this->log->info("data param not set: ". print_r($_GET,true));
1114
			echo 'empty';
1115
		}
1116
		else {
1117
			$viztype = $_GET['type'];
1118
			$data = $_GET['data'];
1119
			$this->data = json_decode($data,true);
1120

    
1121
			$cachedData = $this->data;
1122
			unset($cachedData['yaxisheaders']);
1123
			unset($cachedData['fieldsheaders']);
1124
			unset($cachedData['title']);
1125
			unset($cachedData['subtitle']);
1126
			unset($cachedData['xaxistitle']);
1127
			unset($cachedData['theme']);
1128
			unset($cachedData['xStyle']);
1129

    
1130
			$this->size = $this->data['size'];
1131

    
1132
//TODO added persistent  option
1133
if(!isset($_GET['persistent'])){
1134
$persistent='true';}
1135
else {
1136
$persistent=$_GET['persistent'];}
1137

    
1138
  
1139
echo $this->computeChartObject($viztype,$persistent);
1140

    
1141
		}
1142
	}
1143

    
1144
	function COM_read() {
1145
		if(!isset($_GET['facttable']) || !isset($_GET['dimensions']) || !isset($_GET['measures'])) {
1146
			echo "empty";
1147
		}
1148

    
1149
		$dims = explode(';',$_GET['dimensions']);
1150
		$meas = explode(';',$_GET['measures']);
1151
		echo $this->readtest($dims, $meas, $_GET['facttable'],$_GET['aggregation']);
1152
	
1153
	}
1154

    
1155
	function COM_check_fact() {
1156
		if(!isset($_GET['name'])) {
1157
			echo "empty";
1158
		}
1159
		else{
1160
			$res = $this->database->findTableType($_GET['name']);
1161
			if($res == false) echo 'false';
1162
			else echo $res;
1163
		}
1164
	}
1165
	
1166
	function COM_list_of_dim_names() {
1167
		$res = $this->database->getAllDims();
1168
		if($res != false)
1169
			echo json_encode($res);
1170
		else
1171
			echo "false";
1172
	}
1173

    
1174
	function COM_search_warehouse_by_dims() {
1175
		if(!isset($_GET['dims'])) {
1176
			echo 'empty';
1177
		}
1178
		else {
1179
			$res = $this->database->searchWarehousebyDims(explode(';',$_GET['dims']));
1180
			if($res == false) echo "false";
1181
			else echo $res;	
1182
		}
1183
	}
1184

    
1185
	function COM_create_new_fact_table() {
1186
		if(!isset($_GET['table']) || !isset($_GET['dims']) || !isset($_GET['meas'])) {
1187
			echo 'empty';
1188
		} 
1189
		else {
1190
			//set up the list of dims
1191
			$dims = explode(',',$_GET['dims']);
1192
			//set up the list of meas
1193
			$new_meas = rtrim($_GET['meas'],';');
1194
			$meas = explode(';',$new_meas);
1195
			//call the db function
1196
			$res = $this->database->createNewFact($_GET['table'], $dims, $meas);
1197
			if($res == false)
1198
				echo 'false';
1199
			else
1200
				echo 'true';
1201
		}
1202
	}
1203

    
1204
	function COM_list_of_fact_tables() {
1205
		$res = json_encode($this->database->getFactsNames());
1206
		if($res == null)
1207
			echo 'null';
1208
		else
1209
			echo $res;
1210
	}
1211

    
1212
	function COM_list_of_meas() {
1213
		if(!isset($_GET['table'])) {
1214
			echo 'empty';
1215
		} 
1216
		else {
1217
			$res = $this->database->findMeas($this->database->findTable($_GET['table'],'fact'));
1218
			if($res == false)
1219
				echo 'false';
1220
			else
1221
				echo json_encode($res);
1222
		}
1223
	}
1224

    
1225
	function COM_list_of_dims() {
1226
		$res = $this->database->getDims();
1227
		if($res == false)
1228
			echo 'false';
1229
		else
1230
			echo json_encode($res);
1231
	}
1232

    
1233
	function COM_del_meas() {
1234
		if(!isset($_GET['facttable']) || !isset($_GET['name'])) {
1235
			echo "empty";
1236
		}
1237
		else {
1238
			$res = $this->database->delMeas($_GET['facttable'], $_GET['name']);
1239
			if($res == true)
1240
				echo "true";
1241
			else 
1242
				echo "false";
1243
		}
1244
	}
1245

    
1246
	function COM_add_meas() {
1247
		if(!isset($_GET['facttable']) || !isset($_GET['name']) || !isset($_GET['formal_name']) || !isset($_GET['type'])) {
1248
			echo "empty";
1249
		}
1250
		else {
1251
			$res = $this->database->addMeas($_GET['facttable'], $_GET['name'], $_GET['formal_name'], $_GET['type']);
1252
			if($res == true)
1253
				echo "true";
1254
			else 
1255
				echo "false";
1256
		}
1257
	}
1258

    
1259
	function COM_meas_range_data() {
1260
		if(!isset($_GET['facttable']) || !isset($_GET['measurement'])) {
1261
			echo "empty";
1262
		}
1263
		else {
1264
			$res = $this->database->measRangeData($_GET['facttable'],$_GET['measurement']);
1265
			echo json_encode($res);
1266
		}
1267
	}
1268

    
1269
	function COM_get_dims_fields_list(){
1270
		if(!isset($_GET['table'])) {
1271
			echo "empty";
1272
		}
1273
		else {
1274
			$res = $this->database->getDimsList($_GET['table']);
1275

    
1276
			if($res == false)
1277
				echo "false";
1278
			else if($res == null)
1279
				echo "null";
1280
			else
1281
				echo json_encode($res);
1282
		}
1283
	}
1284

    
1285

    
1286
	function fetchData($table, $dim1, $dim2, $meas, $constraints = null) {
1287
		return $this->database->getJsonData($table, $dim1, $dim2, $meas, $constraints);
1288
	}
1289

    
1290
	function readData($table, $dim1, $dim2, $meas, $constraints = null) {
1291
		return $this->database->readData($table, $dim1, $dim2, $meas, $constraints);
1292
	}
1293

    
1294
	function timeload($start,$end) {
1295
		$this->database->loadTime($start,$end);
1296
	}
1297

    
1298
	function readtest($dimensions,$measures,$table,$aggr) {
1299
		$data = $this->database->readtest($dimensions,$measures,$table,$aggr);
1300
		//echo json_encode($data,JSON_NUMERIC_CHECK);
1301
		echo json_encode($data);
1302
	}
1303
	
1304
	
1305
	/////////////////////////////////////////////////////////////////////////////////////
1306

    
1307
}
1308

    
1309
/*
1310

    
1311
	function createChartDataGroup($givenType) {
1312
		//exoume anagkastika ena mono yaxis
1313
		//ena series gia kathe diaforetiki timi tou tritou pediou
1314
		if($givenType == 'chart'){
1315
			$this->chart['xAxis']['categories'] = array();
1316
			
1317
		}
1318
		else{
1319
			unset($this->chart['xAxis']['categories']);
1320
		}
1321
		$this->chart['series'] = array();
1322
		
1323
		$currentSeries = "";
1324
		$seriesCnt = -1;
1325
		$numOfColumns = count($this->queryResult[0]);
1326
		if(count($this->data['series']) == 0)
1327
			$sNameIndex = -1;
1328
		else
1329
			$sNameIndex = count($this->data['fields']) +1;
1330
			//$sNameIndex = $numOfColumns-1;
1331
		$xaxisIndex = 0;
1332
		$fStartindex = 1;
1333
		$groupindex = 2;
1334
		$this->chart['xAxis']['title']['text'] = $this->data['xaxis']['name'];
1335
		//arxikopoiisi
1336
		$this->chart['yAxis'] = array();
1337
		$this->chart['series'] = array();
1338
		$this->chart['legend']['enabled'] = false;
1339
		//edw tha ektelestei mono mia fora <-------NA TO ALLAKSW
1340
		for($i=0; $i<count($this->data['fields']);$i++){
1341
			//gia kathe field, exoume enan yaxis kai ena series
1342
			$this->chart['yAxis'][] = array();
1343
			$this->chart['yAxis'][$i]['labels'] = array();
1344
			$this->chart['yAxis'][$i]['offset'] = 70;
1345
			//$this->chart['yAxis'][$i]['labels']['formatter'] = 'simple';
1346
			$this->chart['yAxis'][$i]['title'] = array();
1347
			$this->chart['yAxis'][$i]['title']['text'] = $this->data['fields'][$i]['fld'];
1348
			if($i>0){
1349
				$this->chart['yAxis'][$i]['opposite'] = true;
1350
				$this->chart['yAxis'][$i]['gridLineWidth'] = 0;
1351
			}
1352

    
1353
			//$this->chart['series'][] = array();
1354
			//$this->chart['series'][$i]['name'] = $this->data['fields'][$i]['fld'];
1355
			
1356
			//if($i>0) {
1357
			//	$this->chart['series'][$i]['yAxis'] = $i;
1358
			//}
1359
			//$this->chart['series'][$i]['data'] = array();
1360
			//$seriesData[$i] = array();
1361
		}
1362
		
1363
		$res = $this->queryResult['data'];
1364
		//gia kathe diaforetiki timi tou pediou 2, kainourgio category an exoume chart
1365
		//gia kathe diaforetiki timi tou pediou 3, kainourgio series me onoma tin timi tou pediou 3
1366
		//oso eisai sto idio series, apla prosthese ta data
1367
		//an allakseis ftiakse kainourgio
1368
		for($line=0;$line<count($this->queryResult['data']);$line++){
1369
			//list with x axis categories
1370
			if($givenType == 'chart'){
1371
				if(!in_array($res[$line][$xaxisIndex],$this->chart['xAxis']['categories'])){
1372
					$this->chart['xAxis']['categories'][] = $res[$line][$xaxisIndex];
1373
				}
1374
			}
1375
			//new series? 
1376
			if($res[$line][$groupindex] != $currentSeries){
1377
				$currentSeries = $res[$line][$groupindex];
1378
				$this->chart['series'][] = array();
1379
				$seriesCnt++;
1380
				$this->chart['series'][$seriesCnt]['name'] = $currentSeries;
1381
				$this->chart['series'][$seriesCnt]['data'] = array();
1382
			}
1383
			$this->chart['series'][$seriesCnt]['data'][] = $res[$line][$fStartindex];
1384
		}
1385

    
1386
		if(count($this->chart['xAxis']['categories']) > 15){
1387
			$this->chart['xAxis']['labels']['formatter'] = 'trim';
1388
			$this->chart['xAxis']['labels']['rotation'] = 45;
1389
		}
1390

    
1391
	}
1392
*/
1393
?>
(7-7/28)