<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

class Router {


	protected $base_object;
	protected $layout_object;
	protected $pageable_object;
	protected $editable_object;
	protected $id;


	private $db;


	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->pageable_object = false;
		$this->editable_object = false;
		$this->id = false;

		$this->db = $base_object->db;
	} // end __construct


	public function __call($name, $args) {
		echo '<h1>The function with the name '.$name.' was not found in this module!</h1>';
		exit();
	} // end __call


	protected function set_id($id) {
		$this->id = $id;
	} // end set_id


	protected function set_pageable_object($pageable_object) {
		$this->pageable_object = $pageable_object;
	} // end set_pageable_object


	protected function set_editable_object($editable_object) {
		$this->editable_object = $editable_object;
	} // end set_editable_object


	public function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} else if (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = 'default_action';
		}

		return $this->$action();
	} // end run


	protected function list_default_action() {
		// begin pagination
		$items = $this->get_items();
		$page = $this->get_page();
		$pages = $this->pageable_object->get_number_of_pages($items);
		$pagination = $this->pageable_object->get_pagination_array($items, $page);
		$data = $this->pageable_object->get_all_paginated($items, $page);

		$this->layout_object->assign('items', $items);
		$this->layout_object->assign('page', $page);
		$this->layout_object->assign('pages', $pages);
		$this->layout_object->assign('pagination', $pagination);
		$this->layout_object->assign('data', $data);
	} // end list_default_action


	protected function edit_default_action() {
		$data = $this->editable_object->get_data($this->id);

		$this->layout_object->assign('data', $data);
	} // end edit_default_action


	protected function detailed_search() {
		if (isset($_POST['detailed_search'])) {
			$this->search($_POST['detailed_search']);
		}
	} // end detailed_search


	protected function get_attribute($name) {
		if (isset($_POST[$name])) {
			return $_POST[$name];
		} else if (isset($_GET[$name])) {
			return $_GET[$name];
		}

		return false;
	} // end get_attribute


	// from the old list and edit class
	protected function get_items() {
		if (isset($_GET['items']) && is_numeric($_GET['items'])) {
			$items = (int)$_GET['items'];
			$this->base_object->customer->set_config_item('pagination', $items);
		} else {
			$items = $this->base_object->customer->get_config_item('pagination');
			if (is_numeric($items)) {
				$items = (int)$items;
			} else {
				$items = 10;
			}
		}

		return $items;
	} // end get_items


	// from the old list and edit class
	protected function get_page() {
		if (isset($_GET['page']) && is_numeric($_GET['page'])) {
			$page = (int)$_GET['page'];
		} else {
			$page = 1;
		}

		return $page;
	} // end get_page

	protected function require_array($name, $id_name = false) {
		$sql = "SELECT
					*
				FROM ";
		$sql .= $this->db->real_escape_string($name);

		$result = $this->db->query($sql);

		if ($result->num_rows > 0) {
			$data = array();

			while ($obj = $result->fetch_object()) {
				if ($id_name) {
					$data[$obj->$id_name] = $obj;
				} else {
					$data[$obj->id] = $obj;
				}
			}

			$this->layout_object->assign($name, $data);
		}
	} // end require_array

} // end router

/* EOF */