Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
executable file
·641 lines (509 loc) · 17.4 KB
/
Copy pathindex.php
File metadata and controls
executable file
·641 lines (509 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<!DOCTYPE html>
<html lang="en">
<head>
<title>EVRYTHNG</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="files/custom.css" media="all" type="text/css">
<link rel="stylesheet" href="theme/default/style.css" type="text/css">
<link rel="stylesheet" href="theme/style.css" type="text/css">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="lib/OpenLayers.js"></script>
<script type="text/javascript">
var map, layer;
// This is the layer of Markers
var vectorLayer = new OpenLayers.Layer.Vector("Overlay");
//Add a selector control to the vectorLayer with popup functions
var controls = {selector: new OpenLayers.Control.SelectFeature(vectorLayer, { onSelect: createPopup, onUnselect: destroyPopup })};
function init(){
OpenLayers.ProxyHost = "/proxy/?url=";
map = new OpenLayers.Map('map', {maxResolution:'auto'});
layer = new OpenLayers.Layer.WMS( "OpenLayers WMS","http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.addControl(controls['selector']);
controls['selector'].activate();
}
/*
This is a much cleaner solution, but the engine doesn't support GeoRSS at this point.
*/
function addUrl() {
var urlObj = OpenLayers.Util.getElement('url');
var value = urlObj.value;
var parts = value.split("/");
var newl = new OpenLayers.Layer.GeoRSS( parts[parts.length-1], value);
map.addLayer(newl);
urlObj.value = "";
}
/*
Obviously, this is not a clean solution. One could call the locations to display directly
from JS on the fly. The point is to illustrate how to use both JS and PHP apps to create more
interactive displays and experiences around real-time data.
*/
function plotLocations(){
// For each location update
for (var i = 0; i < locations.length; i++) {
var location = locations[i];
var lon=location.longitude;
var lat=location.latitude;
var desc = "<h3>Marker #" + i +"</h3><br><b>Timestamp: </b>" + new Date(location.timestamp)+"<br>";
desc = desc + "<b>Long: </b>"+ lon +", <b>Lat: </b>"+ lat;"<br><br><h3>Custom Fields</h3><br>";
for (var key in location.customFields) {
if (location.customFields.hasOwnProperty(key)) {
desc = desc + "<br><b>"+key+": </b><em>"+location.customFields[key]+"</em>";
}
}
// Add a marker to the map (& a popup with it)
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point( lon, lat ),
{description: desc},
{externalGraphic: 'img/marker.png', graphicHeight: 25, graphicWidth: 21, graphicXOffset:-12, graphicYOffset:-25 }
);
vectorLayer.addFeatures(feature);
}
// Now add the layer to the map
map.addLayer(vectorLayer);
}
function createPopup(feature) {
feature.popup = new OpenLayers.Popup.FramedCloud("pop",
feature.geometry.getBounds().getCenterLonLat(),
null,
'<div class="markerContent">'+feature.attributes.description+'</div>',
null,
true,
function() { controls['selector'].unselectAll(); }
);
//feature.popup.closeOnMove = true;
map.addPopup(feature.popup);
}
function destroyPopup(feature) {
feature.popup.destroy();
feature.popup = null;
}
</script>
</head>
<body onload="init(); plotLocations()">
<div id="masthead">
<a href="/" class="brand">EVRYTHNG Thng Viewer</a>
</div>
<div class="container">
<?
// include the EVRYTHNG API wrapper
include_once "evrythng.php";
setContext($accountApiKey);
// Used for time conversion, by default all data in our
date_default_timezone_set('Europe/London');
?>
<h1>API General</h1>
<h3>API Key</h3>
<input type="text" id="api-key" size="100" value=<? echo "\"".$apiKey."\""; ?> />
<h3>List of Thngs owned</h3>
<form accept-charset="UTF-8" action="index.php" class="simple_form form-inline" method="get" novalidate="novalidate">
<div style="margin:0;padding:0;display:inline"></div>
<div class="input select required">
<select class="select required" name="thng_id">
<?
// This gets the list of all the thngs created with this API key and displays it in a drop-down box
$results = getThngs();
$thngs=json_decode($results);
for ($i=0; $i<sizeof($thngs); $i++) {
echo "<option value=\"".$thngs[$i]->{'id'}."\">".$thngs[$i]->{'name'}." (".$thngs[$i]->{'id'}.")"."</option>\n";
}
?>
</select>
<input class="btn btn-primary" name="commit" value="Display this thng" type="submit">
</div>
</form>
<br><br><br>
<h1>Thng Details</h1>
<?
if($thng_id=$_GET["thng_id"]) { // If a thng_id has been specified using the parameter "thng_id", then get & display the data of the thng
$results = getThng($thng_id);
$thng=json_decode($results);
?>
<table class="table table-striped table-bordered table-condensed">
<tbody>
<tr>
<th>Full JSON</th>
<td><? echo $results; ?></td>
</tr>
<tr>
<th>Thng ID</th>
<td><? echo $thng_id; ?></td>
</tr>
<tr>
<th>Name</th>
<td><? echo $thng->{'name'}; ?> </td>
</tr>
<tr>
<th>Description</th>
<td><? echo $thng->{'description'}; ?></td>
</tr>
<tr>
<th>Created At</th>
<td><?
// Time stamps in the engine are stored in milliseconds, so need to convert into seconds to get epochs
echo date('r', $thng->{'createdAt'}/1000)." (UNIX Timestamp: ".$thng->{'createdAt'}.")";
?></td>
</tr>
<tr>
<th>Updated At</th>
<td><? echo date('r', $thng->{'updatedAt'}/1000)." (UNIX Timestamp: ".$thng->{'updatedAt'}.")"; ?></td>
</tr>
<tr>
<th>Product ID</th>
<td><?
if ($thng->{'product'}){ // some thngs don't have a product
$product_id=$thng->{'product'};
echo "<a href=\"index.php?product_id=".$product_id."\">".$product_id."</a>";
}
?></td>
</tr>
<tr>
<th>Tags</th>
<td><? echo "[\"".implode('","', $thng->{'tags'})."\"]"; ?></td>
</tr>
<tr>
<th>Last Seen: </th>
<td>
<table class="table table-bordered">
<tr>
<th>Longitude: </th>
<td><? echo $thng->{'location'}->{'longitude'}; ?></td>
</tr>
<tr>
<th>Latitude: </th>
<td> <? echo $thng->{'location'}->{'latitude'}; ?></td>
</tr>
<tr>
<th>Time: </th>
<td><? echo $thng->{'location'}->{'timestamp'}; ?></td>
</tr>
</table>
</td>
</tr>
<tr>
<th>Properties</th>
<td>
<table class="table table-bordered">
<?
// now we get all the properties of that thng
$results = getThngProperties($thng_id);
$thng_props=json_decode($results);
for ($i=0; $i<sizeof($thng_props); $i++) {
//$res = isset($arr[$i]);
echo "<tr><th>".$i."</th><td> ".$thng_props[$i]->{'key'}."</td><td>".$thng_props[$i]->{'value'}."</td></tr>";
}
?>
</table>
<h4>Property Browser</h4>
<p>
<label for="unittype">Select unit type:</label>
<select class="select required" id="thng_property_display" name="thng_property_display" onChange="updateGraph(this.value)">
<?
// This gets the list of all the thngs created with this API key and displays it in a drop-down box
// Ideally get only the thngs in the right collection
for ($i=0; $i<sizeof($thng_props); $i++) {
if ($_GET['property']==$thng_props[$i]->{'key'}){
echo "<option value=\"".$thng_props[$i]->{'key'}."\" selected=\"selected\">".$thng_props[$i]->{'key'}."</option>\n";
} else {
echo "<option value=\"".$thng_props[$i]->{'key'}."\">".$thng_props[$i]->{'key'}."</option>\n";
}
}
?>
</select>
</p>
<div id="container" style="height: 500px; min-width: 500px"></div>
</td>
</tr>
</tbody>
</table>
<?
// $property="distance";
// $results = getPropertyPlotData($thng_id,$property);
// $rawData=json_decode($results);
// var_dump($rawData);
?>
<script src="files/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<script>
function updateGraph(property){
var thng_id = '<?php echo $thng_id; ?>';
var json = (function() {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': "evrythng.php?proxy="+property+"&thng_id="+thng_id+"&serverTime=true",
'dataType': "json",
'success': function (data) {
json = data;
}
});
return json;
})();
//alert(json);
plotProperty(json,property);
}
function plotPropertyStock(data,label) {
// Create the chart
$('#container').highcharts('StockChart', {
rangeSelector : {
selected : 1
},
title : {
text : label
},
series : [{
name : label,
data : data,
tooltip: {
valueDecimals: 2
}
}]
});
}
function plotProperty(data,label) {
// Create the chart
$('#container').highcharts({
rangeSelector : {
selected : 1
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%e. %b',
year: '%b'
}
},
title : {
text : label
},
series : [{
name : label,
data : data,
tooltip: {
valueDecimals: 2
}
}]
});
}
$(function() {
/*
Get this data remotely, not tested - just playing around
Obiously we should use more jquery here, not optimal to call it from
php. Ideally, graphs can be easily refreshed only using jQuery, either
directly from the server, or from the local php (hence the wrapper proxy mode)
*/
var json = (function() {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': "rawData.json",
'dataType': "json",
'success': function (data) {
json = data;
}
});
return json;
})();
plotProperty(json,'Temperature')
});
</script>
<h2>Redirection</h2>
<div>
<p>
<?
//echo $results;
// Check here if there is a delete redirection request
if ($_POST['delete_redirection']){
echo "Redirection deleted: /thngs/".$thng_id."/redirection".deleteRedirection($thng_id);
} elseif ($_POST['create_redirection']){
$data = array("defaultRedirectUrl" => "http://localhost/~trifa/quickstart/index.php?thng_id={thngId}&redirected=1");
$data_string = json_encode($data);
echo "<br><b>Data sent:</b>".$data_string;
echo "<br><b>Redirection created:</b>".createRedirection($thng_id,$data_string)."<br>";
}
$results = getRedirection($thng_id);
$redirection = json_decode($results);
if ($redirection->{'status'}==404){
echo "<br><b>No redirection set yet.</b> Click the button below to create one!";
?>
<form method="post" action="">
<input type="submit" name="create_redirection" value="Create one!">
</form>
<?
} else {
?>
<table class="table table-striped table-bordered table-condensed">
<tbody>
<tr>
<th>Created At</th>
<td><?
// Time stamps in the engine are stored in milliseconds, so need to convert into seconds to get epochs
echo date('r', $redirection->{'createdAt'}/1000)." (".$redirection->{'createdAt'}.")";
?></td>
</tr>
<tr>
<th>Last Updated At</th>
<td><? echo date('r', $redirection->{'updatedAt'}/1000)." (".$redirection->{'updatedAt'}.")"; ?></td>
</tr>
<tr>
<th>Short URL</th>
<td><?
$shortURL="http://".$redirection->{'shortDomain'}."/".$redirection->{'shortId'};
echo "<a href=\"$shortURL\">".$shortURL."</a>";
?></td>
</tr>
<tr>
<th>Target URL</th>
<td><?
$targetURL=$redirection->{'defaultRedirectUrl'};
echo "<a href=\"$targetURL\">".$targetURL."</a>";
?></td>
</tr>
<tr>
<th>Hits on Short URL</th>
<td><? echo $redirection->{'hits'}; ?></td>
</tr>
<tr>
<th>QR code</th>
<td><?
// Let's display the QR code image (display directly instead of downloading the file)
echo "<img src=\"http://".$redirection->{'shortDomain'}."/".$redirection->{'shortId'}.".png?w=150&h=150&caption=none\">";
?></td>
</tr>
</tbody>
</table>
<form method="post" action="">
<input type="submit" name="delete_redirection" value="Delete the redirection">
</form>
<?
}
?>
</p>
</div>
<h2>Thng Location</h2>
<div id="map" class="smallmap"></div>
<form onsubmit="return false;">
<input type="submit" onclick="plotLocations(); return false;" value="Plot all locations on map" onsubmit="plotLocations(); return false;">
</form>
<h3>All Locations</h3>
<table class="table table-striped table-bordered table-condensed">
<tbody>
<?
// Now we get all the locations of that thng
$results = getThngLocations($thng_id);
$locations=json_decode($results);
?>
<script>
// Create a js object of all the locations that is used by plotLocations() for putting them on the map
// Unlike other requests we're using JS to parse the locations (so the data is available in JS and can be plotted, etc.)
var locations = JSON.parse('<?php echo $results; ?>');
</script>
<?
echo "<tr><th></th><td>Timestamp</td><td>Long</td><td>Lat</td><td>customFields</td></tr>";
for ($i=0; $i<sizeof($locations); $i++) {
echo "<tr><th>".$i."</th><td> ".date('r', $locations[$i]->{'timestamp'}/1000)."</td><td>".$locations[$i]->{'longitude'}."</td><td>".$locations[$i]->{'latitude'}."</td><td>".json_encode($locations[$i]->{'customFields'})."</td></tr>";
}
?>
</tbody>
</table>
<?
} else { // if no thng_id as param ?>
<div>The thng you select does not belong to a product.</div>
<?
} // End of the if(thng_id)
?>
<h1>Product Details</h1>
<?
if ($_GET["product_id"]){ // If a product_id variable is given, then display that product
$product_id=$_GET["product_id"];
}
// The product view is displayed only if a product is available
if ($product_id){ // get product info
$results = getProduct($product_id);
$product=json_decode($results);
?>
<table class="table table-striped table-bordered table-condensed">
<tbody>
<tr>
<th>Full JSON</th>
<td><? echo $results; ?></td>
</tr>
<tr>
<th>Product ID</th>
<td><? echo $product_id; ?></td>
</tr>
<tr>
<th>Name</th>
<td><? echo $product->{'fn'}; ?> </td>
</tr>
<tr>
<th>Description</th>
<td><? echo $product->{'description'}; ?></td>
</tr>
<tr>
<th>Created At</th>
<td><?
echo date('r', $product->{'createdAt'}/1000)." (".$product->{'createdAt'}.")";
?></td>
</tr>
<tr>
<th>Updated At</th>
<td><? echo date('r', $product->{'updatedAt'}/1000)." (".$product->{'updatedAt'}.")"; ?></td>
</tr>
<tr>
<th>Categories</th>
<td><? echo "[\"".implode('","', $product->{'categories'})."\"]"; ?></td>
</tr>
<tr>
<th>Tags</th>
<td><? echo "[\"".implode('","', $product->{'tags'})."\"]"; ?></td>
</tr>
<tr>
<th>Photos</th>
<td><? echo "[\"".implode('","', $product->{'photos'})."\"]"; ?></td>
</tr>
<tr>
<th>Properties </th>
<td>
<table class="table table-bordered">
<?
// now we get all the properties of that Product
//$results = getProdProps($product_id);
//$prod_props=json_decode($results);
for ($i=0; $i<sizeof($prod_props); $i++) {
//$res = isset($arr[$i]);
echo "<tr><th>".$i."</th><td> ".$prod_props[$i]->{'key'}."</td><td>".$prod_props[$i]->{'value'}."</td></tr>";
}
?>
</table>
</td>
</tr>
<tr>
<th>Identifiers</th>
<td>
<table class="table table-bordered">
<?
$i=0;
foreach($product->{'identifiers'} as $key => $value) {
echo "<tr><th>".$i."</th><td> ".$key."</td><td>".$value."</td></tr>";
$i=$i+1;
}
?>
</table>
</td>
</tr>
</tbody>
</table>
<?} else { // If no product select ?>
<div>The thng you select does not belong to a product.</div>
<? } // End of the product display?>
</div>
</body>
</html>