<?php

namespace Modules\Partners\Customers\Segments;

class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing
{
	/**
	 * Indicates if the entity execution should use or not a transaction.
	 * @var Boolean
	 */
	protected $useSqlTransaction = true;

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

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

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

	/**
	 *
	 * {@inheritDoc}
	 * @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':
				$option->determineOperationAvailability(\Entity\CustomerSegment::insert());
				break;

			case 'update':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\CustomerSegment::update($selectedItem))) {
						break; // if operation is unavailable, break
				}
			}
			break;

			case 'delete':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\CustomerSegment::delete($selectedItem))) {
						break; // if operation is unavailable, break
					}
				}
				break;

			default:
				return false;

		}

		return true;
	}

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

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::insert_createEntityObject()
	 */
	protected function insert_createEntityObject($formData)
	{
		$custSeg = \Entity\CustomerSegment::insert();
		$custSeg->name = $formData['segmentName'];
		return $custSeg;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::update_createEntityObject()
	 */
	protected function update_createEntityObject($formData)
	{
		$custSeg = \Entity\CustomerSegment::update($formData['id']);
		$custSeg->name = $formData['segmentName'];
		return $custSeg;

	}

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

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
	 */
	protected function update_getDefaultFormData($idEntity)
	{
		$formData = [];
		$custSeg = \Entity\CustomerSegment::select($idEntity);
		$formData['segmentName'] = $custSeg->name;

		return $formData;
	}


}