Project

General

Profile

1 37264 eri.katsar
<!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
$myfilters = $myData->incfilters;
36
// if all posible values are included, get them from the db
37
for($i=0;$i<count($myfilters);$i++){
38
	if(isset($myfilters[$i]->values) && $myfilters[$i]->values=='all'){
39
		$temp = $controller->getFilterData($myData->table,$myfilters[$i]->name);
40
		$myfilters[$i]->values = $temp['data'];
41
	}
42
}
43
44
?>
45
  <head>
46
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
47
	<title>choromap</title>
48
	<!-- Include JQuery Core-->
49
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
50
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
51
	<!-- Include styles -->
52
	<!-- <link href="//code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" rel="stylesheet"> -->
53
	<!-- <link href="./js/jquery/css/start/jquery-ui-1.8.20.custom.css" type="text/css">  -->
54
	<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
55
56
	<link type="text/css" href="./js/googlecharts.css" rel="stylesheet" />
57
	<link rel="stylesheet" href="./js/filters.css" />
58
59
60
61
	<!-- Google charts api -->
62
	<script type='text/javascript' src='//www.google.com/jsapi'></script>
63
64
<script type='text/javascript'>
65
	var chart;
66
	var tableData;
67
	var selectedData;
68
	var filterData;
69
	var myFilter;
70
	var rows = null;
71
72
	//the width is set to percentage
73
	var wflag = <?php if(isset($_GET['w']) && $_GET['w']!='' && $_GET['w'][strlen($_GET['w'])-1] == '%'){echo "true";}
74
                	else{echo "false";}?>
75
76
	var mywidth =  <?php if(isset($_GET['w']) && $_GET['w']!=''){
77
		                if($_GET['w'][strlen($_GET['w'])-1] == '%'){
78
		                        echo substr($_GET['w'],0,strlen($_GET['w'])-1);
79
		 		}else{
80
		                        if($_GET['w'][strlen($_GET['w'])-1] == 'x'){
81
		                                echo substr($_GET['w'],0,strlen($_GET['w'])-2);
82
		                        }else
83
				               echo $_GET['w'];
84
		                }
85
                	     }else echo '556'; ?>;
86
	//the height is set to percentage
87
	var hflag = <?php if(isset($_GET['h']) && $_GET['h']!='' && $_GET['h'][strlen($_GET['h'])-1] == '%'){echo "true";}
88
                	else{echo "false";}?>
89
90
	var myheight =  <?php if(isset($_GET['h']) && $_GET['h']!=''){
91
		                if($_GET['h'][strlen($_GET['h'])-1] == '%'){
92
		                        echo substr($_GET['h'],0,strlen($_GET['h'])-1);
93
94
		                }else{
95
		                        if($_GET['w'][strlen($_GET['h'])-1] == 'x'){
96
		                                echo substr($_GET['h'],0,strlen($_GET['h'])-2);
97
		                        }else
98
		                                echo $_GET['h'];
99
		                }
100
                	     }else echo '347'; ?>;
101
102
	if(wflag) mywidth = mywidth * window.innerWidth / 100;
103
	if(hflag) myheight = myheight * window.innerHeight / 100;
104
105
	tableData = <?php echo $resp ?>;
106
107
	selectedData = $.parseJSON(<?php echo $selectedData;?>);
108
109
	google.load('visualization', '1', {'packages': ['geomap']});
110
    google.setOnLoadCallback(draw);
111
112
	function draw() {
113
		drawRegionsMap();
114
		drawToolbar();
115
		credits();
116
		filterData = <?php echo json_encode($myfilters);?>;
117
		renderfilters();
118
		controlsforfilters();
119
120
		document.getElementById("chart_div").style.width = mywidth+"px";
121
	        document.getElementById("chart_div").style.height = myheight+"px";
122
		document.getElementById("toolbar").style.width = (mywidth/2)+"px";
123
		document.getElementById("credits").style.width = (mywidth/2)+"px";
124
	}
