<?php

$PageSecurity = 3;
include('includes/session.inc');
$title = _('Site Maintenance');
include('includes/header.inc');
echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/maintenance.png" title="' . _('Site Setup') . '" alt="">' . ' ' . _('Site Master') .'</p>';

if (isset($_GET['SelectedParam'])){
	$SelectedParam = $_GET['SelectedParam'];
} elseif(isset($_POST['SelectedParam'])){
	$SelectedParam = $_POST['SelectedParam'];
}

if (isset($Errors)) {
	unset($Errors);
}
$Errors = array();
$InputError = 0;

if (isset($_POST['submit'])) {

	//initialise no input errors assumed initially before we test
	$i=1;

	/* actions to take once the user has clicked the submit button
	ie the page has called itself with some user input */

	//first off validate inputs are sensible

	$sql="SELECT count(siteid)
			FROM sites WHERE siteid='".$_POST['siteid']."'";
	$result=DB_query($sql, $db);
	$myrow=DB_fetch_row($result);

	if ($myrow[0]!=0 and !isset($SelectedParam)) {
		$InputError = 1;
		prnMsg( _('That Site ID already exists in the database'),'error');
		$Errors[$i] = 'siteid';
		$i++;
	}

	$msg='';

	if (isset($SelectedParam) AND $InputError !=1) {

		/*SelectedParam could also exist if submit had not been clicked this code would not run in this case cos submit is false of course see the delete code below*/

		if (isset($_POST['siteid']) and isset($_POST['siteid']) ){
			$sql = "UPDATE sites SET
					siteid='" . $_POST['siteid'] . "',
					sitename='" . $_POST['sitename'] . "'
					WHERE siteid = $SelectedParam";
		}
		$msg = _('The Site status record has been updated');

	} else if ($InputError !=1) {

	/*Selected Param is null cos no item selected on first time round so must be adding a record must be submitting new entries in the new status code form */

		if (isset($_POST['siteid']) and $_POST['siteid']>0){

			$sql = 'INSERT INTO sites (
					sitename,
					contact_name,
					telephone,
					address1,
					address2,
					address3,
					project_name)
					VALUES (' . "'
					', '" .
					$_POST['sitename'] ."',
					'" .$_POST['ContactPerson'] ."',
					'" .$_POST['Telephone'] ."',
					'" .$_POST['Address1'] ."',
					'" .$_POST['Address2'] ."',
					'" .$_POST['Address3'] ."',
					'" .$_POST['ProjectName'] ."')";
		} else {
			$sql = 'INSERT INTO sites (
					siteid,
					sitename,
					contact_name,
					telephone,
					address1,
					address2,
					address3,
					project_name)
					VALUES (' . "'" .
					$_POST['siteid'] . "',
					'" .$_POST['sitename'] ."',
					'" .$_POST['ContactPerson'] ."',
					'" .$_POST['Telephone'] ."',
					'" .$_POST['Address1'] ."',
					'" .$_POST['Address2'] ."',
					'" .$_POST['Address3'] ."',
					'" .$_POST['ProjectName'] ."')";
		}

		$msg = _('A new Sites status record has been inserted');
		unset ($SelectedParam);
		unset ($_POST['siteid']);
		unset ($_POST['sitename']);
		unset ($_POST['ContactPerson']);
		unset ($_POST['Telephone']);
		unset ($_POST['Address1']);
		unset ($_POST['Address2']);
		unset ($_POST['Address3']);
		unset ($_POST['ProjectName']);
	}
	//run the SQL from either of the above possibilites
	$result = DB_query($sql,$db);
	if ($msg != '') {
		prnMsg($msg,'success');
	}
} elseif (isset($_GET['delete'])) {
//the link to delete a selected record was clicked instead of the submit button
$sql = 'DELETE FROM sites WHERE siteid = ' . $_GET['delete'] . ' LIMIT 1';
$result = DB_query($sql,$db);
$msg = _('Site deleted');
	//end if status code used in customer or supplier accounts
	unset ($_GET['delete']);
	unset ($SelectedParam);

}

if (!isset($SelectedParam)) {

/* It could still be the second time the page has been run and a record has been selected for modification - SelectedParam will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters
then none of the above are true and the list of status codes will be displayed with
links to delete or edit each. These will call the same page again and allow update/input
or deletion of the records*/

	$sql = 'SELECT * FROM sites';
	$result = DB_query($sql, $db);

	
	echo '<table border=1>';
	echo "<tr>
		<th>". _('Site ID') ."</th>
		<th>". _('Site Name') ."</th>
		<th>". _('Contact Name') ."</th>
		<th>". _('Telephone') ."</th>
		<th>". _('Address1') ."</th>
		<th>". _('Address2') ."</th>
		<th>". _('Address3') ."</th>
		<th>". _('Project Name') ."</th>";

	$k=0; //row colour counter
	while ($myrow=DB_fetch_row($result)) {

		if ($k==1){
			echo '<tr class="EvenTableRows">';
			$k=0;
		} else {
			echo '<tr class="OddTableRows">';
			$k=1;
		}

	printf("<td>%s</td>
		<td>%s</td>
		<td>%s</td>
		<td>%s</td>
		<td>%s</td>
		<td>%s</td>
		<td>%s</td>
		<td>%s</td>
		<td><a href=\"%s?SelectedParam=%s\">" . _('Edit') . "</a></td>
		<td><a href=\"%s?SelectedParam=%s&delete=%s\">". _('Delete') .'</a></td>
		</tr>',
		$myrow[0],
		$myrow[1],
		$myrow[2],
		$myrow[3],
		$myrow[4],
		$myrow[5],
		$myrow[6],
		$myrow[7],
		$_SERVER['PHP_SELF'],
		$myrow[0],
		$_SERVER['PHP_SELF'],
		$myrow[0],
		$myrow[0]);

	} //END WHILE LIST LOOP
	echo '</table>';

} //end of ifs and buts!

