<?php

namespace Modules\TaskManagement\Tasks;

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();

		// get the tasks list
		$listObj = new \App\EntityList\Tasks();
		$listObj->setColumns(['taskDate', 'status', 'statusReason', 'description', 'idCategory', 'idDepartment', 'idOffice', 'idResponsibleUser', 'idAssignorUser',
				'free', 'priority', 'deadline', 'estimatedFinishTime', 'estimatedDuration', 'pivot', 'idPivot', 'idOperationType', 'comments', 'addTime']);

		// taskDate
		if ($filters['taskDate']['start']) {
			$listObj->setFilter('taskDate', '>=', $filters['taskDate']['start']);
		}
		if ($filters['taskDate']['end']) {
			$listObj->setFilter('taskDate', '<=', $filters['taskDate']['end']);
		}

		// deadline
		if ($filters['deadline']['start']) {
			$listObj->setFilter('DATE_FORMAT( FROM_UNIXTIME( deadline ), "%Y-%m-%d")', '>=', $filters['deadline']['start']);
		}
		if ($filters['deadline']['end']) {
			$listObj->setFilter('DATE_FORMAT( FROM_UNIXTIME( deadline ), "%Y-%m-%d")', '<=', $filters['deadline']['end']);
		}

		// category
		if ($filters['idCategory']) {
			$listObj->setFilter('idCategory', '=', $filters['idCategory']);
		}

		// priority
		if ($filters['priority'] && $filters['priority']!='all') {
			$listObj->setFilter('priority', '=', $filters['priority']);
		}

		// idDepartment
		if ($filters['idDepartment']['id']) {
			$listObj->setFilter('idDepartment', '=', $filters['idDepartment']['id']);
		}

		// idOffice
		if ($filters['idOffice']['id']) {
			$listObj->setFilter('idOffice', '=', $filters['idOffice']['id']);
		}

		// idAssignorUser
		if ($filters['idAssignorUser']['id']) {
			$listObj->setFilter('idAssignorUser', '=', $filters['idAssignorUser']['id']);
		}

		// idResponsibleUser
		if ($filters['idResponsibleUser']['id']) {
			$listObj->setFilter('idResponsibleUser', '=', $filters['idResponsibleUser']['id']);
		}

		// idAddUser
		if ($filters['idAddUser']['id']) {
			$listObj->setFilter('idAddUser', '=', $filters['idAddUser']['id']);
		}

		// text
		if ($filters['text']) {
			$listObj->setCustomFilter('description LIKE ?', ["%{$filters['text']}%"]);
		}

		// free
		if ($filters['free']) {
			$listObj->setFilter('free', '=', ($filters['free']=='yes'?1:0));
		}

		// status
		if (is_array($filters['status']) && $filters['status']) {
			$listObj->setFilter('status', 'IN', array_values($filters['status']));
		}

		// pivot
		if ($filters['pivot']) {
			$listObj->setFilter('pivot', '=', $filters['pivot']);
		}

		// idPivot
		if ($filters['idPivot']) {
			$listObj->setFilter('idPivot', '=', $filters['idPivot']);
		}

		// idTask
		if ($filters['idTask']) {
			$listObj->setFilter('id', '=', $filters['idTask']);
		}

		$listObj = $this->entityList_inheritListingParams($listingParams, $listObj);

		// these must be called in this specific order
		$this->recordsList = $listObj->exportRecords();
		$this->totalRecordsCount = $listObj->getRecordsCount();
	}

}
