<?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
 */

include_once './core/item.class.php';
include_once './core/structure.class.php';
include_once './core/shoppingcart.class.php';

class website_item_search {
	private $base_object;
	private $layout_object;

	public function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
	}

	function run($search) {
		// Structure change
		if (!isset($_SESSION['lastsearch']) || $_SESSION['lastsearch'] != $search) {
			unset($_SESSION['itemsPerPage']);
			unset($_SESSION['currentPage']);
			unset($_SESSION['currentManufacturer']);
			unset($_SESSION['listStyleType']);
		}

		$_SESSION['lastsearch'] = $search;

		// Per page change
		if (isset($_POST['pagingSelector']) && $_POST['pagingSelector']) {
			$possibilities = array(12, 24, 48, '9999');

			if (in_array($_POST['pagingSelector'], $possibilities)) {
				$_SESSION['itemsPerPage'] = $_POST['pagingSelector'];
				$_SESSION['currentPage'] = 1;
			}
		}

		if (!isset($_SESSION['itemsPerPage']) || !$_SESSION['itemsPerPage']) {
			$_SESSION['itemsPerPage'] = 24;
		}

		// list style
		if (isset($_POST['listStyleSelector']) && $_POST['listStyleSelector']) {
			$possibilities = array(1, 2, 3);

			if (in_array($_POST['listStyleSelector'], $possibilities)) {
				$_SESSION['listStyleType'] = $_POST['listStyleSelector'];
			}
		}

		if (!isset($_SESSION['listStyleType']) || !$_SESSION['listStyleType']) {
			$_SESSION['listStyleType'] = 1;
		}

		// list sort by
		if (isset($_POST['sort_by']) && $_POST['sort_by']) {
			$_SESSION['sort_by'] = $_POST['sort_by'];
		}

		if (!isset($_SESSION['sort_by']) || !$_SESSION['sort_by']) {
			if ($this->base_object->config->shopConfiguration['sort_item_list_by']) {
				$_SESSION['sort_by'] = $this->base_object->config->shopConfiguration['sort_item_list_by'];
			} else {
				$_SESSION['sort_by'] = 'number_up';
			}
		}

		// Page change
		if (isset($_POST['page'])) {
			$_GET['page'] = $_POST['page'];
		}
		if (isset($_GET['page']) && $_GET['page'] && is_numeric($_GET['page'])) {
			$_SESSION['currentPage'] = $_GET['page'];
		}
		if (!isset($_SESSION['currentPage']) || !$_SESSION['currentPage']) {
			$_SESSION['currentPage'] = 1;
		}
		if (!isset($_SESSION['currentManufacturer'])) {
			$_SESSION['currentManufacturer'] = '';
		}

		$item_object = new Item($this->base_object);

		$item_list = $item_object->search_items($search);
		$item_ids = array();
		if (is_array($item_list)) {
			$item_ids = array_keys($item_list);
		}
		$search_groups = $item_object->search_group_items($search, $item_ids);

		$this->layout_object->assign('search_item', $search['search_item']);
		/*$this->layout_object->assign('search_item2', $search['search_item2']);
		$this->layout_object->assign('search_item3', $search['search_item3']);*/
		$this->layout_object->assign('search_item_categories', $search_groups);
		$this->layout_object->assign('shopArticles', $item_list);
		$this->layout_object->assign('itemsPerPage', $_SESSION['itemsPerPage']);
		$this->layout_object->assign('listStyleType', $_SESSION['listStyleType']);
		$this->layout_object->assign('sort_by', $_SESSION['sort_by']);
		$this->layout_object->assign('itemsFound', $item_object->itemCount);
		$this->layout_object->assign('found_pages', ceil($item_object->itemCount / $_SESSION['itemsPerPage']));
		$this->layout_object->assign('found_showing_page', $_SESSION['currentPage']);

		$this->layout_object->assign('hide_pagination', true);
		$this->layout_object->assign('hide_listoptions', true);

		//$this->layout_object->assign('content_box_title', 'Suchergebnisse');

		$block = new stdClass();
		$block->content = 'list';
		$block->style = 'h2';
		$this->layout_object->assign('block', $block);

		if (!$item_list) {
			$this->layout_object->assign('search_word', $search['search_item'].' '.$search['search_item2'].' '.$search['search_item3']);
			return $this->layout_object->_fetch('content_item_search_empty.tpl');
		} else {
			$this->layout_object->assign('content_box_title', 'Suche nach: '.$search['search_item'].' '.$search['search_item2'].' '.$search['search_item3']);
			return $this->layout_object->_fetch('content_box_item_list.tpl');
		}

	}
}