if (isset($SelectedParam)) {
	echo '<div class="centre"><br><a href="' . $_SERVER['PHP_SELF'] . '">' . _('Show Defined Sites Codes') . '</a><br></div>';
}

if (!isset($_GET['delete'])) {

	echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . '>';

	if (isset($SelectedParam) and ($InputError!=1)) {
		//editing an existing status code

		$sql = "SELECT siteid,
				sitename,
				contact_name,
				telephone,
				address1,
				address2,
				address3,
				project_name
			FROM sites
			WHERE siteid='$SelectedParam'";

		$result = DB_query($sql, $db);
		$myrow = DB_fetch_array($result);

		$_POST['siteid'] = $myrow['siteid'];
		$_POST['sitename'] = $myrow['sitename'];
		$_POST['ContactPerson'] = $myrow['contact_name'];
		$_POST['Telephone'] = $myrow['telephone'];
		$_POST['Address1'] = $myrow['address1'];
		$_POST['Address2'] = $myrow['address2'];
		$_POST['Address3'] = $myrow['address3'];
		$_POST['ProjectName']  = $myrow['project_name'];
		
		echo "<input type=hidden name='SelectedParam' VALUE='" . $SelectedParam . "'>";
		echo "<input type=hidden name='siteid' VALUE='" . $_POST['siteid'] . "'>";
		echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/maintenance.png" title="' . _('Geocode Setup') . '" alt="">'. _('Setup configuration for Geocoding of Customers and Suppliers') .'</p>';
		echo "<table><tr><td>". _('Site ID') .':</td><td>';
		echo $_POST['siteid'] . '</td></tr>';

	} else { //end of if $SelectedParam only do the else when a new record is being entered
		if (!isset($_POST['siteid'])) {
			$_POST['siteid'] = '';
		}
		echo '<table>';
//			<tr>
//				<td>'. _('Geocode Code') .":</td>
//				<td><input " . (in_array('geocodeid',$Errors) ? 'class="inputerror"' : '' ) .
//					" tabindex=1 type='Text' name='geocodeid' VALUE='". $_POST['geocodeid'] ."' size=3 maxlength=2></td>
//			</tr>";
	}

	if (!isset($_POST['sitename'])) {
		$_POST['sitename'] = '';
	}
	echo '<br><tr>
		<td>'. _('Site Name') .":</td>
		<td><input " . (in_array('siteid',$Errors) ? 'class="inputerror"' : '' ) .
		 " tabindex=2 type='text' name='sitename' VALUE='". $_POST['sitename'] ."' size=28 maxlength=300>
	</td></tr>";
	echo '<br><tr>
		<td>'. _('Contact Person') .":</td>
		<td><input " . (in_array('siteid',$Errors) ? 'class="inputerror"' : '' ) .
		 " tabindex=2 type='text' name='ContactPerson' VALUE='". $_POST['ContactPerson'] ."' size=28 maxlength=300>
	</td></tr>";
	echo '<br><tr>
		<td>'. _('Telephone') .":</td>
		<td><input " . (in_array('siteid',$Errors) ? 'class="inputerror"' : '' ) .
		 " tabindex=2 type='text' name='Telephone' VALUE='". $_POST['Telephone'] ."' size=28 maxlength=300>
	</td></tr>";
	echo '<br><tr>
		<td>'. _('Address1') .":</td>
		<td><input " . (in_array('siteid',$Errors) ? 'class="inputerror"' : '' ) .
		 " tabindex=2 type='text' name='Address1' VALUE='". $_POST['Address1'] ."' size=28 maxlength=300>
	</td></tr>";
	echo '<br><tr>
		<td>'. _('Address2') .":</td>
		<td><input " . (in_array('siteid',$Errors) ? 'class="inputerror"' : '' ) .
		 " tabindex=2 type='text' name='Address2' VALUE='". $_POST['Address2'] ."' size=28 maxlength=300>
	</td></tr>";
	echo '<br><tr>
		<td>'. _('Address3') .":</td>
		<td><input " . (in_array('siteid',$Errors) ? 'class="inputerror"' : '' ) .
		 " tabindex=2 type='text' name='Address3' VALUE='". $_POST['Address3'] ."' size=28 maxlength=300>
	</td></tr>";
	echo '<br><tr>
		<td>'. _('Project Name') .":</td>
		<td><input " . (in_array('siteid',$Errors) ? 'class="inputerror"' : '' ) .
		 " tabindex=2 type='text' name='ProjectName' VALUE='". $_POST['ProjectName'] ."' size=28 maxlength=300>
	</td></tr>
	
	</table>
	<div class='centre'><input tabindex=4 type='Submit' name='submit' value='" . _('Enter Information') . "'</div><br><br>
	</form>";



} //end if record deleted no point displaying form to add record
include('includes/footer.inc');
?>
