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

	public $list_table_config = array (
		'title' => 'Stornierungsgr&uuml;nde',
		'db_table' => 'cancellation_reasons',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Stornierungsgrund',
				'sortable' => 1
			),
			array(
				'db_field' => 'type',
				'name' => 'Initiator',
				'rewrite_function' => 'type_values',
				'sortable' => 1
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'rewrite_function' => 'state_text',
				'sortable' => 1
			)
		),
		'search_fields' => array('name'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=CancellationReasons&object_id=',
		'toolbar' => array(
			'delete' => 1,
			'new' => 'index.php?admin_modul=admin_object_edit&object=CancellationReasons',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Stornierungsgrund',
		'edit_fields' => array (
			array(
				'name' => 'Stammdaten',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'name',
				'name' => 'Stornierungsgrund',
				'type' => 'text'
			),
			array(
				'db_field' => 'type',
				'name' => 'Initiator',
				'values' => 'type_values',
				'type' => 'text'
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'values' => 'state_text',
				'type' => 'int'
			)
		),
		'edit_mandatory_fields' => array('name'),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_list&object=CancellationReasons',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'delete' => 1
		),
	);


	// db fields
	private $fields = array(
		'name' => 'text',
		'type' => 'int',
		'active' => 'int'
	);

	// generic
	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 state_text() {
		return array (
			'1' => 'aktiv',
			'0' => 'inaktiv'
		);
	}

	public function type_values() {
		return array (
			'1' => 'Shop Storno',
			'0' => 'Kunden Storno'
		);
	}



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

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

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

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

		if ($filter) {
			if ($filter == 'customer-active') {
				$sql .= " WHERE type='0' AND active='1'";
			} else if ($filter = 'active') {
				$sql .= " WHERE active='1'";
			} else {
				$this->error = 'unknown filter';

				return false;
			}
		}

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

		if ($result->num_rows > 0) {
			$return_data = array();

			while ($obj = $result->fetch_object()) {
				$return_data[$obj->id] = $obj;
			}

			return $return_data;
		}
		$this->erro = 'no result';

		return false;
	} // end get_all

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

		if ($id) {
			$sql .= "WHERE id=".$this->db->real_escape_string($id);
		} else if ($this->id) {
			$sql .= "WHERE id=".$this->db->real_escape_string($this->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 results';

		return false;
	} // end get_data

	public function delete($id = false) {
		$sql = "DELETE FROM cancellation_reasons ";

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

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

	public function create($data) {
		if ($data) {
			$valid = $this->validate($data);
			$size = count($data);

			if ($valid) {
				$sql = "INSERT INTO cancellation_reasons ";
				$data_line = "";
				$value_line = "";

				$i = 1;
				foreach ($data as $key => $value) {
					$data_line .= $key;
					$value_line .= "'".$this->db->real_escape_string($value)."'";

					if ($i < $size) {
						$data_line .= ", ";
						$value_line .= ", ";
						$i++;
					}
				}

				$sql .= "(".$data_line.") ";
				$sql .= " VALUES (".$value_line.")";

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

				if ($result) {
					return $this->db->insert_id;
				}

				$this->error = "insert failed";

				return false;
			}

			$this->error = "invalid data";

			return false;
		}

		$this->error = "no data";

		return false;
	} // end create

	public function update($data, $id = false) {
		if ($data) {
			$valid = $this->validate($data);
			$size = count($data);

			if ($valid) {
				$sql = "UPDATE cancellation_reasons SET ";

				$i = 1;
				foreach ($data as $key => $value) {
					$sql .= $key."='".$this->db->real_escape_string($value)."'";

					if ($i < $size) {
						$sql .= ", ";
						$i++;
					}
				}

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

					return false;
				}

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

				if ($result) {
					return $id;
				}

				$this->error = 'update failed';

				return false;
			}

			$this->error = "invalid data";

			return false;
		}

		$this->error = "no data";

		return false;
	} // end update

	public function validate($data) {
		// TODO: implement this

		return true;
	} // end data_filter

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

		$sql = "SELECT
					*
				FROM
					cancellation_reasons
				LIMIT $items OFFSET $page";

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

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

			return $return_data;
		}

		return false;
	} // end get_all_paginated

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

		$num = $this->db->query($sql)->num_rows;

		return (int)ceil($num / $items);
	} // end get_number_of_pages

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

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

		$num = $result->num_rows;
		$pages = (int)ceil($num / $items);

		$i = 0;
		while ($i < $pages) {
			$data[] = ++$i;
		}

		if ($pages > 10) {
			if ($page <= 5) {
				$data = array_slice($result, 0, 10);
			} else if ($page > ($pages - 4)) {
				$data = array_slice($result, $pages-10, 10);
			} else {
				$data = array_slice($result, $page -5, 10);
			}
		}

		if ($pages == 1) {
			return;
		}

		return $data;
	} // end get_pagination_array
}