Location: PHPKode > projects > eCanteen > ecanteen/maintain/majorcustomer.php
<?
//Copyright David Byrne 2007

//This file is part of eCanteen.

//    eCanteen is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    eCanteen is distributed in the hope that it will be useful
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with eCanteen.  If not, see <http://www.gnu.org/licenses/>.

session_start();
if ($_SESSION['userlevel']>=3)
{
	include("../include/constants.php");   mysql_free_result();
		$filter = "";
  $filterfield = "";
  $wholeonly = "";
  $order = "";
  $ordtype = "";
	 ?>
<html>
<head>
<title>ecanteen -- majorcustomer</title>
<meta name="generator" content="text/html">
<LINK REL="stylesheet" TYPE="text/css" HREF="maintain.css">
</head>
<body>
<table class="bd" width="50%"><tr><td class="hr"><h2>Major Customers</h2></td><td class="bd" width="50%"><button id="Return2Cal" onclick="window.location.href='../index.php?site=<?$_SESSION['SiteID']?>>';return true;" style="width: 190px">Return to Main Menu</button></td>
</tr></table>
<?
  $conn = connect();
  $showrecs = 20;
  $pagerange = 10;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];
	
	if (isset($_POST["order"])) $order = @$_POST["order"];
  if (isset($_POST["type"])) $ordtype = @$_POST["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];
  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];

  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "edit":
      editrec($recid);
      break;
    default:
      select();
      break;
  }

  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

  mysql_close($conn);
?>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>

<?}?>

<? function select()
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }

  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
  $fields = array(
    "CustomerID" => "ID",
    "UserName_L2" => "UserName",
    "FirstName" => "First Name",
    "LastName" => "Last Name",
    "MobileNumber" => "Mobile Number",
    "EmailAddress" => "Email Address",
    "UserLevel_L3" => "UserLevel",
		"Password" => "Password",
    "Credit_L2" => "Credit",
    "Active_L2" => "Active");
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">

<tr><td>Records shown <? echo $startrec + 1 ?> - <? echo $reccount ?> of <? echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="majorcustomer.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>Custom Filter</b>&nbsp;</td>
<td><input type="text" name="filter" value="<? echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">All Fields</option>
<?
  reset($fields);
  foreach($fields as $val => $caption) {
    if ($val == $filterfield) {$selstr = " selected"; } else {$selstr = ""; }
?>
<option value="<? echo $val ?>"<? echo $selstr ?>><? echo htmlspecialchars($caption) ?></option>
<? } ?>
</select></td>
<td><input type="checkbox" name="wholeonly"<? echo $checkstr ?>>Whole words only</td>
</td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="action" value="Apply Filter"></td>
<td><a href="majorcustomer.php?a=reset">Reset Filter</a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<? showpagenav($page,$pagecount,$order,$ordtype); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<?
  reset($fields);
  foreach($fields as $val => $caption) {
?>
<td class="hr"><a class="hr" href="majorcustomer.php?order=<? echo $val ?>&type=<? echo $ordtypestr ?>"><? echo htmlspecialchars($caption) ?></a></td>
<? } ?>
<td class="hr">&nbsp;</td>
</tr>
<?
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<?
  reset($fields);
  foreach($fields as $val => $caption) {
?>
<td class="<? echo $style ?>"><? 
if ($caption=="Active")
			{
			if ($row[$val]==1)
			{
			print "<font color=green>Yes</font>";
			}else{
			print "<font color=red>No</font>";
			}
			}else{
			  echo htmlspecialchars($row[$val]);
			} 


 ?></td>
<? } ?>
<td class="<? echo $style ?>"><a href="majorcustomer.php?a=edit&recid=<? echo $i ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>">Edit</a></td>
</tr>
<?
  }
  mysql_free_result($res);
?>
</table>
<br>
<? showpagenav($page, $pagecount,$order,$ordtype); ?>
<? } ?>

