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

class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing
{
	/**
	 * The list of columns which support ordering.
	 * @var array
	 */
	protected $listOrderingColumns = ['addTime', 'lastName'];

	protected $useSqlTransaction = true;

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

		$this->actionsCfg['insert'] = ['checkSection' => false, 'checkOption' => false, ];
		$this->actionsCfg['update'] = ['checkSection' => false, 'checkOption' => false, ];
		$this->actionsCfg['delete'] = ['checkSection' => false, 'checkOption' => false, ];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionDefineParams()
	 */
	protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection)
	{
		switch($option->getId()){
			case 'insert':
				if (!$option->determineOperationAvailability(\Entity\ContactPerson::insert())) {
					$option->setDisabled();
				}
			break;

			case 'update':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\ContactPerson::update($selectedItem))) {
						break;
					}
				}
			break;

			case 'delete':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\ContactPerson::delete($selectedItem))) {
						break;
					}
				}
			break;

			default:
				return false;
		}

		return true;

	}

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

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::insert_getDefaultFormData()
	 */
	protected function insert_getDefaultFormData($R)
	{
		$formData = [];
		if ($R['corporate']) {
			$formData['idCompany'] = $R['corporate']['id']."|0";
		}
		if ($R['office']) {
			$corporate = \Entity\CorporateOffice::select($R['office']['id'])->idCorporate;
			$formData['idCompany'] = $corporate."|".$R['office']['id'];
		}

		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::insert_createEntityObject()
	 */
	protected function insert_createEntityObject($formData)
	{

		if ($formData['idCompany']['id']) {
			list($idCorporate, $idCorporateOffice) = explode("|", $formData['idCompany']['id']);
		}

		$person = \Entity\ContactPerson::insert();
		$person->_exportToGds = true;

		$person->type 			= 'corporate';
		$person->title 			= (String)$formData['title'];
		$person->firstName		= $formData['firstName'];
		$person->lastName 		= $formData['lastName'];
		$person->job 			= $formData['job'];
		$person->role 			= $formData['role'];
		$person->department		= $formData['department'];
		$person->birthday 		= $formData['birthday'] ? $formData['birthday'] : null;
		$person->email 			= $formData['email'];
		$person->telephone 		= $formData['telephone'];
		$person->telephoneMobile= $formData['telephoneMobile'];
		$person->fax 			= $formData['fax'];
		$person->comments 		= $formData['comments'];
		$person->remarks 		= $formData['remarks'];

		$person->idCompany 		= $idCorporate;
		if ($idCorporateOffice) {
			$person->idOffice	= $idCorporateOffice;
		}

		return $person;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
	 */
	protected function update_getDefaultFormData($idEntity)
	{
		$person = \Entity\ContactPerson::select($idEntity);

		$formData = [];
		$formData['type'] 			= $person->type;
		$formData['idCompany'] 		= $person->idCompany;
		$formData['title'] 			= $person->title;
		$formData['firstName'] 		= $person->firstName;
		$formData['lastName']		= $person->lastName;
		$formData['job'] 			= $person->job;
		$formData['role'] 			= $person->role;
		$formData['department'] 	= $person->department;

		if ($person->idOffice) {
			$formData['idOffice'] 	= $person->idCompany."|".$person->idOffice;
		}

		$formData['birthday'] 		= $person->birthday;
		$formData['email'] 			= $person->email;
		$formData['telephone'] 		= $person->telephone;
		$formData['telephoneMobile']= $person->telephoneMobile;
		$formData['fax'] 			= $person->fax;
		$formData['comments']		= $person->comments;
		$formData['remarks']		= $person->remarks;

		return $formData;

	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::update_createEntityObject()
	 */
	protected function update_createEntityObject($formData)
	{
		$person = \Entity\ContactPerson::update($this->updateIdRecord);
		$person->_exportToGds = true;

		$person->title			= $formData['title'];
		$person->firstName		= $formData['firstName'];
		$person->lastName		= $formData['lastName'];
		$person->job			= $formData['job'];
		$person->role			= $formData['role'];
		$person->department		= $formData['department'];
		$person->birthday		= $formData['birthday'] ? $formData['birthday'] : null;
		$person->email			= $formData['email'];
		$person->telephone		= $formData['telephone'];
		$person->telephoneMobile= $formData['telephoneMobile'];
		$person->fax			= $formData['fax'];
		$person->comments		= $formData['comments'];
		$person->remarks		= $formData['remarks'];

		if ($formData['idOffice']['id']) {
			list(, $idCorporateOffice) = explode("|", $formData['idOffice']['id']);
			$person->idOffice	= $idCorporateOffice;
		}
		else {
			$person->idOffice	= '0';
		}

		return $person;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::delete_createEntityObject()
	 */
	protected function delete_createEntityObject($idRecord)
	{
		return \Entity\ContactPerson::delete($idRecord);
	}
}
