<?php
class ControllerAccountEdit extends Controller {
	var $error = array();

	function index() {
		$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_edit'));

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

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

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

		if (($request->isPost()) && ($this->validate())) {
			$sql = "update customer set firstname = '?', lastname = '?', email = '?', telephone = '?' where customer_id = '?'";
			$database->query($database->parse($sql, trim($request->get('firstname', 'post')), trim($request->get('lastname', 'post')), trim($request->get('email', 'post')), trim($request->get('telephone', 'post')), $customer->getId()));

			$session->set('message', $language->get('text_message'));
  						echo '<script type="text/javascript">';
		    			echo 'alert("Modified sucessfully");';		    			
		    			echo '</script>';
			$response->redirect($url->ssl('account'));
		}

		$view = $this->locator->create('template');

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

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

		$view->set('entry_firstname', $language->get('entry_firstname'));
		$view->set('entry_lastname', $language->get('entry_lastname'));
		$view->set('entry_date_of_birth', $language->get('entry_date_of_birth'));
		$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_password', $language->get('entry_password'));
		$view->set('entry_confirm', $language->get('entry_confirm'));

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

		$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('action', $url->ssl('account_edit'));

		if (($customer->getId()) && (!$request->isPost())) {
			$customer_info = $database->getRow("select customer_id,firstname,lastname,email,telephone,nickname from customer where customer_id = '" . (int)$customer->getId() . "'");
		}
        if ($request->has('nickname', 'post')) {
			$view->set('nickname', $request->get('nickname', 'post'));
		} else {
			$view->set('nickname', @$customer_info['nickname']);
		}
		
		if ($request->has('firstname', 'post')) {
			$view->set('firstname', $request->get('firstname', 'post'));
		} else {
			$view->set('firstname', @$customer_info['firstname']);
		}

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

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

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

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

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

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

		$template->set('content', $view->fetch('content/account_edit.tpl'));

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

	function validate() {
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$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('email', 'post')) > 64) || (!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 ((strlen($request->get('telephone', 'post')) < 7) || (strlen($request->get('telephone', 'post')) > 20)) {
			$this->error['telephone'] = $language->get('error_telephone');
		}

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

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