<?php
/**
* @version      $Id: admin_standard_messages.php 
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       Richard Kammermayer <rk@ta-edv.de>
* Easyshop is a web shop system
*/

class admin_object_list  {
	private $base_object;
	private $config;
	private $object_name;
	private $object = false;
	private $action = false;
	private $max_list_items = 50;
	private $sort_direction = 'up';
	private $sort_item = false;
	private $actual_page = 1;
	private $count_pages = 1;
	private $list_filter = false;
	private $search_string = '';
	private $output = false;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
	}

	function run() {

		
		// get vars
		foreach (array('action', 'max_list_items', 'actual_page', 'sort_item', 'sort_direction', 'search_string', 'list_filter', 'output', 'object') as $field) {
			if (isset($_GET[$field])) {
				$this->{$field} = $_GET[$field];
			} elseif (isset($_POST[$field])) {
				$this->{$field} = $_POST[$field];
			}
		}

		// init object
		$this->object_name = $this->object;
		include_once(ROOT_DIR . './core/'.strtolower($this->object_name).'.class.php');
		$this->object = new $this->object_name($this->base_object);

		if ($this->action == 'get_list_items') {
			return $this->get_list_items();
		}
		else {
			return $this->get_initial_object_table();
		}
	}
	
	private function get_list_items() {
		// get row data
		if ($this->output && $this->output == 'csv') {
			$setting = $this->get_list_setting();
			$setting['data_format'] = 0;
			$list_data = $this->object->get_list_items($setting);
			$list_data['list_table_config'] = $this->object->list_table_config;
			// make csv file
			$csv_header = array();
			foreach ($list_data['list_table_config']['list_fields'] as $list_field) {
				$csv_header[] = $list_field['name'];
			}
			$csv = implode(';',$csv_header)."\r\n";
			foreach ($list_data['list_items'] as $row) {
                                array_shift($row);
                                $row['price_1'] = number_format($row['price_1'],2,',','.').' EUR';
                                $row['price_brutto'] = number_format($row['price_brutto'],2,',','.').' EUR';
				$csv .= implode(';',$row)."\r\n";
			}
			$csv = html_entity_decode(strip_tags($csv),ENT_NOQUOTES,'UTF-8');
			//echo $csv;exit();
			header( "Content-Type: text/csv" );
			header( "Content-Disposition: attachment; filename=export.csv");
			header( "Content-Description: csv File" );
			header( "Pragma: no-cache" );
			header( "Expires: 0" );
			echo utf8_decode($csv);
			exit();
		}
		else {
			$list_data = $this->object->get_list_items($this->get_list_setting());
			$list_data['list_table_config'] = $this->object->list_table_config;
			$this->layout_object->assign('list_data', $list_data);
			return $this->layout_object->fetch('admin_object_list_items.tpl');
		}
	}	
	
	private function get_initial_object_table() {
		// get table funktions (delete, edit, search, filter, sort...) and titlerows:
		$table_data = $this->object->list_table_config;
		$table_data['list_setting'] = $this->get_list_setting();
		$table_data['list_filter'] = $this->object->get_filter();
		$table_data['list_actions'] = $this->object->get_actions();
		$table_data['object_name'] = $this->object_name;
		
		$this->layout_object->assign('table_data', $table_data);
		return $this->layout_object->fetch('admin_object_table.tpl');
	}
	
	private function get_list_setting() {
		// get list setting
		return	array (
				'max_list_items' => $this->max_list_items,
				'sort_direction' => $this->sort_direction,
				'sort_item' => $this->sort_item,
				'actual_page' => $this->actual_page,
				'count_pages' => $this->count_pages,
				'list_filter' => $this->list_filter,
				'data_format' => 1,
				'search_string' => $this->search_string
		);
	}
		
}

