<?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->fetchAccount());	
    	$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 = '?', 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('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->fetchAccount());	
    	$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 = '?', 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('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', 'Your address has been successfully updated');
	  
	  		$response->redirect($url->ssl('account_address'));
    	} 
	  	
		$template->set('content', $this->getForm());

      	$template->set($module->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 
  	}
 
    function setdefault() {
			
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');		
		$request  =& $this->locator->get('request');		
		$response =& $this->locator->get('response');		
		$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')); 
    	} 		
   
       	$database->query("update customer set address_id = '" . (int)$request->get('address_id') . "' where customer_id = '" . (int)$customer->getId() . "'");      	
	  
	  	$response->redirect($url->ssl('account_address'));    		
  	}
	
  	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->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 
  	}

  	function getList() {
ini_set("display_errors", "On");
error_reporting(1);
		$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');
    	$request  =& $this->locator->get('request');
    	
    	$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');

    	if($request->has('page')){
		   $page=$request->get('page');
		}else{
			$page = 1;
		}    
		
    	$customer_id=$customer->getId();
    	$address_data = array();
		
    	$sql="select a.address_id,a.address_1,a.company,a.city,a.firstname,a.lastname,a.postcode, c.name as country, c.address_format, 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 = $customer_id";
		$results = $database->getRows($database->splitQuery($sql, $page, 4));
    	foreach ($results as $result) {
      		$address_data[] = array(
        		'address_id' => $result['address_id'],
        		'address'    => $address->format($result, $result['address_format'], '<br />'),
				'setdefault' => $url->ssl('account_address', 'setdefault', array('address_id' => $result['address_id'])),
        		'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);	
		
		$page_data = array();
	    for ($i = 1; $i <= $database->getPages(); $i++) {
				$page_data[] = array(
	  				'text'  => $language->get('text_pages', $i, $database->getPages()),
	  				'value' => $i
				);
	      	}
	    $view->set('pages', $page_data);  	      	        		
  		$view->set('page', $page);	
  		$view->set('text_results', $language->get('text_results',$page, $database->getPages()));		
  		$view->set('action', $url->href('account_address'));	
  		
		$customer_row = $database->getRow("select `address_id` from `customer` where `customer_id` = $customer_id");
		$view->set('default_address_id', $customer_row['address_id']);

    	$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_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 address_id,firstname,lastname,company,address_1,postcode,city,country_id,zone_id 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('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 country_id,name from country where allow_ship=1 order by name"));

    	$view->set('zones', $database->cache('zone', "select zone_id 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')) < 1) || (strlen($request->get('firstname', 'post')) > 32)) {
      		$this->error['firstname'] = $language->get('error_firstname');
    	}

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

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

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

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

  	function valid