<?php
/**
* @version      $Id: website_item_type_list.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/item.class.php';
include_once './core/structure.class.php';
include_once './core/shoppingcart.class.php';

class website_item_type_list {
	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() {
		$structure_id = $_GET['menu_id'];
		$item_object = new Item($this->base_object);
                
                include_once './core/structure.class.php';
                $structure_object = new Structure($this->base_object);
                $this->layout_object->assign('structure_data', $structure_object->get_by_id($structure_id));
	
		// Structure change
		if (!isset($_SESSION['lastStructureId']) || $_SESSION['lastStructureId'] != $structure_id) {
			unset($_SESSION['itemsPerPage']);
			unset($_SESSION['currentPage']);
			unset($_SESSION['currentManufacturer']);
			unset($_SESSION['listStyleType']);
		}
	
		$_SESSION['lastStructureId'] = $structure_id;
	
	
		// 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']) {
			if ($block->content) {
				$_SESSION['listStyleType'] = $block->content;
			}
			else {
				$_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'] = '';
		}
		
		
		// Infinite scrolling?
		$useInfinityScroll = false;
		
		if ($this->base_object->config->shopConfiguration['infinite_scrolling'] && isset($_GET['loadstep']) && is_numeric($_GET['loadstep']) && $_SESSION['itemsPerPage'] == '9999') {
			$_SESSION['currentPage'] = ($_GET['loadstep'] > 0) ? $_GET['loadstep'] : 1;
			
			$useInfinityScroll = true;
		} else if ($this->base_object->config->shopConfiguration['infinite_scrolling'] && $_SESSION['itemsPerPage'] == '9999') {
			$_SESSION['currentPage'] = 1;
			
			$useInfinityScroll = true;
		}


		// Load items
		$article_list = $item_object->get_item_list_by_type(
			$structure_id,
			$_SESSION['itemsPerPage'],
			$_SESSION['currentPage'],
			true,
			$_SESSION['currentManufacturer'],
			$_SESSION['sort_by'],
			$useInfinityScroll
		);
		
		// Still necessary?
		if (isset($_GET['page']) && ($_GET['page'] > ceil($item_object->itemCount / $_SESSION['itemsPerPage']))) {
			$_SESSION['currentPage'] = $_GET['page'] = ceil($item_object->itemCount / $_SESSION['itemsPerPage']);
			
			$article_list = $item_object->get_item_list_new(
				$structure_id,
				$_SESSION['itemsPerPage'],
				$_SESSION['currentPage'],
				true,
				$_SESSION['currentManufacturer'],
				$_SESSION['sort_by'],
				$useInfinityScroll
			);
		}
		
		// Show the (first) item, if only one item exists in current category
		if ($this->base_object->config->shopConfiguration['show_details_by_one_item_in_structure'] == 1 && count($article_list) == 1) {
			$currentItem = current($article_list);
			if ($currentItem->short_uri) {
				header('Location: ./' . $currentItem->short_uri . '.html');
			} else {
				header('Location: /index.php?item_id=' . $currentItem->id);
			}
	
			exit();
		}
		
		$this->layout_object->assign('shopArticles', $article_list);
		
		// Show only the items for infinite scrolling
		if ($this->base_object->config->shopConfiguration['infinite_scrolling'] && isset($_GET['loadstep']) && is_numeric($_GET['loadstep'])) {
			$data = array(
				'template' => $this->layout_object->fetch('item_box_loop.tpl'),
				'counter' => count($article_list)
			);
			
			echo json_encode($data);
			exit();
			
		// Show default item list
		} else {
			$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('currentManufacturer', $_SESSION['currentManufacturer']);
			
			$this->layout_object->assign('useInfinityScroll', $useInfinityScroll);

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

		
	}
}
