<?php 
class ControllerAccountAddress extends Controller {
	var $error = array();
	  
  	function index() {
		$customer =& $this->locator->get('customer');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');	
	
    	if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('account_address'));

	  		$response->redirect($url->ssl('account_login')); 
    	}
	
    	$language->load('controller/account_address.php');

		$template->set('title', $language->get('heading_title'));
			        
		$template->set('content', $this->getList());

    	$template->set($module->fetch());
	
		$response->set($template->fetch('layout.tpl'));	
  	}

  	function insert() {
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
			
    	if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('account_address'));

	  		$response->redirect($url->ssl('account_login')); 
    	} 

    	$language->load('controller/account_address.php');

		$template->set('title', $language->get('heading_title'));
			
    	if (($request->isPost()) && ($this->validateForm())) {
      		$sql = "insert into address set customer_id = '?', company = '?', firstname = '?', lastname = '?', address_1 = '?', address_2 = '?', postcode = '?', city = '?', zone_id = '?', country_id = '?'";
      		$database->query($database->parse($sql, $customer->getId(), $request->get('company', 'post'), $request->get('firstname', 'post'), $request->get('lastname', 'post'), $request->get('address_1', 'post'), $request->get('address_2', 'post'), $request->get('postcode', 'post'), $request->get('city', 'post'), $request->get('zone_id', 'post'), $request->get('country_id', 'post')));

      		if ($request->has('default', 'post')) {
        		$database->query("update customer set address_id = '" . (int)$database->getLastId() . "' where customer_id = '" . (int)$customer->getId() . "'");
      		}

      		$session->set('message', $language->get('text_insert'));

	  		$response->redirect($url->ssl('account_address'));
    	} 
	  	
		$template->set('content', $this->getForm());

      	$template->set($module->fetch());

      	$response->set($template->fetch('layout.tpl'));	
  	}

  	function update() {
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');		
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
			
    	if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('account_address'));

	  		$response->redirect($url->ssl('account_login')); 
    	} 
		
    	$language->load('controller/account_address.php');

		$template->set('title', $language->get('heading_title'));
		
    	if (($request->isPost()) && ($this->validateForm())) {
      		$sql = "update address set company = '?', firstname = '?', lastname = '?', address_1 = '?', address_2 = '?', postcode = '?', city = '?', zone_id = '?', country_id = '?' where address_id  = '?' and customer_id = '?'";
      		$database->query($database->parse($sql, $request->get('company', 'post'), $request->get('firstname', 'post'), $request->get('lastname', 'post'), $request->get('address_1', 'post'), $request->get('address_2', 'post'), $request->get('postcode', 'post'), $request->get('city', 'post'), $request->get('zone_id', 'post'), $request->get('country_id', 'post'), $request->get('address_id'), $customer->getId()));

      		if ($request->has('default', 'post')) {
        		$database->query("update customer set address_id = '" . (int)$request->get('address_id') . "' where customer_id = '" . (int)$customer->getId() . "'");
      		}
       								
	  		$session->set('message', $language->get('text_update'));
	  
	  		$response->redirect($url->ssl('account_address'));
    	} 
	  	
		$template->set('content', $this->getForm());

      	$template->set($module->fetch());

      	$response->set($template->fetch('layout.tpl'));
  	}

  	function delete() {
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');		
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
			
    	if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('account_address'));

	  		$response->redirect($url->ssl('account_login')); 
    	} 
			
    	$language->load('controller/account_address.php');

		$template->set('title', $language->get('heading_title'));

    	if (($request->get('address_id')) && ($this->validateDelete())) {
      		$database->query("delete from address where address_id = '" . (int)$request->get('address_id') . "' and customer_id = '" . (int)$customer->getId() . "'");
												
			$session->set('message', $language->get('text_delete'));
	  
	  		$response->redirect($url->ssl('account_address'));
    	}
	
		$template->set('content', $this->getList());

    	$template->set($module->fetch());

    	$response->set($template->fetch('layout.tpl'));	
  	}

  	function getList() {
		$address  =& $this->locator->get('address');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
			
    	$view = $this->locator->create('template');

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('text_address_book', $language->get('text_address_book'));
   
    	$view->set('button_new_address', $language->get('button_new_address'));
    	$view->set('button_edit', $language->get('button_edit'));
    	$view->set('button_delete', $language->get('button_delete'));
		$view->set('button_back', $language->get('button_back'));

    	$view->set('error', @$this->error['message']);
    
		$view->set('message', $session->get('message'));
		
    	$session->delete('message');

    	$address_data = array();
		
    	$results = $database->getRows("select *, c.name as country, z.name as zone from address a left join country c on a.country_id = c.country_id left join zone z on a.zone_id = z.zone_id where a.customer_id = '" . (int)$customer->getId() . "'");

    	foreach ($results as $result) {
      		$address_data[] = array(
        		'address_id' => $result['address_id'],
        		'address'    => $address->format($result, $result['address_format'], '<br />'),
        		'update'     => $url->ssl('account_address', 'update', array('address_id' => $result['address_id'])),
				'delete'     => $url->ssl('account_address', 'delete', array('address_id' => $result['address_id']))
      		);
    	}

    	$view->set('addresses', $address_data);

    	$view->set('insert', $url->ssl('account_address', 'insert'));

		$view->set('back', $url->ssl('account'));

    	return $view->fetch('content/account_addresses.tpl');
  	}

  	function getForm() {	
		$config   =& $this->locator->get('config');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
			
    	$view = $this->locator->create('template');

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('entry_firstname', $language->get('entry_firstname'));
    	$view->set('entry_lastname', $language->get('entry_lastname'));
    	$view->set('entry_company', $language->get('entry_company'));
    	$view->set('entry_address_1', $language->get('entry_address_1'));
    	$view->set('entry_address_2', $language->get('entry_address_2'));
    	$view->set('entry_postcode', $language->get('entry_postcode'));
    	$view->set('entry_city', $language->get('entry_city'));
    	$view->set('entry_country', $language->get('entry_country'));
    	$view->set('entry_zone', $language->get('entry_zone'));
    	$view->set('entry_default', $language->get('entry_default'));

    	$view->set('text_new_address', $language->get('text_new_address'));
    	$view->set('text_yes', $language->get('text_yes'));
    	$view->set('text_no', $language->get('text_no'));

    	$view->set('button_continue', $language->get('button_continue'));
    	$view->set('button_back', $language->get('button_back'));

    	$view->set('error_firstname', @$this->error['firstname']);
    	$view->set('error_lastname', @$this->error['lastname']);
    	$view->set('error_address_1', @$this->error['address_1']);
    	$view->set('error_city', @$this->error['city']);

    	$view->set('action', $url->ssl('account_address', $request->get('action'), array('address_id' => $request->get('address_id'))));

    	if (($request->get('address_id')) && (!$request->isPost())) {
      		$address_info = $database->getRow("select distinct * from address where address_id = '" . (int)$request->get('address_id') . "' and customer_id = '" . (int)$customer->getId() . "'");
		}
	
    	if ($request->has('firstname', 'post')) {
      		$view->set('firstname', $request->get('firstname', 'post'));
    	} else {
      		$view->set('firstname', @$address_info['firstname']);
    	}

    	if ($request->has('lastname', 'post')) {
      		$view->set('lastname', $request->get('lastname', 'post'));
    	} else {
      		$view->set('lastname', @$address_info['lastname']);
    	}

    	if ($request->has('company', 'post')) {
      		$view->set('company', $request->get('company', 'post'));
    	} else {
      		$view->set('company', @$address_info['company']);
    	}

    	if ($request->has('address_1', 'post')) {
      		$view->set('address_1', $request->get('address_1', 'post'));
    	} else {
      		$view->set('address_1', @$address_info['address_1']);
    	}

    	if ($request->has('address_2', 'post')) {
      		$view->set('address_2', $request->get('address_2', 'post'));
    	} else {
      		$view->set('address_2', @$address_info['address_2']);
    	}	

    	if ($request->has('postcode', 'post')) {
      		$view->set('postcode', $request->get('postcode', 'post'));
    	} else {
      		$view->set('postcode', @$address_info['postcode']);
    	}

    	if ($request->has('city', 'post')) {
      		$view->set('city', $request->get('city', 'post'));
    	} else {
      		$view->set('city', @$address_info['city']);
    	}

    	if ($request->has('country_id', 'post')) {
      		$view->set('country_id', $request->get('country_id', 'post'));
    	}  elseif (isset($address_info['country_id'])) {
      		$view->set('country_id', $address_info['country_id']);
    	} else {
      		$view->set('country_id', $config->get('config_country_id'));
    	}

    	if ($request->has('zone_id', 'post')) {
      		$view->set('zone_id', $request->get('zone_id', 'post'));
    	}  elseif (isset($address_info['zone_id'])) {
      		$view->set('zone_id', $address_info['zone_id']);
    	} else {
      		$view->set('zone_id', $config->get('config_zone_id'));
    	}

    	$view->set('countries', $database->cache('country', "select * from country order by name"));

    	$view->set('zones', $database->cache('zone', "select * from zone order by country_id, name"));

    	if ($request->has('default', 'post')) {
      		$view->set('default', $request->get('default', 'post'));
    	} else {
      		$view->set('default', ($customer->getAddressId() == $request->get('address_id')));
    	}

    	$view->set('back', $url->ssl('account_address'));

    	return $view->fetch('content/account_address.tpl');
  	}
	
  	function validateForm() {
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
			
    	if ((strlen($request->get('firstname', 'post')) < 3) || (strlen($request->get('firstname', 'post')) > 32)) {
      		$this->error['firstname'] = $language->get('error_firstname');
    	}

    	if ((strlen($request->get('lastname', 'post')) < 3) || (strlen($request->get('lastname', 'post')) > 32)) {
      		$this->error['lastname'] = $language->get('error_lastname');
    	}

    	if ((strlen($request->get('address_1', 'post')) < 3) || (strlen($request->get('address_1', 'post')) > 64)) {
      		$this->error['address_1'] = $language->get('error_address_1');
    	}

    	if ((strlen($request->get('city', 'post')) < 3) || (strlen($request->get('city', 'post')) > 32)) {
      		$this->error['city'] = $language->get('error_city');
    	}

    	if (!$this->error) {
      		return TRUE;
		} else {
      		return FALSE;
    	}
  	}

  	function validateDelete() {
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		
    	$result = $database->getRow("select count(*) as total from address where customer_id = '" . (int)$customer->getId() . "'");

    	if ($result['total'] == 1) {
      		$this->error['message'] = $language->get('error_delete');
    	}

    	if ($customer->getAddressId() == $request->get('address_id')) {
      		$this->error['message'] = $language->get('error_default');
    	}

    	if (!$this->error) {
      		return TRUE;
    	} else {
      		return FALSE;
    	}
  	}
	
  	function zone() {
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		
    	$output = '<select name="zone_id">';

    	$results = $database->cache('zone-' . (int)$request->get('country_id'), "select zone_id, name from zone where country_id = '" . (int)$request->get('country_id') . "' order by name");

    	foreach ($results as $result) {
      		$output .= '<option value="' . $result['zone_id'] . '"';
	
	  		if ($request->get('zone_id') == $result['zone_id']) {
	    		$output .= ' SELECTED';
	  		}
	
	  		$output .= '>' . $result['name'] . '</option>';
    	}

		if (!$results) {
      		$output .= '<option value="0">' . $language->get('text_none') . '</option>';
    	}

    	$output .= '</select>';
	
		$response->set($output);	
	}  
}
?>
