<?php
namespace Modules\Utils\Updater\UpdateCurrentVersion;

class Controller extends \App\Mvc\Controller\Form\PopupAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::initialize()
	 */
	protected function initialize()
	{
		$this->actionsCfg['index'] =     		['authType' => 'none',  'checkSection' => false, 'checkOption' => false,];
		$this->actionsCfg['postRedirect'] =     ['authType' => 'none',  'checkSection' => false, 'checkOption' => false,];
	}

	/**
	 * Set form default fields - if one system - select it.
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		return [];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::preRenderOperations()
	 */
	protected function preRenderOperations(\App\Mvc\ViewAbstract $view, array $formData)
	{
		if (!$formData[$this->formSubmitButtonName]) {
			$msgs = new \App\MessagesCollector();

			try {
				$modelObject = new \Modules\Utils\Updater\UpdateCurrentVersion\Model();
				$modelObject->validate($msgs);

				$msgs->addInfo($this->lang['index']['successCheck']);
			}
			catch (\App\MessagesException $ex) {
				$msgs = $ex->getMessagesCollector();
			}

			$view->getMessagesCollector()->appendMessages($msgs);
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $formData, \App\MessagesCollector $msgs)
	{
		try {
			$modelObject = new \Modules\Utils\Updater\UpdateCurrentVersion\Model();
			$modelObject->validate($msgs);
			$modelObject->execute();

//			$redirectUrl = \Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/Utils/Updater/UpdateCurrentVersion', 'postRedirect');
			$redirectUrl = APP_URL."../tina/disp.php?module=Utils/Updater/UpdateCurrentVersion&action=postRedirect"; // we cannot use \Engine\Mvc\Dispatcher::getInstance() as we are running this from [TINA_URL]/new/

			header("Location: ".$redirectUrl);
			exit();
		}
		catch (\App\MessagesException $ex) {
			$msgs->appendMessages($ex->getMessagesCollector());
			return false;
		}

		return true;
	}


	/**
	 * Action 'postRedirect' used after renaming the directory 'new' in 'tina'
	 * @param Array $R
	 * @return \Engine\Mvc\Response\Html
	 */
	protected function actionPostRedirect($R)
	{
		// collects messages
		$msgs = \App\MessagesCollector::create();

		// mark kit as installed
		try {
			$modelObject = new \Modules\Utils\Updater\UpdateCurrentVersion\Model();
			$modelObject->markKitAsInstalled();

			// attach messages info
			$msgs->addSuccess($this->lang['postRedirect']['success']);
		}
		catch (\App\MessagesException $ex) {
			$msgs->appendMessages($ex->getMessagesCollector());
		}

		// restart main cron
		$shouldStartMainCron = false;
		$main = \App\Cron\Main::getInstance();
		if ($main->getStatus()=='should-restart' && $main->isAlreadyRunning()) {
			\Log::getInstance()->warning(sprintf("[%s] Main cronjob was marked for restart, but it's still running at this moment", __METHOD__));
		}
		// main cronjob was set for restart
		if ($main->getStatus()=='should-restart' && !$main->isAlreadyRunning()) {
			$shouldStartMainCron = true;
			\Log::getInstance()->info(sprintf("[%s] Main cronjob was marked for restart, restarting now", __METHOD__));
		}
		if ($shouldStartMainCron) {
			try {
				$main->start();
			}
			catch (\RuntimeException $ex) {
				\Log::getInstance()->error(sprintf("[%s] Main cronjob wasn't started. Error: %s", __METHOD__, $ex->getMessage()));
			}
		}

		// instantiate the view
		$view = new View_PostRedirect($this);

		// attach messages to view
		$view->getMessagesCollector()->appendMessages($msgs);

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

}