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
$resp = $controller->makeQuery('table');
22

    
23
// the selected data
24
$myData = json_decode($_GET['data']);
25
// the filters to include
26
$myfilters = $myData->incfilters;
27
// if all posible values are included, get them from the db
28
for($i=0;$i<count($myfilters);$i++){
29
	if(isset($myfilters[$i]->values) && $myfilters[$i]->values=='all'){
30
		$temp = $controller->getFilterData($myData->table,$myfilters[$i]->name);
31
		$myfilters[$i]->values = $temp['data'];
32
	}
33
}
34

    
35
?>
36
  <head>
37
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
38
	<title>choromap</title>
39
	<!-- Include JQuery Core-->
40
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
41
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
42
	<!-- Include styles -->
43
	<!-- <link href="//code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" rel="stylesheet"> -->
44
	<!-- <link href="./js/jquery/css/start/jquery-ui-1.8.20.custom.css" type="text/css">  -->
45
	<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
46

    
47
	<link type="text/css" href="./js/googlecharts.css" rel="stylesheet" />
48
	<link rel="stylesheet" href="./js/filters.css" />
49

    
50

    
51

    
52
	<!-- Google charts api -->
53
	<script type='text/javascript' src='//www.google.com/jsapi'></script>
54

    
55
<script type='text/javascript'>
56
	var chart;
57
	var tableData;
58
	var selectedData;
59
	var filterData;
60
	var myFilter;
61
	var rows = null;
62

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

    
67
	var mywidth =  <?php if(isset($_GET['w']) && $_GET['w']!=''){ 
68
		                if($_GET['w'][strlen($_GET['w'])-1] == '%'){
69
		                        echo substr($_GET['w'],0,strlen($_GET['w'])-1);
70
		 		}else{
71
		                        if($_GET['w'][strlen($_GET['w'])-1] == 'x'){
72
		                                echo substr($_GET['w'],0,strlen($_GET['w'])-2);
73
		                        }else
74
				               echo $_GET['w'];
75
		                }
76
                	     }else echo '556'; ?>;
77
	//the height is set to percentage
78
	var hflag = <?php if(isset($_GET['h']) && $_GET['h']!='' && $_GET['h'][strlen($_GET['h'])-1] == '%'){echo "true";}
79
                	else{echo "false";}?>
80

    
81
	var myheight =  <?php if(isset($_GET['h']) && $_GET['h']!=''){
82
		                if($_GET['h'][strlen($_GET['h'])-1] == '%'){
83
		                        echo substr($_GET['h'],0,strlen($_GET['h'])-1);
84

    
85
		                }else{ 
86
		                        if($_GET['w'][strlen($_GET['h'])-1] == 'x'){
87
		                                echo substr($_GET['h'],0,strlen($_GET['h'])-2);
88
		                        }else
89
		                                echo $_GET['h'];
90
		                }
91
                	     }else echo '347'; ?>;
92

    
93
	if(wflag) mywidth = mywidth * window.innerWidth / 100;
94
	if(hflag) myheight = myheight * window.innerHeight / 100;
95

    
96
	tableData = <?php echo $resp ?>;
97

    
98
	selectedData = $.parseJSON(<?php echo $selectedData;?>);
99

    
100
	google.load('visualization', '1', {'packages': ['geomap']});
101
    google.setOnLoadCallback(draw);
102

    
103
	function draw() {
104
		drawRegionsMap();
105
		drawToolbar();
106
		credits();
107
		filterData = <?php echo json_encode($myfilters);?>;
108
		renderfilters();
109
		controlsforfilters();
110

    
111
		document.getElementById("chart_div").style.width = mywidth+"px";
112
	        document.getElementById("chart_div").style.height = myheight+"px";
113
		document.getElementById("toolbar").style.width = (mywidth/2)+"px";
114
		document.getElementById("credits").style.width = (mywidth/2)+"px";
115
	}
