Project

General

Profile

1
<!DOCTYPE html
2
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
<html>
5
<?php
6
require_once('controller.php');
7
$controller = new Controller(false);
8

    
9
if(!isset($_GET['com'])) {
10
	//oops error
11
	echo "no command given";
12
	return;
13
}
14

    
15
if($_GET['com'] != "query") {
16
	echo "unknown command code";
17
	return;
18
}
19
$selectedData = json_encode($_GET['data']);
20
// the response of the db
21

    
22
//make the query
23
if(!isset($_GET['persistent'])){
24
     $persistent="true";
25
}
26
else
27
{
28
$persistent="false";
29
}
30
$resp = $controller->makeQuery('table',$persistent);
31

    
32
// the selected data
33
$myData = json_decode($_GET['data']);
34
// the filters to include
35

    
36
if(isset($myData->incfilters)){
37

    
38
$myfilters = $myData->incfilters;
39
}
40

    
41
else
42
{
43
$myfilters=null;
44
}
45

    
46

    
47
// if all posible values are included, get them from the db
48
for($i=0;$i<count($myfilters);$i++){
49
	if(isset($myfilters[$i]->values) && $myfilters[$i]->values=='all'){
50
		$temp = $controller->getFilterData($myData->table,$myfilters[$i]->name);
51
		$myfilters[$i]->values = $temp['data'];
52
	}
53
}
54

    
55
?>
56
  <head>
57
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
58
	<title>choromap</title>
59
	<!-- Include JQuery Core-->
60
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
61
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
62
	<!-- Include styles -->
63
	<!-- <link href="//code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" rel="stylesheet"> -->
64
	<!-- <link href="./js/jquery/css/start/jquery-ui-1.8.20.custom.css" type="text/css">  -->
65
	<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
66

    
67
	<link type="text/css" href="./js/googlecharts.css" rel="stylesheet" />
68
	<link rel="stylesheet" href="./js/filters.css" />
69

    
70

    
71

    
72
	<!-- Google charts api -->
73
	<script type='text/javascript' src='//www.google.com/jsapi'></script>
74

    
75
<script type='text/javascript'>
76
	var chart;
77
	var tableData;
78
	var selectedData;
79
	var filterData;
80
	var myFilter;
81
	var rows = null;
82

    
83
	//the width is set to percentage
84
	var wflag = <?php if(isset($_GET['w']) && $_GET['w']!='' && $_GET['w'][strlen($_GET['w'])-1] == '%'){echo "true";}
85
                	else{echo "false";}?>
86

    
87
	var mywidth =  <?php if(isset($_GET['w']) && $_GET['w']!=''){ 
88
		                if($_GET['w'][strlen($_GET['w'])-1] == '%'){
89
		                        echo substr($_GET['w'],0,strlen($_GET['w'])-1);
90
		 		}else{
91
		                        if($_GET['w'][strlen($_GET['w'])-1] == 'x'){
92
		                                echo substr($_GET['w'],0,strlen($_GET['w'])-2);
93
		                        }else
94
				               echo $_GET['w'];
95
		                }
96
                	     }else echo '556'; ?>;
97
	//the height is set to percentage
98
	var hflag = <?php if(isset($_GET['h']) && $_GET['h']!='' && $_GET['h'][strlen($_GET['h'])-1] == '%'){echo "true";}
99
                	else{echo "false";}?>
100

    
101
	var myheight =  <?php if(isset($_GET['h']) && $_GET['h']!=''){
102
		                if($_GET['h'][strlen($_GET['h'])-1] == '%'){
103
		                        echo substr($_GET['h'],0,strlen($_GET['h'])-1);
104

    
105
		                }else{ 
106
		                        if($_GET['w'][strlen($_GET['h'])-1] == 'x'){
107
		                                echo substr($_GET['h'],0,strlen($_GET['h'])-2);
108
		                        }else
109
		                                echo $_GET['h'];
110
		                }
111
                	     }else echo '347'; ?>;
112

    
113
	if(wflag) mywidth = mywidth * window.innerWidth / 100;
114
	if(hflag) myheight = myheight * window.innerHeight / 100;
115

    
116
	tableData = <?php echo $resp ?>;
117

    
118
	selectedData = $.parseJSON(<?php echo $selectedData;?>);
