<?php
/**
* @version      $Id: admin_standard_message_editor.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
*/

include_once './core/standard_message.class.php';
include_once './modules/list_and_edit.class.php';

class admin_standard_message_editor {
	private $base_object;
	private $config;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config_object;
		$this->layout_object = $layout_object;
		$this->list_object = new ListAndEdit($base_object, $layout_object);
	}

	function run() {
		//$this->setup_info();

		//var_dump($GLOBALS);
		
		if(isset($_GET['id']) && $_GET['id']) {
			$has_id = true;
		} else if(isset($_POST['id']) && $_POST['id']) {
			$has_id = true;
		} else {
			$has_id = false;
		}
		
		if(isset($_POST['submit'])) {
			$is_submitted = true;
		} else {
			$is_submitted = false;
		}
	
		// get customer groups
		$customer_group_object = new Customer_group($this->base_object);
		$customer_groups = $customer_group_object->get_all_groups();
		$this->layout_object->assign('customer_groups', $customer_groups);
		
		// decide what CRUD method to call and call it in ListAndEdit
		// create
		$standard_message_object = new Standard_message($this->base_object);
		$this->layout_object->assign('events', $standard_message_object->events);
		if($is_submitted && !$has_id) {
			$form_data = $standard_message_object->data_filter($_POST['form_field']);
			$standard_message_object->create($form_data);
			$standard_data = $standard_message_object->get_data();
			$this->layout_object->assign('form_data', $standard_data);
		}// read
		// delete file
		elseif ($has_id && isset($_POST['mod']) && $_POST['mod'] == 'delete_file') {
			$standard_message_object->id = $_POST['id'];
			$standard_message_object->delete_file($_POST['file_id']);
			$file_list = $standard_message_object->get_file_list();
			$this->layout_object->assign('file_list', $file_list);
			return $this->layout_object->fetch('admin_standard_message_editor_filelist.tpl');
		} // upload file
		elseif ($has_id && isset($_POST['mod']) && $_POST['mod'] == 'add_file') {
			$standard_message_object->id = $_POST['id'];
			$standard_message_object->add_uploaded_file($_FILES);
		} // get file list
		elseif ($has_id && isset($_POST['mod']) && $_POST['mod'] == 'get_file_list') {
			$standard_message_object->id = $_POST['id'];
			$file_list = $standard_message_object->get_file_list();
			$this->layout_object->assign('file_list', $file_list);
			return $this->layout_object->fetch('admin_standard_message_editor_filelist.tpl');
		} // get file
		elseif($has_id && isset($_GET['mod']) && $_GET['mod'] == 'file_load'){
			$file = $standard_message_object->get_file($_GET['file_id']);
			$mime_type = $file->mime_type;
			header("Expires: 0");
			header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
			header("Content-Type: $mime_type");
			header("Content-Description: File Transfer");
			header("Content-Disposition: attachment; filename=".$file->file_name);
			header("Content-Transfer-Encoding: binary");
			echo $file->content;
			exit();
		}
		elseif($has_id && !$is_submitted && $_GET['mod'] != 'delete') {
			// template variable name
			$standard_message_object->id = $_GET['id'];
			$standard_data = $standard_message_object->get_data();
			$this->layout_object->assign('form_data', $standard_data);
		} // update
		elseif($has_id && $is_submitted && $_GET['mod'] != 'delete') {
			$standard_message_object->id = $_POST['id'];
			$form_data = $standard_message_object->data_filter($_POST['form_field']);
			$standard_message_object->update($form_data);
			$standard_data = $standard_message_object->get_data();
			$this->layout_object->assign('form_data', $standard_data);
		} // delete
		elseif($has_id && isset($_GET['mod']) && $_GET['mod'] == 'delete'){
			$standard_message_object->id = $_GET['id'];
			$standard_message_object->delete();
		} 
		else {

		}
		return $this->layout_object->fetch('admin_standard_messages_editor.tpl');
	}
	
	
}

?>