<?php
/**
 * @package      Easyway Shop
 * @copyright    Copyright (C) 2005 - 2011 TA-EDV
 * @license      proprietary
 * @author       Richard Kammermayer <rk@ta-edv.de>, John T. Daly <jd@ta-edv.de>
 * 
 * Easyway Shop is a web e-commerce system
 */

include_once './core/deliverer.class.php';

class admin_deliverer_editor {
	
	private $base_object;
	private $config;
	private $layout_object;
	private $deliverer_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config_object;
		$this->layout_object = $layout_object;
		$this->deliverer_object = new Deliverer($base_object);
	}

	function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} elseif (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}
		
		if(isset($_GET['id']) && $_GET['id']) {
			$id = $_GET['id'];
		} else if(isset($_POST['id']) && $_POST['id']) {
			$id = $_POST['id'];
		} else {
			$id = false;
		}
		
		if ($action == 'save') {
			$this->save($id);
		} else {
			return $this->edit($id);
		}
	}
	
	
	private function edit($id) {
		if ($id) {
			// load data for edit
			$deliverer_data = $this->deliverer_object->get_data($id);
			$this->layout_object->assign('form_data', $deliverer_data);
		}
		// create
		
		return $this->layout_object->fetch('admin_deliverer_editor.tpl');
	} // end edit
	
	private function save($id) {
		if ($id) {
			// update
			$this->deliverer_object->set_id($id);
			$form_data = $this->deliverer_object->data_filter($_POST['form_field']);
			$data = $this->deliverer_object->update($form_data);
			
			if ($data) {
				header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_deliverer_editor&id=' . $data);
			} else {
				header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_deliverer_editor');
			}
		} else {
			// create
			$form_data = $this->deliverer_object->data_filter($_POST['form_field']);
			$data = $this->deliverer_object->create($form_data);
			
			if ($data) {
				header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_deliverer_editor&id=' . $data);
			} else {
				header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_deliverer_editor');
			}
		}
		
		exit();
	}
}
