<?php
/**
* @version      $Id: admin_object_edit.php 
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       Richard Kammermayer <rk@ta-edv.de>
* Easyshop is a web shop system
*/

class admin_object_edit  {
	private $base_object;
	private $object_name;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
	}

	public function run() {
		// get vars
		foreach (array('action', 'object', 'object_ids', 'form_data', 'object_id', 'email', 'customer_group', 'lang') as $field) {
			if (isset($_GET[$field])) {
				$this->{$field} = $_GET[$field];
			} elseif (isset($_POST[$field])) {
				$this->{$field} = $_POST[$field];
			} else {
				$this->{$field} = false;
			}
		}
		
		// init object
		$this->object_name = $this->object;
		include_once(ROOT_DIR . './core/'.strtolower($this->object_name).'.class.php');
		$this->object = new $this->object_name($this->base_object);
		
		// call method
		if ($this->action) {
			if ($this->{$this->action}()) {
				$return = array('status' => 'success', 'message' => 'action already.', 'object_id' => $this->object_id);
			}
			else {
				$return = array('status' => 'error', 'message' => 'action failed');
			}
			echo json_encode($return);
			exit();
			
		} else {
			return $this->get_form();
		}
	}
	
	// get object form
	private function get_form() {
		// get table funktions (delete, edit, search, filter, sort...) and titlerows:
		$this->object->init_edit_fields($this->object_id);
		$table_data = $this->object->list_table_config;
		$table_data['object_name'] = $this->object_name;
		$table_data['object_id'] = $this->object_id;
		$table_data['selected_lang'] = $this->lang;
		$this->layout_object->assign('table_data', $table_data);
		if ($this->object_id) {
			$form_data = $this->object->load($this->object_id);
			$this->layout_object->assign('form_data', $form_data);
		}
		echo $this->layout_object->fetch('admin_object_edit.tpl');
		exit();
	}
	
	// save object
	private function save() {
		// save data
		$result = $this->object->save($this->form_data, $this->object_id);
		if ($result && !$this->object_id) {
			$this->object_id = $result;
		}
		return $result;
	}
	
	// get object data
	private function get_data() {
		$form_data = $this->object->load($this->object_id);
		echo json_encode($form_data);
		exit();
	}
	
	// delete object
	private function delete_item() {
		return $this->object->remove($this->object_id);
	}
	
	// delete image file
	private function delete_image() {
		return $this->object->delete_logo($this->object_id);
	}
	
	// add image file
	private function upload_image() {
		return $this->object->save_logo($this->object_id);
	}
	
	// test email
	private function test_email() {
		return $this->object->test_email($this->object_id, $this->email);
	}
	
	// send email
	private function send_email() {
		return $this->object->send_email($this->object_id, $this->customer_group);
	}
	
	// export to shipping
	private function export_to_shipping() {
		return $this->object->export_to_shipping($this->object_ids, 1);
	}
        
	// new customer password
	private function new_password() {
		return $this->object->new_password($this->object_id);
	}
        
	// send email to customer for first activation
	private function first_activation() {
		return $this->object->first_activation($this->object_id);
	}
	
}

