<?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 Gift_certificate extends Main {

	public $list_table_config = array (
		'title' => 'Gutscheine',
		'db_table' => 'gift_certificates',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Bezeichnung',
				'sortable' => 1
			),
			array(
				'db_field' => 'code',
				'name' => 'Freischaltcode',
				'sortable' => 1
			),
			array(
				'db_field' => 'account',
				'name' => 'Wert',
				'sortable' => 1
			),
			array(
				'db_field' => 'available_until',
				'name' => 'G&uuml;ltigkeit',
				'sortable' => 1
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'rewrite_function' => 'state_text',
				'sortable' => 1
			)
		),
		'search_fields' => array('name', 'description'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_gift_certificate_editor&id=',
		'toolbar' => array(
			'delete' => '1',
			'new' => 'index.php?admin_modul=admin_gift_certificate_editor',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		)
	);

	private $object_fields = array(
		'name' => 'text',
		'code' => 'text',
		'description' => 'text',
		'active' => 'integer',
		'available_from' => 'text',
		'available_until' => 'text',
		'customer_valid_type' => 'integer',
		'customer_id' => 'integer',
		'customer_group_id' => 'integer',
		'account' => 'text',
		'account_type' => 'integer',
		'min_order_sum' => 'float',
	);

	protected $base_object;
	protected $db;
	public $id;

	function __construct($base_object) {
		parent::__construct($base_object);
		$this->id = false;
		$this->db = $base_object->db;
		$this->base_object = $base_object;
	} // end __construct

	public function state_text() {
		return array (
			'1' => 'aktiv',
			'0' => 'inaktiv'
		);
	}

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

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

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

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

		return $data;
	} // end get_all

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

		if ($id) {
			$sql .= $this->db->real_escape_string($id);
		} else if ($this->id) {
			$sql .= $this->db->real_escape_string($this->id);
		} else {
			return false;
		}

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

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

		return false;
	} // end get_data

	public function delete() {
		$sql = "DELETE FROM gift_certificates 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 gift_certificates 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 if ($this->object_fields[$var_name] == 'float') {
					$sql .= $var_name.' = '.number_format($value,4,'.','').', ';
				}
				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 gift_certificates 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 get_all_paginated($items , $page, $order = false) {
		$page = ($page - 1) * $items;

		$sql = "SELECT * FROM gift_certificates 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 gift_certificates";

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

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

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

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

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

		$sql = "SELECT name FROM gift_certificates 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

}

?>