<?php

namespace Modules\Admin\DocumentTemplates;

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

		$list = new \App\EntityList\DocumentTemplates();
		$list->setColumns(['active','name','docType','format','fileName','extension','fieldsDetails',
			'allUsers','addTime']);

		// FILTERS ////////////////////////////////////////////////////////////////////////////////
		if ($filters['type']) {
			$list->setFilter('docType', 'LIKE', $filters['type']);
		}

		if ($filters['format']) {
			$list->setFilter('format', 'LIKE', $filters['format']);
		}

		if ($filters['filter']) {
			$list->setFilter('name', 'LIKE', "%{$filters['filter']}%");
		}

		if ($filters['status'] == 'active') {
			$list->setFilter('active', '=', '1');
		}
		if ($filters['status'] == 'inactive') {
			$list->setFilter('active', '=', '0');
		}

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


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

		// load global cfg
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$records = $list->exportRecords();
		foreach ($records as $index => $rowFields) {
			$records[$index]['pivot'] = \App\Config\IssueDocuments::getInstance()->getDocPivot($rowFields['docType']);
		}

		$this->recordsList = $records;
		$this->totalRecordsCount = $list->getRecordsCount();

	}
}