<?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_item_image_configuration {
	
	private $ItemImage;
	private $layout_object;
	
	function __construct($base_object, $layout_object) {
		$this->ItemImage = new ItemImage();
		$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 == 'watermark_image_upload') {
			$this->watermark_image_upload();
		} elseif ($action == 'delete_watermark_image') {
			$this->delete_watermark_image();
		} else {
			$this->get_config_data();
		
			return $this->layout_object->fetch('admin_item_image_configuration.tpl');
		}
	}
	
	private function save_config_data() {
		unset($_POST['submit']);
		
		$data = $_POST;

		$this->ItemImage->save($data);
	}
	
	private function get_config_data() {
		$config_item = $this->ItemImage->load();
		
		if ($config_item) {
			foreach ($config_item as $key => $value) {
				$this->layout_object->assign($key, $value);
			}
		}
	}
	
	private function watermark_image_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('item_watermark_image', $_FILES['Filedata']['name']);
		
		echo 'Wasserzeichen hochgeladen';
	}
	
	private function delete_watermark_image() {
		Config::set_key('item_watermark_image', '');
		// TODO: delete image on server
	}
}

class ItemImage {
	
	protected $_db;
	
	public function __construct() {
		$this->_db = Registry::get('base')->db;
	}
	
	public function load() {
		if (Config::has_key('item_watermark_activ')) {
			$data['item_watermark_activ'] = Config::get_value('item_watermark_activ');
		}
		if (Config::has_key('item_watermark_pos')) {
			$data['item_watermark_pos'] = Config::get_value('item_watermark_pos');
		}
		if (Config::has_key('item_watermark_image') && Config::is_set('item_watermark_image')) {
			$data['item_watermark_image'] = './web/' . SHOP_SYSTEM . '/documents/' . Config::get_value('item_watermark_image');
		}
		
		if (isset($data)) {
			return $data;
		}
		
		return false;
	}
	
	public function save($data) {
		if (isset($data['item_watermark_activ'])) {
			$insert_data['item_watermark_activ'] = $data['item_watermark_activ'];
		}
		if (isset($data['item_watermark_pos'])) {
			$insert_data['item_watermark_pos'] = $data['item_watermark_pos'];
		}
		if (isset($data['item_watermark_image'])) {
			$insert_data['item_watermark_image'] = $data['item_watermark_image'];
		}
		
		foreach($insert_data as $key => $value) {
			Config::set_key($key, $value);
		}
	}
}