forked from CitadelOnTheMove/agt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appForm.php
506 lines (408 loc) · 24.7 KB
/
appForm.php
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
<?php
include_once 'Config.php';
include_once CLASSES . 'resizeImage.php';
require CLASSES . 'init.php';
//$general->logged_out_protect();
/* Read user info from session in case login comes from
* citadel website
*/
if (isset($_SESSION['username'])) {
$userId = $_SESSION['id'];
$username = $_SESSION['username'];
} else { // use built-in login functionality
$user = $users->userdata($_SESSION['id']);
$userId = $user['id'];
$username = $user['username'];
}
?>
<!DOCTYPE HTML>
<html>
<head>
<!--------------- Metatags ------------------->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--------------- CSS files ------------------->
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.css" />
<link rel="stylesheet" href="css/my.css" />
<link rel="stylesheet" href="css/spectrum.css" />
<!-- jQuery Library -->
<script src="js/jquery-1.8.2.min.js"></script>
<!-- jQuery Mobile Library -->
<script src="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.js"></script>
<script src="js/spectrum.js"></script>
<script lang="text/javascript">
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://www.citadelonthemove.eu/DesktopModules/DatasetLibrary/API/Service/GetCities",
cache: false,
error: getCitiesFailure,
dataType: "jsonp"
});
$('.ui-checkbox a').bind("click", function(event, data) {
event.stopPropagation();
window.open($this.attr('href'), $this.attr('target'));
});
$("#createNewAppForm").submit(function(event) {
getSelectedDatasetIds();
});
$("#citySelection").bind("change", function() {
var selectedCityName = $(this).find(":selected").text();
if ($(this).find(":selected").val() !== "-1")
{
$.ajax({
type: "GET",
url: "http://www.citadelonthemove.eu/DesktopModules/DatasetLibrary/API/Service/GetCityCategoriesAndDatasets?city=" + encodeURIComponent(selectedCityName),
cache: false,
error: getCitiesFailure,
dataType: "jsonp"
});
}
});
// Swipe to remove list item
$(document).on("swipeleft swiperight", "#datasetsListView li", function(event) {
var listitem = $(this),
// These are the classnames used for the CSS transition
dir = event.type === "swipeleft" ? "left" : "right",
// Check if the browser supports the transform (3D) CSS transition
transition = $.support.cssTransform3d ? dir : false;
confirmAndDelete(listitem, transition);
});
// If it's not a touch device...
if (!$.mobile.support.touch) {
// Remove the class that is used to hide the delete button on touch devices
$("#datasetsListView").removeClass("touch");
// Click delete split-button to remove list item
$(".delete").on("click", function() {
var listitem = $(this).parent("li");
confirmAndDelete(listitem);
});
}
}); // document.ready
function confirmAndDelete(listitem, transition) {
// Highlight the list item that will be removed
listitem.children(".ui-btn").addClass("ui-btn-active");
// Inject topic in confirmation popup after removing any previous injected topics
$("#confirm .topic").remove();
listitem.find(".topic").clone().insertAfter("#question");
// Show the confirmation popup
$("#confirm").popup("open");
// Proceed when the user confirms
$("#confirm #yes").unbind("click");
$("#confirm #yes").on("click", function() {
// Remove with a transition
datasetsCounter--;
$("#datasetsCounter").html("(" + datasetsCounter + ")");
if (transition) {
listitem
// Add the class for the transition direction
.addClass(transition)
// When the transition is done...
.on("webkitTransitionEnd transitionend otransitionend", function() {
// ...the list item will be removed
listitem.remove();
// ...the list will be refreshed and the temporary class for border styling removed
$("#datasetsListView").listview("refresh").find(".border-bottom").removeClass("border-bottom");
})
// During the transition the previous button gets bottom border
.prev("li").children("a").addClass("border-bottom")
// Remove the highlight
.end().end().children(".ui-btn").removeClass("ui-btn-active");
}
// If it's not a touch device or the CSS transition isn't supported just remove the list item and refresh the list
else {
listitem.remove();
$("#datasetsListView").listview("refresh");
}
});
// Remove active state and unbind when the cancel button is clicked
$("#confirm #cancel").on("click", function() {
listitem.removeClass("ui-btn-active");
$("#confirm #yes").off();
});
}
var cities_html = "";
var categories_html = "";
var datasets_html = "";
var datasetsCounter = 0;
var categoryName = "";
function getCitiesJsonPSuccess(data)
{
cities_html += "<option value='-1'>- Select City/Region -</option>";
$.each(data.cities, function(i, city) {
cities_html += "<option lat='" + city.lat + "' lon='" + city.lon + "' value='" + i + "'>" + city.name + "</option>";
});
$('#citySelection').html(cities_html);
var myselect = $("select#citySelection");
if (myselect[0]) {
myselect[0].selectedIndex = 0;
}
myselect.selectmenu("refresh");
}
function getCitiesFailure(data, status)
{
}
function getCategoriesAndDatasetsJsonPSuccess(data)
{
categories_html = "";
$.each(data.categories, function(i, category) {
categoryName = i;
categories_html += "<div id='" + i + "' data-role='collapsible' data-inset='false'><h3>" + i + "</h3><ul data-role='listview'>";
datasets_html = "";
$.each(category, function(j, dataset) {
datasets_html += "<li class='selectedDataset'><a class='ui-btn ui-btn-icon-right ui-icon-plus' href='#'>" + dataset.title + "</a><span class='datasetId' style='display:none;'>" + dataset.id + "</span><span class='datasetUrl' style='display:none;'>" + dataset.url + "</span><span style='display:none;' class='categoryName'>" + categoryName + "</span></li>";
//datasets_html += "<li class='selectedDataset'><a class='ui-btn ui-btn-icon-right ui-icon-plus' href='#'>" + dataset.title + "</a><span class='datasetId' style='display:none;'>" + dataset.id + "</span><span class='datasetUrl' style='display:none;'>" + dataset.url + "</span></li>";
});
datasets_html = datasets_html + "</ul></div>";
categories_html += datasets_html;
});
$('#datasetsByCategory').html(categories_html);
$("ul").listview();
$('#datasetsByCategory').collapsibleset('refresh');
$(".selectedDataset").on("click", function() {
addDatasetToCart($("a", this).html(), $("span.datasetId", this).html(), $("span.datasetUrl", this).html(), $("span.categoryName", this).html(), $("#citySelection").find(":selected").text(), $("#citySelection").find(":selected").attr('lat'), $("#citySelection").find(":selected").attr('lon'));
// addDatasetToCart($("a", this).html(), $("span.datasetId", this).html(), $("span.datasetUrl", this).html(), $("#citySelection").find(":selected").text(), $("#citySelection").find(":selected").attr('lat'), $("#citySelection").find(":selected").attr('lon'));
});
}
function addDatasetToCart(datasetName, datasetId, datasetUrl, categoryName, cityName, cityLat, cityLon)
{
// see if element(s) exists that matches by checking length
var exists = $('#datasetsListView li:contains(' + datasetName + ')').length;
if (!exists) {
datasetsCounter++;
var datasetTitle = '<li><span style="display:none;" class="datasetId">' + datasetId + '</span><span style="display:none;" class="cityName">' + cityName + '</span><span style="display:none;" class="cityLat">' + cityLat + '</span><span style="display:none;" class="cityLon">' + cityLat + '</span><span style="display:none;" class="categoryName">' + categoryName + '</span><a><div class="topic">' +
'<h2>' + datasetName + ' , <i><span style="font-size:12px">(' + datasetUrl + ')</span></i></h2></div></a><a href="#" class="delete">Delete</a>' +
'</a></li>';
$('#datasetsListView').append(datasetTitle).listview("refresh");
$("#datasetsCounter").html("(" + datasetsCounter + ")");
$(".delete").unbind("click");
$(".delete").on("click", function() {
var listitem = $(this).parent("li");
confirmAndDelete(listitem);
});
}
}
function getSelectedDatasetIds()
{
var datasetIds = [];
var cityNames = [];
var cityLats = [];
var cityLons = [];
var cities = [];
var categoryNames = [];
$('#datasetsListView li').each(function() {
datasetIds.push($("span.datasetId", this).html());
if ($.inArray($("span.categoryName", this).html(), categoryNames) === -1)
{
categoryNames.push($("span.categoryName", this).html());
}
if ($.inArray($("span.cityName", this).html(), cityNames) === -1)
{
cityNames.push($("span.cityName", this).html());
cityLats.push($("span.cityLat", this).html());
cityLons.push($("span.cityLon", this).html());
}
});
var i;
for (i = 0; i < cityNames.length; ++i) {
var city = new City();
city.name = cityNames[i];
city.lat = cityLats[i];
city.lon = cityLons[i];
cities.push(city);
}
$('#datasetIds')
.attr('name', "datasetIds")
.attr('value', datasetIds);
$('#categoryNames')
.attr('name', "categoryNames")
.attr('value', categoryNames);
$('#cities')
.attr('name', "cities")
.attr('value', JSON.stringify(cities));
}
function City() {
this.name;
this.lat;
this.lon;
}
</script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Create your app</h1>
<div class="beta"></div>
</div>
<div data-role="content">
<div id="createNewAppWrapper">
<?php
include_once 'Config.php';
include_once CLASSES . 'Database.class.php';
include_once CLASSES . 'App.class.php';
Database::connect();
// define variables and set to empty values
$darkColorErr = $colorErr = $nameErr = $descriptionErr = $cityIdsErr = $datasetIdsErr = "";
$error = false;
// Check all required fields
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["datasetIds"])) {
$datasetIdsErr = "Dataset is required";
$error = true;
}
if (empty($_POST["name"])) {
$nameErr = "Application Name is required";
$error = true;
}
if (empty($_POST["description"])) {
$descriptionErr = "Application Description is required";
$error = true;
}
if (empty($_POST["color"])) {
$colorErr = "Color is required";
$error = true;
}
if (empty($_POST["darkColor"])) {
$darkColorErr = "Dark Color is required";
$error = true;
}
if (!$error) {
$image = null;
if ((isset($_POST["name"])) && (isset($_POST["description"])) &&
(isset($_POST["datasetIds"])) && (isset($_POST["color"])) &&
(isset($_POST["darkColor"]))) {
if (isset($_FILES["image"])) {
$tmpFile = $_FILES['image']['tmp_name'];
$storeImage = false;
if (!isset($tmpFile)) {
} else {
$fileName = $_FILES['image']['name'];
$fileSize = $_FILES['image']['size'];
$filePath = MY_PATH . $fileName;
//Check filesize
if ($fileSize < 100000) {
move_uploaded_file($tmpFile, $filePath);
/* Determine filetype (jpeg, png, gif) */
switch ($_FILES['image']['type']) {
case 'image/jpeg': $ext = "jpg";
$gd = imagecreatefromjpeg($filePath);
$filePath = str_replace(".jpg", ".png", $filePath);
$storeImage = true;
break;
case 'image/png': $ext = "png";
$gd = imagecreatefrompng($filePath);
$storeImage = true;
break;
case 'image/gif': $ext = "gif";
$gd = imagecreatefromgif($filePath);
$filePath = str_replace(".gif", ".png", $filePath);
$storeImage = true;
break;
}
/* Resize image and store it like png at the server */
if ($storeImage) {
$resized = resizePreservingAspectRatio($gd, 60, 60);
imagepng($resized, $filePath);
$image = base64_encode(file_get_contents($filePath));
}
}
}
}
$newApp = App::createFromArray($_POST, $userId, $image);
if ($newApp->save()) {
Database::commit();
echo '<div class="success">Your application was created successfully!';
echo '<br><br>';
echo ' <a href="index.php?uid=' . $newApp->uid . '" target="_blank" rel="external">See my app!</a>';
echo ' <a style="float:right" href="appForm.php" rel="external">Create a new app</a></div>';
} else {
Database::rollback();
echo '<div class="failure">Your application was not created.';
echo '<br><br>';
echo ' <a style="float:right" href="appForm.php" >Create a new app</a></div>';
}
Database::disconnect();
}
}
}
?>
<?php if ($_SERVER["REQUEST_METHOD"] != "POST" || $error) { ?>
<form id="createNewAppForm" data-ajax="false" enctype="multipart/form-data" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<p>
<?php if (!$general->logged_in()) { ?>
<div class="warning">
<a target="_blank" href="<?php echo CITADELLOGINLINK; ?>" relation="external">You have to login before creating an app!</a>
</div>
<?php
} else {
echo 'Hi <b>' . $username . '</b>! Use this form to create your own app.'; }
?>
</p>
<p><span class="error">* required field.</span></p>
<legend><b>Select City/Region:</b> <span class="error">* <?php echo $cityIdsErr; ?></span></legend>
<div class="ui-field-contain" id="citiesSelectMenu" >
<select name="selectCity" id="citySelection">
</select>
</div>
<br>
<div data-role="collapsible" data-collapsed-icon="carat-d" data-expanded-icon="carat-u" data-inset="false">
<legend><b>Selected datasets </b><span id="datasetsCounter">(0)</span></legend>
<ul id="datasetsListView" class="touch" data-role="listview" data-split-icon="delete">
</ul>
</div>
<br>
<p><span class="error">* required field.</span></p>
<legend><b>Select datasets grouped by category:</b> <span class="error">* <?php echo $datasetIdsErr; ?></span></legend><br/>
<?php
echo '<i>Select a Category to see the available datasets.</i>'
?>
<br><br>
<div id="datasetsByCategory" data-role="collapsibleset" data-collapsed-icon="carat-d" data-expanded-icon="carat-u" data-inset="false">
</div>
<br><br>
<div id="confirm" class="ui-content" data-role="popup" data-theme="a">
<p id="question">Are you sure you want to remove this dataset?</p>
<div class="ui-grid-a">
<div class="ui-block-a">
<a id="yes" class="ui-btn ui-corner-all ui-mini ui-btn-a" data-rel="back">Yes</a>
</div>
<div class="ui-block-b">
<a id="cancel" class="ui-btn ui-corner-all ui-mini ui-btn-a" data-rel="back">Cancel</a>
</div>
</div>
</div><!-- /popup -->
<legend><b>Select the basic color of your app:</b><span class="error">* <?php echo $colorErr; ?></span></legend><br/>
<input type="color" class="full" name="color">
<br><br>
<legend><b>Select the secondary color (active/hovered buttons):</b><span class="error">* <?php echo $darkColorErr; ?></span></legend><br/>
<input type="color" class="full" name="darkColor">
<br><br>
<legend><b>Application Name:</b> <span class="error">* <?php echo $nameErr; ?></span></legend><br/>
<input type="text" name="name" required>
<br/><br/>
<legend><b>Application Description:(max 90 chars)</b> <span class="error">* <?php echo $descriptionErr; ?></span></legend><br/>
<textarea rows="4" cols="50" maxlength="90" name="description" required ></textarea>
<br/><br/>
<legend><b>Application Image:</b></legend><br/>
<legend>(Supported image formats: gif, jpeg, png. Maximum size: 1MB.)</legend><br/>
<input type="file" name="image"/>
<br/><br/>
<input id="datasetIds" type="hidden" />
<input id="categoryNames" type="hidden" />
<input id="cities" type="hidden" />
<?php if (!$general->logged_in()) { ?>
<a target="_blank" href="<?php echo CITADELLOGINLINK; ?>" relation="external">You have to login before creating an app!</a>
<?php } else {
?>
<input type="submit" name="submit" value="Create the app" onclick="window.scrollTo(0, 0);">
<?php } ?>
</form>
<?php
} // end if
Database::disconnect();
?>
</div>
</div>
</div>
</body>
</html>