<?php
namespace Modules\Partners\Corporates\Dashboard;

class Controller extends \ModulesAbstract\Dashboard\Controller
{
	// define actions
	protected $actionsCfg = [
		'index'	=> 					['checkSection' => true, 'checkOption' => false],
		'indexWithoutCorporate'	=> 	['checkSection' => true, 'checkOption' => false],
		'selectFirstTemplate' => 	['checkSection' => false, 'checkOption' => false],
		'changeTemplate' => 		['checkSection' => false, 'checkOption' => false],
		'editDashboardElement' => 	['checkSection' => false, 'checkOption' => false],
		'editWidgetElement' => 		['checkSection' => false, 'checkOption' => false],
		'renderElement' => 			['checkSection' => false, 'checkOption' => false],
		'callMethod' => 			['checkSection' => false, 'checkOption' => false],
		'lock' =>					['checkSection' => false, 'checkOption' => false],
		'unlock' =>					['checkSection' => false, 'checkOption' => false],
	];

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\Dashboard\Controller::getDashboardId()
	 */
	protected function getDashboardId()
	{
		return 'Modules\\Partners\\Corporates\\Dashboard\\Dashboard';
	}

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\Dashboard\Controller::getStorageObj()
	 */
	protected function getStorageObj()
	{
		$storageObj = new \App\Dashboard\Storage\Database();
		return $storageObj;
	}

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\Dashboard\Controller::populateOption()
	 */
	protected function populateOption(\Engine\Dashboard\GeneralAbstract $dashboardObj, $idEntity)
	{
		$dashboardObj->setOption('idCorporate', $idEntity);
		$dashboardObj->setTitle($this->lang['dashboard']['title']);
	}

	/**
	 * Action index
	 */
	protected function actionIndex($R)
	{
		// display dashboard for corporate
		if ($R['idEntity']) {
			return parent::actionIndex($R);
		}

		return $this->execute('indexWithoutCorporate', $R);

	}

	/**
	 * Render index withou corporate
	 * @param array $R
	 * @return \Engine\Mvc\Response\Html
	 */
	protected function actionIndexWithoutCorporate($R)
	{
		$msgs = new \App\MessagesCollector();

		if ($R['save']) {
			// check idCorporate
			if (!$R['corporate']['id']) {
				$msgs->addError($this->lang['indexWithoutCorporate']['msgs']['noCorporateSelected']);
			}
			else {
				$params['idEntity'] = $R['corporate']['id'];
				return $this->execute('index', $params);
			}
		}

		$view = new View_IndexWithoutCorporate($this);
		$view->getMessagesCollector()->appendMessages($msgs);

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