-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimportDatasets.php
313 lines (276 loc) · 14.1 KB
/
importDatasets.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
<?php
include_once 'Config.php';
include_once CLASSES . 'Util.class.php';
require CLASSES . 'init.php';
include_once 'Config.php';
include_once CLASSES . 'Database.class.php';
include_once CLASSES . 'App.class.php';
include_once CLASSES . 'City.class.php';
include_once CLASSES . 'Dataset.class.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'];
}
$userDatasetUploadLimitReached = false;
?>
<!DOCTYPE HTML>
<html>
<head>
<!-- Metatags -->
<title>Import Dataset</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS files -->
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile.structure-1.2.0.min.css" />
<link rel="stylesheet" href="css/my.css" />
<!-- Google Maps JavaScript API v3 -->
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<!-- jQuery Library -->
<script src="js/jquery-1.8.2.min.js"></script>
<!-- jQuery Mobile Library -->
<script src="js/jquery.mobile-1.2.0.min.js"></script>
<script>
function validateForm(form)
{
if ($('input[name=city]:checked').val() === "0")
{
getCoordinates(form);
return false;
}
else
{
return true;
}
}
function getCoordinates(form) {
if ($("#cityName").val() !== "" && $("#country").val() !== "") {
$.mobile.showPageLoadingMsg();
var cityName = "'" + $("#cityName").val() + "," + $("#country").val() + "'";
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': cityName}, function(results,
status) {
if (status === google.maps.GeocoderStatus.OK) {
$("#lat").val(results[0].geometry.location.lat());
$("#lon").val(results[0].geometry.location.lng());
form.submit();
} else {
alert("This city could not be found." + status);
$.mobile.hidePageLoadingMsg();
return false;
}
});
}
else if ($("#country").val() === "") {
alert("Please give a country");
}
else if ($("#cityName").val() === "") {
alert("Please give a city");
}
return false;
}
$(document).ready(function() {
$('input[type=radio][name=city]').change(function() {
if ($(this).attr('id') === "city0") {
$('#addNewCity').fadeIn();
}
else
$('#addNewCity').fadeOut();
});
});
</script>
<?php
Database::connect();
Database::begin();
$userDatasetsCount = Dataset::getDatasetsOfUser($userId);
?>
<?php
if ($userDatasetsCount + 1 > MAX_DATASETS_PER_USER) {
$userDatasetUploadLimitReached = true;
?>
<script>
$(document).ready(function() {
$('[type="submit"]').button('disable');
$("input[type='text']").textinput('disable');
$("input[type='url']").textinput('disable');
$("input[type='radio']").checkboxradio('disable');
});
</script>
<?php
}
?>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Import Dataset</h1>
</div>
<div data-role="content">
<div id="importNewDatasetWrapper">
<?php
// define variables and set to empty values
$datasetTypeErr = $datasetUrlErr = $datasetNameErr = $datasetFileErr = $cityErr = "";
$latitude = $longitude = $cityName = $newUid = $datasetName = $city = $datasetUrl = $datasetType = $datasetFile = "";
$error = false;
$creatingNewCity = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["city"]) && empty($_POST["newCity"])) {
$cityErr = "City is required";
$error = true;
} else if (!empty($_POST["city"]) && !empty($_POST["newCity"])) {
$cityErr = "Only one city must be selected";
$error = true;
} else if (!empty($_POST["city"])) {
$city = $_POST["city"];
} else {
$creatingNewCity = true;
$cityName = Util::clear_input($_POST["newCity"]);
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];
}
if (empty($_POST["datasetName"])) {
$datasetNameErr = "Dataset Name is required";
$error = true;
} else {
$datasetName = Util::clear_input($_POST["datasetName"]);
}
if (empty($_POST["datasetUrl"]) && empty($_FILES["userFile"]["name"])) {
$datasetUrlErr = "Dataset Url or Dataset file is required";
$error = true;
} else if (!empty($_POST["datasetUrl"]) && !empty($_FILES["userFile"]["name"])) {
$datasetUrlErr = "Only one of a Dataset Url OR a Dataset file must be given";
$error = true;
} else if (!empty($_POST["datasetUrl"])) {
$datasetUrl = Util::clear_input($_POST["datasetUrl"]);
} else {
$uid = trim(Util::getGUID(), '{}');
$datasetFile = $_FILES['userFile'];
if (strpos($datasetFile['name'], '.json') !== FALSE) {
$splittedDatasetFileName = explode(".json", $datasetFile['name']);
$datasetFileName = $splittedDatasetFileName[0] . '-citadel-' . $uid . '.json';
} else {
$datasetFileName = $datasetFile['name'] . '-citadel-' . $uid . '.json';
}
$target_Path = HTDOCS_ROOT . BASE_DIR . "data/";
$target_Path = $target_Path . basename($datasetFileName);
move_uploaded_file($datasetFile['tmp_name'], $target_Path);
$datasetUrl = SERVERNAME . BASE_DIR . "data/" . $datasetFileName;
}
if (empty($_POST["datasetType"])) {
$datasetTypeErr = "Dataset Type is required";
$error = true;
} else {
$datasetType = Util::clear_input($_POST["datasetType"]);
}
}
?>
<?php if ($_SERVER["REQUEST_METHOD"] != "POST" || $error) { ?>
<?php
if ($userDatasetUploadLimitReached) {
echo '<p>Hi <b>' . $username . '</b>! You have uploaded <b>' . $userDatasetsCount . '</b> dataset(s) so far. You are not allowed to upload any more datasets.</p>';
echo '<br/><a data-ajax="false" class="ui-link" href="appForm.php" >Back to the app creation form</a>';
} else {
?>
<form id="importNewDataset" data-ajax="false" onsubmit="return validateForm(this);"
method="post" action="importDatasets.php" enctype='multipart/form-data'>
<p>
<?php if (!$general->logged_in()) { ?>
<div class="warning">
<a target="_blank" href="<?php echo CITADELLOGINLINK; ?>" relation="external">You have to login before importing a dataset!</a>
</div>
<?php
} else {
echo 'Hi <b>' . $username . '</b>! You have uploaded <b>' .
$userDatasetsCount . '</b> dataset(s) so far. You are allowed to upload a maximum of <b>' .
MAX_DATASETS_PER_USER . '</b> datasets.';
echo '<a style="float:right" href="logout.php" data-ajax="false">log out</a>';
}
?>
</p>
<p><span class="error">* required field.</span></p>
<b>Dataset Name:</b> <span class="error">* <?php echo $datasetNameErr; ?></span><br/><br/>
<input type="text" id="dat" name="datasetName" required>
<br><br>
<div style="border:1px solid #CCC;padding:0 20px 20px;border-radius:10px;">
<h3>You must either enter a dataset url or upload a dataset file in
<a href="http://www.citadelonthemove.eu/en-us/citadelcommonschemaforpois.aspx" target="_blank"
rel="external">Citadel Json format</a></h3>
<b>Dataset Url:</b> <span class="error">* <?php echo $datasetUrlErr; ?></span><br/>
<input type="url" name="datasetUrl" ><br />
<b>Dataset File:</b><span class="error">* <?php echo $datasetFileErr; ?></span><br/>
<input type='file' name='userFile'>
<a href="http://www.rbox.tv/citadel/converter/php/" target="_blank" rel="external">
Click here to easily convert your dataset in the Citadel Json format!</a>
</div>
<br><br>
<b>Dataset Type:</b> (e.g. Pois, Parking, Events, etc) <span class="error">* <?php echo $datasetTypeErr; ?></span><br/><br/>
<input type="text" name="datasetType" required>
<br><br>
<!--div style="border:1px solid #CCC;padding:0 20px 20px;border-radius:10px;">
<h3>You must either select a city or add a new one </h3-->
<b>Select a city:</b> <span class="error">* <?php echo $cityErr; ?></span><br/><br/>
<div id="datasetsCheckboxes" data-role=controlgroup>
<?php
$sql = 'SELECT * FROM cities ORDER BY name';
foreach (Database::$dbh->query($sql) as $row) {
echo '<input type="radio" name="city" id="city' . $row['id'] . '" value="' . $row['id'] . '">
<label for="city' . $row['id'] . '">' . $row['name'] . '</label>';
}
?>
<input type="radio" name="city" id="city0" value="0">
<label for="city0">Add a new city</label>
</div>
<br><br>
<div id="addNewCity" style="display:none;">
<b>Country:</b><span class="error">*</span>
<input type="text" id="country" name="country" value="">
<b>City:</b><span class="error">*</span>
<input type="text" id="cityName" name="newCity" value="">
<input type="text" style="display:none;" id="lat" name="latitude" value="">
<input type="text" style="display:none;" id="lon" name="longitude" value="">
</div>
<!--/div-->
<?php if (!$general->logged_in()) { ?>
<a target="_blank" href="<?php echo CITADELLOGINLINK; ?>" relation="external">You have to login before importing a dataset!</a>
<?php } else {
?>
<input type="submit" name="import" value="Import">
<?php } ?>
<br><br>
<?php echo '<a style="float:right" href="appForm.php" >Back to the app creation form</a>'; ?>
</form>
<?php
} // end if $userDatasetUploadLimitReached
} // end if ($_SERVER["REQUEST_METHOD"] != "POST" || $error)
?>
<?php
if (isset($_POST['import']) || $_SERVER["REQUEST_METHOD"] == "POST") {
if (!$error && ($userDatasetsCount < MAX_DATASETS_PER_USER)) {
if ($creatingNewCity) {
$city = City::saveNewCity($cityName, $latitude, $longitude);
}
Dataset::saveNewDataset($datasetName, $datasetUrl, $datasetType, $city, $userId, $latitude, $longitude);
Database::commit();
echo '<div class="success">Your dataset was imported successfully!';
echo '<br><br>';
echo ' <a href="appForm.php" rel="external" >Back to the app creation form</a></div>';
}
}
Database::disconnect();
?>
</div>
</div>
</div>
</body>
</html>