<?php
namespace Modules\Admin\UsersTrails;

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();
		$listObj = new \App\EntityList\UserTraces();
		$listObj->applyUserRestriction(false);
		$listObj->setColumns(['module', 'action', 'entityType', 'entityOperation', 'idEntity', 'extraData', 'timestamp', 'idUser']);


		if ($filters['timeInterval']['start']) {
			$listObj->setFilter('timestamp', '>=', \Engine\DateTime::createFromIsoDateOrDatetime($filters['timeInterval']['start'])->formatTime());
		}
		else {
			$listObj->setFilter('timestamp', '>=', \Engine\DateTime::create('-1month')->formatTime());
		}

		if ($filters['timeInterval']['end']) {
			$listObj->setFilter('timestamp', '<=', \Engine\DateTime::createFromIsoDateOrDatetime($filters['timeInterval']['end'])->formatTime());
		}

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

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

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

		// entity type
		if ($filters['entityType']) {
			$listObj->setFilter('entityType', 'LIKE', $filters['entityType']);
		}

		// operation
		if ($filters['operation']) {
			$listObj->setFilter('entityOperation', 'LIKE', $filters['operation']);
		}

		// entity code
		if ($filters['entityCode']) {
			$listObj->setFilter('idEntity', 'LIKE', $filters['entityCode']);
		}

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

		// EXTRA DATA /////////////////////////////////////////////////////////////////////
		foreach ($trailsList as $key => $visit) {
			if($visit['extraData']) {
				$trailsList[$key]['extraDataArr'] = unserialize($visit['extraData']);
			}
		}

		$this->recordsList = $trailsList;
		$this->totalRecordsCount = $listObj->getRecordsCount();
	}
}