editstops.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. function get_paddle_dependency($stopid)
  3. {
  4. global $sql;
  5. $safenum=mysqli_escape_string($sql,$stopid);
  6. $qry="SELECT id FROM paddles WHERE stopid='$safenum' GROUP BY id";
  7. $res=mysqli_query($sql,$qry);
  8. $accum=array();
  9. while($row=mysqli_fetch_assoc($res))
  10. {
  11. $accum[]=$row['id'];
  12. }
  13. return $accum;
  14. }
  15. function changed_stops()
  16. {
  17. $accum=array();
  18. $temp=load_stops();
  19. foreach($_SESSION['stops'] as $key => $record)
  20. {
  21. if(isset($temp[$key]))
  22. {
  23. if(array_diff($temp[$key],$record))
  24. {
  25. $accum[$key]=$record;
  26. $accum[$key]['comment'] = "Modified";
  27. }
  28. }
  29. else
  30. {
  31. $accum[$key]=$record;
  32. $accum[$key]['comment'] = "Added";
  33. }
  34. }
  35. foreach($temp as $key => $record)
  36. {
  37. if(!isset($_SESSION['stops'][$key]))
  38. {
  39. $accum[$key]=$record;
  40. $accum[$key]['comment'] = "Deleted";
  41. }
  42. }
  43. return $accum;
  44. }
  45. function load_stops()
  46. {
  47. global $sql;
  48. $qry="SELECT id,latitude,longitude,name FROM stops";
  49. $res=mysqli_query($sql, $qry);
  50. $accum=array();
  51. while($row=mysqli_fetch_assoc($res))
  52. {
  53. $accum[$row['id']]=$row;
  54. }
  55. return $accum;
  56. }
  57. function commit_stops()
  58. {
  59. global $sql;
  60. mysqli_query($sql, "BEGIN");
  61. mysqli_query($sql, "DELETE FROM stops");
  62. foreach($_SESSION['stops'] as $record)
  63. {
  64. $escrec=escape_array($record);
  65. mysqli_query($sql, "INSERT INTO stops SET id='${escrec['id']}', latitude='${escrec['latitude']}', longitude='${escrec['longitude']}', name='${escrec['name']}'");
  66. }
  67. mysqli_query($sql, "update paddles set stopid=-1 where (select count(id) from stops where stops.id = paddles.stopid) = 0");
  68. mysqli_query($sql, "COMMIT");
  69. $_SESSION['stops_dirty']=false;
  70. }
  71. function begin_stop_table($sortheaders)
  72. {
  73. if($sortheaders)
  74. {
  75. echo '<center><table width="80%" border="1"><tr><th><a href="index.php?sortby=id&type=num">Stop ID</a></th><th>Latitude</th><th>Longitude</th><th><a href="index.php?sortby=name&type=str">Stop Name</a></th><th>Action</th></tr>';
  76. }
  77. else
  78. {
  79. echo '<center><table width="80%" border="1"><tr><th>Stop ID</th><th>Latitude</th><th>Longitude</th><th>Stop Name</th><th>Action</th></tr>';
  80. }
  81. }
  82. function stop_compare($a, $b)
  83. {
  84. if(isset($_SESSION['stop_sortby']))
  85. {
  86. $sortby=$_SESSION['stop_sortby'];
  87. }
  88. else
  89. {
  90. $sortby='name';
  91. }
  92. $sorttype="str";
  93. if(isset($_SESSION['stop_sorttype']))
  94. {
  95. $sorttype=$_SESSION['stop_sorttype'];
  96. }
  97. if(!strcmp($sorttype,"str"))
  98. {
  99. return strcmp($a[$sortby],$b[$sortby]);
  100. }
  101. else if(!strcmp($sorttype,"num"))
  102. {
  103. if(floatval($a[$sortby]) == floatval($b[$sortby])) return 0;
  104. return ( floatval($a[$sortby]) < floatval($b[$sortby]) ) ? -1 : 1;
  105. }
  106. return 0;
  107. }
  108. function sort_stops()
  109. {
  110. uasort($_SESSION['stops'],'stop_compare');
  111. }
  112. //--------------------------------------------------------HANDLE LOADING STOPS IF THEY AREN'T
  113. if(!isset($_SESSION['stops']))
  114. {
  115. $_SESSION['stops']=load_stops();
  116. sort_stops();
  117. $_SESSION['stops_dirty']=false;
  118. }
  119. //--------------------------------------------------------HANDLE SORTING STOP LIST DISPLAY
  120. if(isset($_REQUEST['sortby']))
  121. {
  122. $_SESSION['stop_sortby']=$_REQUEST['sortby'];
  123. if(isset($_REQUEST['type']))
  124. {
  125. $_SESSION['stop_sorttype']=$_REQUEST['type'];
  126. }
  127. sort_stops();
  128. redirect('index.php');
  129. }
  130. //-----------------------------------------------------BELOW HANDLES ADDING A STOP------------------------
  131. if(isset($_REQUEST['addstop']))
  132. {
  133. $okay=true;
  134. $rec=array( 'id' => $_REQUEST['num'], 'latitude' => $_REQUEST['lat'], 'longitude' => $_REQUEST['lon'], 'name' => $_REQUEST['name']);
  135. if(!preg_match('/[\d]+/',$rec['id']))
  136. {
  137. $okay=false;
  138. echo "Driver Number must be entirely numeric.<br>";
  139. }
  140. foreach($_SESSION['stops'] as $record)
  141. {
  142. if($record['id'] == $_REQUEST['num'])
  143. {
  144. $okay=false;
  145. echo "Stop number must be unique (not already exist in the database).<br>";
  146. break;
  147. }
  148. }
  149. if( (!preg_match('/[+-]?[\d]+[\.]?[\d]+/',$rec['latitude'])) || (!preg_match('/[+-]?[\d]+[\.]?[\d]+/',$rec['longitude'])) )
  150. {
  151. $okay=false;
  152. echo "Latitude and Longitude must be valid GPS coordinates.<br>";
  153. }
  154. if($okay)
  155. {
  156. $_SESSION['stops'][$_REQUEST['num']] = $rec;
  157. $_SESSION['stops_dirty']=true;
  158. sort_stops();
  159. redirect('index.php');
  160. }
  161. else
  162. {
  163. begin_stop_table(false);
  164. echo "<tr><form><td><input type=\"text\" name=\"num\" value=\"${rec['id']}\"></td><td><input type=\"text\" name=\"lat\" value=\"${rec['latitude']}\"></td><td><input type=\"text\" name=\"lon\" value=\"${rec['longitude']}\"></td><td><input type=\"text\" size=\"32\" name=\"name\" value=\"${rec['name']}\"></td><td><input type=\"submit\" name=\"addstop\" value=\"Add a Stop\"></td></form></tr>";
  165. echo '</table></center>';
  166. exit;
  167. }
  168. }
  169. //-----------------------------------------------------BELOW HANDLES EDITING A STOP------------------------
  170. if(isset($_REQUEST['editstop']))
  171. {
  172. $okay=true;
  173. if(isset($_REQUEST['editsub']))
  174. {
  175. $rec=array( 'id' => $_REQUEST['id'], 'latitude' => $_REQUEST['lat'], 'longitude' => $_REQUEST['lon'], 'name' => $_REQUEST['name']);
  176. }
  177. else
  178. {
  179. $rec=$_SESSION['stops'][$_REQUEST['id']];
  180. $okay=false;
  181. }
  182. if( (!preg_match('/[+-]?[\d]+[\.]?[\d]+/',$rec['latitude'])) || (!preg_match('/[+-]?[\d]+[\.]?[\d]+/',$rec['longitude'])) )
  183. {
  184. $okay=false;
  185. echo "Latitude and Longitude must be valid GPS coordinates.<br>";
  186. }
  187. if($okay)
  188. {
  189. $_SESSION['stops'][$rec['id']] = $rec;
  190. if(count(changed_stops()) > 0)
  191. {
  192. $_SESSION['stops_dirty']=true;
  193. sort_stops();
  194. }
  195. redirect('index.php');
  196. }
  197. else
  198. {
  199. begin_stop_table(false);
  200. echo "<tr><form><td><input type=\"hidden\" name=\"editstop\"><input type=\"hidden\" name=\"id\" value=\"${rec['id']}\">${rec['id']}</td><td><input type=\"text\" name=\"lat\" value=\"${rec['latitude']}\"></td><td><input type=\"text\" name=\"lon\" value=\"${rec['longitude']}\"></td><td><input type=\"text\" size=\"32\" name=\"name\" value=\"${rec['name']}\"></td><td><input type=\"submit\" name=\"editsub\" value=\"Submit\"></td></form></tr>";
  201. echo '</table></center>';
  202. exit;
  203. }
  204. }
  205. //-----------------------------------------------------HANDLE THE COMMIT STOPS OPERATION-------------------
  206. if(isset($_REQUEST['commit_stops']))
  207. {
  208. $changes=changed_stops();
  209. begin_stop_table(false);
  210. $count=0;
  211. foreach($changes as $record)
  212. {
  213. $num=$record['id'];
  214. if( ($count % 2) == 1)
  215. {
  216. $bkgr="#E0FFE0";
  217. }
  218. else
  219. {
  220. $bkgr="#FFE0E0";
  221. }
  222. echo "<tr bgcolor=\"$bkgr\"><td>$num</td><td>${record['latitude']}</td><td>${record['longitude']}</td><td>${record['name']}</td><td>${record['comment']}</td></tr>";
  223. $count++;
  224. }
  225. echo "<tr><td colspan=\"5\"><center><big>Really apply these $count changes?</big></center></td></tr>";
  226. echo "<tr><td colspan=\"5\"><center><big><a href=\"index.php\">(Cancel)</a> <a href=\"index.php?commit_stopsconf\">(OK)</a></big></center></td></tr>";
  227. echo '</table></center>';
  228. exit;
  229. }
  230. else if(isset($_REQUEST['commit_stopsconf']))
  231. {
  232. commit_stops();
  233. unset($_SESSION['stops']);
  234. redirect('index.php');
  235. }
  236. //-----------------------------------------------------HANDLE THE REVERT STOPS OPERATION-------------------
  237. if(isset($_REQUEST['revert_stops']))
  238. {
  239. $changes=changed_stops();
  240. begin_stop_table(false);
  241. $count=0;
  242. foreach($changes as $record)
  243. {
  244. $num=$record['id'];
  245. if( ($count % 2) == 1)
  246. {
  247. $bkgr="#E0FFE0";
  248. }
  249. else
  250. {
  251. $bkgr="#FFE0E0";
  252. }
  253. echo "<tr bgcolor=\"$bkgr\"><td>$num</td><td>${record['latitude']}</td><td>${record['longitude']}</td><td>${record['name']}</td><td>${record['comment']}</td></tr>";
  254. $count++;
  255. }
  256. echo "<tr><td colspan=\"5\"><center><big>Really lose these $count changes?</big></center></td></tr>";
  257. echo "<tr><td colspan=\"5\"><center><big><a href=\"index.php\">(Cancel)</a> <a href=\"index.php?revert_stopsconf\">(OK)</a></big></center></td></tr>";
  258. echo '</table></center>';
  259. exit;
  260. }
  261. else if(isset($_REQUEST['revert_stopsconf']))
  262. {
  263. unset($_SESSION['stops']);
  264. redirect('index.php');
  265. }
  266. //-----------------------------------------------------HANDLE THE DELETE STOP OPERATION--------------------
  267. if(isset($_REQUEST['delstop']))
  268. {
  269. begin_stop_table(false);
  270. $bkgr="#E0FFE0";
  271. $record=$_SESSION['stops'][$_REQUEST['id']];
  272. echo '<tr><td colspan="5"><center><big>Really delete this stop?</big></center></td></tr>';
  273. echo "<tr bgcolor=\"$bkgr\"><td>${record['id']}</td><td>${record['latitude']}</td><td>${record['longitude']}</td><td>${record['name']}</td><td>${record['comment']}</td></tr>";
  274. $deps=get_paddle_dependency($_REQUEST['id']);
  275. $count=count($deps);
  276. if($count > 0)
  277. {
  278. echo '<tr><td colspan="5" style="color: red"><big>';
  279. echo "Deleting this stop will alter the following $count paddles: <b>";
  280. $first=true;
  281. foreach($deps as $val)
  282. {
  283. if($first)
  284. {
  285. echo "$val";
  286. $first=false;
  287. }
  288. else
  289. {
  290. echo ", $val";
  291. }
  292. }
  293. echo "</b></big></td></tr>";
  294. }
  295. echo "<tr><td colspan=\"5\"><center><big><a href=\"index.php\">(Cancel)</a> <a href=\"index.php?delstopconf&id=${_REQUEST['id']}\">(OK)</a></big></center></td></tr>";
  296. echo '</table></center>';
  297. exit;
  298. }
  299. else if(isset($_REQUEST['delstopconf']))
  300. {
  301. unset($_SESSION['stops'][$_REQUEST['id']]);
  302. $_SESSION['stops_dirty']=true;
  303. redirect('index.php');
  304. }
  305. //-----------------------------------------------------BELOW GENERATES THE MAIN SCREEN FOR STOP MANAGEMENT
  306. begin_stop_table(true);
  307. $count=0;
  308. foreach($_SESSION['stops'] as $record)
  309. {
  310. $num=$record['id'];
  311. $delurl="index.php?delstop&id=$num";
  312. $editurl="index.php?editstop&id=$num";
  313. if( ($count % 2) == 1)
  314. {
  315. $bkgr="#E0FFE0";
  316. }
  317. else
  318. {
  319. $bkgr="#FFE0E0";
  320. }
  321. echo "<tr bgcolor=\"$bkgr\"><td>$num</td><td>${record['latitude']}</td><td>${record['longitude']}</td><td>${record['name']}</td><td><a href=\"$delurl\">(Delete)</a> <a href=\"$editurl\">(Edit)</a></td></tr>";
  322. $count++;
  323. }
  324. echo '<tr><form><td><input type="text" name="num"></td><td><input type="text" name="lat"></td><td><input type="text" name="lon"></td><td><input type="text" size="32" name="name"></td><td><input type="submit" name="addstop" value="Add a Stop"></td></form></tr>';
  325. if($_SESSION['stops_dirty'])
  326. {
  327. echo '<tr bgcolor="#FFA0A0"><td colspan="4"><center><big><a href="index.php?commit_stops">(Commit Changes to Stops)</a> <a href="index.php?revert_stops">(Revert to Saved Stops)</a></big></center></td></tr>';
  328. }
  329. echo '</table></center>';
  330. ?>