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

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


	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))) {
					// stop on first unavailable item
						break;
					}
				}
			break;

			case 'delete':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\ContactPerson::delete($selectedItem))){
					// stop on first unavailable item
						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['supplier']) {
			$formData['idCompany'] = $R['supplier'];
		}

		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::insert_createEntityObject()
	 */
	protected function insert_createEntityObject($formData)
	{
		$person = \Entity\ContactPerson::insert();

		$person->type 			= 'supplier';
		$person->idCompany 		= $formData['idCompany']['id'];
		$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'];

		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'] 			= (String)$person->title;
		$formData['firstName'] 		= $person->firstName;
		$formData['lastName']		= $person->lastName;
		$formData['job'] 			= $person->job;
		$formData['role'] 			= $person->role;
		$formData['department'] 	= $person->department;
		$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->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'];

		return $person;
	}


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