<?php
/**
* @version      $Id: admin_country_editor.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
*/

include_once './core/country.class.php';
include_once './core/shipping_area.class.php';

class admin_country_editor {
	
	private $base_object;
	private $config;
	private $layout_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config_object;
		$this->layout_object = $layout_object;
		$this->customer_group_object = new Customer_group($base_object);
	}

	function run() {
		if(isset($_GET['id']) && $_GET['id']) {
			$has_id = true;
		} else if(isset($_POST['id']) && $_POST['id']) {
			$has_id = true;
		} else {
			$has_id = false;
		}
		
		if(isset($_POST['submit'])) {
			$is_submitted = true;
		} else {
			$is_submitted = false;
		}
	
		
		// get shipping areas
		$shipping_area_object = new Shipping_area($this->base_object);
		$shipping_areas = $shipping_area_object->get_all();
		$this->layout_object->assign('shipping_areas', $shipping_areas);
		
		$customer_groups = $this->customer_group_object->get_all();
		$this->layout_object->assign('customer_groups', $customer_groups);
		
		// decide what CRUD method to call and call it in ListAndEdit
		// create
		$country_object = new Country($this->base_object);
		if($is_submitted && !$has_id) {
			$form_data = $country_object->data_filter($_POST['form_field']);
			$country_object->create($form_data);
			$save_data = array();
			foreach($customer_groups as $customer_group) {
				if (isset($_POST['form_field']['customer_group_'.$customer_group->id.'_shipping_area_id']) && $_POST['form_field']['customer_group_'.$customer_group->id.'_shipping_area_id']) {
					$save_data[$customer_group->id] = $_POST['form_field']['customer_group_'.$customer_group->id.'_shipping_area_id'];
				}
			}
			$country_object->save_customer_group_shipping_areas($country_object->id, $save_data);
			
			// get country data
			$country_data = $country_object->get_data();
			$this->layout_object->assign('form_data', $country_data);
		}// read
		elseif($has_id && !$is_submitted && $_GET['mod'] != 'delete') {
			// template variable name
			$country_object->id = $_GET['id'];
			$country_data = $country_object->get_data();
			$this->layout_object->assign('form_data', $country_data);
		} // update
		elseif($has_id && $is_submitted && $_GET['mod'] != 'delete') {
			$country_object->id = $_POST['id'];
			$form_data = $country_object->data_filter($_POST['form_field']);
			$country_object->update($form_data);
			$save_data = array();
			foreach($customer_groups as $customer_group) {
				if (isset($_POST['form_field']['customer_group_'.$customer_group->id.'_shipping_area_id']) && $_POST['form_field']['customer_group_'.$customer_group->id.'_shipping_area_id']) {
					$save_data[$customer_group->id] = $_POST['form_field']['customer_group_'.$customer_group->id.'_shipping_area_id'];
				}
			}
			$country_object->save_customer_group_shipping_areas($country_object->id, $save_data);
			
			// get country data
			$country_data = $country_object->get_data();
			$this->layout_object->assign('form_data', $country_data);
		} // delete
		elseif($has_id && isset($_GET['mod']) && $_GET['mod'] == 'delete'){
			$country_object->id = $_GET['id'];
			$country_object->delete();
		}
		else {

		}
		return $this->layout_object->fetch('admin_country_editor.tpl');
	}
	
	
}

?>