<?php

namespace Modules\Utils\Calendar\Listing;

/**
 * The model is used in order to add a paginated list of dates
 * @author
 *
 */
class Model extends \App\Mvc\Model_ListingAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Model_ListingAbstract::list_execute()
	 */
	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{
		$filters = $listingParams->getFilters();

		$calendarEventsEnt = new \App\EntityList\CalendarEvents();
		$calendarEventsEnt->setColumns([ "title", "access","addTime", "idUser", "startDateTime", "endDateTime", "dismissDateTime", "mailStatus", "alertDateTime", "allDay"]);

		if ($filters['title']) {
			$calendarEventsEnt->setFilter('title', 'LIKE', "{$filters['title']}%");
		}

		if ($filters['addTime']['start']) {
			$formatedStartTime = \Engine\DateTime::createFromIsoDate($filters['addTime']['start'])->formatTime();
			$calendarEventsEnt->setFilter("addTime", ">=", $formatedStartTime);
		}
		if ($filters['addTime']['end']) {
			$formatedEndTime = \Engine\DateTime::createFromIsoDate($filters['addTime']['end'])->formatTime();
			$calendarEventsEnt->setFilter("addTime", "<=", $formatedEndTime);
		}

		if ($filters['alertDateTime']['start']) {
			$formatedStartTime = \Engine\DateTime::createFromIsoDate($filters['alertDateTime']['start'])->format("Y-m-d 00:00:00");
			$calendarEventsEnt->setFilter("alertDateTime", ">=", $formatedStartTime);
		}
		if ($filters['alertDateTime']['end']) {
			$formatedEndTime = \Engine\DateTime::createFromIsoDate($filters['alertDateTime']['end'])->format("Y-m-d 23:59:59");
			$calendarEventsEnt->setFilter("alertDateTime", "<=", $formatedEndTime);
		}

		if ($filters['startDateTime']['start']) {
			$formatedStartTime = \Engine\DateTime::createFromIsoDate($filters['startDateTime']['start'])->format("Y-m-d 00:00:00");
			$calendarEventsEnt->setFilter("startDateTime", ">=", $formatedStartTime);
		}
		if ($filters['startDateTime']['end']) {
			$formatedEndTime = \Engine\DateTime::createFromIsoDate($filters['startDateTime']['end'])->format("Y-m-d 23:59:59");
			$calendarEventsEnt->setFilter("startDateTime", "<=", $formatedEndTime);
		}


		if ($filters['dismissDateTime']['start']) {
			$formatedStartTime = \Engine\DateTime::createFromIsoDate($filters['dismissDateTime']['start'])->format("Y-m-d 00:00:00");
			$calendarEventsEnt->setFilter("dismissDateTime", ">=", $formatedStartTime);
		}
		if ($filters['dismissDateTime']['end']) {
			$formatedEndTime = \Engine\DateTime::createFromIsoDate($filters['dismissDateTime']['end'])->format("Y-m-d 23:59:59");
			$calendarEventsEnt->setFilter("dismissDateTime", "<=", $formatedEndTime);
		}


		if ($filters['idUser']['id']) {
			$calendarEventsEnt->setFilter('idUser', '=', $filters['idUser']['id']);
		}

		if ($filters['idUser']['id'] != CURRENT_ID_USER) {
			$calendarEventsEnt->setFilter('access', '=', 'public');
		}

		$calendarEventsEnt = $this->entityList_inheritListingParams($listingParams, $calendarEventsEnt); // for sorting purposes
		$this->recordsList = $calendarEventsEnt->exportRecords();
		$this->totalRecordsCount = $calendarEventsEnt->getRecordsCount();

	}

}