<?php
/**
* @version      $Id: admin_cs_ticket_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/cs_ticket.class.php';
include_once './core/customer.class.php';
include_once './core/cs_ticket_message.class.php';

class admin_cs_ticket_editor {
	
	private $base_object;
	private $config;
	private $layout_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config_object;
		$this->layout_object = $layout_object;
	}

	function run() {
		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;
		}

		// decide what CRUD method to call and call it in ListAndEdit
		// create new ticket
		$ticket_object = new Cs_ticket($this->base_object);
		
		// please deleted this!
		$ticket_object->fetch_email();
		
		$customer_object = new Customer($this->base_object);
		if ($has_id && isset($_GET['action']) && $_GET['action'] == 'set_ticket_state') {
			$this->set_ticket_state($_GET['id']);
		}
		elseif($is_submitted && !$has_id) {
			// get customer email
			if ($_POST['form_field']['customer_id']) {
				$customer_object->customer_id = $_POST['form_field']['customer_id'];
				$customer = $customer_object->get_data();
				$_POST['form_field']['m_to'] = $customer->email;
			}
			// create new ticket
			$ticket_id = $ticket_object->create($_POST['form_field']);
			$data = $ticket_object->get_data();
			if ($data->customer_id) {
				$customer_object->customer_id = $data->customer_id;
				$data->customer = $customer_object->get_data();
			}
			$message_number = $data->last_message_number;
			$data->selected_message_number = $message_number;
			$this->layout_object->assign('form_data', $data);
		} // new ticket create form
		elseif(!$is_submitted && !$has_id) {
			$data = new stdClass();
			$data->email_from = $this->base_object->config->shopConfiguration['system_email'];
			if (isset($_GET['customer_id'])) {
				$customer_object->customer_id = $_GET['customer_id'];
				$data->customer = $customer_object->get_data();
			}
			
			if (!isset($data->customer)) {
				$data->customers = $customer_object->get_all();
			}
			$this->layout_object->assign('form_data', $data);
		}// send response
		elseif($has_id && $is_submitted) {
			$ticket_object->id = $_GET['id'];
			$ticket_object->send_response($_POST['form_field']);
			$data = $ticket_object->get_data();
			if ($data->customer_id) {
				$customer_object->customer_id = $data->customer_id;
				$data->customer = $customer_object->get_data();
			}
			$message_number = $data->last_message_number;
			$data->selected_message_number = $message_number;
			$this->layout_object->assign('form_data', $data);
		}// response
		elseif($has_id && !$is_submitted && $_GET['mod'] == 'response') {
			// template variable name
			$ticket_object->id = $_GET['id'];
			$data = $ticket_object->get_data();
			$data->email_from = $this->base_object->config->shopConfiguration['system_email'];
			if (isset($_GET['message_number']) && $_GET['message_number']) {
				$message_number = $_GET['message_number'];
			}
			else {
				$message_number = $data->last_message_number;
			}
			foreach($data->ticket_messages as $message) {
				if($message->message_number == $message_number) {
					$data->selected_message = $message;
				}
			}
			if ($data->customer_id) {
				$customer_object->customer_id = $data->customer_id;
				$data->customer = $customer_object->get_data();
			}
			$data->response = 1;
			$this->layout_object->assign('form_data', $data);
		} // get file
		elseif($has_id && isset($_GET['mod']) && $_GET['mod'] == 'file_load'){
			$ticket_message_object = new Cs_ticket_message($this->base_object);
			$file = $ticket_message_object->get_file($_GET['file_id']);
			
			header("Expires: 0");
			header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
			header("Content-Type: application/force-download");
			header("Content-Description: File Transfer");
			header("Content-Disposition: attachment; filename=".$file->file_name);
			header("Content-Transfer-Encoding: binary");
			echo $file->content;
			exit();
		}// read
		elseif($has_id && !$is_submitted && $_GET['mod'] != 'delete') {
			// template variable name
			$ticket_object->id = $_GET['id'];
			$data = $ticket_object->get_data();
			if ($data->customer_id) {
				$customer_object->customer_id = $data->customer_id;
				$data->customer = $customer_object->get_data();
			}
			
			if (isset($_GET['message_number']) && $_GET['message_number']) {
				$message_number = $_GET['message_number'];
			}
			else {
				$message_number = $data->last_message_number;
			}
			$data->selected_message_number = $message_number;
			$this->layout_object->assign('form_data', $data);
		}
		else {

		}
		return $this->layout_object->fetch('admin_cs_ticket_editor.tpl');
	}
	
	private function set_ticket_state($id) {
		if ($id) {
			if (isset($_GET['value'])) {
				$value = $_GET['value'];
			} elseif (isset($_POST['value'])) {
				$value = $_POST['value'];
			} else {
				$value = false;
			}
			if ($value) {
				$ticket_object = new Cs_ticket($this->base_object);
				$result = $ticket_object->set_ticket_state($value, $id);
				if ($result) {
					// success
					$return = array('status' => 'success', 'message' => 'status set');
				} else {
					// cant save
					$return = array('status' => 'error', 'message' => 'can not save');
				}
			} else {
				// no active marker
				$return = array('status' => 'error', 'message' => 'no value');
			}
		} else {
			// no id
			$return = array('status' => 'error', 'message' => 'no id');
		}
		
		echo json_encode($return);
		exit();
	}
	
	
}

?>