<?php
/**
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       John T. Daly <jd@ta-edv.de>
*/

include_once './core/customer.class.php';
include_once './core/customerhelper.class.php';
include_once './core/customergroups.class.php';
include_once './core/customeraddress.class.php';

class admin_customer_actions  {
	
	private $base_object;
	private $config;
	private $layout_object;
	private $customer_object;
	private $customergroups_object;
	private $customer_address_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config;
		$this->layout_object = $layout_object;
		$this->customer_object = $base_object->customer;
		$this->customergroups_object = new CustomerGroups($base_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'];
		} elseif (isset($_POST['id'])) {
			$id = $_POST['id'];
		} else {
			$id = false;
		}
		
		if (isset($_GET['address_id'])) {
			$address_id = $_GET['address_id'];
		} elseif (isset($_POST['address_id'])) {
			$address_id = $_POST['address_id'];
		} else {
			$address_id = false;
		}
		
		if (isset($_GET['customer_id'])) {
			$customer_id = $_GET['customer_id'];
		} elseif (isset($_POST['customer_id'])) {
			$customer_id = $_POST['customer_id'];
		} else {
			$customer_id = false;
		}
		
		if (isset($_POST['address'])) {
			$address = $_POST['address'];
		} else {
			$address = false;
		}
		
		if ($action == 'delete_customer') {
			$this->delete_customer($id);
		} elseif ($action == 'delete_customers') {
			$this->delete_customers();
		} elseif ($action == 'get_customers') {
			$this->get_customers();
		} elseif ($action == 'verify_customer_data') {
			$this->verify_customer_data();
		} elseif ($action == 'get_customer_by_id') {
			$this->get_customer_by_id($id);
		} elseif ($action == 'get_customer_addresses_by_id') {
			$this->get_customer_addresses_by_id($id);
		} elseif ($action == 'set_standard_address') {
			$this->set_standard_address();
		} elseif ($action == 'delete_address') {
			$this->delete_address();
		} elseif ($action == 'set_info') {
			$this->set_info($id);
		} elseif ($action == 'edit_customer_address') {
			$this->edit_customer_address($address_id);
		} elseif ($action == 'new_customer_address') {
			$this->new_customer_address($customer_id);
		} elseif ($action == 'update_customer_address') {
			$this->update_customer_address($address, $address_id);
		} elseif ($action == 'create_customer_address') {
			$this->create_customer_address($address, $customer_id);
		} elseif ($action == 'ping') {
			$this->ping();
		}
	}
	
	private function verify_customer_data() {
		if (isset($_POST['customer_field'])) {
			$customer_data = $_POST['customer_field'];
		} else {
			$customer_data = false;
		}
		
		if (isset($_POST['customer_address_field'])) {
			$customer_address = $_POST['customer_address_field'];
		} else {
			$customer_address = array();
		}
		
		if ($customer_data) {
			$return = CustomerHelper::verify_customer_data($customer_data, $customer_address);
		} else {
			$return = array('status' => 'error', 'message' => 'no data');
		}
		
		echo json_encode($return);
		
		exit();
	}
	
	private function delete_customer($id) {
		if ($id) {
			$result = CustomerHelper::delete_customer($id);
			
			if ($result) {
				// deleted
				$return = array('status' => 'success', 'message' => 'deleted');
			} else {
				// cant save
				$return = array('status' => 'error', 'message' => 'can not delete');
			}
		} else {
			// no id
			$return = array('status' => 'error', 'message' => 'no id');
		}
		
		echo json_encode($return);
		exit();
	}
	
	private function delete_customers() {
		if (isset($_GET['ids'])) {
			$ids = $_GET['ids'];
		} elseif (isset($_POST['ids'])) {
			$ids = $_POST['ids'];
		} else {
			$ids = false;
		}
		
		if ($ids) {
			$result = CustomerHelper::delete_customers($ids);
			
			if ($result) {
				// deleted
				$return = array('status' => 'success', 'message' => 'deleted');
			} else {
				// cant save
				$return = array('status' => 'error', 'message' => 'can not delete');
			}
		} else {
			// no ids
			$return = array('status' => 'error', 'message' => 'no ids');
		}
		
		echo json_encode($return);
		exit();
	}
	
	private function get_customers() {
		$range = $this->get_range();
		
		if ($range) {
			$group = $this->get_group();
			$groups = $this->customergroups_object->get_all();
			
			$items = $this->get_items();
			$page = $this->get_page();
			$pages = $this->customer_object->get_number_of_pages($items, $range, $group);
			$pagination = $this->customer_object->get_pagination_array($items, $page, $range, $group);
			$sort = $this->get_sort();
			$sort_direction = $this->get_sort_direction();
			
			$this->layout_object->assign('items', $items);
			$this->layout_object->assign('page', $page);
			$this->layout_object->assign('pages', $pages);
			$this->layout_object->assign('pagination', $pagination);
			$this->layout_object->assign('range', $range);
			$this->layout_object->assign('selected_group', $group);
			$this->layout_object->assign('groups', $groups);
			if ($sort) {
				$this->layout_object->assign('sort', $sort);
			}
			if ($sort_direction) {
				$this->layout_object->assign('sort_direction', $sort_direction);
			}
			
			$data = $this->base_object->customer->get_all_paginated($items, $page, $range, $sort, $sort_direction, $group);
			$this->layout_object->assign('customer_list', $data);
			echo $this->layout_object->fetch('table_customer_list.tpl');
		} else {
			// no range
			echo "ERROR: no range";
		}
		
		exit();
	}
	
	private function get_range() {
		if (isset($_GET['range'])) {
			$range = $_GET['range'];
		} elseif (isset($_POST['range'])) {
			$range = $_POST['range'];
		} else {
			$range = false;
		}
		
		return $range;
	}
	
	private function get_items() {
		if(isset($_GET['items']) && is_numeric($_GET['items'])){
			$items = (int)$_GET['items'];
			$this->base_object->customer->set_config_item('pagination', $items);
		} else {
			$items = $this->base_object->customer->get_config_item('pagination');
			if(is_numeric($items)) {
				$items = (int)$items;
			} else {
				$items = 10;
			}
		}
		
		return $items;
	} // end get_items
	
	private function get_page() {
		if (isset($_GET['page']) && is_numeric($_GET['page'])) {
			$page = (int)$_GET['page'];
		} else {
			$page = 1;
		}
		
		return $page;
	} // end get_page
	
	private function get_group() {
		if (isset($_GET['group']) && is_numeric($_GET['group'])) {
			$group = (int)$_GET['group'];
		} else {
			$group = false;
		}
		
		return $group;
	} // end get_group
	
	private function get_sort() {
		if (isset($_GET['sort']) && $_GET['sort']) {
			$sort = $_GET['sort'];
		} elseif (isset($_POST['sort']) && $_POST['sort']) {
			$sort = $_POST['sort'];
		} else {
			$sort = false;
		}
		
		return $sort;
	} // end get_sort
	
	private function get_sort_direction() {
		if (isset($_GET['sort_direction']) && $_GET['sort_direction']) {
			$sort_direction = $_GET['sort_direction'];
		} elseif (isset($_POST['sort_direction']) && $_POST['sort_direction']) {
			$sort_direction = $_POST['sort_direction'];
		} else {
			$sort_direction = false;
		}
		
		return $sort_direction;
	}
	
	private function get_customer_by_id($id) {
		if ($id) {
			$result = $this->customer_object->get_data($id);
			
			if ($result) {
				$return = array('status' => 'success', 'data' => $result);
			} else {
				$return = array('status' => 'error', 'message' => 'no return data');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'no id');
		}
		
		echo json_encode($return);
		exit();
	}
	
	private function get_customer_addresses_by_id($id) {
		if ($id) {
			//$result = $this->customer_address_object->get_data_by_customer_id($id);
			$result = $this->customer_object->get_address_by_id_hacked($id);
			
			if ($result) {
				$return = array('status' => 'success', 'data' => $result);
			} else {
				$return = array('status' => 'error', 'message' => 'no return data');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'no id');
		}
		
		echo json_encode($return);
		exit();
	}
	
	private function set_standard_address() {
		if (isset($_GET['address_id'])) {
			$address_id = $_GET['address_id'];
		} else {
			$address_id = false;
		}
		
		if (isset($_GET['customer_id'])) {
			$customer_id = $_GET['customer_id'];
		} else {
			$customer_id = false;
		}
		
		if ($address_id) {
			if ($customer_id) {
				$result = Customer::set_standard_address($customer_id, $address_id);
				if ($result) {
					$return = array('status' => 'success');
				} else {
					$return = array('status' => 'error', 'message' => "ERROR: no result");
				}
			} else {
				$return = array('status' => 'error', 'message' => "ERROR: no user id");
			}
		} else {
			$return = array('status' => 'error', 'message' => "ERROR: no address id");
		}	
		
		echo json_encode($return);
		
		exit();
	}
	
	private function delete_address() {
		if (isset($_GET['address_id'])) {
			$address_id = $_GET['address_id'];
		} else {
			$address_id = false;
		}
		
		if (isset($_GET['customer_id'])) {
			$customer_id = $_GET['customer_id'];
		} else {
			$customer_id = false;
		}
		
		if ($address_id) {
			if ($customer_id) {
				$result = Customer::delete_address($customer_id, $address_id);
				if ($result) {
					$return = array('status' => 'success');
				} else {
					$return = array('status' => 'error', 'message' => "ERROR: no result");
				}
			} else {
				$return = array('status' => 'error', 'message' => "ERROR: no user id");
			}
		} else {
			$return = array('status' => 'error', 'message' => "ERROR: no address id");
		}	
		
		echo json_encode($return);
		
		exit();
	}
	
	private function set_info($id) {
		if ($id) {
			$result = $this->customer_object->update(array('info' => $_POST['info']));
			
			if ($result) {
				$return = array('status' => 'success', 'data' => $result);
			} else {
				$return = array('status' => 'error', 'message' => 'no return data');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'no id');
		}
		
		echo json_encode($return);
		exit();
	}
	
	private function edit_customer_address($address_id) {
		if ($address_id) {
			$customer_address_data = $this->customer_address_object->get_data_by_address_id($address_id);
			
			$this->layout_object->assign('action', './index.php?admin_modul=admin_customer_actions&action=update_customer_address&address_id=' . $address_id);
			$this->layout_object->assign('address', $customer_address_data);
		}
		
		// get countries
		$country_object = new Country($this->base_object);
		$countries = $country_object->get_all();
		$this->layout_object->assign('countries', $countries);
		
		echo $this->layout_object->fetch('admin_address_editor.tpl');
		exit();
	}
	
	private function new_customer_address($customer_id) {
		if (isset($customer_id)) {
			$customer_data = $this->customer_object->get_data($customer_id);
	
			$address_defaults = new stdClass();
			$address_defaults->customer_id = $customer_id;
			$address_defaults->company = $customer_data->company;
			$address_defaults->firstname = $customer_data->firstname;
			$address_defaults->surname = $customer_data->surname;
			$address_defaults->orders = 0;
			
			$this->layout_object->assign('action', './index.php?admin_modul=admin_customer_actions&action=create_customer_address&customer_id=' . $customer_id);
			$this->layout_object->assign('address', $address_defaults);
		}
	
		// get countries
		$country_object = new Country($this->base_object);
		$countries = $country_object->get_all();
		$this->layout_object->assign('countries', $countries);
	
		echo $this->layout_object->fetch('admin_address_editor.tpl');
		exit();
	}
	
	private function update_customer_address($data, $address_id) {
		if ($data) {
			if ($address_id) {
				$result = $this->customer_address_object->update($data, $address_id);
				
				if ($result) {
					$return_data = array('status' => 'success', 'data' => $result);
				} else {
					$error = $this->customer_address_object->get_error();
						
					if ($error == 'invalid data') {
						$return_data = array('status' => 'error', 'message' => $error, 'data' => $this->customer_address_object->get_validation_errors());
					} else {
						$return_data = array('status' => 'error', 'message' => $error);
					}
				}
			} else {
				$return_data = array('status' => 'error', 'message' => 'no address id');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'no data');
		}
				
		echo json_encode($return_data);
		exit();
	}
	
	private function create_customer_address($data, $customer_id) {
		if ($data) {
			if ($customer_id) {
				$result = $this->customer_address_object->create($data, $customer_id);
				
				if ($result) {
					$return_data = array('status' => 'success', 'data' => $result);
				} else {
					$error = $this->customer_address_object->get_error();
					
					if ($error == 'invalid data') {
						$return_data = array('status' => 'error', 'message' => $error, 'data' => $this->customer_address_object->get_validation_errors());
					} else {
						$return_data = array('status' => 'error', 'message' => $error);
					}
				}
			} else {
				$return_data = array('status' => 'error', 'message' => 'no customer id');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'no data');
		}
		
		echo json_encode($return_data);
		exit();
	}
	
	private function ping() {
		$data = $_GET;
		
		unset($data['admin_modul']);
		unset($data['action']);
		
		echo "YOU ARE IN PING<br /><br />";
		
		foreach ($data as $key => $value) {
			echo $key . ' : ' . $value . '<br />';
		}
		
		exit();
	}
}