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
/*todo eri hide toolbar!!!*/	
126
drawToolbar();
127
		credits();
128
		filterData = <?php echo json_encode($myfilters);?>;
129
		renderfilters();
130
		controlsforfilters();
131

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

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

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

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

    
204
  
205

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

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

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

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

    
235

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

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

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

    
250

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

    
255
		var len = filterData.length
256
		for(var i=0;i<len;i++){
257
			//checkboxes
258
			if(filterData[i].values != undefined){
259
				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'>";
260
				for(var v=0;v<filterData[i].values.length;v++){
261
					str+="<input type='checkbox' value='"+filterData[i].values[v]+"' name='form"+filterData[i].name+"'>"+filterData[i].values[v]+"<br>";
262
				}
263
				str +=	"</form></div></div></div>";
264
				$(container).append(str);
265
			}
266
			//slider <------ NOT WORKING YET
267
			else{
268
				var str = "<div class='filter'><div class='filterHeader'><p>"+filterData[i].showname+"</p><input type='hidden' value='"+filterData[i].name+"' class='filtername'/></div>";
269
				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>";
270

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

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

    
308

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

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

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

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

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

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

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

    
435
}
436

    
437

    
438

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

    
451

    
452

    
453

    
454

    
(7-7/30)