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

class ItemHelper { 

	//=========================================
	//
	//	THIS CLASS IS STATIC ONLY
	//
	//=========================================
	private function __construct() {}

	private function __clone() {}

	//=========================================
	//
	//	THE PUBLIC INTERFACE
	//
	//=========================================

	public static function get_number_of_items_by_manufacturer($id, $active_only = false) {
		$db = Registry::get('base')->db;

		$sql = "SELECT
					COUNT(id) AS num
				FROM
					items
				WHERE
					manufacturer_id=".$db->real_escape_string($id);

		// TODO: active is a hell of a lot more complicated with availability now, active still exists but nobody uses it
		/*if ($active_only) {
			$sql .= " AND
					active=1";
		}*/

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			return $result->fetch_object()->num;
		}

		return false;
	}

	// TODO: SQL QUERY IN LOOP OVER ITEM OBJECT
	// TO FIX THIS ITEM CLASS NEEDS TO BE REDESIGNED
	public static function get_best_selling_items($num = 3, $structure_id = 0) {
		$items = ItemHelper::get_best_selling_items_objects($num, $structure_id);
		$item_object = new Item(Registry::get('base'));

		foreach ($items as $item) {
			$item_object->set_id($item->id);
			$return_data[] = $item_object->get_data();
		}

		return $return_data;
	}

	public static function get_item_attributes($item, $with_variants = false) {
		$item_schema_object = new ItemSchema(Registry::get('base'));
		$schema_data = $item_schema_object->get_data($item->schema_id, true);

		$return_data = array();

		foreach ($schema_data->attributes as $value) {
			if ($value->name != '') {
				$attribute_value = $item->{"attribute_".$value->number};
				$tmp = new stdClass();

				if ($value->number != $schema_data->selectable_attribute_1 && $value->number != $schema_data->selectable_attribute_2) {
					if ($attribute_value != '') {
						$tmp->name = $value->name;
						$tmp->value = $attribute_value;
						$return_data[] = $tmp;
					}
				} else {
					if ($value->name != '') {
						$tmp->name = $value->name;
						if ($value->number == $schema_data->selectable_attribute_1) {
							$tmp->value = $item->variant1;
							$tmp->variant = 1;
						} else if ($value->number == $schema_data->selectable_attribute_2) {
							$tmp->value = $item->variant2;
							$tmp->variant = 2;
						}

						$return_data[] = $tmp;
					}
				}
			}
		}

		return $return_data;
	} // end get_item_attributes

	public static function get_best_selling_items_objects($num, $structure_id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT
					oi.item_id AS id,
					COUNT(oi.id) AS num
				FROM
					order_item oi
				RIGHT JOIN
					items i
				ON
					i.id = oi.item_id
				WHERE
					oi.item_id is not null
				AND
					i.active=1
				AND
					i.structure_id > 0";

		if ($structure_id > 0) {
			$sql .= "
				AND (
					i.structure_id = ".$db->real_escape_string($structure_id)." 
				OR
					i.structure_id_2 = ".$db->real_escape_string($structure_id)."
				OR
					i.structure_id_3 = ".$db->real_escape_string($structure_id)."
				OR
					i.structure_id_4 = ".$db->real_escape_string($structure_id)."
				OR
					i.structure_id_5 = ".$db->real_escape_string($structure_id)."
				OR
					i.structure_id_6 = ".$db->real_escape_string($structure_id)."
				OR
					i.structure_id_7 = ".$db->real_escape_string($structure_id)."
				OR
					i.structure_id_8 = ".$db->real_escape_string($structure_id)."
				OR
					i.structure_id_9 = ".$db->real_escape_string($structure_id)."
				OR
					i.structure_id_10 = ".$db->real_escape_string($structure_id)."

				)
			";
		}

		$sql .= "
				GROUP BY oi.item_id
				ORDER BY num DESC
				LIMIT 0, ".$db->real_escape_string($num);

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				$return_data[] = $obj;
			}

			return $return_data;
		}

		return false;
	}

	// TODO: where is that default taxrate
	public static function get_item_taxrate($item_id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT
					t.steuersatz
				FROM
					tax t
				RIGHT JOIN
					items i
				ON
					i.tax_id = t.id
				WHERE
					i.id=".$db->real_escape_string($item_id);

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			return $result->fetch_object()->steuersatz;
		}

		return false;
	}

	public static function get_item_variant_prices($item_id, $group_id, $taxrate = 19) {
		$item_object = new Item(Registry::get('base'));
		$item_object->set_id($item_id);

		$data = $item_object->getVariantPricesForCustomerGroup($group_id, false, false, $taxrate);

		return $data;
	}

	public static function get_item_variant_image($item_id, $attribute_1, $attribute_2) {
		$db = Registry::get('base')->db;

		$sql = "SELECT
					*
				FROM
					item_files
				WHERE
					item_id='".$db->real_escape_string($item_id)."'
				AND
					attribut1='".$db->real_escape_string($attribute_1)."'
				AND
					attribut2='".$db->real_escape_string($attribute_2)."'
				LIMIT
					1";

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			return $result->fetch_object()->file_name;
		}

		return false;
	}

	public static function get_additional_item_images($item_id) {
		$db = Registry::get('base')->db;

		$sql = "
			SELECT
				file_name
			FROM
				item_files
			WHERE
				item_id = '".$db->real_escape_string($item_id)."'
			AND
				type = 0
			ORDER BY
				rang
		";

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			$images = array();
			$pos = 0;

			while ($I = $result->fetch_object()) {
				$images[++$pos] = $I->file_name;
			}

			return $images;
		}

		return false;
	}
}
