<?php

namespace Modules\Admin\Config\UsersPasswordSecurity;


class Controller extends \App\Mvc\Controller\Form\WindowAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['index'] =     ['checkSection' => true,];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{

		$cfg=\App\Config\PasswordSecurity::getInstance()->getAll();

		return [
			'minLength'=>			$cfg['minLength'],
			'validity'=>			$cfg['validity'],
			'historyLimit'=>		$cfg['historyLimit'],
			'digitsCount'=>			$cfg['digitsCount'],
			'upperLettersCount'=>	$cfg['upperLettersCount'],
			'specialCharsCount'=>	$cfg['specialCharsCount'],
			'sessionLifetime' =>	round($cfg['sessionLifetime']/60, 2),
			'loginWithPassword' =>	$cfg['loginWithPassword'],
			'loginWithAzure' =>		$cfg['loginWithAzure'],
		];

	}


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

		try {

			$cfg=\App\Config\PasswordSecurity::getAdmin();

			$cfg->set('minLength', $formData['minLength']);
			$cfg->set('validity', $formData['validity']);
			$cfg->set('historyLimit', $formData['historyLimit']);
			$cfg->set('digitsCount', $formData['digitsCount']);
			$cfg->set('upperLettersCount', $formData['upperLettersCount']);
			$cfg->set('specialCharsCount', $formData['specialCharsCount']);
			$cfg->set('sessionLifetime', round($formData['sessionLifetime']*60));
			$cfg->set('loginWithPassword', $formData['loginWithPassword']);
			$cfg->set('loginWithAzure', $formData['loginWithAzure']);

			$cfg->save();

			$msgs->addSuccess($this->lang['index']['saveOkMsg']);
			return true;

		} catch (\App\Config\MessageException $e) {
			$msgs->addWarning($e->getMessage());
			return false;
		}

	}


}