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

#[\AllowDynamicProperties]
class CustomerGroups {

	private $table_fields = array(
		'name' => 'text',
		'description' => 'text',
		'show_tax' => 'integer',
		'price_tax_text' => 'text',
		'state_after_registration' => 'integer',
		'adaptPricesFromId' => 'integer'
	);

	private $db;
	private $id;
	private $user_id;
	private $error;

	public function __construct($base_object = false, $id = false) {
		if ($base_object) {
			$this->db = $base_object->db;
		} else {
			$this->db = Registry::get('base')->db;
		}
		$this->id = $id;
		$this->user_id = false;
		$this->structure_id = 0;
		$this->error = '';
	} // end cunstructor

	public function get_id() {
		return id;
	} // end get_id

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

	public function get_user_id() {
		return $this->user_id;
	} // end get_user_id

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

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

	function get_all_names() {
		$data = array();
		$result = $this->db->query('SELECT id, name FROM customer_groups');
		if ($result) {
			while ($obj = $result->fetch_object()) {
				$data[$obj->id] = $obj->name;
			}
		}
		return $data;
	}

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

		if ($filter) {
			if ($filter == 'registrable') {
				$sql .= " WHERE registrable = 1";
			} else if ($filter == 'default') { 
				$sql .= " WHERE is_default = 1";
			} else {
				$this->error = 'no valid filter';

				return false;
			}
		}

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

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

			return $return;
		}

		return false;
	}

	public function get_data($id = false) {
		$sql = "SELECT * FROM customer_groups";

		if ($id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($id);
			$this->id = $id;
		} else if ($this->id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($id);
		} else {
			$this->error = 'no id';

			return false;
		}

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

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}
		$this->error = 'no result';

		return false;
	} // end get_data

	public function get_by_user_id($user_id = false) {
		$sql = "SELECT cg.* FROM customers c JOIN customer_groups cg ON cg.id = c.group_id";

		if ($user_id) {
			$sql .= " WHERE c.id=".$this->db->real_escape_string($user_id);
			$this->user_id = $user_id;
		} else if ($this->user_id) {
			$sql .= " WHERE c.id=".$this->db->real_escape_string($this->user_id);
		} else {
			$this->error = "no user id";

			return false;
		}

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

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}
		$this->error = "no result";

		return false;
	} // end get_by_user_id

	public function create($data) {
		if ($data) {
			$sql = "INSERT INTO customer_groups 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() {
		if ($data) {
			$sql = "UPDATE customer_groups 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 delete() {
		$sql = "DELETE FROM customer_groups WHERE  isLocked = 0 AND id=".$this->id;

		$this->db->query($sql);
		return;
	} // end delete

	public function get_all_paginated($items, $page, $order = false) {
		$page = ($page - 1) * $items;

		$sql = "SELECT * FROM customer_groups ORDER BY isLocked DESC, name LIMIT $items OFFSET $page";

		$result = $this->db->query($sql);
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}

		return $data;
	} // end get_all_paginated

	public function get_number_of_pages($items) {
		$sql = "SELECT COUNT(id) FROM customer_groups";

		return get_number_of_pages($items, $sql);
	} // end get_number_of_pages

	public function get_pagination_array($items, $page) {
		$sql = "SELECT COUNT(id) FROM customer_groups";

		return get_pagination_array($items, $page, $sql);
	} // end get_pagination_array

	// DEPRECATED
	public function get_by_user($id = false) {
		if ($id) {
			$sql = "SELECT cg.* FROM customers c JOIN customer_groups cg ON cg.id = c.group_id
					WHERE c.id=".$this->db->real_escape_string($id);

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

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

		return false;
	}

	public function get($id = false) {
		if ($id) {
			$sql = "SELECT * FROM customer_groups WHERE id=".$this->db->real_escape_string($id);

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

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

		return false;
	}
}

?>