<?php

namespace Modules\Admin\UserProfile;

class Controller extends \App\Mvc\Controller\Form\WindowAbstract
{

	/**
	 * the index action was changed because the language must be saved in cookie
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::actionIndex()
	 */
	protected function actionIndex($R)
	{
		// collects messages
		$msgs = \App\MessagesCollector::create();
		global $S;

		$modelOutput = null;

		if ($R[$this->formSubmitButtonName]) {
			$modelOutput = $this->processFormData($R, $msgs);
		}
		if ($modelOutput) {
			$S['global']['lang'] = $R['language'];
			setcookie("tinaLang", $R['language'], time()+604800);
			return new \Engine\Mvc\Response\HtmlRedirect(\Engine\Mvc\Dispatcher::getInstance()->getAppUrl($this->module, 'index'));
		}

		$view = new View_Index($this);

		// get form data for this operation
		if (!$R[$this->formSubmitButtonName]) {
			$formData=$this->getDefaultFormData($R);
		} else {
			$formData=$R;
		}

		// send data to the view
		$view->addInput('formData', $formData);
		$view->getMessagesCollector()->appendMessages($msgs);
		$view->addInput('formSubmitButtonName', $this->formSubmitButtonName);

		// do some more stuff (if needed)
		$this->preRenderOperations($view, $formData);

		// render the view
		return new \Engine\Mvc\Response\Html($view->render());
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		$user = \Entity\User::select(CURRENT_ID_USER);

		$formData['lastname'] = 		$user->lastName;
		$formData['firstname'] = 		$user->firstName;
		$formData['title'] = 			$user->titleCode;
		$formData['language'] = 		$user->language;
		$formData['series'] = 			$user->ID_series;
		$formData['number'] = 			$user->ID_number;
		$formData['issuedBy'] = 		$user->ID_releaseBy;
		$formData['issueDate'] = 		$user->ID_releaseDate;
		$formData['pnc'] = 				$user->PNC;
		$formData['address'] = 			$user->address;
		$formData['email'] = 			$user->email;
		$formData['emailSignature'] = 	$user->emailSignature;
		$formData['birthday'] = 		$user->birthday;
		$formData['tel'] = 				$user->telephone;

		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{
		global $S;

		$return = null;
		try {
			\DB::startTransaction();

			$user = \Entity\User::update(CURRENT_ID_USER);

			$user->language = $R['language'];

			// user can update personal data
			if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('userCanUpdatePersonalData')) {
				$user->lastName = 			$R['lastname'];
				$user->firstName = 			$R['firstname'];
				$user->title =	 			$R['title'];
				$user->ID_series = 			$R['series'];
				$user->ID_number = 			$R['number'];
				$user->ID_releaseBy = 		$R['issuedBy'];
				$user->ID_releaseDate = 	$R['issueDate'] ? $R['issueDate'] : null;
				$user->PNC = 				$R['pnc'];
				$user->birthday = 			$R['birthday'] ? $R['birthday'] : null;
			}
			// user can update contact data
			if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('userCanUpdateContactData')) {
				$user->address = 			$R['address'];
				$user->email = 				$R['email'];
				$user->telephone = 			$R['tel'];
			}
			// user can update his signature
			if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('userCanUpdateSignature')) {
				$user->emailSignature = 	$R['emailSignature'];
			}

			$user->save();

			\DB::commitTransaction();

			if ($S['global']['lang'] != $R['language']){
				$return = true; // the default language must be changed
			}
			else {
				$return = false;
			}

			foreach ($user->getMessages()->getMessages() as $msg) {
				$msgs->addMessage($msg);
			}
		}
		catch (\App\Entity\Exception $e) {
			\DB::rollbackTransaction();

			$msgs->appendMessages($e->getMessagesCollector());
		}
			
		return $return;
	}

}