<?php

namespace Modules\Admin\ReportsDataStatus\Disable;

class Controller extends \App\Mvc\Controller\Form\PopupAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();
		$this->actionsCfg['index'] = ['checkSection' => 'Modules/Admin/ReportsDataStatus', 'checkOption' => false,];
	}

	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		return $R;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(array $R, \App\MessagesCollector $msgs)
	{
		$formData = $R;
		if ($formData['disable']) {

			if (strtolower($formData['confirmation']) !== strtolower($this->globalLang['yes'])) {
				$msgs->addError(sprintf($this->lang['messages']['confirmationNotOk'], $this->globalLang['yes']));
			}

			if ($msgs->hasErrorOrWarning()) {
				return false;
			}

			try {
				\App\ReportsData\Params::getAdmin()->saveStatusToDisabled();
				$msgs->addSuccess($this->lang['messages']['success']['disable']);
				return true;
			} catch (\App\MessagesException $e) {
				$msgs->appendMessages($e->getMessagesCollector());
				return false;
			}
		}

		if ($formData['enable']) {
			try {
				\App\ReportsData\Params::getAdmin()->saveStatusToPending();
				$msgs->addSuccess($this->lang['messages']['success']['enable']);
				return true;
			} catch (\App\MessagesException $e) {
				$msgs->appendMessages($e->getMessagesCollector());
				return false;
			}
		}
	}

}