<?php
namespace App\Mvc\View\Listing\Members; class Params { protected $cacheKey; protected $defaults = []; protected $data = []; protected $dataChanged = []; public function __construct($cacheKey) { $this->cacheKey = $cacheKey; } public function setDefaultFilters(?Array $filters=null) { $this->defaults['filters'] = $filters; return $this; } public function getDefaultFilters() { return $this->defaults['filters']; } public function setFilters(?Array $filters=null) { $this->dataChanged['filters'] = true; $this->data['filters'] = $filters; return $this; } public function mergeFilters(?Array $newFilters=null) { $oldFilters = $this->getFilters(); $filters = $oldFilters; foreach ($newFilters as $k=>$v) { $filters[$k] = $v; } return $this->setFilters($filters); } public function getFilters() { return ($this->data['filters']?:$this->defaults['filters']); } public function setDynamicColumnsSettings(Array $dynamicColumnsSettings) { $this->dataChanged['dynamicColumnsSettings'] = true; $this->data['dynamicColumnsSettings'] = $dynamicColumnsSettings; return $this; } public function getDynamicColumnsSettings() { return ($this->data['dynamicColumnsSettings']?:[]); } public function setDefaultParams(Array $params) { $this->defaults['listingParams'] = $params; return $this; } public function setParam($key, $value) { $this->dataChanged['listingParams'] = true; $this->data['listingParams'][$key] = $value; return $this; } public function getParam($key) { return (isset($this->data['listingParams'][$key])?$this->data['listingParams'][$key]:$this->defaults['listingParams'][$key]); } public function setCurrentPage($page) { return $this->setParam('currentPage', $page); } public function getCurrentPage() { return (int)$this->getParam('currentPage'); } public function setResultsPerPage($rpp) { return $this->setParam('resultsPerPage', $rpp); } public function getResultsPerPage() { return (int)$this->getParam('resultsPerPage'); } public function setOrderColumn($column) { return $this->setParam('orderColumn', $column); } public function getOrderColumn() { return $this->getParam('orderColumn'); } public function setOrderDirection($direction) { return $this->setParam('orderDirection', $direction); } public function getOrderDirection() { return $this->getParam('orderDirection'); } public function reset() { $this->data = [ 'listingParams' => [], 'filters' => [], ]; $this->dataChanged['listingParams'] = true; $this->dataChanged['filters'] = true; return $this; } public function save() { if ($this->dataChanged['listingParams']??null) { \Engine\Storage\General::getInstance('session')->set(sprintf("%s/%s/listingParams", __CLASS__, $this->cacheKey), $this->data['listingParams']); } if ($this->dataChanged['filters']??null) { \Engine\Storage\General::getInstance('session')->set(sprintf("%s/%s/filters", __CLASS__, $this->cacheKey), $this->data['filters']); } if ($this->dataChanged['dynamicColumnsSettings']??null) { \Engine\Storage\General::getInstance('persistent')->set(sprintf("%s/%s/dynamicColumnsSettings", __CLASS__, $this->cacheKey), $this->data['dynamicColumnsSettings']); } return $this; } public function load() { $this->data['listingParams'] = \Engine\Storage\General::getInstance('session')->get(sprintf("%s/%s/listingParams", __CLASS__, $this->cacheKey)); $this->data['filters'] = \Engine\Storage\General::getInstance('session')->get(sprintf("%s/%s/filters", __CLASS__, $this->cacheKey)); $this->data['dynamicColumnsSettings'] = \Engine\Storage\General::getInstance('persistent')->get(sprintf("%s/%s/dynamicColumnsSettings", __CLASS__, $this->cacheKey)); return $this; } }