<? function showroweditor($row)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><? echo htmlspecialchars("Customer ID")."&nbsp;" ?></td>
<td class="dr"><? echo str_replace('"', '&quot;', trim($row["CustomerID"])) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("User Name")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="UserName_L2" maxlength="20" value="<? echo str_replace('"', '&quot;', trim($row["UserName_L2"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("First Name")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="FirstName" maxlength="30" value="<? echo str_replace('"', '&quot;', trim($row["FirstName"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Last Name")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="LastName" maxlength="30" value="<? echo str_replace('"', '&quot;', trim($row["LastName"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Mobile Number")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="MobileNumber" maxlength="10" value="<? echo str_replace('"', '&quot;', trim($row["MobileNumber"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Email Address")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="EmailAddress" maxlength="50" value="<? echo str_replace('"', '&quot;', trim($row["EmailAddress"])) ?>"></td>
</tr>

<tr>
<td class="hr"><? echo htmlspecialchars("UserLevel")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="UserLevel_L3" value="<? echo str_replace('"', '&quot;', trim($row["UserLevel_L3"])) ?>"></td>
</tr>

<tr>
<td class="hr"><? echo htmlspecialchars("Password")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="Password" value="<? echo str_replace('"', '&quot;', trim($row["Password"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Credit")."&nbsp;" ?></td>
<td class="dr"><? echo str_replace('"', '&quot;', trim($row["Credit_L2"])) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Active")."&nbsp;" ?></td>
<td class="dr"><?

if ($row["Active_L2"]==True)
{
print "<input type=\"checkbox\" name=\"Active_L2\" checked />";
}else{
print "<input type=\"checkbox\" name=\"Active_L2\" />";
}
?></td></tr>

</table>

<input type="hidden" name="order" value="<? echo $_GET["order"] ?>">
<input type="hidden" name="type" value="<? echo $_GET["type"] ?>">

<? } ?>

<? function showpagenav($page, $pagecount,$order, $ordtype)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="majorcustomer.php?a=add&order=<? echo $order ?>&type=<? echo $ordtype ?>">Add Record</a>&nbsp;</td>
<? if ($page > 1) { ?>
<td><a href="majorcustomer.php?page=<? echo $page - 1 ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>">&lt;&lt;&nbsp;Prev</a>&nbsp;</td>
<? } ?>
<?
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><? echo $j ?></b></td>
<? } else { ?>
<td><a href="majorcustomer.php?page=<? echo $j ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>"><? echo $j ?></a></td>
<? } } } else { ?>
<td><a href="majorcustomer.php?page=<? echo $startpage ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>"><? echo $startpage ."..." .$count ?></a></td>
<? } } } ?>
<? if ($page < $pagecount) { ?>
<td>&nbsp;<a href="majorcustomer.php?page=<? echo $page + 1 ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>">Next&nbsp;&gt;&gt;</a>&nbsp;</td>
<? } ?>
</tr>
</table>
<? } ?>

<? function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="majorcustomer.php?order=<? echo $_GET["order"] ?>&type=<? echo $_GET["type"] ?>">Index Page</a></td>
<? if ($recid > 0) { ?>
<td><a href="majorcustomer.php?a=<? echo $a ?>&recid=<? echo $recid - 1 ?>&order=<? echo $_GET["order"] ?>&type=<? echo $_GET["type"] ?>">Prior Record</a></td>
<? } if ($recid < $count) { ?>
<td><a href="majorcustomer.php?a=<? echo $a ?>&recid=<? echo $recid + 1 ?>&order=<? echo $_GET["order"] ?>&type=<? echo $_GET["type"] ?>">Next Record</a></td>
<? } ?>
</tr>
</table>
<hr size="1" noshade>
<? } ?>

<? function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="majorcustomer.php">Index Page</a></td>
</tr>
</table>
<hr size="1" noshade>
<form action="majorcustomer.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?
$row = array(
  "CustomerID" => "",
  "UserName_L2" => "",
  "FirstName" => "",
  "LastName" => "",
  "MobileNumber" => "",
  "EmailAddress" => "",
  "UserLevel_L3" => "",
  "Credit_L2" => "",
  "Active_L2" => "",
  "~SiteID\$" => "");
