$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 '
| Stop ID | Latitude | Longitude | Stop Name | Action |
';
}
else
{
echo '| Stop ID | Latitude | Longitude | Stop Name | Action |
';
}
}
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.
";
}
foreach($_SESSION['stops'] as $record)
{
if($record['id'] == $_REQUEST['num'])
{
$okay=false;
echo "Stop number must be unique (not already exist in the database).
";
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.
";
}
if($okay)
{
$_SESSION['stops'][$_REQUEST['num']] = $rec;
$_SESSION['stops_dirty']=true;
sort_stops();
redirect('index.php');
}
else
{
begin_stop_table(false);
echo "
";
echo '
';
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.
";
}
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 "
";
echo '
';
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 "| $num | ${record['latitude']} | ${record['longitude']} | ${record['name']} | ${record['comment']} |
";
$count++;
}
echo "| Really apply these $count changes? |
";
echo "| (Cancel) (OK) |
";
echo '';
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 "| $num | ${record['latitude']} | ${record['longitude']} | ${record['name']} | ${record['comment']} |
";
$count++;
}
echo "| Really lose these $count changes? |
";
echo "| (Cancel) (OK) |
";
echo '';
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 '| Really delete this stop? |
';
echo "| ${record['id']} | ${record['latitude']} | ${record['longitude']} | ${record['name']} | ${record['comment']} |
";
$deps=get_paddle_dependency($_REQUEST['id']);
$count=count($deps);
if($count > 0)
{
echo '| ';
echo "Deleting this stop will alter the following $count paddles: ";
$first=true;
foreach($deps as $val)
{
if($first)
{
echo "$val";
$first=false;
}
else
{
echo ", $val";
}
}
echo " |
";
}
echo "| (Cancel) (OK) |
";
echo '';
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 "| $num | ${record['latitude']} | ${record['longitude']} | ${record['name']} | (Delete) (Edit) |
";
$count++;
}
echo '
';
if($_SESSION['stops_dirty'])
{
echo '| (Commit Changes to Stops) (Revert to Saved Stops) |
';
}
echo '';
?>