<?php

namespace App\Mvc\Controller\Form;

/**
 * The class manages the controller for a general form page in a popup.
 * @author Gabriel
 */
abstract class PopupAbstract extends \App\Mvc\Controller\Form\WindowAbstract
{

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\ControllerAbstract::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

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

	/**
	 * The main "save form" action.
	 * @return string
	 */
	protected function actionIndex($R)
	{

		// collects messages
		$msgs = \App\MessagesCollector::create();

		$modelOutput = null;
		// SAVE THE FORM DATA /////////////////////////////////////////////////////////////////////
		if ($R[$this->formSubmitButtonName]??null) {
			$modelOutput = $this->processFormData($R, $msgs);
		}

		// instantiate the view
		$view = $this->buildViewObjectFromCurrentModuleForAction($this->getAction());

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

		// send data to the view
		$view->addInput('formData', $formData);
		$view->getMessagesCollector()->appendMessages($msgs);
		$view->addInput('formSubmitButtonName', $this->formSubmitButtonName);
		if ($modelOutput) {
			$view->setClosePopup(true);
		}

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

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

	}



}