<?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/manufacturer.class.php';
include_once './core/structure.class.php';
include_once './core/item.class.php';

class website_manufacturers_details {

	private $base_object;
	private $layout_object;
	private $manufacturer_object;
	private $item_object;

	public function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->manufacturer_object = new Manufacturer($base_object);
		$this->item_object = new Item($base_object);
	}

	public function run($suri) {
		$path = $this->manufacturer_object->get_image_url();
		$this->layout_object->assign('img_path', $path);

		// get manufacturer
		$manufacturer = $this->manufacturer_object->get_by_suri($suri);
		$this->layout_object->assign('manufacturer', $manufacturer);

		// get manufacturer items
		$items = $this->item_object->get_by_manufacturer($id);

		// get structures
		$structure = new Structure($base_object);
		$structureItems = $structure->getWhereManufacturerItems($manufacturer->id);

		$structure_tree = $structure->get_all_tree_full_path(1);
		$fullpath_tree = array();
		foreach ($structure_tree as $obj) {
			$fullpath_tree[$obj->id] = $obj;
		}

		$structureItems_new = array();
		foreach ($structureItems as $structure_item) {
			$structure_item->path = $fullpath_tree[$structure_item->id]->path_name;
			$structure_item->name = $structure_item->name;
			$path_name = ($fullpath_tree[$structure_item->id]->path_name) ? $fullpath_tree[$structure_item->id]->path_name.'->'.$structure_item->name : $structure_item->name;
			$structureItems_new[$path_name] = $structure_item;
		}

		ksort($structureItems_new);
		$this->layout_object->assign('structureItems', $structureItems_new);

		foreach ($structureItems as $structureItem) {
			$metaparts[] = $manufacturer->name.' '.$structureItem->name;
		}
		$this->layout_object->metaTagObject->get_by_group('HERSTELLERDETAILS');
		$this->layout_object->metaTagObject->overwriteVar('Hersteller', $manufacturer->name);
		$this->layout_object->metaTagObject->overwriteMeta('keywords', implode(', ', $metaparts));
		$this->layout_object->metaTagObject->overwriteMeta('description', trim($manufacturer->description));

		if (Config::has_key('manufacturers_show_items') && Config::is_set('manufacturers_show_items')) {
			$show_items = Config::get_value('manufacturers_show_items');
		} else {
			$show_items = 0;
		}

		if ($show_items) {
			//$manufacturer_items = $this->item_object->get_all_by_manufacturer($manufacturer->id);
			$manufacturer_items = $this->item_object->search_items(array('manufacturer_id' => $manufacturer->id));

			$this->layout_object->assign('manufacturer_items', $manufacturer_items);
		}

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