<?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_once './Includes/configs/functions.php';
require_once './Includes/libs/Smarty.class.php';
require_once './Includes/libs/SmartyPaginate.class.php';
require_once './Includes/libs/zipcode.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"));

//Check if single user mode 1 or multi-user mode 0
if ($site_mode == 0) {
	$smarty->assign("site_mode",1);
}
//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"])) {
		$smarty->assign("listdata",getListing());
		$smarty->assign("userdetails",getUserRecords($_SESSION["user"]));
	}
}

//search query builder below. Will build a query string for all $_POST search items
if (isset($_POST["submit"]) && $_POST["submit"] == SEARCH) {
	codeClean($_POST);
	SmartyPaginate::disconnect();
	
	if (!empty($_POST["min_price"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="price > '" . number_format($_POST["min_price"], 2, ".", "") . "'";
	}
	if (!empty($_POST["max_price"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="price < '" . number_format($_POST["max_price"], 2, ".", "") . "'";
	}
	if (!empty($_POST["start_zip"])) {
		$zip = new zipcode_class;
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		if (!empty($_POST["zip_range"]))
			$zips = $zip->get_zips_in_range($_POST["start_zip"], $_POST["zip_range"], _ZIPS_SORT_BY_DISTANCE_ASC, true); 
		if (empty($zips)) {
			$addOnSQL.="zip = " . $_POST["start_zip"] . "";
		} else {
			// this fetches all the zip codes in the $zips variable
			$zipcodes = array_keys($zips); 
			$zip_list = implode(",",$zipcodes); 
			$addOnSQL.="zip IN (" . $zip_list . ")";
		}
	}
	if (!empty($_POST["state"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="state = '" . $_POST["state"] . "'";
	}	
	if (!empty($_POST["vehicle_type"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="vehicle_type = '" . $_POST["vehicle_type"] . "'";
	}	
	if (!empty($_POST["make"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="make = '" . $_POST["make"] . "'";
	}
	if (!empty($_POST["model"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="model = '" . $_POST["model"] . "'";
	}
	if (!empty($_POST["year"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="year = '" . $_POST["year"] . "'";
	}
	if (!empty($_POST["listing_condition"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="listing_condition = '" . $_POST["listing_condition"] . "'";
	}
	if (!empty($_POST["trans"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="trans = '" . $_POST["trans"] . "'";
	}
	if (!empty($_POST["drive_train"])) {
		if (!empty($addOnSQL))
			$addOnSQL.=" AND ";
		$addOnSQL.="drive_train = '" . $_POST["drive_train"] . "'";
	}

	//print "$addOnSQL";
	$_SESSION["addOnSQL"] = $addOnSQL;
	
	//assign db results to the template getListing($id, $sellerid="")
	$id = 0;

} elseif (isset($_GET["next"])) {
	SmartyPaginate::disconnect();
	//assign db results to the template getListing($id, $sellerid="")
	if (isset($_SESSION["addOnSQL"])) {
		$id = 0;
		$addOnSQL = $_SESSION["addOnSQL"];
	} else {
		$id = 'all';
		$addOnSQL = '';
	}
} else {
	SmartyPaginate::disconnect();
	unset($_SESSION["addOnSQL"]);
	$id = 'all';
	$addOnSQL = '';
}

/* Broken query needs closer examination to be proper
if (isset($_GET["action"]) && $_GET["action"] == "sort") {
	switch($_GET["type"]) {
		case 'model':
			if (isset($_GET["ASC"])) {
				$_SESSION["sort"] = " ORDER BY vehicle_type ASC";
			} elseif (isset($_GET["DESC"])) {
				$_SESSION["sort"] = " ORDER BY vehicle_type DESC";
			}
			break;
		case 'year':
			if (isset($_GET["ASC"])) {
				$_SESSION["sort"] = " ORDER BY year ASC";
			} elseif (isset($_GET["DESC"])) {
				$_SESSION["sort"] = " ORDER BY year DESC";
			}
			break;
		case 'price':
			if (isset($_GET["ASC"])) {
				$_SESSION["sort"] = " ORDER BY price ASC";
			} elseif (isset($_GET["DESC"])) {
				$_SESSION["sort"] = " ORDER BY price DESC";
			}
			break;
		default:
			$_SESSION["sort"] = "ORDER BY id DESC";
			break;
	}
} */

if (isset($_GET["id"])) {
	$id = (int) $_GET["id"];
	$listingdata = getListing($id);

	$smarty->assign("singlelistdata",$listingdata);
	$smarty->assign("sellerinfo",getUserDetails($listingdata[0]["sellerid"]));
	$smarty->assign("featurelist",getFeaturesList());
	$smarty->assign("viewsingle",1);
	
	if (isset($_GET["largeIMG"]))
		$smarty->assign("largeIMG",$_GET["largeIMG"]);
} else {
	//required connect for pagination
	SmartyPaginate::connect();
	//set items per page
	SmartyPaginate::setLimit($listings_per_page);

	$smarty->assign("listdata",getListing($id,$addOnSQL,"",""));
	//assign {$paginate} var
	SmartyPaginate::assign($smarty);
}

$smarty->assign("listings",1);
$smarty->display('listings.tpl');
?>