-
Notifications
You must be signed in to change notification settings - Fork 33
/
myroutes_edit.php
173 lines (141 loc) · 4.83 KB
/
myroutes_edit.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
<?php
use src\Models\ApplicationContainer;
use src\Models\OcConfig\OcConfig;
use src\Utils\Database\XDb;
use src\Utils\I18n\I18n;
require_once __DIR__ . '/lib/common.inc.php';
//user logged in?
$loggedUser = ApplicationContainer::GetAuthorizedUser();
if (! $loggedUser) {
$target = urlencode(tpl_get_current_page());
tpl_redirect('login.php?target=' . $target);
exit;
}
$googleMapKey = OcConfig::instance()->getGoogleMapKey();
if (isset($_REQUEST['routeid'])) {
$route_id = $_REQUEST['routeid'];
} else {
exit('Error: No routeID');
}
$remove = 0;
if (isset($_REQUEST['delete'])) {
$remove = 1;
}
if (isset($_POST['delete'])) {
$route_id = $_POST['routeid'];
$remove = 1;
}
if (isset($_POST['routeid'])) {
$route_id = $_POST['routeid'];
}
$view = tpl_getView();
$view->setTemplate('myroutes_edit');
$user_id = $loggedUser->getUserId();
if (isset($_POST['back'])) {
tpl_redirect('myroutes.php');
exit;
}
$route_rs = XDb::xSql(
'SELECT `user_id`,`name`, `description`, `radius`
FROM `routes`
WHERE `route_id`= ? AND `user_id`= ?',
$route_id,
$user_id
);
$record = XDb::xFetchArray($route_rs);
tpl_set_var('routes_name', $record['name']);
$rname = $_POST['name'] ?? '';
$rdesc = $_POST['desc'] ?? '';
$rradius = $_POST['radius'] ?? '';
$view->addLocalJs(
'https://maps.googleapis.com/maps/api/js?libraries=geometry&key=' . $googleMapKey
. '&language=' . I18n::getCurrentLang()
);
if ($record['user_id'] == $loggedUser->getUserId()) {
if ($remove == 1) {
//remove
XDb::xSql('DELETE FROM `routes` WHERE `route_id`= ? AND `user_id`= ? ', $route_id, $user_id);
XDb::xSql('DELETE FROM `route_points` WHERE `route_id`= ? ', $route_id);
tpl_redirect('myroutes.php');
exit;
}
}
// start submit
if (isset($_POST['submit']) && $remove == 0) {
XDb::xSql(
'UPDATE `routes` SET `name`= ? ,`description`= ?,`radius`= ?
WHERE `route_id`= ? ',
$rname,
$rdesc,
$rradius,
$route_id
);
if ($_FILES['file']['tmp_name'] != '') {
XDb::xSql('DELETE FROM `route_points` WHERE `route_id`= ? ', $route_id);
$upload_filename = $_FILES['file']['tmp_name'];
// Read file KML with route
exec('/usr/bin/gpsbabel -i kml -f ' . $upload_filename . ' -x interpolate,distance=0.25k -o kml -F ' . $upload_filename);
$xml = simplexml_load_file($upload_filename);
// get length route
foreach ($xml->Document->Folder as $f) {
foreach ($f->Folder as $folder) {
$dis = $folder->description;
$dis1 = explode(' ', trim($dis));
$len = (float) $dis1[27];
XDb::xSql(
'UPDATE `routes` SET `length`= ?
WHERE `route_id`= ? ',
$len,
$route_id
);
}
}
$coords = [];
foreach ($xml->Document->Folder as $xmlElement) {
foreach ($xmlElement->Folder as $folder) {
foreach ($folder->Placemark->LineString->coordinates as $coordinates) {
if ($coordinates) {
$coords_raw = explode(' ', trim($coordinates));
foreach ($coords_raw as $coords_raw_part) {
if ($coords_raw_part) {
$coords_raw_parts = explode(',', $coords_raw_part);
$coords[] = $coords_raw_parts[0];
$coords[] = $coords_raw_parts[1];
}
}
}
}
}
}
// end of read
//we get the point data in to an array called $points:
$points = [];
for ($i = 0; $i < count($coords) - 1; $i = $i + 2) {
$points[] = ['lon' => $coords[$i], 'lat' => $coords[$i + 1]];
if (($coords[$i] + 0 == 0) or ($coords[$i + 1] + 0 == 0)) {
break;
}
}
// add it to the route_points database:
$point_num = 0;
foreach ($points as $point) {
$point_num++;
$result = XDb::xSql(
'INSERT into route_points (route_id, point_nr, lat, lon)
VALUES ( ?, ?, ?, ?)',
$route_id,
$route_id,
$point['lat'],
$point['lon']
);
}
} //end update points
tpl_redirect('myroutes.php');
exit;
} //end submit
tpl_set_var('name', htmlspecialchars($record['name'], ENT_COMPAT));
tpl_set_var('desc', htmlspecialchars($record['description'], ENT_COMPAT));
tpl_set_var('radius', $record['radius']);
tpl_set_var('routeid', $route_id);
//make the template and send it out
$view->buildView();