116

    
117
	function drawRegionsMap() {
118
		$('h1').html(selectedData['title']);
119
		$('h2').html(selectedData['subtitle']);
120
		//var rows = new Array();
121
		var headerrow = new Array();
122

    
123
		var tlen = tableData['data'].length;
124
		if(tlen > 0){
125
			rows = new Array();
126
			if(selectedData['xaxistitle'] && selectedData['xaxistitle']!=''){
127
				headerrow.push(selectedData['xaxistitle']);
128
			}
129
			else{
130
				var fld = selectedData['xaxis']['name'].split('-');
131
				if(selectedData['xaxis']['agg']!='')
132
					headerrow.push(selectedData['xaxis']['agg']+" of "+fld[fld.length-1]);
133
				else
134
					headerrow.push(fld[fld.length-1]);
135
			}
136
			for(var i in selectedData['fields']){
137
				if(selectedData['fieldsheaders'] && selectedData['fieldsheaders'][i] && selectedData['fieldsheaders'][i]!=''){
138
					headerrow.push(selectedData['fieldsheaders'][i]);
139
				}
140
				else{
141
					fld = selectedData['fields'][i]['fld'].split('-');
142
					if(selectedData['fields'][i]['agg']!='')
143
						headerrow.push(selectedData['fields'][i]['agg']+" of "+fld[fld.length-1]);
144
					else
145
						headerrow.push(fld[fld.length-1]);
146
				}
147
			}
148
			//headerrow.push({type: 'string', role: 'tooltip'});
149
			rows.push(headerrow);
150
			for(var i=0;i<tlen;i++){
151
				if(tableData['data'][i][0] != "UNKNOWN"){
152
					var row = new Array();
153
					for(var j in tableData['data'][i]){
154
						row.push(tableData['data'][i][j]);
155
					}
156
					rows.push(row);
157
				}
158
			}
159
			//alert(JSON.stringify(rows));
160
		}
161
		else{
162
			for(var i=1;i<rows.length;i++){
163
				for(var j=1;j<rows[i].length;j++){
164
					rows[i][j]=0;
165
				}
166
			}
167
		}
168
		var data = google.visualization.arrayToDataTable(rows);
169

    
170
        	var options = {
171
			displayMode: 'auto',
172
			region: '150',
173
			//width: mywidth,
174
			//heigth: myheight,
175
			colors: [0xCED8F6,0x282F93]
176
		};
177
	$("#chart_div").empty();
178
        var chart = new google.visualization.GeoMap(document.getElementById('chart_div'));
179
        chart.draw(data, options);
180
    };
181

    
182
	function drawToolbar() {
183
		var components = [
184
			{type: 'html', datasource: 'datasource.php?com=query&data='+<?php echo $selectedData;?>},
185
			{type: 'csv', datasource: 'datasource.php?com=query&data='+<?php echo $selectedData;?>},
186
			{type: 'htmlcode', datasource: '<?php echo $stats_server ?>/datasource.php?com=query&data='+<?php echo $selectedData;?>,
187
			style: 'width: 800px; height: 700px; border: 3px solid purple;'}
188
		];
189

    
190
		var container = document.getElementById('toolbar');
191
		google.visualization.drawToolbar(container, components);
192
	}
193

    
194
	function numberWithCommas(num){
195
		if(typeof num == "string")
196
			var array = num;
197
		else
198
			var array = num.toString().split('');
199
		var index = -3;
200
		var len = array.length;
201
		while(len + index > 0){
202
			array.splice(index, 0 , ',');
203
			index -= 4;
204
		}
205
		return array.join('');
206
	}
207

    
208

    
209
	function isNumber (o) {
210
		return ! isNaN (o-0) && o !== null && o !== "" && o !== false;
211
	}
212

    
213
	function credits(){
214
		var today = new Date();
215
		var dd = today.getDate();
216
		var mm = today.getMonth()+1; //January is 0!
217
		var yyyy = today.getFullYear();
218

    
219
		var cr = document.getElementById("credits");
220
		cr.innerHTML = "from OpenAIRE via Google Charts (date: " + dd + "/" + mm + "/" + yyyy +")";
221
	}
