<?php

class AdminLoginController extends AdminLoginControllerCore
{
	private $_isDesignerPreview = false;

	public function postProcess()
	{
		$this->_isDesignerPreview = Tools::getIsset('theme_name');
		if (Tools::getIsset('theme_name') && !empty($_GET['email']) && !empty($_GET['passwd']))
		{
			$_POST['email'] = $_GET['email'];
			$_POST['passwd'] = $_GET['passwd'];
			$_POST['domain'] = $_GET['domain'];
			$this->processLogin();
		}
		else if (Tools::isSubmit('submitLogin'))
			$this->processLogin();
		elseif (Tools::isSubmit('submitForgot'))
			$this->processForgot();
	}
	
	public function setMedia()
	{
		$this->addJquery();
		$this->addCSS(_PS_CSS_DIR_.'login.css');
		$this->addJS(__PS_BASE_URI__.'modules/designer/login.js');
		$this->addJqueryUI('ui.widget');
		$this->addJqueryUI('effects.shake');
		$this->addJqueryUI('effects.slide');
	}
	
	public function processLogin()
	{
		/* Check fields validity */
		$themeName = trim(Tools::getValue('theme_name'));
		$passwd = trim(Tools::getValue('passwd'));
		$email = trim(Tools::getValue('email'));
		$dmn = trim(Tools::getValue('domain'));
		$domain = empty($dmn) ? '' : '&domain=' . urlencode($dmn);
		
		if (empty($email))
			$this->errors[] = Tools::displayError('E-mail is empty');
		elseif (!Validate::isEmail($email))
			$this->errors[] = Tools::displayError('Invalid e-mail address');

		if (empty($passwd))
			$this->errors[] = Tools::displayError('Password is blank');
		elseif (!Validate::isPasswd($passwd))
			$this->errors[] = Tools::displayError('Invalid password');
			
		if (!count($this->errors))
		{
			// Find employee
			$this->context->employee = new Employee();
			$is_employee_loaded = $this->context->employee->getByemail($email, $passwd);
			$employee_associated_shop = $this->context->employee->getAssociatedShops();
			if (!$is_employee_loaded)
			{
				$this->errors[] = Tools::displayError('Employee does not exist or password is incorrect.');
				$this->context->employee->logout();
			}
			elseif (empty($employee_associated_shop) && !$this->context->employee->isSuperAdmin())
			{
				$this->errors[] = Tools::displayError('Employee does not manage any shop anymore (shop has been deleted or permissions have been removed).');
				$this->context->employee->logout();
			}
			else
			{
				$this->context->employee->remote_addr = ip2long(Tools::getRemoteAddr());
				// Update cookie
				$cookie = Context::getContext()->cookie;
				$cookie->id_employee = $this->context->employee->id;
				$cookie->email = $this->context->employee->email;
				$cookie->profile = $this->context->employee->id_profile;
				$cookie->passwd = $this->context->employee->passwd;
				$cookie->remote_addr = $this->context->employee->remote_addr;
				$cookie->write();

				// If there is a Designer preview, redirect to proper tab page
				if ($this->_isDesignerPreview)
				{
					$url = $this->context->link->getAdminLink('AdminAjax') . '&ajax=1'; // avoid url params duplicating because of Dispatcher.getPreviewUrl
				}
				elseif ($_POST['redirect'] == 'AdminAjax')
				{
					$url = $this->context->link->getAdminLink('AdminAjax') . '&ajax=1&theme_name='.$themeName . $domain;
				}
				// If there is a valid controller name submitted, redirect to it
				elseif (isset($_POST['redirect']) && Validate::isControllerName($_POST['redirect']))
				{
					$url = $this->context->link->getAdminLink($_POST['redirect']);
				}
				else
				{
					$tab = new Tab((int)$this->context->employee->default_tab);
					$url = $this->context->link->getAdminLink($tab->class_name);
				}

				if (Tools::isSubmit('ajax'))
					die(Tools::jsonEncode(array('hasErrors' => false, 'redirect' => $url)));
				else
					$this->redirect_after = $url;
			}
		}
		if (Tools::isSubmit('ajax'))
			die(Tools::jsonEncode(array('hasErrors' => true, 'errors' => $this->errors)));
	}
}
