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

#[\AllowDynamicProperties]
class website {

	private $db;
	private $config;
	private $base_object;

	public function __construct($base_object) {
		$this->config_object = $base_object->config;
		$this->db = $base_object->db;
		$this->base_object = $base_object;
	} // end __construct

	function recommendation($rec_data) {

		$recommendation_data['autoshow'] = true;

		if (strtolower($rec_data['captcha']) != strtolower($_SESSION['recommendation']) || !$rec_data['captcha']) {
			$rec_return_msg = 'Bitte geben Sie das Spamschutz-Wort ein (Captcha)';
		}

		if (strlen($rec_data['r_name']) < 5 || strlen($rec_data['s_name']) < 5) {
			$rec_return_msg = 'Bitte geben Sie vollst&auml;ndige Namen des Senders und Empf&auml;ngers ein';
		}

		$ready_r_mail = mail_tools::checkmail($rec_data['r_mail']);
		$ready_s_mail = mail_tools::checkmail($rec_data['s_mail']);
		if (!$ready_r_mail || !$ready_s_mail) {
			$rec_return_msg = 'Eine der E-Mail Adressen ist ung&uuml;ltig';
		}

		if (!$rec_return_msg) {
			$rec_msg = str_replace(array('http', 'www', "\r", "\n"), '', $rec_data['message']);
			$message = '<b>Guten Tag '.addslashes($rec_data['r_name']).',</b><br><br>'.addslashes($rec_data['s_name']).' ('.$ready_s_mail.') m&ouml;chte Ihnen folgende Seite empfehlen: <a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'">'.addslashes($rec_data['title']).'</a><br><br>Folgende Nachricht hat '.addslashes($rec_data['s_name']).' f&uuml;r Sie hinterlassen:<br>'.addslashes($rec_msg).'<br><br>';

			mail_tools::send_mail('Eine Empfehlung', $message, $ready_r_mail, 'extern', false, $ready_s_mail, $ready_s_mail, $rec_data['s_name']);

			$recommendation_data['ok_msg'] = 'Vielen Dank f&uuml;r Ihre Empfehlung!';
		} else {
			foreach ($rec_data as $key => $val) {
				$recommendation_data[$key] = $val;
			}

			$recommendation_data['error_msg'] = $rec_return_msg;
		}

		return $recommendation_data;
	} // end recommendation

	function get_menue() {
		return;
	} // end get_menue

	// metatags aus der db
	// build metatags from ...
	function metatags() {
		return;
	} // end metatags

	function getTagClouds($home = false) {
		$tags = array();
		if (!$home) {
			$sql_limit = 18;
			$fontsize = 10;
			$em = 0.35;
			$opacity = 1;
			$op = 0.04;
		} else {
			$sql_limit = 20;
			$fontsize = 10;
			$em = 0.30;
			$opacity = 1;
			$op = 0.02;
		}

		$sql = "
			SELECT
				word, wgruppe, link, rate, shortURI
			FROM
				search_words
			WHERE
				aktiv = 1
			ORDER BY
				rate
			DESC
			LIMIT
				0,".$sql_limit."
		";
		$rs = mysql_query($sql);

		while ($T = mysql_fetch_object($rs)) {

			$fontsize = $fontsize - $em;
			$opacity = $opacity - $op;

			if ($T->wgruppe) {
				$tags[$T->word] = '<a href="/'.$T->wgruppe.'/" class="tag_cloud" style="font-size:'.$fontsize.'em;opacity:'.$opacity.';">'.$T->word.'</a>';
			} else if ($T->shortURI != 'NULL' && strlen($T->shortURI) >= 1) {
				$tags[$T->word] = '<a href="/'.$T->shortURI.'.html" class="tag_cloud" style="font-size:'.$fontsize.'em;opacity:'.$opacity.';">'.$T->word.'</a>';
			} else if ($T->link) {
				$tags[$T->word] = '<a href="'.$T->link.'" target="_blank" class="tag_cloud" style="font-size:'.$fontsize.'em;opacity:'.$opacity.';">'.$T->word.'</a>';
			} else {
				$tags[$T->word] = '<a href="#" class="tag_cloud" style="font-size:'.$fontsize.'em;opacity:'.$opacity.';" onClick="searchProducts(\''.$T->word.'\');">'.$T->word.'</a>';
			}
		}

		if (is_array($tags)) {
			ksort($tags, SORT_STRING);
		}

		return $tags;
	} // end getTagClouds

	// xml sitemap is google sitemap put it here

	// html sitemap for own use


	// main widget modules