showroweditor($row)
?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<? } ?>
<? function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form action="majorcustomer.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xCustomerID" value="<? echo $row["CustomerID"] ?>">
<? showroweditor($row) ?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<?
  mysql_free_result($res);
} ?>

<? function connect()
{
  $conn = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
  mysql_select_db(DB_NAME);
  return $conn;
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "select `CustomerID`, `UserName_L2`, `FirstName`, `LastName`, `MobileNumber`, `EmailAddress`, `Password`, `UserLevel_L3`, `Credit_L2`, `Active_L2`, `~SiteID\$` from `majorcustomer`  where (`~SiteID\$`=".$_SESSION['SiteID'].")";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " and " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " and (`CustomerID` like '" .$filterstr ."') or (`UserName_L2` like '" .$filterstr ."') or (`FirstName` like '" .$filterstr ."') or (`LastName` like '" .$filterstr ."') or (`MobileNumber` like '" .$filterstr ."') or (`EmailAddress` like '" .$filterstr ."') or (`Password` like '" .$filterstr ."') or (`UserLevel_L3` like '" .$filterstr ."') or (`Credit_L2` like '" .$filterstr ."') or (`Active_L2` like '" .$filterstr ."') or (`~SiteID\$` like '" .$filterstr ."')";
  }
  if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`";
  if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "select count(*) from `majorcustomer` where (`~SiteID\$`=".$_SESSION['SiteID'].")";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " and " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " and (`CustomerID` like '" .$filterstr ."') or (`UserName_L2` like '" .$filterstr ."') or (`FirstName` like '" .$filterstr ."') or (`LastName` like '" .$filterstr ."') or (`MobileNumber` like '" .$filterstr ."') or (`EmailAddress` like '" .$filterstr ."') or (`Password` like '" .$filterstr ."') or (`UserLevel_L3` like '" .$filterstr ."') or (`Credit_L2` like '" .$filterstr ."') or (`Active_L2` like '" .$filterstr ."') or (`~SiteID\$` like '" .$filterstr ."')";
  }
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;
 $password=md5($_POST["Password"]);
 if ($_POST["Active_L2"]=="on")
	{
		$Active=1;
	}else{
		$Active=0;
	}
  $sql = "insert into `majorcustomer` ( `UserName_L2`, `FirstName`, `LastName`, `MobileNumber`, `EmailAddress`, `Password`, `UserLevel_L3`, `Credit_L2`, `Active_L2`, `~SiteID\$`) values (" .sqlvalue(@$_POST["UserName_L2"], true) .", " .sqlvalue(@$_POST["FirstName"], true) .", " .sqlvalue(@$_POST["LastName"], true) .", " .sqlvalue(@$_POST["MobileNumber"], true) .", " .sqlvalue(@$_POST["EmailAddress"], true) .", '" .$password ."', " .sqlvalue(@$_POST["UserLevel_L3"], false) .", 0, " .$Active .",".$_SESSION['SiteID'].")";
//echo $sql;
//break;
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;
	if ($_POST["Active_L2"]=="on")
	{
		$Active=1;
	}else{
		$Active=0;
	}
$password=md5($_POST["Password"]);
  $sql = "update `majorcustomer` set `UserName_L2`=" .sqlvalue(@$_POST["UserName_L2"], true) .", `FirstName`=" .sqlvalue(@$_POST["FirstName"], true) .", `LastName`=" .sqlvalue(@$_POST["LastName"], true) .", `MobileNumber`=" .sqlvalue(@$_POST["MobileNumber"], true) .", `EmailAddress`=" .sqlvalue(@$_POST["EmailAddress"], true).", `UserLevel_L3`=" .sqlvalue(@$_POST["UserLevel_L3"], false).",`Password`='" .$password."', `Active_L2`=" .$Active." where " ."(`CustomerID`=" .sqlvalue(@$_POST["xCustomerID"], false) .")";

  mysql_query($sql, $conn) or die(mysql_error());
} ?>
Return current item: eCanteen