<?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");
} else {
	//Administrator is viewing page, so display all
	if (isset($_SESSION["logged_in"])) {
		$smarty->assign("logged_in",1);
		$smarty->assign("username",$_SESSION["user"]);
	}
	
	if (checkIfAdmin($_SESSION["user"],$_SESSION["pass"])) {
		$_SESSION["admin"] = 1;
	
	   	if (isset($_SESSION["admin"])) {
   		   	$smarty->assign("admin",1);

			//if single option edit get data then display form
			if ($_GET["action"] == "editsingle") {
				$smarty->assign("editsingle",1);
				if ($_GET["section"] == "manufacturer") {
					$smarty->assign("manufactureredit",1);
					$smarty->assign("edit",getSingleOption($_GET["section"], $_GET["id"]));
				} elseif ($_GET["section"] == "features") {
					$smarty->assign("featuresedit",1);
					$smarty->assign("edit",getSingleOption($_GET["section"], $_GET["id"]));
				} elseif ($_GET["section"] == "drive_train") {
					$smarty->assign("drivetrainedit",1);
					$smarty->assign("edit",getSingleOption($_GET["section"], $_GET["id"]));
				} elseif ($_GET["section"] == "vehicle_type") {
					$smarty->assign("vehicletypeedit",1);
					$smarty->assign("edit",getSingleOption($_GET["section"], $_GET["id"]));
				}
			//build a list of all options in a given section
			} elseif ($_GET["action"] == "edit") {
				$smarty->assign("editlist",1);
				//if redirected from editing display a success message
				if ($_GET["message"] == "success") {
					$smarty->assign("success","You have succesfully edited $_GET[option]");
				}
				//if redirected after delete display a success message
				if ($_GET["message"] == "deleted") {
					$smarty->assign("success","You have succesfully deleted $_GET[option]");
				}
				//get section then display list of data
				if ($_GET["section"] == "manufacturer") {
					$smarty->assign("manufacturerslist",getManufacturersList());
				} elseif ($_GET["section"] == "features") {
					$smarty->assign("featurelist",getFeaturesList());
				} elseif ($_GET["section"] == "drive_train") {
					$smarty->assign("drivetrainlist",getDriveTrainsList());
				} elseif ($_GET["section"] == "vehicle_type") {
					$smarty->assign("typeslist",getTypesList());
				}
			//epdate your options that have been entered one at a time
			} elseif ($_POST["vehicle_edit"] == "Save Changes") {
				$_POST = codeclean($_POST);
				$res = updateSingleOption($_POST["table"], $_POST["option_data"], $_POST["id"], $_POST["lists_default"]);
				if ($res == 99) {
					header("Location: vehicle_options.php?action=edit&section=$_POST[table]&message=success&option=$_POST[option_data]");
				}
			//delete your options one at a time
			} elseif ($_POST["vehicle_delete"] == "Delete Option") {
				$res = deleteOption($_POST["table"], $_POST["id"]);
				if ($res == 99) {
					header("Location: vehicle_options.php?action=edit&section=$_POST[table]&message=deleted&option=$_POST[option_data]");
				}
			} else {
				//section for adding new options
				if ($_POST["addoption"] != "") {
					$_POST = codeclean($_POST);
					$res = addOption($_POST["option"], $_POST["table"]);
					if ($res == 99) {
						$smarty->assign("success","You have added $_POST[option] as a new option");
					} elseif ($res == 1){
						$smarty->assign("error_message","You have already added $_POST[option] as an option");
					} else {
						$smarty->assign("error_message","You cannot submit blank options.");
					}
				}
			}
		}
	}
}

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