<?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
 */

class Search { 

	protected function __construct() {}

	private function __clone() {}

	public static function customers($search) {
		$db = Registry::get('base')->db;
		$search = $db->real_escape_string($search);
		$sql = "SELECT *
				FROM customers
				WHERE CONCAT(firstname, surname, company, number, email)
				LIKE '%$search%'
				LIMIT 20";

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

		$hits = $result->num_rows;

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

			return array('hits' => $hits, 'results' => $return);
		}

		return false;
	}

	public static function orders($search) {
		$db = Registry::get('base')->db;
		$search = $db->real_escape_string($search);
		$sql = "SELECT * FROM orders 
				WHERE CONCAT_WS('-', order_number, order_revision, customer_name, paypal_transaction_id, paypal_email, paypal_payerid, invoice_number)
				LIKE '%$search%'
				AND order_status != 9
				AND order_status != 10
				AND order_addendum != 'N'
				LIMIT 20";

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

		$hits = $result->num_rows;

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

			return array('hits' => $hits, 'results' => $return);
		}

		return false;
	}

	public static function items($search, $active=0) {
		$db = Registry::get('base')->db;

		$search = $db->real_escape_string($search);
		$sql = "SELECT * FROM items WHERE ";
		if ($active) {
			$sql .= " active=1 AND ";
		}

		// for intelectra
		$sql .= " CONCAT_WS(' ', number, attribute_7, name, short_description, vendor_item_number, ean_code, seo_keywords)
				LIKE '%$search%'
				LIMIT 20";
		// --------------------------
		/*
		$sql .= " CONCAT_WS(' ', number, name, short_description, vendor_item_number, ean_code, seo_keywords)
				LIKE '%$search%'
				LIMIT 20";
				*/
		/*$sql .= " number LIKE '%$search%' OR
					name LIKE '%$search%' OR
					short_description LIKE '%$search%' OR
					vendor_item_number LIKE '%$search%' OR
					ean_code LIKE '%$search%' OR
					seo_keywords LIKE '%$search%' LIMIT 20";*/

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

		$hits = $result->num_rows;

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

			return array('hits' => $hits, 'results' => $return);
		}

		return false;
	}

	public static function category_items($search, $category_id, $active=0) {
		$db = Registry::get('base')->db;
		$search = $db->real_escape_string($search);
		$sql = "SELECT * FROM items WHERE";
		if ($active) {
			$sql .= " active=1 AND ";
		}
		$category_id = $db->real_escape_string($category_id);
		$sql .= " CONCAT(number, name, short_description, vendor_item_number, ean_code)
				LIKE '%$search%' AND
				(
					structure_id = $category_id OR
					structure_id_2 = $category_id OR
					structure_id_3 = $category_id OR
					structure_id_4 = $category_id OR
					structure_id_5 = $category_id OR
					structure_id_6 = $category_id OR
					structure_id_7 = $category_id OR
					structure_id_8 = $category_id OR
					structure_id_9 = $category_id
				)
				LIMIT 20";
		$base_object = Registry::get('base');
		$result = $db->query($sql);

		$hits = $result->num_rows;

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

			return array('hits' => $hits, 'results' => $return);
		}

		return false;
	}

	public static function manufacturers($search) {
		$db = Registry::get('base')->db;
		$search = $db->real_escape_string($search);
		$sql = "SELECT * FROM manufacturers WHERE CONCAT(name, address, city) LIKE '%$search%' LIMIT 20";

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

		$hits = $result->num_rows;

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

			return array('hits' => $hits, 'results' => $return);
		}

		return false;
	}

}