<?php
/**
* @version      $Id: admin_vendor_editor.php 
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* Easyshop is a web shop system
*/

include_once './core/vendors/edit.class.php';

class admin_vendor_editor {
	private $base_object;
	private $layout_object;
	private $vendor_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->vendor_object = new Vendors_Edit($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['object_id'])) {
			$id = $_GET['object_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) {
		// Debug-Ausgabe
		error_log("Vendor Editor - default_action mit ID: " . print_r($id, true));
		
		if ($id) {
			$data = $this->vendor_object->get_data($id);
			
			// Debug-Ausgabe
			error_log("Vendor Data: " . print_r($data, true));
			
			if ($data) {
				// Stelle sicher, dass alle notwendigen Felder vorhanden sind
				$form_data = new stdClass();
				$form_data->id = $data->id;
				$form_data->name = $data->hersteller_name;
				$form_data->marke_name = $data->marke_name;
				$form_data->strasse = $data->strasse;
				$form_data->strasse2 = $data->strasse2;
				$form_data->PLZ = $data->PLZ;
				$form_data->ort = $data->ort;
				$form_data->land = $data->land;
				$form_data->email = $data->email;
				$form_data->homepage = $data->homepage;
				
				if (isset($data->logo) && $data->logo != '') {
					$logo = $this->vendor_object->get_image_url() . $data->logo;
					$this->layout_object->assign('logo', $logo);
				}
				
				$this->layout_object->assign('form_data', $form_data);
				$this->layout_object->assign('is_edit', true);
			} else {
				$this->layout_object->assign('error_message', "Dieser Lieferant existiert nicht.");
			}
		} else {
			// Neuer Lieferant
			$this->layout_object->assign('is_edit', false);
		}
		
		return $this->layout_object->fetch('admin_vendor_editor.tpl');
	} // end default_action
	
	private function delete_logo($id) {
		if ($id) {
			if ($this->vendor_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->vendor_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->vendor_object->delete($id);
			
			if ($result) {
				$return = array('status' => 'success');
			} else {
				$return = array('status' => 'error');
			}
		} else {
			$return = array('status' => 'error');
		}
		
		echo json_encode($return);
		
		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->vendor_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_vendor_editor 