<?php
namespace ModulesAbstract\PluginReports\Config;

/**
 * Controller that handles the report-config section
 * It should basically display a form in the main window, and save it into the DB when submitted
 * @author Lucian Sirbu
 */
abstract class ControllerAbstract extends \App\Mvc\ControllerAbstract
{
	protected $absLang= [];

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

		$this->absLang = \App\Loader_Lang::getInstance()->loadAppFolder('_customLoad/modulesAbstract/pluginReports/config')->getSection('ModulesAbstract/PluginReports/Config');

		$this->actionsCfg['index']= [];
		$this->actionsCfg['save']= [];

		// THIS IS DEFINED IN THE LOCAL REPORT CONTROLLER /////////////////////////////////////////
		// related to the action in this file
		// $this->actionsCfg['generateInputMatrixRow']= [];
	}

	abstract protected function createModel();

	/**
	 * Render the main page
	 * @param array $R
	 * @return \Engine\Mvc\Response\Html
	 */
	protected function actionIndex($R)
	{
		// instantiate the main view
		$view = $this->buildViewObjectFromCurrentModuleForAction('index');

		$model = $this->createModel();

		// NOTE: adding &config[ignoreConfigs]=1 to the URL will basically reset ALL configs. It should be used if the conig form somehow gets broken and no toher recovery is possible. !!! USE WITH CAUTION !!!
		if (SHOW_DEBUGS && isset($R['config']['ignoreConfigs'])) {
			$view->addInput('formFields', []);
		}
		else {
			$view->addInput('formFields', $model->getConfigVariables());
		}

		if (\Engine\Storage\General::getInstance('session')->get(sprintf("%s:saved", __CLASS__))) {
			$view->getMessagesCollector()->addSuccess($this->absLang['index']['action']['save']['success']);
			\Engine\Storage\General::getInstance('session')->delete(sprintf("%s:saved", __CLASS__));
		}

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

	protected function actionSave($R)
	{
		// instantiate the main view
		$view = $this->buildViewObjectFromCurrentModuleForAction('index');

		$model = $this->createModel();
		$model->setMessagesCollector($view->getMessagesCollector());

		$saved = false;
		if ($_POST) {
			try {
				$model->setConfigVariables($R);
				$saved = true;
				$view->getMessagesCollector()->addSuccess($this->absLang['index']['action']['save']['success']);
			}
			catch (\App\Config\MessageException $e) {
				$view->getMessagesCollector()->addWarning($e->getMessage());
			}
			catch(\App\MessagesException $e) {
				$view->getMessagesCollector()->appendMessages($e->getMessagesCollector());
			}
		}
		else {
			$saved = true;
		}

		if ($saved) {
			\Engine\Storage\General::getInstance('session')->set(sprintf("%s:saved", __CLASS__), true);
			return new \Engine\Mvc\Response\HtmlRedirect(\Engine\Mvc\Dispatcher::getInstance()->getAppUrl($this->getModule(), ''));
		}
		// else

		$view->addInput('formFields', $R);
		return new \Engine\Mvc\Response\Html($view->render());
	}

	/**
	 * DO NOT REMOVE THIS - IT IS USED IN DIFFERENT REPORTS FOR THE MATRIX IN SETUP
	 * Render the main page
	 * @param array $R
	 * @return \Engine\Mvc\Response\Html
	 */
	protected function actionGenerateInputMatrixRow($R)
	{
		$matrix = \Engine\Cache\General::getInstance('disk')->get($R['inputMatrixStorageId']);
		if (!$matrix) {
			throw new \Engine\Mvc\ControllerException("Matrix not found");
		}

		$view = $this->buildViewObjectFromCurrentModuleForAction('index');

		return new \Engine\Mvc\Response\Json(['responseRow' => $view->renderInputMatrixRowOnAjaxRequest($matrix, $R['index'], $R['counter'])]);
	}

}
