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

class Country extends Main {

	protected $db;
	protected $base_object;

	public $list_table_config = array (
		'title' => 'Länder',
		'db_table' => 'countries',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Land',
				'sortable' => 1
			),
			array(	'db_field' => 'iso_code_2',
				'name' => 'ISO 3166 ALPHA-2',
				'sortable' => 1
			),
			array(	'db_field' => 'iso_code_3',
				'name' => 'ISO 3166 ALPHA-3',
				'sortable' => 1
			),
			array(
				'db_field' => 'shipping_area_id',
				'name' => 'Lieferzone',
				'rewrite_function' => 'shipping_areas',
				'sortable' => 1
			)
		),
		'search_fields' => array('name', 'iso_code_2'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_country_editor&id=',
		'toolbar' => array(
			'delete' => 1,
			'new' => 'index.php?admin_modul=admin_country_editor',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		)
	);


	private $object_fields = array(
		'name' => 'text',
		'shipping_area_id' => 'integer',
		'rang' => 'integer',
		'iso_code_2' => 'text',
		'iso_code_3' => 'text',
		'customer_ust_id_required' => 'integer'
	);

	public $id;

	function __construct($base_object) {

		$this->base_object = $base_object;
		$this->db = $base_object->db;
		$this->id = false;
	} // end __construct

	function shipping_areas() {
		include_once './core/shipping_area.class.php';
		$shipping_area_object = New Shipping_area($this->base_object);
		return $shipping_area_object->get_all_names();
	}

	public function get_all($filter = false) {
		$sql = "SELECT * FROM countries";

		if (isset($filter['isset_shipping_area']) && $filter['isset_shipping_area'] == 1) {
			$sql .= ' WHERE shipping_area_id > 0';
		}
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = $obj;
		}

		return $data;
	} // end get_all

	public function get_all_names() {
		$sql = "SELECT id, name FROM countries";
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = $obj->name;
		}

		return $data;
	} // end get_all

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

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

	public function get_data() {
		if ($this->id) {
			$sql = "SELECT * 
					FROM countries 
					WHERE id = ".$this->id;

			$result = $this->db->query($sql);
			$obj = $result->fetch_object();
			if ($obj) {
				$obj->customer_group_shipping_area = array();
				$sql = "SELECT * 
						FROM country_customergroup_shippingarea 
						WHERE country_id = ".$this->id;
				$result = $this->db->query($sql);
				while ($obj2 = $result->fetch_object()) {
					$obj->customer_group_shipping_area[$obj2->customer_group_id] = $obj2->shipping_area_id;
				}
				return $obj;
			}
		}

		return false;
	} // end get_data

	public function delete() {
		$sql = "DELETE FROM countries 
				WHERE id=".$this->id;

		$this->db->query($sql);

		return;
	}// end delete

	public function data_filter($request) {
		$data = array();

		foreach ($this->object_fields as $field_name => $var_type) {
			if (isset($request[$field_name])) {
				if ($var_type == 'text') {
					$data[$field_name] = $request[$field_name];
				} else {
					$data[$field_name] = (int)$request[$field_name];
				}
			}
		}

		return $data;
	} // end data_filter

	public function create($data) {
		if ($data) {
			$sql = "INSERT INTO countries
					SET ";

			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);
				if ($this->object_fields[$var_name] == 'integer') {
					$sql .= $var_name.' = '.$value.', ';
				} else {
					$sql .= $var_name.' = "'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$this->db->query($sql);
			$this->id = $this->db->insert_id;
		}

		return;
	} // end create

	public function update($data) {
		if ($data) {
			$sql = "UPDATE countries
					SET ";

			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);
				if ($this->object_fields[$var_name] == 'integer') {
					$sql .= $var_name.'='.$value.', ';
				} else {
					$sql .= $var_name.'="'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$sql .= ' WHERE id='.$this->id;
			$this->db->query($sql);
		}

		return;
	} // end update

	public function save_customer_group_shipping_areas($country_id, $data) {
		// delete old discounts
		$sql = "DELETE FROM country_customergroup_shippingarea WHERE country_id=".$this->db->real_escape_string($country_id);
		$result = $this->db->query($sql);

		if ($data) {
			// set new shipping areas
			$sql = "
				INSERT INTO country_customergroup_shippingarea (
					country_id,
					shipping_area_id,
					customer_group_id
				) VALUES 
			";
			foreach ($data as $customer_group_id => $shipping_area_id) {
				$sql .= "(
					".$this->db->real_escape_string($country_id).",
					".$this->db->real_escape_string($shipping_area_id).",
					".$this->db->real_escape_string($customer_group_id)."), ";
			}
			$sql = substr($sql, 0, -2);
			$result = $this->db->query($sql);
		}
		return;
	}




	public function delete_by_id($id) {
		$rs = $this->db->query("DELETE FROM countries WHERE id=$id;");
	} // end delete_by_id

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

		$sql = "SELECT
					name
				FROM
					countries
				WHERE id = $id";

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

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

			return $return->name;
		}

		return false;
	} // end get_name_by_id


	public static function getShippingAreaById($countryId, $customer_group_id = false) {
		$db = Registry::get('base')->db;

		$sql = "SELECT shipping_area_id
				FROM countries
				WHERE id = $countryId";

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

		$shipping_area_id = false;
		if ($result->num_rows > 0) {
			$shipping_area_id = $result->fetch_object()->shipping_area_id;
		}
		if ($customer_group_id) {
			$sql = "SELECT * FROM country_customergroup_shippingarea
				WHERE country_id = $countryId AND customer_group_id = $customer_group_id";
			$result = $db->query($sql);
			if ($result->num_rows > 0) {
				$shipping_area_id = $result->fetch_object()->shipping_area_id;
			}
		}

		return $shipping_area_id;
	}

} // end Country

?>