<?php
/**
 * @package      Easyway Shop
 * @copyright    Copyright (C) 2005 - 2011 TA-EDV
 * @license      proprietary
 * @author       John T. Daly <jd@ta-edv.de>
 * 
 * Program Description:
 * Easyway Shop is an e-commerce system with many features.
 * 
 * File Description:
 * 
 */

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

class admin_invoice_configuration {
	
	private $invoice;
	private $layout_object;
	
	function __construct($base_object, $layout_object) {
		$this->invoice = new Invoice();
		$this->layout_object = $layout_object;
	}
	
	public function run() {
		if(isset($_GET['action'])) {
			$action = $_GET['action'];
		} elseif (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = '';
		}

		if($action == 'submit') {
			$this->save_config_data();
		} elseif ($action == 'header_upload') {
			$this->header_upload();
		} elseif ($action == 'footer_upload') {
			$this->footer_upload();
		} elseif ($action == 'delete_footer') {
			$this->delete_footer();
		} elseif ($action == 'delete_header') {
			$this->delete_header();
		} else {
			$this->get_config_data();
		
			return $this->layout_object->fetch('admin_invoice_configuration.tpl');
		}
	}
	
	private function save_config_data() {
		unset($_POST['submit']);
		
		$data = $_POST;

		$this->invoice->save($data);
	}
	
	private function get_config_data() {
		$config_item = $this->invoice->load();
		
		if ($config_item) {
			foreach ($config_item as $key => $value) {
				$this->layout_object->assign($key, $value);
			}
		}
	}
	
	private function header_upload() {
		if ($_FILES['Filedata']['tmp_name'] != '') {
			$path = './web/' . SHOP_SYSTEM . '/documents/';
			move_uploaded_file($_FILES['Filedata']['tmp_name'], $path . $_FILES['Filedata']['name']);
		}
		
		Config::set_key('bill_header_image', $_FILES['Filedata']['name']);
		
		echo 'Header hochgeladen';
	}
	
	private function footer_upload() {
		if ($_FILES['Filedata']['tmp_name'] != '') {
			$path = './web/' . SHOP_SYSTEM . '/documents/';
			move_uploaded_file($_FILES['Filedata']['tmp_name'], $path . $_FILES['Filedata']['name']);
		}
		
		Config::set_key('bill_footer_image', $_FILES['Filedata']['name']);
		
		echo 'Footer Grafik hochgeladen';
	}
	
	private function delete_header() {
		Config::set_key('bill_header_image', '');
		//Config::set_key('bill_header_height', '');
		// TODO: delete image on server
	}
		
	private function delete_footer() {
		Config::set_key('bill_footer_image', '');
		// TODO: delete image on server
	}
}

class Invoice {
	
	protected $_db;
	
	public function __construct() {
		$this->_db = Registry::get('base')->db;
	}
	
	public function load() {
		if (Config::has_key('bill_header_text_window')) {
			$data['bill_header_text_window'] = Config::get_value('bill_header_text_window');
		}
		if (Config::has_key('bill_header_text_address')) {
			$data['bill_header_text_address'] = Config::get_value('bill_header_text_address');
		}
		if (Config::has_key('bill_header_alignment_address')) {
			$data['bill_header_alignment_address'] = Config::get_value('bill_header_alignment_address');
		}
		if (Config::has_key('bill_header_image') && Config::is_set('bill_header_image')) {
			$data['bill_header_image'] = './web/' . SHOP_SYSTEM . '/documents/' . Config::get_value('bill_header_image');
		}
		if (Config::has_key('bill_footer_text_margin_bottom')) {
			$data['bill_footer_text_margin_bottom'] = Config::get_value('bill_footer_text_margin_bottom');
		}
		else {
			$data['bill_footer_text_margin_bottom'] = -25;
		}
		if (Config::has_key('bill_footer_alignment_left')) {
			$data['bill_footer_alignment_left'] = Config::get_value('bill_footer_alignment_left');
		}
		if (Config::has_key('bill_footer_text_left')) {
			$data['bill_footer_text_left'] = Config::get_value('bill_footer_text_left');
		}
		if (Config::has_key('bill_footer_alignment_center')) {
			$data['bill_footer_alignment_center'] = Config::get_value('bill_footer_alignment_center');
		}
		if (Config::has_key('bill_footer_text_center')) {
			$data['bill_footer_text_center'] = Config::get_value('bill_footer_text_center');
		}
		if (Config::has_key('bill_footer_alignment_right')) {
			$data['bill_footer_alignment_right'] = Config::get_value('bill_footer_alignment_right');
		}
		if (Config::has_key('bill_footer_text_right')) {
			$data['bill_footer_text_right'] = Config::get_value('bill_footer_text_right');
		}
		if (Config::has_key('bill_footer_image') && Config::is_set('bill_footer_image')) {
			$data['bill_footer_image'] = './web/' . SHOP_SYSTEM . '/documents/' . Config::get_value('bill_footer_image');
		}
		
		
		if (isset($data)) {
			return $data;
		}
		
		return false;
	}
	
	public function save($data) {
		if (isset($data['bill_header_text_window'])) {
			$insert_data['bill_header_text_window'] = $data['bill_header_text_window'];
		}
		if (isset($data['bill_header_text_address'])) {
			$insert_data['bill_header_text_address'] = $data['bill_header_text_address'];
		}
		if (isset($data['bill_header_alignment_address'])) {
			$insert_data['bill_header_alignment_address'] = $data['bill_header_alignment_address'];
		}
		if (isset($data['bill_header_image']) && $data['bill_header_image'] != '') {
			$insert_data['bill_header_image'] = $data['bill_header_image'];
		}
		if (isset($data['bill_footer_text_margin_bottom']) && $data['bill_footer_text_margin_bottom'] != '') {
			$insert_data['bill_footer_text_margin_bottom'] = $data['bill_footer_text_margin_bottom'];
		}
		if (isset($data['bill_footer_image']) && $data['bill_footer_image'] != '') {
			$insert_data['bill_footer_image'] = $data['bill_footer_image'];
		}
		if (isset($data['bill_footer_alignment_left'])) {
			$insert_data['bill_footer_alignment_left'] = $data['bill_footer_alignment_left'];
		}
		if (isset($data['bill_footer_text_left'])) {
			$insert_data['bill_footer_text_left'] = $data['bill_footer_text_left'];
		}
		if (isset($data['bill_footer_alignment_center'])) {
			$insert_data['bill_footer_alignment_center'] = $data['bill_footer_alignment_center'];
		}
		if (isset($data['bill_footer_text_center'])) {
			$insert_data['bill_footer_text_center'] = $data['bill_footer_text_center'];
		}
		if (isset($data['bill_footer_alignment_right'])) {
			$insert_data['bill_footer_alignment_right'] = $data['bill_footer_alignment_right'];
		}
		if (isset($data['bill_footer_text_right'])) {
			$insert_data['bill_footer_text_right'] = $data['bill_footer_text_right'];
		}
		
		foreach($insert_data as $key => $value) {
			Config::set_key($key, $value);
		}
	}
}