	function main_widget_menu() {
		// TODO: muss noch dynamisch werden!!!
		$menu_id = 0;
		$selected_ids = array();
		if ($_GET['menu_id']) {
			$selected_ids[] = $_GET['menu_id'];
			$menu_id = $_GET['menu_id'];
		}
		if ($_GET['parent_id']) { 
			$selected_ids[] = $_GET['parent_id'];
			$menu_id = $_GET['parent_id'];
		}

		$structure_object = new Structure($this->base_object);
		$data = $structure_object->get_all_tree($menu_id, $selected_ids, $this->base_object->customer_group->id);

		return array('menu_items' => $data);
	}

	function main_widget_menu_2() {
		$structure_object = new Structure($this->base_object);
		$selected_ids = array();
		if (isset($_GET['menu_id'])) {
			$selected_ids[] = $_GET['menu_id'];
		}
		if (isset($_GET['parent_id'])) {
			$selected_ids[] = $_GET['parent_id'];
		}
		$data = $structure_object->get_all_tree(5, $selected_ids, $this->base_object->customer_group->id);
		return array('menu_items' => $data);
	}

	function main_widget_menu_3($config = false) {
		// get structure data
		$structure_object = new Structure($this->base_object);
		$data = $structure_object->get_all_children_flat(3, 1, 3);
		$return_data = array('menu_items' => $data);

		$count = 0;
		foreach ($data as $m_obj) {
			if ($m_obj->parent_id == 3) {
				$count++;
			}
		}

		if (is_array($config) && isset($config['fullWidth']) && $config['fullWidth'] === true) {
			$return_data['menu_item_width'] = (int)(992 / $count);
			$return_data['menu_first_item_width'] = $return_data['menu_item_width'] + (992 % $count);
		} else {
			$return_data['menu_item_width'] = (int)((992 - ($count * 2)) / $count);
			$return_data['menu_first_item_width'] = $return_data['menu_item_width'] + (992 - ($count * 2)) % $count;
		}

		// get allgemein menu
		$selected_ids = array();
		if (isset($_GET['menu_id'])) {
			$selected_ids[] = $_GET['menu_id'];
		}
		if (isset($_GET['parent_id'])) {
			$selected_ids[] = $_GET['parent_id'];
		}
		$return_data['menu_items_allgemein'] = $structure_object->get_all_tree(5, $selected_ids, $this->base_object->customer_group->id);

		// get manufacturer by structure
		foreach ($return_data['menu_items'] as $obj) {
			if ($obj->parent_id == 3) {
				$return_data['manufacturers'][$obj->id] = Manufacturer::get_manufacturers_by_structure_id($obj->id);
			}
		}

		return $return_data;
	}

	function main_widget_menu_advanced($config_override = false) {
		$config = array(
			'parentId' => 3,
			'depth' => 3,
			'splitInParentGroups' => false,
			'width' => 992
		);
		$config = array_merge($config, $config_override);

		// get structure data
		$structure_object = new Structure($this->base_object);
		$data = $structure_object->get_all_children_flat($config['parentId'], 1, $config['depth'], $this->base_object->customer_group->id);

		$count = 0;
		$counts = array();
		$menuItemsGrouped = array();

		foreach ($data as $m_obj) {
			if ($m_obj->parent_id == $config['parentId']) {
				++$count;
			}

			if (!isset($counts[$m_obj->parent_id])) { $counts[$m_obj->parent_id] = 0; }
			++$counts[$m_obj->parent_id];

			if ($config['splitInParentGroups']) {
				$menuItemsGrouped[$m_obj->parent_id][] = $m_obj;
			}
		}

		if ($config['splitInParentGroups']) {
			$menuItems = $menuItemsGrouped;
		} else {
			$menuItems = $data;
		}

		$return_data = array(
			'menu_items' => $menuItems,
			'counts' => $counts
		);

		if (is_array($config) && isset($config['fullWidth']) && $config['fullWidth']) {
			$return_data['menu_item_width'] = (int)($config['width'] / $count);
			$return_data['menu_first_item_width'] = $return_data['menu_item_width'] + ($config['width'] % $count);
		} else {
			$return_data['menu_item_width'] = (int)(($config['width'] - ($count * 2)) / $count);
			$return_data['menu_first_item_width'] = $return_data['menu_item_width'] + ($config['width'] - ($count * 2)) % $count;
		}

		//echo '<pre>';print_r($return_data);

		return $return_data;
	}


	function main_widget_footer_menu() {
		$structure_object = new Structure($this->base_object);
		$selected_ids = array();

		$data = $structure_object->get_all_tree(7, $selected_ids, $this->base_object->customer_group->id);

		return array('menu_items' => $data);
	}