125
126
	function drawRegionsMap() {
127
		$('h1').html(selectedData['title']);
128
		$('h2').html(selectedData['subtitle']);
129
		//var rows = new Array();
130
		var headerrow = new Array();
131
132
		var tlen = tableData['data'].length;
133
		if(tlen > 0){
134
			rows = new Array();
135
			if(selectedData['xaxistitle'] && selectedData['xaxistitle']!=''){
136
				headerrow.push(selectedData['xaxistitle']);
137
			}
138
			else{
139
				var fld = selectedData['xaxis']['name'].split('-');
140
				if(selectedData['xaxis']['agg']!='')
141
					headerrow.push(selectedData['xaxis']['agg']+" of "+fld[fld.length-1]);
142
				else
143
					headerrow.push(fld[fld.length-1]);
144
			}
145
			for(var i in selectedData['fields']){
146
				if(selectedData['fieldsheaders'] && selectedData['fieldsheaders'][i] && selectedData['fieldsheaders'][i]!=''){
147
					headerrow.push(selectedData['fieldsheaders'][i]);
148
				}
149
				else{
150
					fld = selectedData['fields'][i]['fld'].split('-');
151
					if(selectedData['fields'][i]['agg']!='')
152
						headerrow.push(selectedData['fields'][i]['agg']+" of "+fld[fld.length-1]);
153
					else
154
						headerrow.push(fld[fld.length-1]);
155
				}
156
			}
157
			//headerrow.push({type: 'string', role: 'tooltip'});
158
			rows.push(headerrow);
159
			for(var i=0;i<tlen;i++){
160
				if(tableData['data'][i][0] != "UNKNOWN"){
161
					var row = new Array();
162
					for(var j in tableData['data'][i]){
163
						row.push(tableData['data'][i][j]);
164
					}
165
					rows.push(row);
166
				}
167
			}
168
			//alert(JSON.stringify(rows));
169
		}
170
		else{
171
			for(var i=1;i<rows.length;i++){
172
				for(var j=1;j<rows[i].length;j++){
173
					rows[i][j]=0;
174
				}
175
			}
176
		}
177
		var data = google.visualization.arrayToDataTable(rows);
178
179 37784 eri.katsar
//TODO ERI: changed map type from geoMap to geoChart so that Region 150 ( Europe) includes Russia
180
                var options = {
181
                        displayMode: 'auto',
182
                         region:'150',
183
                         width: mywidth,
184
                        heigth: myheight,
185
                        colors: ['CED8F6','282F93']
186
                };
187
        $("#chart_div").empty();
188
189
190
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
191
192
193
194
      chart.draw(data, options);
195 37264 eri.katsar
    };
196
197
	function drawToolbar() {
198
		var components = [
199
			{type: 'html', datasource: 'datasource.php?com=query&data='+<?php echo $selectedData;?>},
200
			{type: 'csv', datasource: 'datasource.php?com=query&data='+<?php echo $selectedData;?>},
201
			{type: 'htmlcode', datasource: '<?php echo $stats_server ?>/datasource.php?com=query&data='+<?php echo $selectedData;?>,
202
			style: 'width: 800px; height: 700px; border: 3px solid purple;'}
203
		];
204
205
		var container = document.getElementById('toolbar');
206
		google.visualization.drawToolbar(container, components);
207
	}
208
209
	function numberWithCommas(num){
210
		if(typeof num == "string")
211
			var array = num;
212
		else
213
			var array = num.toString().split('');
214
		var index = -3;
215
		var len = array.length;
216
		while(len + index > 0){
217
			array.splice(index, 0 , ',');
218
			index -= 4;
219
		}
220
		return array.join('');
221
	}
222
223
224
	function isNumber (o) {
225
		return ! isNaN (o-0) && o !== null && o !== "" && o !== false;
226
	}
227
228
	function credits(){
229
		var today = new Date();
230
		var dd = today.getDate();
231
		var mm = today.getMonth()+1; //January is 0!
232
		var yyyy = today.getFullYear();
233
234
		var cr = document.getElementById("credits");
235
		cr.innerHTML = "from OpenAIRE via Google Charts (date: " + dd + "/" + mm + "/" + yyyy +")";
236
	}
