100f6af48SAndreas Gohr<?php 200f6af48SAndreas Gohr 3*ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 400f6af48SAndreas Gohr 5b2b64eb3SAndreas Gohr/** 6b2b64eb3SAndreas Gohr * Manage dynamic parameters for aggregations 7b2b64eb3SAndreas Gohr * 8*ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 9b2b64eb3SAndreas Gohr */ 1000f6af48SAndreas Gohrclass SearchConfigParameters { 1100f6af48SAndreas Gohr 1200f6af48SAndreas Gohr /** @var string parameter name to pass filters */ 13b2b64eb3SAndreas Gohr public static $PARAM_FILTER = 'flt'; 1400f6af48SAndreas Gohr /** @var string parameter name to pass offset */ 15b2b64eb3SAndreas Gohr public static $PARAM_OFFSET = 'ofs'; 1600f6af48SAndreas Gohr /** @var string parameter name to pass srt */ 17b2b64eb3SAndreas Gohr public static $PARAM_SORT = 'srt'; 1800f6af48SAndreas Gohr 1900f6af48SAndreas Gohr /** @var SearchConfig */ 2000f6af48SAndreas Gohr protected $searchConfig; 2100f6af48SAndreas Gohr 2200f6af48SAndreas Gohr /** @var null|array */ 2300f6af48SAndreas Gohr protected $sort = null; 2400f6af48SAndreas Gohr /** @var int */ 2500f6af48SAndreas Gohr protected $offset = 0; 2600f6af48SAndreas Gohr /** @var array */ 2700f6af48SAndreas Gohr protected $filters = array(); 2800f6af48SAndreas Gohr 2900f6af48SAndreas Gohr /** 3000f6af48SAndreas Gohr * Initializes the dynamic parameters from $INPUT 3100f6af48SAndreas Gohr * 3200f6af48SAndreas Gohr * @param SearchConfig $searchConfig 3300f6af48SAndreas Gohr */ 3400f6af48SAndreas Gohr public function __construct(SearchConfig $searchConfig) { 3500f6af48SAndreas Gohr global $INPUT; 3600f6af48SAndreas Gohr $this->searchConfig = $searchConfig; 3700f6af48SAndreas Gohr /** @var \helper_plugin_struct_config $confHlp */ 3800f6af48SAndreas Gohr $confHlp = plugin_load('helper', 'struct_config'); 3900f6af48SAndreas Gohr 4000f6af48SAndreas Gohr if($INPUT->has(self::$PARAM_SORT)) { 4100f6af48SAndreas Gohr list($colname, $sort) = $confHlp->parseSort($INPUT->str(self::$PARAM_SORT)); 42aa124708SAndreas Gohr $this->setSort($colname, $sort); 4300f6af48SAndreas Gohr } 4400f6af48SAndreas Gohr 4500f6af48SAndreas Gohr if($INPUT->has(self::$PARAM_FILTER)) { 4600f6af48SAndreas Gohr foreach($INPUT->arr(self::$PARAM_FILTER) as $colcomp => $filter) { 4700f6af48SAndreas Gohr list($colname, $comp, $value,) = $confHlp->parseFilterLine('AND', $colcomp . $filter); 48b2b64eb3SAndreas Gohr $this->addFilter($colname, $comp, $value); 4900f6af48SAndreas Gohr } 5000f6af48SAndreas Gohr } 5100f6af48SAndreas Gohr 5200f6af48SAndreas Gohr if($INPUT->has(self::$PARAM_OFFSET)) { 5300f6af48SAndreas Gohr $this->setOffset($INPUT->int(self::$PARAM_OFFSET)); 5400f6af48SAndreas Gohr } 5500f6af48SAndreas Gohr } 5600f6af48SAndreas Gohr 5700f6af48SAndreas Gohr /** 5800f6af48SAndreas Gohr * Returns the full qualified name for a given column 5900f6af48SAndreas Gohr * 6000f6af48SAndreas Gohr * @param string|Column $column 6100f6af48SAndreas Gohr * @return bool|string 6200f6af48SAndreas Gohr */ 6300f6af48SAndreas Gohr protected function resolveColumn($column) { 64*ba766201SAndreas Gohr if(!is_a($column, '\dokuwiki\plugin\struct\meta\Column')) { 6500f6af48SAndreas Gohr $column = $this->searchConfig->findColumn($column); 6600f6af48SAndreas Gohr if(!$column) return false; 6700f6af48SAndreas Gohr } 6800f6af48SAndreas Gohr /** @var Column $column */ 6900f6af48SAndreas Gohr return $column->getFullQualifiedLabel(); 7000f6af48SAndreas Gohr } 7100f6af48SAndreas Gohr 7200f6af48SAndreas Gohr /** 7300f6af48SAndreas Gohr * Sets the sorting column 7400f6af48SAndreas Gohr * 7500f6af48SAndreas Gohr * @param string|Column $column 7600f6af48SAndreas Gohr * @param bool $asc 7700f6af48SAndreas Gohr */ 7800f6af48SAndreas Gohr public function setSort($column, $asc = true) { 7900f6af48SAndreas Gohr $column = $this->resolveColumn($column); 8000f6af48SAndreas Gohr if(!$column) return; 8100f6af48SAndreas Gohr $this->sort = array($column, $asc); 8200f6af48SAndreas Gohr } 8300f6af48SAndreas Gohr 8400f6af48SAndreas Gohr /** 8500f6af48SAndreas Gohr * Remove the sorting column 8600f6af48SAndreas Gohr */ 8700f6af48SAndreas Gohr public function removeSort() { 8800f6af48SAndreas Gohr $this->sort = null; 8900f6af48SAndreas Gohr } 9000f6af48SAndreas Gohr 9100f6af48SAndreas Gohr /** 9200f6af48SAndreas Gohr * Set the offset 9300f6af48SAndreas Gohr * 9400f6af48SAndreas Gohr * @param int $offset 9500f6af48SAndreas Gohr */ 9600f6af48SAndreas Gohr public function setOffset($offset) { 9700f6af48SAndreas Gohr $this->offset = $offset; 9800f6af48SAndreas Gohr } 9900f6af48SAndreas Gohr 10000f6af48SAndreas Gohr /** 10100f6af48SAndreas Gohr * Removes the offset 10200f6af48SAndreas Gohr */ 10300f6af48SAndreas Gohr public function removeOffset() { 10400f6af48SAndreas Gohr $this->offset = 0; 10500f6af48SAndreas Gohr } 10600f6af48SAndreas Gohr 10700f6af48SAndreas Gohr /** 10800f6af48SAndreas Gohr * Adds another filter 10900f6af48SAndreas Gohr * 11007993756SAndreas Gohr * When there is a filter for that column already, the new filter overwrites it. Setting a 11107993756SAndreas Gohr * blank value is the same as calling @see removeFilter() 11207993756SAndreas Gohr * 11300f6af48SAndreas Gohr * @param string|Column $column 11400f6af48SAndreas Gohr * @param string $comp the comparator 11500f6af48SAndreas Gohr * @param string $value the value to compare against 11600f6af48SAndreas Gohr */ 11700f6af48SAndreas Gohr public function addFilter($column, $comp, $value) { 11800f6af48SAndreas Gohr $column = $this->resolveColumn($column); 11900f6af48SAndreas Gohr if(!$column) return; 12000f6af48SAndreas Gohr 12107993756SAndreas Gohr if(trim($value) === '') { 12207993756SAndreas Gohr $this->removeFilter($column); 12307993756SAndreas Gohr } else { 12400f6af48SAndreas Gohr $this->filters[$column] = array($comp, $value); 12500f6af48SAndreas Gohr } 12607993756SAndreas Gohr } 12700f6af48SAndreas Gohr 12800f6af48SAndreas Gohr /** 12900f6af48SAndreas Gohr * Removes the filter for the given column 13000f6af48SAndreas Gohr * 13100f6af48SAndreas Gohr * @param $column 13200f6af48SAndreas Gohr */ 13300f6af48SAndreas Gohr public function removeFilter($column) { 13400f6af48SAndreas Gohr $column = $this->resolveColumn($column); 13500f6af48SAndreas Gohr if(!$column) return; 13600f6af48SAndreas Gohr if(isset($this->filters[$column])) unset($this->filters[$column]); 13700f6af48SAndreas Gohr } 13800f6af48SAndreas Gohr 13900f6af48SAndreas Gohr /** 14000f6af48SAndreas Gohr * Remove all filter 14100f6af48SAndreas Gohr */ 14200f6af48SAndreas Gohr public function clearFilters() { 14300f6af48SAndreas Gohr $this->filters = array(); 14400f6af48SAndreas Gohr } 14500f6af48SAndreas Gohr 14600f6af48SAndreas Gohr /** 14707993756SAndreas Gohr * @return array the current filters 14807993756SAndreas Gohr */ 14907993756SAndreas Gohr public function getFilters() { 15007993756SAndreas Gohr return $this->filters; 15107993756SAndreas Gohr } 15207993756SAndreas Gohr 15307993756SAndreas Gohr /** 154d60f71efSAndreas Gohr * Get the current parameters 155d60f71efSAndreas Gohr * 156d60f71efSAndreas Gohr * It creates a flat key value in a form that can be used to 157d60f71efSAndreas Gohr * create URLs or Form parameters 158d60f71efSAndreas Gohr * 159d60f71efSAndreas Gohr * 160d60f71efSAndreas Gohr * @return array 16100f6af48SAndreas Gohr */ 16200f6af48SAndreas Gohr public function getURLParameters() { 16300f6af48SAndreas Gohr $params = array(); 16400f6af48SAndreas Gohr if($this->offset) { 16500f6af48SAndreas Gohr $params[self::$PARAM_OFFSET] = $this->offset; 16600f6af48SAndreas Gohr } 16700f6af48SAndreas Gohr 16800f6af48SAndreas Gohr if($this->sort) { 16900f6af48SAndreas Gohr list($column, $asc) = $this->sort; 17000f6af48SAndreas Gohr if(!$asc) $column = "^$column"; 17100f6af48SAndreas Gohr $params[self::$PARAM_SORT] = $column; 17200f6af48SAndreas Gohr } 17300f6af48SAndreas Gohr 17400f6af48SAndreas Gohr if($this->filters) { 175d60f71efSAndreas Gohr 17600f6af48SAndreas Gohr foreach($this->filters as $column => $filter) { 17700f6af48SAndreas Gohr list($comp, $value) = $filter; 178d60f71efSAndreas Gohr $key = self::$PARAM_FILTER . '[' . $column . $comp . ']'; 179d60f71efSAndreas Gohr $params[$key] = $value; 18000f6af48SAndreas Gohr } 18100f6af48SAndreas Gohr } 18200f6af48SAndreas Gohr 18300f6af48SAndreas Gohr return $params; 18400f6af48SAndreas Gohr } 18500f6af48SAndreas Gohr 18600f6af48SAndreas Gohr /** 18700f6af48SAndreas Gohr * Updates the given config array with the values currently set 18800f6af48SAndreas Gohr * 18900f6af48SAndreas Gohr * This should only be called once at the initialization 19000f6af48SAndreas Gohr * 19100f6af48SAndreas Gohr * @param array $config 19200f6af48SAndreas Gohr * @return array 19300f6af48SAndreas Gohr */ 19400f6af48SAndreas Gohr public function updateConfig($config) { 19500f6af48SAndreas Gohr if($this->offset) { 19600f6af48SAndreas Gohr $config['offset'] = $this->offset; 19700f6af48SAndreas Gohr } 19800f6af48SAndreas Gohr 19900f6af48SAndreas Gohr if($this->sort) { 20000f6af48SAndreas Gohr list($column, $asc) = $this->sort; 20100f6af48SAndreas Gohr $config['sort'] = array( 20200f6af48SAndreas Gohr array($column, $asc) 20300f6af48SAndreas Gohr ); 20400f6af48SAndreas Gohr } 20500f6af48SAndreas Gohr 20600f6af48SAndreas Gohr if($this->filters) { 20700f6af48SAndreas Gohr if(empty($config['filter'])) $config['filter'] = array(); 20800f6af48SAndreas Gohr foreach($this->filters as $column => $filter) { 20900f6af48SAndreas Gohr list($comp, $value) = $filter; 21000f6af48SAndreas Gohr $config['filter'][] = array($column, $comp, $value, 'AND'); 21100f6af48SAndreas Gohr } 21200f6af48SAndreas Gohr } 21300f6af48SAndreas Gohr 21400f6af48SAndreas Gohr return $config; 21500f6af48SAndreas Gohr } 21600f6af48SAndreas Gohr 21700f6af48SAndreas Gohr} 218