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

	public $list_table_config = array (
		'title' => 'Artikelattribute',
		'db_table' => 'item_schema',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Bezeichnung',
				'sortable' => 1
			),
			array(
				'db_field' => 'description',
				'name' => 'Beschreibung',
				'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_item_schema_editor&id=',
		'toolbar' => array(
				'delete' => '1',
				'new' => 'index.php?admin_modul=admin_item_schema_editor',
				'copy' => 0,
				'select_all' => 1,
				'edit' => 0,
				'actions' => 0,
				'filter' => 0,
				'search' => 1
		)
	);

	protected $base_object;
	protected $db;

	// db fields
	private $fields = array(
	);

	// generic
	private $id;
	private $error;

	public 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 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, $with_attributes = false) {
		$sql = "SELECT * FROM item_schema";

		if ($filter) {
			if ($filter == ' ') {
				$sql .= " ";
			} 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;
			}

			if ($with_attributes) {
				$sql = "SELECT * FROM item_schema_attributes";

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

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

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

		return false;
	} // end get_all

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

		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($this->id);
		} else {
			$this->error = 'no id';

			return false;
		}

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

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

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

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

				while ($obj = $result->fetch_object()) {
					if ($obj->name != '') {
						$attributes[] = $obj;
					}
				}

				$structure->attributes = $attributes;

				return $structure;
			}

			return $result->fetch_object();
		}
		$this->error = 'no result';

		return false;
	} // end get_data

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

		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) {
		echo "implement create in itemschema"; exit();
	} // end create

	public function update($data, $id = false) {
		echo "implement update in itemschema"; exit();
	} // 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 item_schema 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 item_schema";

		$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 item_schema";

		$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

	public function delete_by_id($id) {
		$sql = "DELETE FROM item_schema_attributes
				WHERE schema_id=".$id;
		$this->db->query($sql);
		$sql = "DELETE FROM item_schema
				WHERE id=".$id;
		$this->db->query($sql);
		return;
	}
}