119

    
120
	google.load('visualization', '1', {'packages': ['geomap']});
121
    google.setOnLoadCallback(draw);
122

    
123
	function draw() {
124
		drawRegionsMap();
125
		drawToolbar();
126
		credits();
127
		filterData = <?php echo json_encode($myfilters);?>;
128
		renderfilters();
129
		controlsforfilters();
130

    
131
		document.getElementById("chart_div").style.width = mywidth+"px";
132
	        document.getElementById("chart_div").style.height = myheight+"px";
133
		document.getElementById("toolbar").style.width = (mywidth/2)+"px";
134
		document.getElementById("credits").style.width = (mywidth/2)+"px";
135
	}
136

    
137
	function drawRegionsMap() {
138
		$('h1').html(selectedData['title']);
139
		$('h2').html(selectedData['subtitle']);
140
		//var rows = new Array();
141
		var headerrow = new Array();
142

    
143
		var tlen = tableData['data'].length;
144
		if(tlen > 0){
145
			rows = new Array();
146
			if(selectedData['xaxistitle'] && selectedData['xaxistitle']!=''){
147
				headerrow.push(selectedData['xaxistitle']);
148
			}
149
			else{
150
				var fld = selectedData['xaxis']['name'].split('-');
151
				if(selectedData['xaxis']['agg']!='')
152
					headerrow.push(selectedData['xaxis']['agg']+" of "+fld[fld.length-1]);
153
				else
154
					headerrow.push(fld[fld.length-1]);
155
			}
156
			for(var i in selectedData['fields']){
157
				if(selectedData['fieldsheaders'] && selectedData['fieldsheaders'][i] && selectedData['fieldsheaders'][i]!=''){
158
					headerrow.push(selectedData['fieldsheaders'][i]);
159
				}
160
				else{
161
					fld = selectedData['fields'][i]['fld'].split('-');
162
					if(selectedData['fields'][i]['agg']!='')
163
						headerrow.push(selectedData['fields'][i]['agg']+" of "+fld[fld.length-1]);
164
					else
165
						headerrow.push(fld[fld.length-1]);
166
				}
167
			}
168
			//headerrow.push({type: 'string', role: 'tooltip'});
169
			rows.push(headerrow);
170
			for(var i=0;i<tlen;i++){
171
				if(tableData['data'][i][0] != "UNKNOWN"){
172
					var row = new Array();
173
					for(var j in tableData['data'][i]){
174
						row.push(tableData['data'][i][j]);
175
					}
176
					rows.push(row);
177
				}
178
			}
179
			//alert(JSON.stringify(rows));
180
		}
181
		else{
182
			for(var i=1;i<rows.length;i++){
183
				for(var j=1;j<rows[i].length;j++){
184
					rows[i][j]=0;
185
				}
186
			}
187
		}
188
		var data = google.visualization.arrayToDataTable(rows);
189

    
190
//TODO ERI: changed map type from geoMap to geoChart so that Region 150 ( Europe) includes Russia
191
                var options = {
192
                        displayMode: 'auto',
193
                         region:'150',
194
                         width: mywidth,
195
                        heigth: myheight,
196
                        colors: ['CED8F6','282F93']
197
                };
198
        $("#chart_div").empty();
199
        
200
 
201
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
202

    
203
  
204

    
205
      chart.draw(data, options);
206
    };
207

    
208
	function drawToolbar() {
209
		var components = [
210
			{type: 'html', datasource: 'datasource.php?com=query&data='+<?php echo $selectedData;?>},
211
			{type: 'csv', datasource: 'datasource.php?com=query&data='+<?php echo $selectedData;?>},
212
			{type: 'htmlcode', datasource: '<?php echo $stats_server ?>/datasource.php?com=query&data='+<?php echo $selectedData;?>,
213
			style: 'width: 800px; height: 700px; border: 3px solid purple;'}
214
		];
215

    
216
		var container = document.getElementById('toolbar');
217
		google.visualization.drawToolbar(container, components);
218
	}
219

    
220
	function numberWithCommas(num){
221
		if(typeof num == "string")
222
			var array = num;
223
		else
224
			var array = num.toString().split('');
225
		var index = -3;
226
		var len = array.length;
227
		while(len + index > 0){
228
			array.splice(index, 0 , ',');
229
			index -= 4;
230
		}
231
		return array.join('');
232
	}