222

    
223

    
224
function renderfilters(){
225
	if(filterData!=null){
226
		var container = document.getElementById("filters");
227

    
228
		var len = filterData.length
229
		for(var i=0;i<len;i++){
230
			//checkboxes
231
			if(filterData[i].values != undefined){
232
				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'>";
233
				for(var v=0;v<filterData[i].values.length;v++){
234
					str+="<input type='checkbox' value='"+filterData[i].values[v]+"' name='form"+filterData[i].name+"'>"+filterData[i].values[v]+"<br>";
235
				}
236
				str +=	"</form></div></div></div>";
237
				$(container).append(str);
238
			}
239
			//slider <------ NOT WORKING YET
240
			else{
241
				var str = "<div class='filter'><div class='filterHeader'><p>"+filterData[i].showname+"</p><input type='hidden' value='"+filterData[i].name+"' class='filtername'/></div>";
242
				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>";
243

    
244
				str += "<span class='price-tip slider-user-min'><span id='userMin"+filterData[i].name+"'>"+filterData[i]['min']+"</span></span>";
245
				str += "<span class='price-tip slider-user-max'><span id='userMax"+filterData[i].name+"'>"+filterData[i]['max']+"</span></span>";
246
				str += "<span class='min' style='display:none'><span id='productMin"+filterData[i].name+"'>"+filterData[i]['min']+"</span></span>";
247
				str += "<span class='max' style='display:none'><span id='productMax"+filterData[i].name+"'>"+filterData[i]['max']+"</span></span>";
248
				str += "</div></div>";
249
				$(container).append(str);
250
				myFilter = filterData[i];
251
				$(function(){
252
					$("#slider"+myFilter.name).slider({
253
						step: 1,
254
						range: true,
255
						min: parseInt(myFilter['min']),
256
						max: parseInt(myFilter['max']),
257
						values: [parseInt(myFilter['min']), parseInt(myFilter['max'])],
258
						slide: function(event, ui){
259
							$(this).parent().parent().find('.slider-user-min').find('span').html(ui.values[0]);
260
							$(this).parent().parent().find('.slider-user-max').find('span').html(ui.values[1]);
261
							slidermove(myFilter.name,ui.values[0],ui.values[1]);
262
						}
263
					});
264
				});
265
			}
266
		}
267
	}
268
}
269

    
270
function slidermove(field,min,max){
271
	var myfilters = selectedData['filters'];
272
	var len = myfilters.length;
273
	for(var i=0;i<len;i++){
274
		if(myfilters[i]['name'] == field){
275
			myfilters[i]['min'] = min;
276
			myfilters[i]['max'] = max;
277
			break;
278
		}
279
	}
280

    
281

    
282
	//ajax call to get the new data
283
	$.ajax({
284
	   type: "GET",
285
	   url: "ajaxRouter9.php",
286
	   data: "com=query&type=table&data="+encodeURIComponent(JSON.stringify(selectedData)),
287
	   success: function(resp){
288
		 // we have the response
289
		chartDataFromServer = JSON.parse(resp);
290
		tableData = JSON.parse(resp);
291
		drawRegionsMap();
292
		drawToolbar();
293
		credits();
294
		},
295
	   error: function(e){
296
		 alert('Error:' + e);
297
	   }
298
	});
299
}
300

    
301
function controlsforfilters(){
302
	$("a.all").live("click",function(){
303
		$(this).siblings('.filterForm').find('input').attr("checked","checked");
304
		newValues($(this).siblings('.filterForm').find('input')[0]);
305
		return false;
306
	});
307

    
308
	$("a.none").live("click",function(){
309
                $(this).siblings('.filterForm').find('input').removeAttr("checked");
310
		newValues($(this).siblings('.filterForm').find('input')[0]);		
311
		return false;
312
        });
313

    
314
	$(".formContainer input").live("click",function(){
315
		newValues(this);
316
	});
317
}
318

    
319
function newValues(obj){
320
	var field = $(obj).parent().attr('id');
321
	var myfilters = selectedData['filters'];
322
	var thisfilter;
323
	for(var i=0;i<selectedData['incfilters'].length;i++){
324
		if(selectedData['incfilters'][i]['name'] == field){
325
			thisfilter = selectedData['incfilters'][i];
326
			break;
327
		}
328
	}
329

    
330
	//change the url params
331
	//if is it checked
332
	if($(obj).is(":checked")){
333
		$("a.none").show();
334
		var flag = false;
335
		var i;
336
		for(i=0;i<myfilters.length;i++){
337
			if(myfilters[i]['name'] == field){
338
				flag=true;
339
				break;
340
			}
341
		}
342
		//if there is already such a filter delete all values and insert all the checked
343
		if(flag){
344
			myfilters[i].values = new Array();
345
			$(obj).parent().find("input:checked").each(function(){
346
				myfilters[i].values.push($(this).val());
347
			});
348
			//myfilters[i].values.push($(obj).val());
349
		}
350
		//if there is no such filter
351
		else{
352
			//create one
353
			var newfilter = new Object();
354
			newfilter.name = field;
355
			newfilter.values = new Array();
356
			newfilter.values[0] = $(obj).val();
357
			myfilters.push(newfilter);
358
		}
359
	}
360
	//if it was unchecked
361
	else{
362
		if($(obj).parent().find("input:checked").length == 0)
363
			$("a.none").hide();
364

    
365
		//there should be already such a filter
366
		var flag = false;
367
                var i;
368
                for(i=0;i<myfilters.length;i++){
369
                        if(myfilters[i]['name'] == field){
370
                                flag=true;
371
                                break;
372
                        }
373
                }
374
		//remove the value
375
		var j;
376
		for(j=0;j<myfilters[i].values.length;j++){
377
			if(myfilters[i].values[j] == $(obj).val()){
378
				myfilters[i].values.splice(j,1);
379
				break;
380
			}
381
		}
382
		//if no more values, add the values in the incfilter list and if it is all remove the filter
383
		if(myfilters[i].values.length == 0){
384
			if(thisfilter.values[0]=="all")
385
				myfilters.splice(i,1);
386
			else
387
				myfilters[i].values = thisfilter.values;
388
		}
389
	}
390
	//ajax call to get the new data
391
	$.ajax({
392
	   type: "GET",
393
	   url: "ajaxRouter9.php",
394
	   data: "com=query&type=table&data="+encodeURIComponent(JSON.stringify(selectedData)),  
395
	   success: function(resp){
396
		 // we have the response
397
		chartDataFromServer = JSON.parse(resp);
398
		tableData = JSON.parse(resp);
399
		drawRegionsMap();
400
		drawToolbar();
401
		credits();
402
		},
403
	   error: function(e){
404
		 alert('Error: ' + e);
405
	   }
406
	});
407

    
408
}
409

    
410

    
411

    
412
	</script>
413
  </head>
414
  <body>
415
	<h1 id='title'></h1>
416
	<h2 id='subtitle'></h2>
417
	<div id="chart_div" style="margin:auto;"></div>
418
	<div id='toolbar' style="margin:auto;padding-top:40px"></div>
419
	<div id="credits" style=""></div>
420
	<div id="filters"></div>
421
  </body>
422
</html>
(6-6/28)