<?php

require_once "include/datamodel.class.php";
require_once "include/view.class.php";
require_once "include/auth.class.php";
require_once "include/misc.lib.php";
require_once "include/ui.inc.php";
require_once "include/phpmailer.class.php";
$auth = new Auth(true);
$user = $auth->getUser();
if ($user->user_rank == 1) {
    View::Enable("admin_permission");
    $purge_stack = $_MODEL->getEntities("object")->where("object_statut", "=", 1)->andWhere("object_end_date", "<", time())->run();
    if (isset($_POST["action"]) && $_POST["action"] == "purge") {
        $sql_purge = '	SELECT	o.object_name,
									o.object_picture_url,
									u1.user_firstname AS "seller_firstname",
									u1.user_lastname AS "seller_lastname",
									u1.user_email AS "seller_email",
									u1.user_address AS "seller_address",
									u1.user_phone AS "seller_phone",
									u2.user_firstname AS "bidder_firstname",
									u2.user_lastname AS "bidder_lastname",
									u2.user_email AS "bidder_email",
									u2.user_address AS "bidder_address",
									u2.user_phone AS "bidder_phone",
									MAX(b.bid_price) AS "price" 
							FROM objects AS o 
							INNER JOIN bids AS b ON b.bid_object_id = o.object_id 
							INNER JOIN users AS u1 ON u1.user_id = o.object_seller_user_id 
							INNER JOIN users AS u2 ON u2.user_id = b.bid_bidder_user_id 
<?php

require_once "include/datamodel.class.php";
require_once "include/view.class.php";
require_once "include/auth.class.php";
require_once "include/misc.lib.php";
require_once "include/ui.inc.php";
$auth = new Auth(false);
UI::Header($auth);
if (isset($_GET["sort"])) {
    View::Enable("search_sort_" . $_GET["sort"]);
} else {
    $_GET["sort"] = "";
}
if (!isset($_GET["search"])) {
    $_GET["search"] = "";
}
$searchterm = !empty($_GET["search"]) ? "%" . $_GET["search"] . "%" : "%";
$page = isset($_GET["page"]) ? (int) $_GET["page"] : 0;
if ($page < 0) {
    $page = 0;
}
$blocksize = 8;
$object_counter = $_MODEL->getEntities("object")->where("object_name", "LIKE", $searchterm)->andWhere("object_statut", "=", 1)->andWhere("object_start_date", "<", time())->andWhere("object_end_date", ">", time())->count();
$totals = $object_counter->size > 0 ? $object_counter->size - 1 : 0;
$maxpage = floor($totals / $blocksize);
if ($page > $maxpage) {
    $page = $maxpage;
}
$orderBy = "";
if ($_GET["sort"] == "name_asc") {
                View::Enable("prenom_error");
            }
            if (in_array("user_lastname", $result->errors["integrity"])) {
                View::Enable("nom_error");
            }
            if (in_array("user_email", $result->errors["integrity"])) {
                View::Enable("email_error");
            }
            if (in_array("user_password", $result->errors["integrity"])) {
                View::Enable("password_error");
            }
            if (in_array("user_address", $result->errors["integrity"])) {
                View::Enable("adresse_error");
            }
            if (in_array("user_phone", $result->errors["integrity"])) {
                View::Enable("telephone_error");
            }
        }
    }
}
UI::Header($auth);
?>
	<div class="wrap">
		<main>

			<h1>Inscription //</h1>
			
			<div class="column">
				<div class="specproduct leftradius" id="signinfobox">

    } else {
        @unlink($uri);
        if (!$valid_date) {
            View::Enable("date_error");
        }
        if (!$valid_file) {
            View::Enable("photo_error");
        }
        if (in_array("object_name", $result->errors["integrity"])) {
            View::Enable("nom_error");
        }
        if (in_array("object_description", $result->errors["integrity"])) {
            View::Enable("description_error");
        }
        if (in_array("object_minimal_price", $result->errors["integrity"]) || $_POST["prix_minimum"] == "" || !is_numeric($_POST["prix_minimum"])) {
            View::Enable("prix_minimum_error");
        }
    }
}
UI::Header($auth);
?>
	<div class="wrap">
		<main>

			<h1>Vendre //</h1>
			
				<div class="largebox bothradius">
				
					<div id="signform" class="sideform">
						<?php 
View::Display("ajout_disponible", function () {
require_once "include/auth.class.php";
require_once "include/misc.lib.php";
require_once "include/ui.inc.php";
require_once "include/view.class.php";
$auth = new Auth(false);
if ($auth->isLogged()) {
    header("Location: index.php");
    exit;
} else {
    if (Misc::checkArray(array("email", "password"), $_POST)) {
        if (Auth::login($_POST["email"], $_POST["password"])) {
            header("Location: index.php");
            exit;
        } else {
            View::Enable("error");
        }
    }
}
UI::Header($auth);
?>
	<div class="wrap">
		<main>

			<h1>Connexion //</h1>
			
			<div class="column">
				<div class="specproduct leftradius" id="loginfobox">


				</div>
                        $bid_counter = $_MODEL->getEntities("bid")->innerJoin("user")->on("bid.bid_bidder_user_id", "=", "user.user_id")->where("bid_object_id", "=", $object->object_id)->count();
                        $bid_entities = $_MODEL->getEntities("bid")->innerJoin("user")->on("bid.bid_bidder_user_id", "=", "user.user_id")->where("bid_object_id", "=", $object->object_id)->limit(5)->orderBy(array("bid_date" => "DESC"))->run();
                        $i_am_the_best = $bid_entities->size > 0 && $bid_entities->results[0]["user"]->user_id == $user->user_id;
                        if ($i_am_the_best) {
                            View::Enable("i_am_the_best");
                        }
                        if ($bid_entities->size == 0) {
                            $object_price = $object->object_minimal_price;
                        } else {
                            $object_price = $bid_entities->results[0]["bid"]->bid_price;
                        }
                    }
                }
            }
        } else {
            View::Enable("need_connexion");
        }
    }
    //end if objet existe
}
?>
	<div class="wrap">
		<main>
			<h1>Produit //</h1>
			
			
			<?php 
View::Display("object_exists", function () {
    global $object, $seller_user, $object_price, $bid_entities, $bid_counter;
    ?>