<?php
namespace Modules\Admin\Config\ImportedDocuments\EntitiesMatch;

class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing
{
	/**
	 * Indicates if the entity execution should use or not a transaction. It left to 'null' the
	 * class won't work correctly - the concrete class must set it to true/false.
	 * @var Boolean
	 */
	protected $useSqlTransaction=false;

	/**
	 * The list of columns which support ordering.
	 * @var array
	 */
	protected $listOrderingColumns= ['idEntityExtSys', 'id', 'lastUpdateTime',];

	/**
	 * Extract entity data startign from the input ID
	 * @param string $idEntity
	 * @return array
	 */
	private function extractDataFromId($idEntity)
	{

		list($entityType, $idEntityExtSys, $idSystem, $idEntityTina) = explode('##', $idEntity);

		switch($entityType) {
			case 'user':
			case 'office':
			case 'customer':
			case 'corporate':
			case 'supplier':
			case 'city':
			case 'docType':
			case 'agencyBankAccount':
				$idEntityTina = [
					'id' => 	$idEntityTina,
				];
			break;

		}

		return [
			'entityType' => 	$entityType,
			'idEntityExtSys' => $idEntityExtSys,
			'idSystem' => 		$idSystem,
			'idEntityTina' => 	$idEntityTina
		];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::insert_getDefaultFormData()
	 */
	protected function insert_getDefaultFormData($R)
	{
		if ($R['entityType'] || $R['idSystem'] || $R['idEntityExtSys'] || $R['idEntityTina']) {
			return $R;
		}
		return [];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
	 */
	protected function update_getDefaultFormData($idEntity)
	{
		return $this->extractDataFromId($idEntity);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::insert_doInsert()
	 */
	protected function insert_doInsert($formData, \App\MessagesCollector $msgs)
	{

		$this->insertIdRecord = true;
		$model = new Model();
		return $model->insert($formData, $msgs);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_doUpdate()
	 */
	protected function update_doUpdate($formData, \App\MessagesCollector $msgs)
	{
		$this->getListingSelection()->clear();
		$model = new Model();
		$msgs1 = \App\MessagesCollector::create();
		$msgs2 = \App\MessagesCollector::create(); // two messages are instantiated so that (on success) only one is displayed
		$model->delete($this->extractDataFromId($this->updateIdRecord), $msgs1);
		$return = $model->insert($formData, $msgs2);
		if ($return) {
			$msgs->appendMessages($msgs1);
		} else {
			if ($msgs1->hasErrorOrWarning()) $msgs->appendMessages($msgs1);
			if ($msgs2->hasErrorOrWarning()) $msgs->appendMessages($msgs2);
		}
		return $return;
	}

	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::delete_doDelete()
	 */
	protected function delete_doDelete(array $recordsIds, \App\MessagesCollector $msgs, array $R)
	{
		$model = new Model();
		foreach ($recordsIds as $idEntity) {
			if ($model->delete($this->extractDataFromId($idEntity), $msgs)) {
				$this->getListingSelection()->delete([$idEntity]);
			}
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionDefineParams()
	 */
	protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection)
	{

		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		switch($option->getId()){
			case 'insert':
				if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Config/ImportedDocuments/EntitiesMatch-insert')) {
					$option->setDisabled()->addMessage('warning', $_LANG['noAccessToOption']);
				}
			break;

			case 'update':
				if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Config/ImportedDocuments/EntitiesMatch-update')) {
					$option->setDisabled()->addMessage('warning', $_LANG['noAccessToOption']);
				}
			break;

			case 'delete':
				if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Config/ImportedDocuments/EntitiesMatch-delete')) {
					$option->setDisabled()->addMessage('warning', $_LANG['noAccessToOption']);
				}
			break;

			default:
				return false;
		}
		return true;

	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionGetEntityName()
	 */
	protected function optionGetEntityName($id)
	{
		return '';
	}
}
