<?PHP
//================================================================================
// PLEASE DO NOT REMOVE THIS HEADER!!!
//
// COPYRIGHT NOTICE
// This script is licensed under the GPL
// 
// Copyright 2007-2008 Alias 454 Studios and Brandon Keep (c) All rights reserved.
// Created 11/18/2007   
// Brandon Keep,    http://www.openautoclassifieds.com
//                  http://www.alias454studios.com/scripts/
//
// Last Modified 05-05-2008 by
// Brandon Keep,    http://alias454studios.com
//================================================================================
// This software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//================================================================================

require 'Includes/configs/admin_config.php';

$smarty = new Smarty;

//User not an administrator, redirect to main login page automatically.
//if (!isset($_SESSION["admin"])) {
if (!checkIfAdmin($_SESSION["user"],$_SESSION["pass"])) {
	header("Location: ../login.php");
	exit();
} else {
	//check if logged in
	if (isset($_SESSION["logged_in"])) {
		$smarty->assign("logged_in",1);
		$smarty->assign("username",$_SESSION["user"]);
	}
	
	if (checkIfAdmin($_SESSION["user"],$_SESSION["pass"])) {
		$_SESSION["admin"] = 1;
		
		//Administrator is viewing page, so display all
		if (isset($_SESSION["admin"])) {
			$smarty->assign("admin",1);
			SmartyPaginate::disconnect();
			//required connect for pagination
			SmartyPaginate::connect();
			//set items per page
			SmartyPaginate::setLimit($users_per_page);

			//Checks for actions   
			if (isset($_GET["action"]) && $_GET["action"] == 'delete') {
				$res = deleteUser($_GET["userid"]);

				if ($res == 99)
					$smarty->assign('success',DELETE_USER);
			
			} elseif (isset($_GET["action"]) && $_GET["action"] == 'fullview') {
				$smarty->assign('details',getUserDetails($_GET["userid"]));
			} elseif (isset($_POST["submit"]) && $_POST["submit"] == 'update') {
				$res = updateUserDetails($_POST["user"], $_POST["email"], $_POST["first_name"], $_POST["last_name"], $_POST["phone"], $_POST["alt_phone"], $_POST["fax"], $_POST["user_level"], $_POST["admin_notes"], $_POST["userid"]);

				if ($res == 99) {
					$smarty->assign('success',UPDATE_SUCCESS);
					$smarty->assign('details', getUserDetails($_POST["userid"]));
				}
			}
			//assign db results to the template
			$smarty->assign('results', getUserDetails("all"));
			//assign {$paginate} var
			SmartyPaginate::assign($smarty);
		}
	}
}

$smarty->display('listuser.tpl');
?>