<?php

/**
 * @version      $Id: website_search.php
 * @package      Easyshop
 * @copyright    Copyright (C) 2005 - 2011 TA-EDV
 * @license      proprietary
 * @author       Richard Kammermayer <rk@ta-edv.de>
 * Easyshop is a web shop system
 */
include_once './core/search.class.php';

class website_search {

	public $base_object;
	public $layout_object;

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

	function run() {
		$search = $_GET['search'];

		if (isset($_GET['type']) && $_GET['type'] == 'shoppingcart') {
			/**
			 * Theory ...
			 */
//			$searchAr = array('search_item' => $search);
//			include_once './core/item.class.php';
//			$Item = new Item($this->base_object);
//			$item_list = $Item->search_items($searchAr);
//			
//			$this->layout_object->assign('search_item', $searchAr['search_item']);
//			$this->layout_object->assign('shopArticles', $item_list);
//			$this->layout_object->assign('itemsPerPage', $_SESSION['itemsPerPage']);
//
//			$this->layout_object->assign('hide_pagination', true);
//			$this->layout_object->assign('hide_listoptions', true);
//
//			$block = new stdClass();
//			$block->content = 'list';
//			$block->style = 'h2';
//			$this->layout_object->assign('block', $block);
//			
//			return $this->layout_object->_fetch('content_box_item_list.tpl');
			
			$items = Search::items($search, 1);
			$this->layout_object->assign('item_hits', $items['hits']);


			$this->layout_object->assign('item_search', $items['results']);
			return $this->layout_object->_fetch('shoppingcart_search.tpl');
		} else {
			// Include individual search
			if (isset($this->base_object->config->shopConfiguration['custom_global_modules']) && $this->base_object->config->shopConfiguration['custom_global_modules']) {
				include_once ROOT_DIR . 'web/' . SHOP_SYSTEM . '/php/website_live_search.inc.php';

				if (isset($return)) {
					return $return;
				}
			}
				
			// Default search
			if (isset($_GET['item_category']) && $_GET['item_category'] > 0) {
				$items = Search::category_items($search, $_GET['item_category'], 1);
			} else {
				$items = Search::items($search, 1);
			}
			$item_result = array();
			$c = 0;
			foreach ($items['results'] as $item) {
				$c++;
				if (($c % 2) == 0) {
					$item->cl = 1;
				}
				$item_result[] = $item;
			}
			$this->layout_object->assign('item_hits', $items['hits']);
			$this->layout_object->assign('item_search', $item_result);

			$manufacturers = Search::manufacturers($search);
			$this->layout_object->assign('manufacturer_hits', $manufacturers['hits']);
			$this->layout_object->assign('manufacturer_search', $manufacturers['results']);

			return $this->layout_object->_fetch('live_search.tpl');
		}
	}

}
