<?php
/**
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       John T. Daly <jd@ta-edv.de>
* 
* Easyway Shop is a web e-commerce system
*/

include_once './core/deliverer.class.php';
include_once './core/countryhelper.class.php';

class admin_deliverer_actions {
	
	private $base_object;
	private $layout_object;
	private $deliverer_object;
	
	function __construct($base_object, $layout_object) {
		$this->layout_object = $layout_object;
		$this->base_object = $base_object;
		$this->deliverer_object = new Deliverer($base_object);
	}
		
	function run() {
		if (isset($_GET['id'])) {
			$id = $_GET['id'];
		} elseif(isset($_POST['id'])) {
			$id = $_POST['id'];
		} else {
			$id = false;
		}
		
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} elseif (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}
		
		if ($action == "setup_shipping_method_charges") {
			$this->setup_shipping_method_charges();
		} elseif ($action == 'setup_shipping_methods') {
			$this->setup_shipping_methods();
		} elseif ($action == 'delete') {
			$this->delete($id);
		} elseif ($action == "ping") {
			$this->ping();
		}
	}
	
	public function setup_shipping_methods() {
		if (isset($_GET['country_id'])) {
			$country_id = $_GET['country_id'];
		} else {
			$country_id = false;
		}
		
		if ($country_id) {
			$result = DelivererHelper::get_deliverers_by_country($country_id);
			
			if ($result) {
				$return = array('status' => 'success', 'data' => $result);
			} else {
				$message = DelivererHelper::get_deliverers_by_country_error();
				
				if ($message == '') {
					$message = 'no result';
				}
				
				$return = array('status' => 'error', 'message' => $message);
			}
		} else {
			$return = array('status' => 'error', 'message' => 'missing arguments');
		}
		
		echo json_encode($return);
		exit();
	}
	
	public function setup_shipping_method_charges() {
		if (isset($_GET['deliverer_id'])) {
			$deliverer_id = $_GET['deliverer_id'];
		} else {
			$deliverer_id = false;
		}
		
		if (isset($_GET['country_id'])) {
			$country_id = $_GET['country_id'];
		} else {
			$country_id = false;
		}
		
		if ($deliverer_id && $country_id) {
			$shipping_area_id = CountryHelper::get_shipping_area_by_country_id($country_id);
			$deliverer_object = new Deliverer(Registry::get('base'));
			
			$result = $deliverer_object->getAllActive($shipping_area_id);
			
			if ($result) {
				$return = array('status' => 'success', 'data' => $result);
			} else {
				$return = array('status' => 'error', 'message' => 'no result');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'missing arguments');
		}
		
		echo json_encode($return);
		exit();
	}
	
	public function delete($id) {
		if ($id) {
			$result = $this->deliverer_object->delete($id);
			
			if ($result) {
				$return_data = array('status' => 'success', 'data' => $result);
			} else {
				$return_data = array('status' => 'error', 'message' => "Kann nicht gel&ouml;schen werden");
			}
		} else {
			$return_data = array('status' => 'error', 'message' => "Keine ID.");
		}
		
		echo json_encode($return_data);
		exit();
	}
	
	private function ping() {
		echo "<pre>";
		print_r($_GET);
		
		exit();
	}
}