<?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 04-12-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_once './Includes/configs/functions.php';
require_once './Includes/libs/Smarty.class.php';
include_once "./Includes/language/$site_lang.php";

$smarty = new Smarty;

$smarty->assign("manufacturerslist",getSearchList("make"));
$smarty->assign("modelslist",getSearchList("model"));
$smarty->assign("typeslist",getSearchList("vehicle_type"));

//if the user is logged in let them be else redirect them to login
if (isset($_SESSION["logged_in"])){
	$smarty->assign("logged_in",1);
	$smarty->assign("username",$_SESSION["user"]);
	//check privs to find out if user 
	//is a seller, admin or user
	$status = checkPrivs();
	$smarty->assign("$status",1);
	
	//if (isset($_SESSION["admin"]) || isset($_SESSION["seller"])) {
	if (checkIfAdmin($_SESSION["user"],$_SESSION["pass"]) || checkIfSeller($_SESSION["user"],$_SESSION["pass"])) {
		$smarty->assign("manufacturerslist",getManufacturersList());
		$smarty->assign("featurelist",getFeaturesList());
		$smarty->assign("drivetrainlist",getDriveTrainsList());
		$smarty->assign("typeslist",getTypesList());
		$smarty->assign("userdetails",getUserRecords($_SESSION["user"]));
		$smarty->assign("stateslist",getStatesList());

		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			if (isset($_POST["submit"]) && $_POST["submit"] == EDIT_LISTING_FORM_UPDATE_BUTTON) {
				$_POST = codeClean($_POST);
				$serializedfeature = serialize($_POST["features"]);
				$res = updateListing($_POST["id"],$_POST["ad_title"],$_POST["make"],$_POST["model"],$_POST["vehicle_type"],$_POST["doors"],$_POST["color"],$_POST["mileage"],$_POST["year"],$_POST["listing_condition"],$_POST["engine"],$_POST["trans"],$_POST["drive_train"],$_POST["mpg"],$_POST["price"],$_POST["adddesc"],$serializedfeature,$_POST["vin"],$_POST["stock"],$_POST["state"],$_POST["zip"],$_POST["sold"],$_POST["sellerid"],$_POST["seller"]);
				if ($res == 99) {
					$smarty->assign("success",EDIT_LISTING_SUCCESS);
				} else {
					$smarty->assign("error_message",EDIT_LISTING_ERROR);
				}
			} elseif (isset($_POST["addimage"])) {
				$res = uploadListImage($listings_stored_path, $_FILES["image"]["name"], $_FILES["image"]["tmp_name"], $_POST["max"], $_POST["id"], "listing_", $_SESSION["user"]);
			
				if ($res == 99) {
					$smarty->assign("success",EDIT_USER_UPLOAD_SUCCESS);
				} elseif ($res == 1) {
					$smarty->assign("error_message",EDIT_USER_MAX_ERROR);
				} elseif ($res == 2) {
					$smarty->assign("error_message",EDIT_USER_BAD_EXT);
				} else {
    				$smarty->assign("error_message",EDIT_USER_UPLOAD_ERROR);
				}
			} elseif (isset($_POST["delete"]) && $_POST["delete"] == EDIT_LISTING_FORM_DELETE_BUTTON) {
				$id = (int) $_POST["id"];
				$res = deleteFullListing($id);
				if ($res = 99)
					$smarty->assign("success",EDIT_LISTING_REMOVE_SUCCESS);
			} else {
				$smarty->assign("error_message",EDIT_LISTING_GENERAL_ERROR);
			}
			//check if admin or seller then do stuff accordingly
			//if (isset($_SESSION["admin"])) {
			if (checkIfAdmin($_SESSION["user"],$_SESSION["pass"])) {
				$id = (int) $_POST["id"];
				$smarty->assign("listdata",getListing($id));
				//$smarty->assign("images",getListImages($_POST["id"]));
				$smarty->assign("sellerslist",getSellers());
			//} elseif (isset($_SESSION["seller"])) {
			} elseif (checkIfSeller($_SESSION["user"],$_SESSION["pass"])) {
				$sellerid = getSellerId($_SESSION["user"]);
				if (!empty($sellerid)) {
					$smarty->assign("listdata",getListing($_POST["id"],"",$sellerid));
					$smarty->assign("seller",$_SESSION["user"]);
				} 
			}
		} elseif (!empty($_GET["id"])) {
			if (isset($_GET["action"]) && $_GET["action"] =="delete") {
				if (isset($_GET["sellerid"])) {
					$userid = getUserId($_SESSION["user"]);
					//if (isset($_SESSION["admin"]) || $_GET["sellerid"] == "$userid") {
					if (checkIfAdmin($_SESSION["user"],$_SESSION["pass"]) || $_GET["sellerid"] == "$userid") {
						$res = deleteListingsImage($_GET["id"],$_GET["imageid"],$_SESSION["user"]);
						if ($res == 99) {
							$smarty->assign("success",EDIT_LISTING_DELETE_IMG_SUCCESS);
						} else {
							$smarty->assign("error_message",EDIT_LISTING_DELETE_IMG_ERROR);
						}
					} else {
				 		$smarty->assign("error_message",EDIT_LISTING_EDIT_OWN_IMG_ERROR);
					}
				}
			} elseif (isset($_GET["action"]) && $_GET["action"] == "markmain") {
				if (isset($_GET["sellerid"])) {
					$userid = getUserId($_SESSION["user"]);
					//if (isset($_SESSION["admin"]) || $_GET["sellerid"] == "$userid") {
					if (checkIfAdmin($_SESSION["user"],$_SESSION["pass"]) || $_GET["sellerid"] == "$userid") {
						$res = markImageMain($_GET["id"],$_GET["imageid"],$_SESSION["user"]);
						if ($res == 99) {
							$smarty->assign("success",EDIT_LISTING_MARK_MAIN_SUCCESS);
						} else {
							$smarty->assign("error_message",EDIT_LISTING_MARK_MAIN_ERROR);
						}
					} else {
				 		$smarty->assign("error_message",EDIT_LISTING_EDIT_OWN_IMG_ERROR);
					}
				}
			}
			
			//if (isset($_SESSION["admin"])) {
			if (checkIfAdmin($_SESSION["user"],$_SESSION["pass"])) {
				$smarty->assign("listdata",getListing($_GET["id"]));
				$smarty->assign("sellerslist",getSellers());
			//} elseif (isset($_SESSION["seller"])) {
			} elseif (checkIfSeller($_SESSION["user"],$_SESSION["pass"])) {
				$sellerid = getSellerId($_SESSION["user"]);
				if (!empty($sellerid)) {
					$smarty->assign("listdata",getListing($_GET["id"],"",$sellerid));
					$smarty->assign("seller",$_SESSION["user"]);
				}
			} else {
				$smarty->assign("error_message",EDIT_LISTING_GENERAL_ERROR);
			}		
		} else {
			//throw an error to select a listing to edit
			$smarty->assign("error_message",EDIT_LISTING_SELECT_ERROR);
		}
	} else {
		header("Location, member.php");
		exit();
	}
} else {
	header("Location: login.php");
	exit();
}

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