<?php
/**
* @version      $Id: admin_customer_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/customer_group.class.php';
include_once './core/customer.class.php';
include_once './core/customeraddress.class.php';
include_once './core/order.class.php';
include_once './core/cs_ticket.class.php';
include_once './core/cs_ticket_message.class.php';
include_once './core/orderstatus.class.php';
include_once './core/admin_role.class.php';
include_once './modules/list_and_edit.class.php';

class admin_customer_editor {
	
	private $base_object;
	private $config;
	private $layout_object;
	private $customer_address_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_address_object = new CustomerAddress($base_object);
              
	}

	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'];
		} else {
			$id = false;
		}
		
		// get countries
		$country_object = new Country($this->base_object);
		$countries = $country_object->get_all();
		$this->layout_object->assign('countries', $countries);
		
		// get customer groups
		$customer_group_object = new Customer_group($this->base_object);
		$customer_groups = $customer_group_object->get_all_groups();
		$this->layout_object->assign('customer_groups', $customer_groups);

		// get all admin roles
		$admin_role_object = new Admin_role($this->base_object);
		$admin_roles = $admin_role_object->get_all();
		$this->layout_object->assign('admin_roles', $admin_roles);
		
		if ($action == 'save') {
			return $this->save();
		} else {
			return $this->default_action($id);
		}
	} // end run
	
	private function default_action($id) {
		if ($id) {
			$customer_object = new Customer($this->base_object);
			$ticket_object = new Cs_ticket($this->base_object);
			$ticket_message_object = new Cs_ticket_message($this->base_object);
			
			$data = $customer_object->get_data($id);
			
			if ($data) {
                            
                                // get customer groups
                                $customer_group_object = new Customer_group($this->base_object);
                                $this->layout_object->assign('customer_group_data', $customer_group_object->get_data($data->group_id));
                            
				$orders = OrderHelper::get_all_customer_orders($id);

				/*$all_tickets = $ticket_object->get_all_customer_tickets($id);
				$tickets = array();
				foreach($all_tickets as $ticket) {
					// get first message
					$ticket->first_message = $ticket_message_object->get_first_message($ticket->id);
					$tickets[] = $ticket;
				}*/
				
				$customer_address_object = new CustomerAddress($this->base_object);
				$customer_address_data = $customer_address_object->get_data_by_customer_id($id);
				
				$this->layout_object->assign('customer_addresses', $customer_address_data);
				$this->layout_object->assign('orders', $orders);
				$this->layout_object->assign('tickets', $tickets);
				$this->layout_object->assign('customer_data', $data);
			} else {
				$this->layout_object->assign('error_message', "Dieser Kunde existiert nicht.");
			}
		}
		
		return $this->layout_object->fetch('admin_customer_editor.tpl');
	} // end default_action
	
	private function save() {
		if (isset($_POST['customer_field'])) {
			$data = $_POST['customer_field'];
			
			if ($data['pass1'] != '') {
				if ($data['pass1'] == $data['pass2']) {
					$data['pass'] = $data['pass1'];
				}
			}
			
			unset($data['pass1']);
			unset($data['pass2']);
		} else {
			$data = false;
		}
		
		if (isset($_POST['customer_address_field'])) {
			$address = $_POST['customer_address_field'];
		} else {
			$address = false;
		}
		
		if ($data) {	
			$unlocked_message = 0;
			if ($address) {
				$customer_object = new Customer($this->base_object);
				
				$result =  $customer_object->create($data);
				
				// customer unlocked email message
				if ($data['locked'] == -1) {
					$data['locked'] == 0;
					$unlocked_message = 1;
				}
				
				
				
				if ($result) {
					$address['customer_id'] = $result;
					
					if ($unlocked_message) {
						// send message to customer
						$ticket_object = new Cs_ticket($this->base_object);
						$ticket_object->event(12, $result, false);
					}
					
					$res = $this->customer_address_object->create($address);
                                        
                                        // customer new password with email message
                                        if ($_POST['customer_password_action'] == 1) {
                                                $customer_object->new_password($result);
                                        }
					
					header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_customer_editor&id=' . $result);
				} else {
					header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_customer_editor');
				}
			} else {
				$customer_object = new Customer($this->base_object);
				$customer_object->set_id($data['id']);
                                
				unset($data['id']);
				
				// customer unlocked email message
				if ($data['locked'] == -1) {
					$data['locked'] == 0;
					$unlocked_message = 1;
				}
                                
				$result = $customer_object->update($data);
				
				if ($unlocked_message) {
					// send message to customer
					$ticket_object = new Cs_ticket($this->base_object);
					$ticket_object->event(12, $result, false);
				}
                                
                                // customer new password with email message
                                if ($_POST['customer_password_action'] == 1) {
                                        $customer_object->new_password($result);
                                }
				
				header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_customer_editor&id=' . $result);
			}
		} else {
			header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_customer_editor');
		}
	} // end save
	
} // end admin_customer_editor