233

    
234

    
235
	function isNumber (o) {
236
		return ! isNaN (o-0) && o !== null && o !== "" && o !== false;
237
	}
238

    
239
	function credits(){
240
		var today = new Date();
241
		var dd = today.getDate();
242
		var mm = today.getMonth()+1; //January is 0!
243
		var yyyy = today.getFullYear();
244

    
245
		var cr = document.getElementById("credits");
246
		cr.innerHTML = "from OpenAIRE via Google Charts (date: " + dd + "/" + mm + "/" + yyyy +")";
247
	}
248

    
249

    
250
function renderfilters(){
251
	if(filterData!=null){
252
		var container = document.getElementById("filters");
253

    
254
		var len = filterData.length
255
		for(var i=0;i<len;i++){
256
			//checkboxes
257
			if(filterData[i].values != undefined){
258
				var str = "<div class='filter'><div class='filterHeader'><p>"+filterData[i].showname+"</p><input type='hidden' value='"+filterData[i].name+"' class='filtername'/></div><a class='all' href='#'>select all</a><a class='none' href='#' style='display:none;float:right'>clear</a><div class='filterForm'><div class='formContainer'><form id='"+filterData[i].name+"' class='filterform'>";
259
				for(var v=0;v<filterData[i].values.length;v++){
260
					str+="<input type='checkbox' value='"+filterData[i].values[v]+"' name='form"+filterData[i].name+"'>"+filterData[i].values[v]+"<br>";
261
				}
262
				str +=	"</form></div></div></div>";
263
				$(container).append(str);
264
			}
265
			//slider <------ NOT WORKING YET
266
			else{
267
				var str = "<div class='filter'><div class='filterHeader'><p>"+filterData[i].showname+"</p><input type='hidden' value='"+filterData[i].name+"' class='filtername'/></div>";
268
				str += "<div class='filter-slider'><div class='slider-container'><div id='slider"+filterData[i].name+"' class='ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all' aria-disabled='false'></div></div>";
269

    
270
				str += "<span class='price-tip slider-user-min'><span id='userMin"+filterData[i].name+"'>"+filterData[i]['min']+"</span></span>";
271
				str += "<span class='price-tip slider-user-max'><span id='userMax"+filterData[i].name+"'>"+filterData[i]['max']+"</span></span>";
272
				str += "<span class='min' style='display:none'><span id='productMin"+filterData[i].name+"'>"+filterData[i]['min']+"</span></span>";
273
				str += "<span class='max' style='display:none'><span id='productMax"+filterData[i].name+"'>"+filterData[i]['max']+"</span></span>";
274
				str += "</div></div>";
275
				$(container).append(str);
276
				myFilter = filterData[i];
277
				$(function(){
278
					$("#slider"+myFilter.name).slider({
279
						step: 1,
280
						range: true,
281
						min: parseInt(myFilter['min']),
282
						max: parseInt(myFilter['max']),
283
						values: [parseInt(myFilter['min']), parseInt(myFilter['max'])],
284
						slide: function(event, ui){
285
							$(this).parent().parent().find('.slider-user-min').find('span').html(ui.values[0]);
286
							$(this).parent().parent().find('.slider-user-max').find('span').html(ui.values[1]);
287
							slidermove(myFilter.name,ui.values[0],ui.values[1]);
288
						}
289
					});
290
				});
291
			}
292
		}
293
	}
294
}
295

    
296
function slidermove(field,min,max){
297
	var myfilters = selectedData['filters'];
298
	var len = myfilters.length;
299
	for(var i=0;i<len;i++){
300
		if(myfilters[i]['name'] == field){
301
			myfilters[i]['min'] = min;
302
			myfilters[i]['max'] = max;
303
			break;
304
		}
305
	}
306

    
307

    
308
	//ajax call to get the new data
309
	$.ajax({
310
	   type: "GET",
311
	   url: "ajaxRouter9.php",
312
	   data: "com=query&type=table&data="+encodeURIComponent(JSON.stringify(selectedData)),
313
	   success: function(resp){
314
		 // we have the response
315
		chartDataFromServer = JSON.parse(resp);
316
		tableData = JSON.parse(resp);
317
		drawRegionsMap();
318
		drawToolbar();
319
		credits();
320
		},
321
	   error: function(e){
322
		 alert('Error:' + e);
323
	   }
324
	});
325
}
326

    
327
function controlsforfilters(){
328
	$("a.all").live("click",function(){
329
		$(this).siblings('.filterForm').find('input').attr("checked","checked");
330
		newValues($(this).siblings('.filterForm').find('input')[0]);
331
		return false;
332
	});
333

    
334
	$("a.none").live("click",function(){
335
                $(this).siblings('.filterForm').find('input').removeAttr("checked");
336
		newValues($(this).siblings('.filterForm').find('input')[0]);		
337
		return false;
338
        });
339

    
340
	$(".formContainer input").live("click",function(){
341
		newValues(this);
342
	});
343
}
344

    
345
function newValues(obj){
346
	var field = $(obj).parent().attr('id');
347
	var myfilters = selectedData['filters'];
348
	var thisfilter;
349
	for(var i=0;i<selectedData['incfilters'].length;i++){
350
		if(selectedData['incfilters'][i]['name'] == field){
351
			thisfilter = selectedData['incfilters'][i];
352
			break;
353
		}
354
	}
355

    
356
	//change the url params
357
	//if is it checked
358
	if($(obj).is(":checked")){
359
		$("a.none").show();
360
		var flag = false;
361
		var i;
362
		for(i=0;i<myfilters.length;i++){
363
			if(myfilters[i]['name'] == field){
364
				flag=true;
365
				break;
366
			}
367
		}
368
		//if there is already such a filter delete all values and insert all the checked
369
		if(flag){
370
			myfilters[i].values = new Array();
371
			$(obj).parent().find("input:checked").each(function(){
372
				myfilters[i].values.push($(this).val());
373
			});
374
			//myfilters[i].values.push($(obj).val());
375
		}
376
		//if there is no such filter
377
		else{
378
			//create one
379
			var newfilter = new Object();
380
			newfilter.name = field;
381
			newfilter.values = new Array();
382
			newfilter.values[0] = $(obj).val();
383
			myfilters.push(newfilter);
384
		}
385
	}
386
	//if it was unchecked
387
	else{
388
		if($(obj).parent().find("input:checked").length == 0)
389
			$("a.none").hide();
390

    
391
		//there should be already such a filter
392
		var flag = false;
393
                var i;
394
                for(i=0;i<myfilters.length;i++){
395
                        if(myfilters[i]['name'] == field){
396
                                flag=true;
397
                                break;
398
                        }
399
                }
400
		//remove the value
401
		var j;
402
		for(j=0;j<myfilters[i].values.length;j++){
403
			if(myfilters[i].values[j] == $(obj).val()){
404
				myfilters[i].values.splice(j,1);
405
				break;
406
			}
407
		}
408
		//if no more values, add the values in the incfilter list and if it is all remove the filter
409
		if(myfilters[i].values.length == 0){
410
			if(thisfilter.values[0]=="all")
411
				myfilters.splice(i,1);
412
			else
413
				myfilters[i].values = thisfilter.values;
414
		}
415
	}
416
	//ajax call to get the new data
417
	$.ajax({
418
	   type: "GET",
419
	   url: "ajaxRouter9.php",
420
	   data: "com=query&type=table&data="+encodeURIComponent(JSON.stringify(selectedData)),  
421
	   success: function(resp){
422
		 // we have the response
423
		chartDataFromServer = JSON.parse(resp);
424
		tableData = JSON.parse(resp);
425
		drawRegionsMap();
426
		drawToolbar();
427
		credits();
428
		},
429
	   error: function(e){
430
		 alert('Error: ' + e);
431
	   }
432
	});
433

    
434
}
435

    
436

    
437

    
438
	</script>
439
  </head>
440
  <body>
441
	<h1 id='title'></h1>
442
	<h2 id='subtitle'></h2>
443
	<div id="chart_div" style="margin:auto;"></div>
444
	<div id='toolbar' style="margin:auto;padding-top:40px"></div>
445
	<div id="credits" style=""></div>
446
	<div id="filters"></div>
447
  </body>
448
</html>
449

    
450

    
451

    
452

    
453

    
(6-6/28)