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

include_once './core/customer.class.php';
include_once './core/country.class.php';
include_once './core/orderstatus.class.php';
include_once './core/order.class.php';
include_once './core/orderhelper.class.php';
include_once './core/customeraddress.class.php';
include_once './core/export.class.php';

class website_customercenter {
	
	private $base_object;
	private $layout_object;
	private $customer_address_object;
	
	public function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->customer_address_object = new CustomerAddress($base_object);
		
		$freetextfield_functions = './web/' . SHOP_SYSTEM . '/code/freetextfield_functions.php';
		if (file_exists($freetextfield_functions)) {
			include $freetextfield_functions;
		}
	}
		
	function run() {
		$customer_id = $this->base_object->customer->customer_id;

		if ($customer_id) {			
			if(isset($_POST['action'])) {
				$action = $_POST['action'];
			} elseif(isset($_GET['action'])) {
				$action = $_GET['action'];
			}
			
			if ($action == 'add_address') {
				$this->add_address($customer_id);
			} elseif ($action == 'edit_base_data') {
				$this->edit_base_data($customer_id);
			} elseif ($action == 'edit_payment_data') {
				$this->edit_payment_data($customer_id);
			} elseif ($action == 'set_standard') {
				$this->set_standard($customer_id);
			} elseif ($action == 'delete_address') {
				$this->delete_address($customer_id);
			} elseif ($action == 'get_invoice_pdf') {
				return $this->get_invoice_pdf();
			} elseif ($action == 'get_address') {
				$this->get_address();
			} elseif ($action == 'freetextfield_action') {
				$this->freetextfield_action($customer_id);
			} elseif ($action == 'get_item_export') {
				$this->get_item_export($customer_id);
			} else {
				return $this->default_action($customer_id);
			}
		} else {
			return $this->layout_object->_fetch('content_login.tpl');
		}
	}
	
	private function get_item_export($customer_id) {
		$customer_object = $this->base_object->customer;
		$customer_data = $customer_object->get_data($customer_id);

		$export_object = new Export($this->base_object);
		$export_object->set_customer_number($customer_data->number);

		if (isset($_GET['rf_special']) && $_GET['rf_special'] == 1) {
			$export_object->set_seperator('|');
			$csv_data = $export_object->get_export_csv('customer_rf_special');
		}
		else {
			$export_object->set_seperator(';');
			$csv_data = $export_object->get_export_csv('customer');
		}
		
		header( "Content-Type: text/csv" ); 
		header( "Content-Disposition: attachment; filename=items.csv"); 
		header( "Content-Description: csv File" ); 
		header( "Pragma: no-cache" ); 
		header( "Expires: 0" ); 
  
		echo utf8_decode($csv_data);
		exit();
	} // end get_item_export
	
	private function get_address() {
		if (isset($_GET['id'])) {
			$address_id = $_GET['id'];
		} else {
			$address_id = false;
		}
		
		if ($address_id) {
			$address_data = CustomerHelper::get_customer_address($address_id);
			
			if ($address_data) {
				$result = array('status' => 'success', 'data' => $address_data);
			} else {
				$result = array('status' => 'error');
			}
		} else {
			$result = array('status' => 'error');
		}
		
		echo json_encode($result);
		
		exit();
	}
	
	private function default_action($customer_id) {
		$country = new Country($this->base_object);
		$order = new Order($this->base_object);
		
		$this->layout_object->assign('this_url', $this->get_this_url());
		
		$customer_object = $this->base_object->customer;
		$customer_data = $customer_object->get_by_id($customer_id);
		$this->layout_object->assign('customer', $customer_data);
			
		$customer_name = Customer::get_name_by_id($customer_id);
		$this->layout_object->assign('customer_name', $customer_name);
			
		$customer_addresses = Customer::get_address_by_id($customer_id);
		$this->layout_object->assign('customer_addresses', $customer_addresses);
		
		$this->layout_object->assign('default_country', 27);
		$this->layout_object->assign('countries', $country->get_all());
		
		$this->layout_object->assign('orderstatus', OrderStatus::get_all());
		$this->layout_object->assign('orderitems', $order->get_all_order_items_by_customer_id($customer_id));
		$this->layout_object->assign('orders', OrderHelper::get_all_customer_orders($customer_id));
		
		if ($customer_data->has_item_export) {
			$this->layout_object->assign('item_export', true);
		}
		
		$this->freetextfield_setup($customer_id);
			
		return $this->layout_object->_fetch('content_customer_center.tpl');
	} // end default_action
	
	private function freetextfield_setup($customer_id) {
		if (function_exists("freetextfield1_setup")) {
			freetextfield1_setup($this->base_object->db, $this->layout_object ,$customer_id);
		}
		
		if (function_exists("freetextfield2_setup")) {
			freetextfield2_setup($this->base_object->db, $this->layout_object ,$customer_id);
		}
		
		if (function_exists("freetextfield3_setup")) {
			freetextfield3_setup($this->base_object->db, $this->layout_object ,$customer_id);
		}
		
		if (function_exists("freetextfield4_setup")) {
			freetextfield4_setup($this->base_object->db, $this->layout_object ,$customer_id);
		}
		
		if (function_exists("freetextfield5_setup")) {
			freetextfield5_setup($this->base_object->db, $this->layout_object ,$customer_id);
		}
		
		if (function_exists("freetextfield6_setup")) {
			freetextfield6_setup($this->base_object->db, $this->layout_object ,$customer_id);
		}
	} // end freetextfield_functions
	
	private function freetextfield_action($customer_id) {
		if (isset($_POST['freetextfield'])) {
			$freetextfield = $_POST['freetextfield'];
		} elseif (isset($_GET['freetextfield'])) {
			$freetextfield = $_GET['freetextfield'];
		} else {
			$freetextfield = false;
		}
		
		if($freetextfield) {
			if ($freetextfield == '1') {
				if (function_exists("freetextfield1_action")) {
					freetextfield1_action($this->base_object->db, $this->layout_object ,$customer_id);
				}
			} elseif ($freetextfield == '2') {
				if (function_exists("freetextfield2_action")) {
					freetextfield2_action($this->base_object->db, $this->layout_object ,$customer_id);
				}
			} elseif ($freetextfield == '3') {
				if (function_exists("freetextfield3_action")) {
					freetextfield3_action($this->base_object->db, $this->layout_object ,$customer_id);
				}
			} elseif ($freetextfield == '4') {
				if (function_exists("freetextfield4_action")) {
					freetextfield4_action($this->base_object->db, $this->layout_object ,$customer_id);
				}
			} elseif ($freetextfield == '5') {
				if (function_exists("freetextfield5_action")) {
					freetextfield5_action($this->base_object->db, $this->layout_object ,$customer_id);
				}
			} elseif ($freetextfield == '6') {
				if (function_exists("freetextfield6_action")) {
					freetextfield6_action($this->base_object->db, $this->layout_object ,$customer_id);
				}
			}
		}
		
		exit();
	} // end freetextfield_action
		
	private function add_address($customer_id) {
		unset($_POST['action']);
		
		$data = $_POST;
		$data['customer_id'] = $customer_id;
		
		if ($data['id'] == '') {
			unset($data['id']);
		}
		$data['orders'] = 0;
		
		if (isset($data['id'])) {
			$id = $data['id'];
			unset($data['id']);
                        
                        $diff = $this->customer_address_object->check_data_diff($data, $id);
                        $this->customer_address_object->update($data, $id);
                        
                        if ($diff) {
                            $html_table = $this->customer_address_object->make_diff_table($diff);
                            include_once('./core/cs_ticket.class.php');
                            $ticket_object = new Cs_ticket($this->base_object);
                            $ticket_object->event(15, $customer_id, false, array('customer_data_diff' => $html_table));
                        }
		} else {
			$this->customer_address_object->create($data);
                        
                        include_once('./core/cs_ticket.class.php');
                        $ticket_object = new Cs_ticket($this->base_object);
                        $ticket_object->event(15, $customer_id, false, array('customer_data_diff' => 'Kunde hat eine neue Adresse hinzugefügt.'));
		}
		
		header('location: ' . $_SERVER["HTTP_REFERER"]);
	}
	
	private function edit_base_data($customer_id) {
		$customer = $this->base_object->customer;
		
		unset($_POST['action']);
		
		$pass1 = $_POST['pass1'];
		unset($_POST['pass1']);
		
		$pass2 = $_POST['pass2'];
		unset($_POST['pass2']);
		
		$data = $_POST;
		
		if ($pass1 != '') {
			if ($pass1 == $pass2) {
				$data['pass'] = $pass1;
			}
		}
			
                $diff = $customer->check_data_diff($customer_id, $data);
		$result = $customer->update($data);
                    
                // email notification
                if ($diff && !(count($diff) == 1 && isset($diff['pass']))) {
                    $html_table = $customer->make_diff_table($diff);
                    include_once('./core/cs_ticket.class.php');
                    $ticket_object = new Cs_ticket($this->base_object);
                    $ticket_object->event(15, $customer_id, false, array('customer_data_diff' => $html_table));
                }
		header('location: ' . $_SERVER["HTTP_REFERER"]);
	}
	
	private function edit_payment_data($customer_id) {
		$customer = $this->base_object->customer;
		
		unset($_POST['action']);
		
		$result = $customer->update($_POST);
		
		$result = $customer->update($data);

                // email notification
                include_once('./core/cs_ticket.class.php');
                $ticket_object = new Cs_ticket($this->base_object);
                $ticket_object->event(15, $customer_id);
                
		header('location: ' . $_SERVER["HTTP_REFERER"]);
	}
	
	private function set_standard($customer_id) {
		$address_id = $_GET['id'];
		
		$result = Customer::set_standard_address($customer_id, $address_id);
                
                // event notification
		include_once('./core/cs_ticket.class.php');
                $ticket_object = new Cs_ticket($this->base_object);
                $ticket_object->event(15, $customer_id, false, array('customer_data_diff' => 'Der Kunde hat seine Standardadresse geändert.'));

                    echo json_encode(array('success' => $result));
		
		exit();
	}
	
	private function delete_address($customer_id) {
		$address_id = $_GET['id'];
		
		$result = Customer::delete_address($customer_id, $address_id);
		
		echo json_encode(array('success' => $result));
		
		exit();
	}
	
	private function get_this_url() {
		$base_url = 'https://' . $_SERVER["SERVER_NAME"];
		if($_SERVER['SERVER_PORT'] != '80') {
			$base_url .= ':' . $_SERVER['SERVER_PORT'];
		}
		
		return $base_url . '/index.php?menu_id=' . $_GET['menu_id'] . '&parent_id=' . $_GET['parent_id'];
	}
	
	private function get_invoice_pdf() {
		if (isset($_GET['order_id'])) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}
		
		if ($order_id) {
			if ($this->show_invoice($order_id)) {
				OrderHelper::get_order_invoice_pdf($order_id);
			} else {
				return $this->layout_object->_fetch('error_404.tpl');
			}
		} else {
			return $this->layout_object->_fetch('error_404.tpl');
		}
	}
	
	// TODO: check if you can even show this order
	private function show_invoice($order_id) {
		// has invoce date?
		// belongs to logged in customer
		return true;
	}
}