	function main_widget_shopping_groups() {

		$structure_object = new Structure($this->base_object);

		$selected_ids = $_GET['parent_ids'];
		/*$selected_children_ids = array_reverse($selected_ids);
		$level = count($selected_ids);
		$children = $structure_object->get_children($selected_children_ids[1], $level, $selected_ids);*/

		$item_group_items = $structure_object->get_progressive_tree($selected_ids);
		/*$menu_items = $structure_object->get_all_tree(3, $selected_ids);		// Not in use, because only the active-items are returned
		foreach ($item_group_items as $item) {
			if ($item->id == $selected_children_ids[1]) {
				$new_items[] = $item;

				foreach ($children as $child) {
					$new_items[] = $child;
				}

			} else {
				$new_items[] = $item;
			}
		}*/

		$data = array(
			'item_group_items' => $item_group_items
		);
		return $data;
	}

	function main_widget_item() {
		return array();
	}

	function main_widget_login() {
		return array();
	}

	function main_widget_search() {
		$data = array();
		$structure_object = new Structure($this->base_object);
		$data['item_categories'] = $structure_object->get_all_tree(3);
		return $data;
	}

	function main_widget_tagcloud() {
		return array();
	}

	function main_widget_text() {
		return array();
	}

	function main_widget_opinion() {
		return array();
	}

	function main_widget_newsletter() {
		return array();
	}

	function main_widget_item_memory() {
		$memory_object = new Memory();
		$items_in_memory = $memory_object->count();

		return array('memoryitems' => $items_in_memory);
	}

	function main_widget_shoppingcart() {
		$shoppingcart_object = new Shoppingcart($this->base_object);
		$data = $shoppingcart_object->get_overview();

		// get item data
		$data['items'] =  $shoppingcart_object->get_data();

		return array('shoppingcart' => $data);
	}

	function main_widget_manufacturer() {
		$manufacturer_object = new Manufacturer($this->base_object);
		$data = $manufacturer_object->get_active();
		return array('manufacturers' => $data);
	}

	function main_widget_language() {
		// get languages
		include_once './core/language.class.php';
		$language_object = new Language($this->base_object);
		$languages = $language_object->get_all_active();
		return array('languages' => $languages);
	}

	function main_widget_item_categories() {
		$structure_object = new Structure($this->base_object);
		$selected_ids = $_GET['parent_ids'];
		$data = $structure_object->get_all_tree(3, $selected_ids, $this->base_object->customer_group->id);
		$this->activated_structures = $structure_object->activated_structures;
		return array('menu_items' => $data);
	}

	// holzeisstock-theme
	function main_widget_item_categories_dropdown() {
		$structure_object = new Structure($this->base_object);
		$data = $structure_object->get_all_children_flat(3, 1, 3, $this->base_object->customer_group->id);

		return array('menu_items' => $data);
	}


	function main_widget_navigation() {
		$structure_object = new Structure($this->base_object);
		$selected_ids = array();
		if ($_GET['menu_id']) {
			$selected_ids[] = $_GET['menu_id'];
		}
		if ($_GET['parent_id']) {
			$selected_ids[] = $_GET['parent_id'];
		}
		$data = $structure_object->get_all_tree(5, $selected_ids);

		return array('menu_items' => $data);
	}

	function main_widget_info() {
		return array();
	}

	function main_widget_contact() {
		return array();
	}

	function main_widget_customer_opinion() {
		return array();
	}

	function main_widget_header_images() {
		$theme_images = 'web/'.SHOP_SYSTEM.'/themes/'.$this->base_object->config->shopConfiguration['THEME'].'/media/images/header/';
		$structure_id = $this->base_object->structure_id;
		$images = false;

		if ($structure_id) {
			$images = array();

			if (is_dir(ROOT_DIR.$theme_images.$structure_id)) {
				$handle = opendir(ROOT_DIR.$theme_images.$structure_id);
				$directory = $theme_images.$structure_id;
			} else {
				$handle = opendir(ROOT_DIR.$theme_images.'default');
				$directory = $theme_images.'default';
			}

			while (false !== ($file = readdir($handle))) {
				if ($file != '.' && $file != '..' && is_file(ROOT_DIR.$directory.'/'.$file)) {
					$images[] = array(
						'imagefile' => '/'.$directory.'/'.$file,
						'animation' => 'RANDOM',
						'direction' => 'RANDOM',
						'speed' => 2500,
						'pause' => 2500
					);
				}
			}

			closedir($handle);

			if ($images[0]) {
				shuffle($images);
				$images = json_encode($images);
			}
		}

		return array('header_animated_images' => $images);
	}

	function main_widget_logorodeo() {
		return array();
	}

	function main_widget_footercontact($config = array()) {
		$return = array(
			'config' => $config
		);

		return $return;
	}

}

?>