| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- <?php
- function get_paddle_dependency($stopid)
- {
- global $sql;
- $safenum=mysqli_escape_string($sql,$stopid);
- $qry="SELECT id FROM paddles WHERE stopid='$safenum' GROUP BY id";
- $res=mysqli_query($sql,$qry);
-
- $accum=array();
-
- while($row=mysqli_fetch_assoc($res))
- {
- $accum[]=$row['id'];
- }
-
- return $accum;
- }
- function changed_stops()
- {
- $accum=array();
-
- $temp=load_stops();
-
- foreach($_SESSION['stops'] as $key => $record)
- {
- if(isset($temp[$key]))
- {
- if(array_diff($temp[$key],$record))
- {
- $accum[$key]=$record;
- $accum[$key]['comment'] = "Modified";
- }
- }
- else
- {
- $accum[$key]=$record;
- $accum[$key]['comment'] = "Added";
- }
- }
-
- foreach($temp as $key => $record)
- {
- if(!isset($_SESSION['stops'][$key]))
- {
- $accum[$key]=$record;
- $accum[$key]['comment'] = "Deleted";
- }
- }
-
- return $accum;
- }
- function load_stops()
- {
- global $sql;
- $qry="SELECT id,latitude,longitude,name FROM stops";
- $res=mysqli_query($sql, $qry);
-
- $accum=array();
-
- while($row=mysqli_fetch_assoc($res))
- {
- $accum[$row['id']]=$row;
- }
-
- return $accum;
- }
- function commit_stops()
- {
- global $sql;
- mysqli_query($sql, "BEGIN");
- mysqli_query($sql, "DELETE FROM stops");
-
- foreach($_SESSION['stops'] as $record)
- {
- $escrec=escape_array($record);
- mysqli_query($sql, "INSERT INTO stops SET id='${escrec['id']}', latitude='${escrec['latitude']}', longitude='${escrec['longitude']}', name='${escrec['name']}'");
- }
- mysqli_query($sql, "update paddles set stopid=-1 where (select count(id) from stops where stops.id = paddles.stopid) = 0");
-
- mysqli_query($sql, "COMMIT");
-
- $_SESSION['stops_dirty']=false;
- }
- function begin_stop_table($sortheaders)
- {
- if($sortheaders)
- {
- 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>';
- }
- else
- {
- 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>';
- }
- }
- function stop_compare($a, $b)
- {
- if(isset($_SESSION['stop_sortby']))
- {
- $sortby=$_SESSION['stop_sortby'];
- }
- else
- {
- $sortby='name';
- }
-
- $sorttype="str";
-
- if(isset($_SESSION['stop_sorttype']))
- {
- $sorttype=$_SESSION['stop_sorttype'];
- }
-
- if(!strcmp($sorttype,"str"))
- {
- return strcmp($a[$sortby],$b[$sortby]);
- }
- else if(!strcmp($sorttype,"num"))
- {
- if(floatval($a[$sortby]) == floatval($b[$sortby])) return 0;
- return ( floatval($a[$sortby]) < floatval($b[$sortby]) ) ? -1 : 1;
- }
-
- return 0;
- }
- function sort_stops()
- {
- uasort($_SESSION['stops'],'stop_compare');
- }
- //--------------------------------------------------------HANDLE LOADING STOPS IF THEY AREN'T
- if(!isset($_SESSION['stops']))
- {
- $_SESSION['stops']=load_stops();
- sort_stops();
- $_SESSION['stops_dirty']=false;
- }
- //--------------------------------------------------------HANDLE SORTING STOP LIST DISPLAY
- if(isset($_REQUEST['sortby']))
- {
- $_SESSION['stop_sortby']=$_REQUEST['sortby'];
-
- if(isset($_REQUEST['type']))
- {
- $_SESSION['stop_sorttype']=$_REQUEST['type'];
- }
- sort_stops();
- redirect('index.php');
- }
- //-----------------------------------------------------BELOW HANDLES ADDING A STOP------------------------
- if(isset($_REQUEST['addstop']))
- {
- $okay=true;
- $rec=array( 'id' => $_REQUEST['num'], 'latitude' => $_REQUEST['lat'], 'longitude' => $_REQUEST['lon'], 'name' => $_REQUEST['name']);
-
- if(!preg_match('/[\d]+/',$rec['id']))
- {
- $okay=false;
- echo "Driver Number must be entirely numeric.<br>";
- }
- foreach($_SESSION['stops'] as $record)
- {
- if($record['id'] == $_REQUEST['num'])
- {
- $okay=false;
- echo "Stop number must be unique (not already exist in the database).<br>";
- break;
- }
- }
- if( (!preg_match('/[+-]?[\d]+[\.]?[\d]+/',$rec['latitude'])) || (!preg_match('/[+-]?[\d]+[\.]?[\d]+/',$rec['longitude'])) )
- {
- $okay=false;
- echo "Latitude and Longitude must be valid GPS coordinates.<br>";
- }
- if($okay)
- {
- $_SESSION['stops'][$_REQUEST['num']] = $rec;
- $_SESSION['stops_dirty']=true;
- sort_stops();
- redirect('index.php');
- }
- else
- {
- begin_stop_table(false);
- 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>";
- echo '</table></center>';
- exit;
- }
-
- }
- //-----------------------------------------------------BELOW HANDLES EDITING A STOP------------------------
- if(isset($_REQUEST['editstop']))
- {
- $okay=true;
- if(isset($_REQUEST['editsub']))
- {
- $rec=array( 'id' => $_REQUEST['id'], 'latitude' => $_REQUEST['lat'], 'longitude' => $_REQUEST['lon'], 'name' => $_REQUEST['name']);
- }
- else
- {
- $rec=$_SESSION['stops'][$_REQUEST['id']];
- $okay=false;
- }
-
- if( (!preg_match('/[+-]?[\d]+[\.]?[\d]+/',$rec['latitude'])) || (!preg_match('/[+-]?[\d]+[\.]?[\d]+/',$rec['longitude'])) )
- {
- $okay=false;
- echo "Latitude and Longitude must be valid GPS coordinates.<br>";
- }
- if($okay)
- {
- $_SESSION['stops'][$rec['id']] = $rec;
-
- if(count(changed_stops()) > 0)
- {
- $_SESSION['stops_dirty']=true;
- sort_stops();
- }
-
- redirect('index.php');
- }
- else
- {
- begin_stop_table(false);
- 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>";
- echo '</table></center>';
- exit;
- }
-
- }
- //-----------------------------------------------------HANDLE THE COMMIT STOPS OPERATION-------------------
- if(isset($_REQUEST['commit_stops']))
- {
- $changes=changed_stops();
-
- begin_stop_table(false);
-
- $count=0;
- foreach($changes as $record)
- {
- $num=$record['id'];
-
- if( ($count % 2) == 1)
- {
- $bkgr="#E0FFE0";
- }
- else
- {
- $bkgr="#FFE0E0";
- }
- echo "<tr bgcolor=\"$bkgr\"><td>$num</td><td>${record['latitude']}</td><td>${record['longitude']}</td><td>${record['name']}</td><td>${record['comment']}</td></tr>";
- $count++;
- }
-
- echo "<tr><td colspan=\"5\"><center><big>Really apply these $count changes?</big></center></td></tr>";
- 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>";
-
-
- echo '</table></center>';
- exit;
- }
- else if(isset($_REQUEST['commit_stopsconf']))
- {
- commit_stops();
- unset($_SESSION['stops']);
- redirect('index.php');
- }
- //-----------------------------------------------------HANDLE THE REVERT STOPS OPERATION-------------------
- if(isset($_REQUEST['revert_stops']))
- {
- $changes=changed_stops();
-
- begin_stop_table(false);
-
- $count=0;
- foreach($changes as $record)
- {
- $num=$record['id'];
-
- if( ($count % 2) == 1)
- {
- $bkgr="#E0FFE0";
- }
- else
- {
- $bkgr="#FFE0E0";
- }
- echo "<tr bgcolor=\"$bkgr\"><td>$num</td><td>${record['latitude']}</td><td>${record['longitude']}</td><td>${record['name']}</td><td>${record['comment']}</td></tr>";
- $count++;
- }
-
- echo "<tr><td colspan=\"5\"><center><big>Really lose these $count changes?</big></center></td></tr>";
- 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>";
-
-
- echo '</table></center>';
- exit;
- }
- else if(isset($_REQUEST['revert_stopsconf']))
- {
- unset($_SESSION['stops']);
- redirect('index.php');
- }
- //-----------------------------------------------------HANDLE THE DELETE STOP OPERATION--------------------
- if(isset($_REQUEST['delstop']))
- {
- begin_stop_table(false);
-
- $bkgr="#E0FFE0";
- $record=$_SESSION['stops'][$_REQUEST['id']];
- echo '<tr><td colspan="5"><center><big>Really delete this stop?</big></center></td></tr>';
- 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>";
- $deps=get_paddle_dependency($_REQUEST['id']);
- $count=count($deps);
-
- if($count > 0)
- {
- echo '<tr><td colspan="5" style="color: red"><big>';
- echo "Deleting this stop will alter the following $count paddles: <b>";
- $first=true;
- foreach($deps as $val)
- {
- if($first)
- {
- echo "$val";
- $first=false;
- }
- else
- {
- echo ", $val";
- }
- }
- echo "</b></big></td></tr>";
- }
- 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>";
- echo '</table></center>';
- exit;
- }
- else if(isset($_REQUEST['delstopconf']))
- {
- unset($_SESSION['stops'][$_REQUEST['id']]);
- $_SESSION['stops_dirty']=true;
- redirect('index.php');
- }
- //-----------------------------------------------------BELOW GENERATES THE MAIN SCREEN FOR STOP MANAGEMENT
- begin_stop_table(true);
-
- $count=0;
- foreach($_SESSION['stops'] as $record)
- {
- $num=$record['id'];
- $delurl="index.php?delstop&id=$num";
- $editurl="index.php?editstop&id=$num";
- if( ($count % 2) == 1)
- {
- $bkgr="#E0FFE0";
- }
- else
- {
- $bkgr="#FFE0E0";
- }
-
- 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>";
- $count++;
- }
-
- 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>';
-
- if($_SESSION['stops_dirty'])
- {
- 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>';
- }
-
- echo '</table></center>';
- ?>
|