<?php
/**
 * class index_controller
 *
 * Description for class index_controller
 *
 * @author:
*/
namespace area\presenter\controller;

use core\presentation;

class index_controller extends _controller {
	private $p = null;

	public function __construct() {
		global $__routing;

		parent::__construct();

		$id = isset($__routing[1]["id"]) ? $__routing[1]["id"] : "";
		
		$this->p = new presentation($id);
		if(!empty($id) && !$this->p->existsOwner()) {
			$this->viewError("404");
		}
	}
	
	public function index_action() {
		if(!isset($_SESSION["presenter_presentations"])) {
			$_SESSION["presenter_presentations"] = array(
				"find" => "",
				"page" => 1,
				"limit" => 10,
				"sort" => "date_created",
				"sortType" => "desc"
			);
		}

		if(isset($_GET["f"])) {
			$_SESSION["presenter_presentations"]["find"] = strip_tags(trim(stripslashes($_GET["f"])));
		}

		if(isset($_GET["p"]) && (int)$_GET["p"] > 0) {
			$_SESSION["presenter_presentations"]["page"] = (int)strip_tags(trim(stripslashes($_GET["p"])));
		}

		if(isset($_GET["s"]) && !empty($_GET["s"])) {
			$_SESSION["presenter_presentations"]["sort"] = strip_tags(trim(stripslashes($_GET["s"])));
		}

		if(isset($_GET["t"]) && !empty($_GET["t"])) {
			$_SESSION["presenter_presentations"]["sortType"] = strip_tags(trim(stripslashes($_GET["t"])));
		}

		$find = $_SESSION["presenter_presentations"]["find"];
		$page = $_SESSION["presenter_presentations"]["page"];
		$limit = $_SESSION["presenter_presentations"]["limit"];
		$sort = $_SESSION["presenter_presentations"]["sort"];
		$sortType = $_SESSION["presenter_presentations"]["sortType"];
		
		$numRows = $this->p->countListOwner($find);
		$numPages = ceil($numRows / $limit);

		if($page > $numPages) {
			$page = 1;
		}

		$this->setVariable("searchQuery", $find);
		$this->setVariable("page", $page);
		$this->setVariable("sort", $sort);
		$this->setVariable("sortType", $sortType);
		$this->setVariable("numPages", $numPages);
		return $this->view($this->p->listOwner($find, $page, $limit, $sort, $sortType));
	}
}
?>