<?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 = '?', fax = '?', status = '1' where customer_id = '?'";
			$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'), $customer->getId()));

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

			$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 * from customer where customer_id = '" . (int)$customer->getId() . "'");
		}

		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']);
		}

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

		$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->fetch());

		$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')) < 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('email', 'post')) > 32) || (!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 = '?' and customer_id != '?'", $request->get('email', 'post'), $customer->getId()))) {
			$this->error['message'] = $language->get('error_exists');
		}

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