<?php
/**
* @version      $Id: admin_manufacturer_editor.php 
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       John T. Daly <jtd@ta-edv.de>
* Easyshop is a web shop system
*/

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

class admin_manufacturer_editor {
	private $base_object;
	private $layout_object;
	private $manufacturer_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->manufacturer_object = new Manufacturer($base_object);
	} // end __construct

	function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} elseif (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}
		
		if (isset($_GET['id'])) {
			$id = $_GET['id'];
		} elseif (isset($_POST['id'])) {
			$id = $_POST['id'];
		} else {
			$id = false;
		}
		
		if ($action == 'save') {
			$this->save();
		} elseif ($action == 'upload') {
			$this->upload($id);
		} elseif ($action == 'delete') {
			$this->delete($id);
		} elseif ($action == 'delete_logo') {
			$this->delete_logo($id);
		} else {
			return $this->default_action($id);
		}
	} // end run
	
	private function default_action($id) {
		if ($id) {
			$data = $this->manufacturer_object->get_by_id($id);
			
			if ($data) {
				if ($data->logo != '') {
					$logo = $this->manufacturer_object->get_image_url() . $data->logo;
					$this->layout_object->assign('logo', $logo);
				}
				
				$this->layout_object->assign('form_data', $data);
			} else {
				$this->layout_object->assign('error_message', "Dieser Hersteller existiert nicht.");
			}
		}
		// implied else, no action taken when you create something new
		
		return $this->layout_object->fetch('admin_manufacturer_editor.tpl');
	} // end default_action
	
	private function delete_logo($id) {
		if ($id) {
			if ($this->manufacturer_object->set_logo($id, '')) {
				$return = array('status' => 'success');
			} else {
				$return = array('status' => 'error');
			}
			
		} else {
			$return = array('status' => 'error');
		}
		
		echo json_encode($return);
		
		exit();
	}
	
	private function upload($id) {
		$log_object = Logger::get_instance();
		$log_object->info('fileupload','jetzt');
		if ($id) {
			$log_object->info('id',$id);
			$temp_file = $_FILES['Filedata']['tmp_name'];
			if ($temp_file != '') {
				$log_object->info('file','datei');
				$result = $this->manufacturer_object->save_logo($id, $temp_file);
				
				if ($result) {
					$return = array('status' => 'success');
				} else {
					$return = array('status' => 'error', 'message' => "ERROR: can't save", 'data', $result);
				}
			} else {
				$return = array('status' => 'error', 'message' => 'ERROR: no file');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'ERROR: no id');
		}
		
		echo json_encode($return);
		
		exit();
	} // end upload
	
	private function delete($id) {
		if ($id) {
			$result = $this->manufacturer_object->delete($id);
			
			if ($result) {
				$return = array('status' => 'success');
			} else {
				$return = array('status' => 'error');
			}
		} else {
			$return = array('status' => 'error');
		}
		
		echo json_encode($return);
		
		exit();
	}
	
	// only used in ajax, so i terminate with exit
	private function save() {
		if (isset($_POST['form_field'])) {
			$data = $_POST['form_field'];
			
			if (isset($data['active'])) {
				$data['active'] = '1';
			} else {
				$data['active'] = '0';
			}
			
			if ($data['id'] == '') {
				unset($data['id']);
			}
			
			$result = $this->manufacturer_object->save($data);
			
			if ($result) {
				$return = array('status' => 'success', 'id' => $result);
			} else {
				$return = array('status' => 'error', 'message' => 'save failed');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'no forms data');
		}
		
		echo json_encode($return);
		
		exit();
	} // end save
	
} // end admin_manufacturer_editor
