<?php 
class ControllerAccountCreate extends Controller {
	var $error = array();
	      
  	function index() {
		$cache    =& $this->locator->get('cache');
		$cart     =& $this->locator->get('cart');
		$config   =& $this->locator->get('config');
		$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()) {
	  		$response->redirect($url->ssl('account'));
    	}

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

    	$template->set('title', $language->get('heading_title'));
	
    	if (($request->isPost()) && ($this->validate())) {
      		$sql = "insert into customer set firstname = '?', lastname = '?', email = '?', telephone = '?', fax = '?', password = '?', newsletter = '?', status = '1', date_added = now()";
      		$database->query($database->parse($sql, $request->get('firstname', 'post'), $request->get('lastname', 'post'), $request->get('email', 'post'), $request->get('telephone', 'post'), $request->get('fax', 'post'), md5($request->get('password', 'post')), $request->get('newsletter', 'post')));

      		$customer->login($request->get('email', 'post'), $request->get('password', 'post'));

      		$sql = "insert into address set customer_id = '?', firstname = '?', lastname = '?', company = '?', address_1 = '?', address_2 = '?', city = '?', postcode = '?', country_id = '?', zone_id = '?'";
      		$database->query($database->parse($sql, $customer->getId(), $request->get('firstname', 'post'), $request->get('lastname', 'post'), $request->get('company', 'post'), $request->get('address_1', 'post'), $request->get('address_2', 'post'), $request->get('city', 'post'), $request->get('postcode', 'post'), $request->get('country_id', 'post'), $request->get('zone_id', 'post')));

      		$database->query("update customer set address_id = '" . (int)$database->getLastId() . "' where customer_id = '" . (int)$customer->getId() . "'");
			
			$mail = $this->locator->create('mail');
      		
			$mail->setTo($request->get('email', 'post'));
	  		$mail->setFrom($config->get('config_email'));
	  		$mail->setSender($config->get('config_store'));
	  		$mail->setSubject($language->get('email_subject', $config->get('config_store')));
	  		$mail->setCharacterSet($language->get('charset'));
	  		$mail->setText($language->get('email_message', $request->get('firstname', 'post'), $config->get('config_store'), $url->ssl('account_login'), $config->get('config_store')));
      		$mail->send();
	  	  
	  		$response->redirect($url->ssl('account_success'));
    	} 
 
    	$view = $this->locator->create('template');

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

		$view->set('text_yes', $language->get('text_yes'));
		$view->set('text_no', $language->get('text_no'));
    	$view->set('text_account_already', $language->get('text_account_already', $url->ssl('account_login')));
    	$view->set('text_your_details', $language->get('text_your_details'));
    	$view->set('text_your_address', $language->get('text_your_address'));
    	$view->set('text_your_password', $language->get('text_your_password'));
		$view->set('text_newsletter', $language->get('text_newsletter'));
				
    	$view->set('entry_firstname', $language->get('entry_firstname'));
    	$view->set('entry_lastname', $language->get('entry_lastname'));
    	$view->set('entry_email', $language->get('entry_email'));
    	$view->set('entry_telephone', $language->get('entry_telephone'));
    	$view->set('entry_fax', $language->get('entry_fax'));
    	$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_newsletter', $language->get('entry_newsletter'));
    	$view->set('entry_password', $language->get('entry_password'));
    	$view->set('entry_confirm', $language->get('entry_confirm'));

		$view->set('button_continue', $language->get('button_continue'));
    
		$view->set('error', @$this->error['message']);
		$view->set('error_firstname', @$this->error['firstname']);
    	$view->set('error_lastname', @$this->error['lastname']);
    	$view->set('error_email', @$this->error['email']);
    	$view->set('error_telephone', @$this->error['telephone']);
    	$view->set('error_password', @$this->error['password']);
    	$view->set('error_confirm', @$this->error['confirm']);
    	$view->set('error_address_1', @$this->error['address_1']);
    	$view->set('error_city', @$this->error['city']);

    	$view->set('action', $url->ssl('account_create'));

    	$view->set('firstname', $request->get('firstname', 'post'));

    	$view->set('lastname', $request->get('lastname', 'post'));

    	$view->set('email', $request->get('email', 'post'));

    	$view->set('telephone', $request->get('telephone', 'post'));

    	$view->set('fax', $request->get('fax', 'post'));

    	$view->set('company', $request->get('company', 'post'));

    	$view->set('address_1', $request->get('address_1', 'post'));

    	$view->set('address_2', $request->get('address_2', 'post'));

    	$view->set('postcode', $request->get('postcode', 'post'));

    	$view->set('city', $request->get('city', 'post'));

    	if ($request->has('country_id', 'post')) {
      		$view->set('country_id', $request->get('country_id', 'post'));
    	} 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'));
    	} else {
      		$view->set('zone_id', $config->get('config_zone_id'));
    	}

      	$view->set('agreed', $request->get('agreed', 'post'));
		
    	$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"));

    	$view->set('password', $request->get('password', 'post'));

    	$view->set('confirm', $request->get('confirm', 'post'));
		
		$view->set('newsletter', $request->get('newsletter', 'post'));

		if ($config->get('config_account_id')) {
			$information_info = $database->getRow("select * from information i left join information_description id on (i.information_id = id.information_id) where i.information_id = '" . (int)$config->get('config_account_id') . "'");
		
			$view->set('agree', $language->get('text_agree', $url->href('information', FALSE, array('information_id' => $config->get('config_account_id'))), $information_info['title']));
		}
						
		$template->set('content', $view->fetch('content/account_create.tpl'));
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));	
  	}

  	function validate() {
		$database =& $this->locator->get('database');
		$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 (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $request->get('email', 'post'))) {
      		$this->error['email'] = $language->get('error_email');
    	}

    	if ($database->getRow($database->parse("select * from customer where email = '?'", $request->get('email', 'post')))) {
      		$this->error['message'] = $language->get('error_exists');
    	}

    	if ((strlen($request->get('password', 'post')) < 4) || (strlen($request->get('password', 'post')) > 20)) {
      		$this->error['password'] = $language->get('error_password');
    	}

    	if ($request->get('confirm', 'post') != $request->get('password', 'post')) {
      		$this->error['confirm'] = $language->get('error_confirm');
    	}

    	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 ((strlen($request->get('telephone', 'post')) < 3) || (strlen($request->get('telephone', 'post')) > 32)) {
      		$this->error['telephone'] = $language->get('error_telephone');
    	}

    	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);
  	}  
}
?>
