<?php

namespace Modules\Admin\Cities;

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 cities list
		$listObj = new \App\EntityList\Cities();
		$listObj->setColumns(['name', 'name2', 'code3', 'codeLocal', 'region', 'region2', 'district', 'district2', 'addTime', 'countryCode']);
		if ($filters['name']) {
			$listObj->setCustomFilter('name LIKE ? OR name2 LIKE ?', ["%{$filters['name']}%", "%{$filters['name']}%"]);
		}

		if ($filters['code3']) {
			$listObj->setCustomFilter('code3 LIKE ? OR codeLocal LIKE ?', ["{$filters['code3']}", "{$filters['code3']}"]);
		}

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

		if ($filters['region']) {
			$listObj->setCustomFilter('region LIKE ? OR region2 LIKE ?', ["{$filters['region']}%", "{$filters['region']}%"]);
		}

		if ($filters['district']) {
			$listObj->setCustomFilter('district LIKE ? OR district2 LIKE ?', ["{$filters['district']}%", "{$filters['district']}%"]);
		}

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

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

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

}