237
238
239
function renderfilters(){
240
	if(filterData!=null){
241
		var container = document.getElementById("filters");
242
243
		var len = filterData.length
244
		for(var i=0;i<len;i++){
245
			//checkboxes
246
			if(filterData[i].values != undefined){
247
				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'>";
248
				for(var v=0;v<filterData[i].values.length;v++){
249
					str+="<input type='checkbox' value='"+filterData[i].values[v]+"' name='form"+filterData[i].name+"'>"+filterData[i].values[v]+"<br>";
250
				}
251
				str +=	"</form></div></div></div>";
252
				$(container).append(str);
253
			}
254
			//slider <------ NOT WORKING YET
255
			else{
256
				var str = "<div class='filter'><div class='filterHeader'><p>"+filterData[i].showname+"</p><input type='hidden' value='"+filterData[i].name+"' class='filtername'/></div>";
257
				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>";
258
259
				str += "<span class='price-tip slider-user-min'><span id='userMin"+filterData[i].name+"'>"+filterData[i]['min']+"</span></span>";
260
				str += "<span class='price-tip slider-user-max'><span id='userMax"+filterData[i].name+"'>"+filterData[i]['max']+"</span></span>";
261
				str += "<span class='min' style='display:none'><span id='productMin"+filterData[i].name+"'>"+filterData[i]['min']+"</span></span>";
262
				str += "<span class='max' style='display:none'><span id='productMax"+filterData[i].name+"'>"+filterData[i]['max']+"</span></span>";
263
				str += "</div></div>";
264
				$(container).append(str);
265
				myFilter = filterData[i];
266
				$(function(){
267
					$("#slider"+myFilter.name).slider({
268
						step: 1,
269
						range: true,
270
						min: parseInt(myFilter['min']),
271
						max: parseInt(myFilter['max']),
272
						values: [parseInt(myFilter['min']), parseInt(myFilter['max'])],
273
						slide: function(event, ui){
274
							$(this).parent().parent().find('.slider-user-min').find('span').html(ui.values[0]);
275
							$(this).parent().parent().find('.slider-user-max').find('span').html(ui.values[1]);
276
							slidermove(myFilter.name,ui.values[0],ui.values[1]);
277
						}
278
					});
279
				});
280
			}
281
		}
282
	}
283
}
284
285
function slidermove(field,min,max){
286
	var myfilters = selectedData['filters'];
287
	var len = myfilters.length;
288
	for(var i=0;i<len;i++){
289
		if(myfilters[i]['name'] == field){
290
			myfilters[i]['min'] = min;
291
			myfilters[i]['max'] = max;
292
			break;
293
		}
294
	}
295
296
297
	//ajax call to get the new data
298
	$.ajax({
299
	   type: "GET",
300
	   url: "ajaxRouter9.php",
301
	   data: "com=query&type=table&data="+encodeURIComponent(JSON.stringify(selectedData)),
302
	   success: function(resp){
303
		 // we have the response
304
		chartDataFromServer = JSON.parse(resp);
305
		tableData = JSON.parse(resp);
306
		drawRegionsMap();
307
		drawToolbar();
308
		credits();
309
		},
310
	   error: function(e){
311
		 alert('Error:' + e);
312
	   }
313
	});
314
}
315
316
function controlsforfilters(){
317
	$("a.all").live("click",function(){
318
		$(this).siblings('.filterForm').find('input').attr("checked","checked");
319
		newValues($(this).siblings('.filterForm').find('input')[0]);
320
		return false;
321
	});
322
323
	$("a.none").live("click",function(){
324
                $(this).siblings('.filterForm').find('input').removeAttr("checked");
325
		newValues($(this).siblings('.filterForm').find('input')[0]);
326
		return false;
327
        });
328
329
	$(".formContainer input").live("click",function(){
330
		newValues(this);
331
	});
332
}
333
334
function newValues(obj){
335
	var field = $(obj).parent().attr('id');
336
	var myfilters = selectedData['filters'];
337
	var thisfilter;
338
	for(var i=0;i<selectedData['incfilters'].length;i++){
339
		if(selectedData['incfilters'][i]['name'] == field){
340
			thisfilter = selectedData['incfilters'][i];
341
			break;
342
		}
343
	}
344
345
	//change the url params
346
	//if is it checked
347
	if($(obj).is(":checked")){
348
		$("a.none").show();
349
		var flag = false;
350
		var i;
351
		for(i=0;i<myfilters.length;i++){
352
			if(myfilters[i]['name'] == field){
353
				flag=true;
354
				break;
355
			}
356
		}
357
		//if there is already such a filter delete all values and insert all the checked
358
		if(flag){
359
			myfilters[i].values = new Array();
360
			$(obj).parent().find("input:checked").each(function(){
361
				myfilters[i].values.push($(this).val());
362
			});
363
			//myfilters[i].values.push($(obj).val());
364
		}
365
		//if there is no such filter
366
		else{
367
			//create one
368
			var newfilter = new Object();
369
			newfilter.name = field;
370
			newfilter.values = new Array();
371
			newfilter.values[0] = $(obj).val();
372
			myfilters.push(newfilter);
373
		}
374
	}
375
	//if it was unchecked
376
	else{
377
		if($(obj).parent().find("input:checked").length == 0)
378
			$("a.none").hide();
379
380
		//there should be already such a filter
381
		var flag = false;
382
                var i;
383
                for(i=0;i<myfilters.length;i++){
384
                        if(myfilters[i]['name'] == field){
385
                                flag=true;
386
                                break;
387
                        }
388
                }
389
		//remove the value
390
		var j;
391
		for(j=0;j<myfilters[i].values.length;j++){
392
			if(myfilters[i].values[j] == $(obj).val()){
393
				myfilters[i].values.splice(j,1);
394
				break;
395
			}
396
		}
397
		//if no more values, add the values in the incfilter list and if it is all remove the filter
398
		if(myfilters[i].values.length == 0){
399
			if(thisfilter.values[0]=="all")
400
				myfilters.splice(i,1);
401
			else
402
				myfilters[i].values = thisfilter.values;
403
		}
404
	}
405
	//ajax call to get the new data
406
	$.ajax({
407
	   type: "GET",
408
	   url: "ajaxRouter9.php",
409
	   data: "com=query&type=table&data="+encodeURIComponent(JSON.stringify(selectedData)),
410
	   success: function(resp){
411
		 // we have the response
412
		chartDataFromServer = JSON.parse(resp);
413
		tableData = JSON.parse(resp);
414
		drawRegionsMap();
415
		drawToolbar();
416
		credits();
417
		},
418
	   error: function(e){
419
		 alert('Error: ' + e);
420
	   }
421
	});
422
423
}
424
425
426
427
	</script>
428
  </head>
429
  <body>
430
	<h1 id='title'></h1>
431
	<h2 id='subtitle'></h2>
432
	<div id="chart_div" style="margin:auto;"></div>
433
	<div id='toolbar' style="margin:auto;padding-top:40px"></div>
434
	<div id="credits" style=""></div>
435
	<div id="filters"></div>
436
  </body>
437
</html>