<?php

namespace Modules\Utils\Calendar\Listing;

/**
 * Controller for the newly built-MVC `Calendar` section
 * @author FlorinRusu
 *
 */
/**
 * Controller for the calendarEvent section - does insert/update
 * @author FlorinRusu
 *
 */
class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing
{
	/**
	 * 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 = true;


	protected $listOrderingColumns = ['alertDateTime', 'startDateTime', 'addTime'];

	/**
	 * (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, ];

	}



	protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection)
	{
		switch($option->getId()){

			case 'insert':
				$option->determineOperationAvailability(\Entity\CalendarEvent::insert());
				break;

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

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

			default:
				return false;
		}
		return true;
	}

	protected function optionGetEntityName($id)
	{
		return $id;
	}


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::insert_getDefaultFormData()
	 */
	protected function insert_getDefaultFormData($R)
	{
		$defaultFormData = [];
		$defaultFormData['startDateTime'] = $this->cfg['defaultFields']['startDateTime'];
		$defaultFormData['endDateTime'] = $this->cfg['defaultFields']['endDateTime'];

		$defaultFormData['startDate'] = $this->cfg['defaultFields']['startDate'];
		$defaultFormData['endDate'] = $this->cfg['defaultFields']['endDate'];

		$defaultFormData['alertPeriod'] =  $this->cfg['defaultFields']['alertPeriod'];

		$defaultFormData['access'] = $this->cfg['defaultFields']['access'];
		$defaultFormData['idUser']['id']=$this->cfg['defaultFields']['idUser'];

		return $defaultFormData;
	}


	protected function update_getDefaultFormData($idEntity)
	{
		$entityObj = \Entity\CalendarEvent::select($idEntity);
		if (!$entityObj->recordExists()) {
			return null;
		}

		$defaultFormData = [];
		$defaultFormData['title'] = $entityObj->title;
		$defaultFormData['description'] = $entityObj->description;
		$defaultFormData['location'] = $entityObj->location;
		$defaultFormData['allDay'] = $entityObj->allDay;
		$defaultFormData['startDateTime'] = $entityObj->startDateTime;
		$defaultFormData['endDateTime'] = $entityObj->endDateTime;
		$defaultFormData['startDate'] = \Engine\DateTime::createFromIsoDatetime($entityObj->startDateTime)->formatDateIso();
		$defaultFormData['endDate'] = \Engine\DateTime::createFromIsoDatetime($entityObj->endDateTime)->formatDateIso();
		$defaultFormData['mailStatus'] = $entityObj->mailStatus;
		$defaultFormData['idUser'] = $entityObj->idUser;
		$defaultFormData['access'] = $entityObj->access;
		$defaultFormData['alertDateTime'] = $entityObj->alertDateTime;

		return $defaultFormData;
	}


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

		$entityObj = \Entity\CalendarEvent::insert();
		$entityObj->title = $formData['title'];
		$entityObj->description = $formData['description'];
		$entityObj->location = $formData['location'];
		$entityObj->allDay = $formData['allDay'];

		if (!$formData['allDay']) {
			$entityObj->startDateTime	= $formData['startDateTime'];
			$entityObj->endDateTime		= $formData['endDateTime'];
			if ($formData['alertPeriod']) {
				$entityObj->alertDateTime = date(DATETIME_FORMAT, strtotime($formData['startDateTime'].' '.$formData['alertPeriod']));
			}
		} else {
			$entityObj->startDateTime	= $formData['startDate'];
			$entityObj->endDateTime		= $formData['endDate'];
			if ($formData['alertPeriod']) {
				$entityObj->alertDateTime = date(DATETIME_FORMAT, strtotime($formData['startDate'].' '.$formData['alertPeriod']));
			}
		}

		$entityObj->mailStatus	= (int)$formData['mailStatus'];
		$entityObj->idUser		= $formData['idUser']['id'];
		$entityObj->access 		= $formData['access'];
		$entityObj->idAddUser	= CURRENT_ID_USER;

		return $entityObj;

	}

	protected function update_createEntityObject($formData)
	{
		$entityObj = \Entity\CalendarEvent::update($this->updateIdRecord);
		$entityObj->title = $formData['title'];
		$entityObj->description = $formData['description'];
		$entityObj->location = $formData['location'];
		$entityObj->allDay = $formData['allDay'];

		if (!$formData['allDay']) {
			$entityObj->startDateTime	= $formData['startDateTime'];
			$entityObj->endDateTime		= $formData['endDateTime'];
		} else {
			$entityObj->startDateTime	= $formData['startDate'];
			$entityObj->endDateTime		= $formData['endDate'];
		}

		$entityObj->alertDateTime = $formData['alertDateTime'];
		$entityObj->mailStatus	= (int)$formData['mailStatus'];
		$entityObj->access 		= $formData['access'];

		return $entityObj;

	}

	protected function delete_createEntityObject($idRecord)
	{
		return \Entity\CalendarEvent::delete($idRecord);
	}




}