<?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/config.class.php';

// TODO: rewrite this to be a normal class with 4 special static stati
class OrderStatus {

	// db fields
	private $table_fields = array(
		'name' => 'text',
		'type' => 'text',
		'description' => 'text',
		'selector' => 'text',
		'show' => 'show'
	);

	// generic
	private $base_object;
	private $db;
	private $id;
	private $error;

	function __construct($base_object, $id = false) {
		$this->base_object = $base_object;
		$this->db = $base_object->db;
		$this->id = $id;
		$this->error = '';
	} // end constructor

	public function get_id() {
		return $this->id;
	}

	public function set_id($id) {
		$this->id = $id;
	} // end set_id

	public function get_error() {
		return $this->error;
	} // end get_error

	public static function get_all($filter = false) {
		$db = Registry::get('base')->db;

		$sql = "SELECT * FROM order_status";

		if ($filter) {
			if ($filter == 'showable') {
				$sql .= " WHERE show=1";
			} else {
				return false;
			}
		}

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

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

			return $return;
		}

		return false;
	} // end get_all

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

		$sql = "SELECT *
		FROM order_status WHERE selector != 'revision'";

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

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

			return $return;
		}

		return false;
	} // end get_all

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

		$sql = "SELECT name FROM order_status WHERE id=".$db->real_escape_string($id);

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

		if ($result->num_rows > 0) {
			$obj = $result->fetch_object();

			return $obj->name;
		}

		return false;
	} // end get_name

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

		$sql = "SELECT selector FROM order_status WHERE id=".$db->real_escape_string($id);

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

		if ($result->num_rows > 0) {
			$obj = $result->fetch_object();

			return $obj->selector;
		}

		return false;
	} // end get_selector

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

		$sql = "SELECT * FROM order_status WHERE id=".$db->real_escape_string($id);

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

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

		return false;
	} // end get_data

	public static function cancel() {
		//if (Config::has_key('order_status_cancel_default')) {
		//	return Config::get_value('order_status_cancel_default');
		//}

		return 6;
	}

	public static function is_cancel($status) {
		return $status == OrderStatus::cancel();
	}

	public static function undefined() {
		//if (Config::has_key('order_status_undefined_default')) {
		//	return Config::get_value('order_status_undefined_default');
		//}

		return 8;
	}

	public static function is_undefined($status) {
		return $status == OrderStatus::undefined();
	}

	public static function finished() {
		//if (Config::has_key('order_status_finished_default')) {
		//	return Config::get_value('order_status_finished_default');
		//}

		return 7;
	}

	public static function is_finished($status) {
		return $status == OrderStatus::finished();
	}

	public static function open() {
		//if (Config::has_key('order_status_open_default')) {
		//	return Config::get_value('order_status_open_default');
		//}

		return 1;
	}

	public static function is_open($status) {
		return $status == OrderStatus::open();
	}

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

		$sql = "SELECT id, name FROM order_status";

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

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

			return $return;
		}

		return false;
	}
}