<?php
/**
* @version      $Id: website_dyncontent.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/site_content.class.php';
include_once './core/structure.class.php';
include_once './core/manufacturer.class.php';
include_once './core/config.class.php';
include_once './core/itemhelper.class.php';
include_once './core/manufacturerhelper.class.php';

class website_dyncontent {
	
	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) {
		$maincontent = '';
		
		if ($structure_id) {
			$site_content_object = new SiteContent($this->base_object);
			$structure_object = new Structure($this->base_object);
                        $this->layout_object->assign('structure_id', $structure_id);
			
			$site_content = $site_content_object->get_by_structure_id($structure_id);
			$structure_content = $structure_object->get_by_id($structure_id);

			if (isset($this->base_object->config->shopConfiguration['full_shop_name'])) {
				$shop_name = $this->base_object->config->shopConfiguration['full_shop_name'];
			} else {
				$shop_name = SHOP_SYSTEM;
			}
			
			$need_left_column = 0;
			// load content blocks
			if ($site_content) {
				foreach ($site_content as $block) {
					if ($block->activ == 1) {
						$need_left_column = 1;
						if ($block->type == 'article_list') {
							$maincontent .= $this->item_list($structure_id, $block);
							$need_left_column = 1;
						}
						elseif ($block->type == 'textbox') {
							$maincontent .= $this->textbox($structure_id, $block);
							$need_left_column = 1;
						}
						elseif ($block->type == 'slider') {
							$maincontent .= $this->slider($structure_id, $block);
						}
						elseif ($block->type == 'banner') {
							$maincontent .= $this->banner($structure_id, $block);
						}
						elseif ($block->type == 'manufacturers') {
							$maincontent .= $this->manufacturers($structure_id, $block);
						}
						elseif ($block->type == 'popular') {
							if (isset($this->base_object->config->shopConfiguration['popular_content_feature']) && $this->base_object->config->shopConfiguration['popular_content_feature'] == 2) {
								$maincontent .= $this->popular2($structure_id, $block);
							} else {
								$maincontent .= $this->popular($structure_id, $block);
							}
						}
						elseif ($block->type == 'substructure') {
							$maincontent .= $this->substructure($structure_id, $block);
						}
					}
				}
			}
			
		}
		
		if (!$need_left_column) {
			$this->layout_object->assign('site_type', 'full_width');
		}
		
		return $maincontent;
	}
	
	private function popular($structure_id, $block) {
		$popular_items = ItemHelper::get_best_selling_items(3, $block->content);
		$popular_brands = ManufacturerHelper::get_best_selling_manufacturers(3, $block->content);

		$this->layout_object->assign('popular_items', $popular_items);
		$this->layout_object->assign('popular_manufacturers', $popular_brands);
		
		return $this->layout_object->_fetch('content_box_popular.tpl');
	}
	
	private function popular2($structure_id, $block) {
		$popular_items = ItemHelper::get_best_selling_items(3, $block->content);
		$popular_brands = ManufacturerHelper::get_best_selling_manufacturers(10, $block->content);
	
		$this->layout_object->assign('popular_items', $popular_items);
		$this->layout_object->assign('popular_manufacturers', $popular_brands);
	
		return $this->layout_object->_fetch('content_box_popular2.tpl');
	}
	
	private function substructure($structure_id, $block) {
		// Set configuration
		$config = array();
		eval($block->configuration);
		$block->configuration = $config;

		if (isset($block->configuration['cat_id']) && is_numeric($block->configuration['cat_id'])) {
			$structure_id = $block->configuration['cat_id'];
		}
		
		// get substructures
		$structure_object = new Structure($this->base_object);
		
		if (isset($block->configuration['ebenen']) && is_numeric($block->configuration['ebenen']) && $block->configuration['ebenen'] > 1) {
			$substructures = $structure_object->get_structure_family($structure_id);
			
			$tpl = 'content_box_substructure_family.tpl';
		} else {
			$substructures = $structure_object->get_subcategory_objects($structure_id);
			$tpl = 'content_box_substructure.tpl';
		}

		$this->layout_object->assign('block', $block);
		$this->layout_object->assign('substructures', $substructures);
		
		$output = $this->layout_object->_fetch($tpl);
		
		return $output;
	}
	
	private function item_list($structure_id, $block) {
		$item_object = new Item($this->base_object);
	
		// 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;
	
		// Manufacturer change
		if (isset($_POST['manufacturerSelector']) && $_POST['manufacturerSelector']) {
			if (is_numeric($_POST['manufacturerSelector'])) {
				$_SESSION['currentManufacturer'] = $_POST['manufacturerSelector'];
			} else {
				unset($_SESSION['currentManufacturer']);
			}
	
			unset($_SESSION['currentPage']);
		}
	
		// 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_new(
			$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('block', $block);

			$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_list.tpl');
		}
	}
	
	private function item_list_old($structure_id, $block) {
		$item_object = new Item($this->base_object);
		
		// Structure change
		if ($_SESSION['lastStructureId'] != $structure_id) {
			unset($_SESSION['itemsPerPage']);
			unset($_SESSION['currentPage']);
			unset($_SESSION['currentManufacturer']);
		}

		$_SESSION['lastStructureId'] = $structure_id;
		
		// Manufacturer change
		if ($_POST['manufacturerSelector']) {
			if (is_numeric($_POST['manufacturerSelector'])) {
				$_SESSION['currentManufacturer'] = $_POST['manufacturerSelector'];
			} else {
				unset($_SESSION['currentManufacturer']);
			}

			unset($_SESSION['currentPage']);
		}
		
		// Per page change
		if ($_POST['pagingSelector']) {
			$possibilities = array(12, 24, 48, 'all');
			
			if (in_array($_POST['pagingSelector'], $possibilities)) {
				$_SESSION['itemsPerPage'] = $_POST['pagingSelector'];
				$_SESSION['currentPage'] = 1;
			}
		}
		if (!$_SESSION['itemsPerPage']) {
			$_SESSION['itemsPerPage'] = 24;
		}
				
		// Page change
		if ($_GET['page'] && is_numeric($_GET['page'])) {
			$_SESSION['currentPage'] = $_GET['page'];
		}
		if (!$_SESSION['currentPage']) {
			$_SESSION['currentPage'] = 1;
		}

		$article_list = $item_object->get_item_list($structure_id, $_SESSION['itemsPerPage'], $_SESSION['currentPage'], true, $_SESSION['currentManufacturer']);
		if ($_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($structure_id, $_SESSION['itemsPerPage'], $_SESSION['currentPage'], true, $_SESSION['currentManufacturer']);
		}

		
		
		// 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);
		$this->layout_object->assign('shopView', $shopView);
		$this->layout_object->assign('block', $block);
		
		$this->layout_object->assign('itemsPerPage', $_SESSION['itemsPerPage']);
		$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']);

		return $this->layout_object->_fetch('content_box_item_list.tpl');
	}
	
	private function textbox($structure_id, $block) {
		$config = array();
		eval($block->configuration);
                
                // replace meta script
                $var_data = array();
                $var_data['[#HIDDEN_TEXT_START]'] = '<span class="text_box_hidden_text">';
                $var_data['[#HIDDEN_TEXT_END]'] = ' </span> <a class="text_box_link_show_text" href="#">mehr&gt;&gt;</a><a class="text_box_link_hide_text" href="#">&lt;&lt;weniger</a>';
                $block->content = strtr($block->content, $var_data);
                
                
		$block->configuration = $config;
		$this->layout_object->assign('block', $block);
		
		return $this->layout_object->_fetch('content_box_textbox.tpl');
	}
	
	private function banner($structure_id, $block) {
		$config = array();
		eval($block->configuration);
		$block->configuration = $config;
		$this->layout_object->assign('block', $block);
		
		return $this->layout_object->_fetch('content_box_banner.tpl');
	}

	private function slider($structure_id, $block) {
		// get config
		$quantity =  (Config::has_key('slider_items') && Config::is_set('slider_items')) ? Config::get_value('slider_items') : 10;
		$sort = ($this->base_object->config->shopConfiguration['sort_item_list_by']) ? $this->base_object->config->shopConfiguration['sort_item_list_by'] : 'number_up';

		$structure_object = new Structure($this->base_object);
		$structure_content = $structure_object->get_by_id($block->content);

		$item_object = new Item($this->base_object);
		
		if ($structure_content->item_type) {
			$article_list = $item_object->get_item_list_by_type($block->content, $quantity, 1, true, false, $sort);
		} else {
			$article_list = $item_object->get_item_list_new($block->content, $quantity,1, true, false, $sort);
		}
		
		//echo '<pre>';print_r($article_list);exit();
		$this->layout_object->assign('shopArticles', $article_list);
		$this->layout_object->assign('block', $block);
		
		return $this->layout_object->_fetch('content_box_slider.tpl');
	}
	
	private function manufacturers($structure_id, $block) {
		$this->layout_object->assign('manufacturers', Manufacturer::get_all_active_static());
		
		return $this->layout_object->_fetch('content_box_manufacturers.tpl');
	}
}
