<?php
/**
* @version      $Id: list_and_edit.php
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       John T. Daly <jtd@ta-edv.de>
* Easyshop is a web shop system
*/

class ListAndEdit {
	private $layout_object;
	private $base_object;
	
	function __construct($base_object, $layout_object) {
		$this->layout_object = $layout_object;
		$this->base_object = $base_object;
	}
	
	function create() {
		$temp = $this->get_POST();
		//var_dump($temp);
		$temp = $this->core_class_obj->set_row($this->get_POST());
		// let set_row return the id and load page with read?
	} // end create
	
	function read($var_name) {
		$temp = $this->get_GET();
		$data = $this->core_class_obj->get_by_id($temp['id']);
		$this->layout_object->assign($var_name, $data);
	} // end read
	
	function update($fields) {
		$data = $this->get_POST();
		$id = $this->get_id();
		if(isset($data['id'])) {
			$data = $this->strip_id($data);
		}
		$data = $this->make_booleans($data, $fields);
		//var_dump($data);
		$temp = $this->core_class_obj->set_by_id($data, $id);
		//var_dump($temp);
	} // end update

	function delete() {
		$data = $this->get_GET();
		$id = $data['id'];
		$this->core_class_obj->delete_by_id($id);
	} // end delete
	
	/**
	 * Get all vars given in POST, removes module name, submit from submit button
	 * and empty values.
	 */
	private function get_POST() {
		$data = $_POST;
		if(isset($data['modul'])) {
			unset($data['modul']);
		}
		
		if(isset($data['submit'])) {
			unset($data['submit']);
		}
		
		foreach($data as $key => $value) {
			if($data[$key] == ""){
				unset($data[$key]);
			}
		}
		
		return $data;
	} // end get_POST
	
	private function get_GET() {
		 return $_GET;
	} // end get_GET
	
	private function strip_id($data) {
		if(isset($data['id'])) {
			unset($data['id']);
			
			return $data;
		}
		
		return $data;
	} // end strip_id
	
	protected function get_id() {
		if(isset($_GET['id'])) return $_GET['id'];
		if(isset($_POST['id'])) return $_POST['id'];
		
		return false;
	} // end get_id

	protected function get_submitted() {
	    if(isset($_POST['submit'])) {
		return true;
	    }

	    return false;
	}
	
	private function make_booleans($data, $fields) {
		//var_dump($fields);
		//var_dump('<br />');
		foreach($fields as $field) {
			//var_dump($data[$field]);
			if(isset($data[$field])) {
				$data[$field] = 1;
			} else {
				$data[$field] = 0;
			}
			//var_dump($data[$field]);
			//var_dump('<br />');
		}
		
		return $data;
	} // end make_booleans
	
	function get_items() {
		if(isset($_GET['items']) && is_numeric($_GET['items'])){
			$items = (int)$_GET['items'];
			$this->base_object->customer->set_config_item('pagination', $items);
		} else {
			$items = $this->base_object->customer->get_config_item('pagination');
			if(is_numeric($items)) {
				$items = (int)$items;
			} else {
				$items = 10;
			}
		}
		
		return $items;
	} // end get_items
	
	function get_page() {
		if(isset($_GET['page']) && is_numeric($_GET['page'])){
			$page = (int)$_GET['page'];
		} else {
			$page = 1;
		}
		
		return $page;
	} // end get_page
	
	function get_tab() {
		if(isset($_GET['tab'])) {
			$tab = $_GET['tab'];
		} else {
			$tab = 'all';
		}
		
		return $tab;
	}
} // end ListAndEdit
