1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5/** 6 * Manage dynamic parameters for aggregations 7 * 8 * @package dokuwiki\plugin\struct\meta 9 */ 10class SearchConfigParameters { 11 12 /** @var string parameter name to pass filters */ 13 public static $PARAM_FILTER = 'flt'; 14 /** @var string parameter name to pass offset */ 15 public static $PARAM_OFFSET = 'ofs'; 16 /** @var string parameter name to pass srt */ 17 public static $PARAM_SORT = 'srt'; 18 19 /** @var SearchConfig */ 20 protected $searchConfig; 21 22 /** @var null|array */ 23 protected $sort = null; 24 /** @var int */ 25 protected $offset = 0; 26 /** @var array */ 27 protected $filters = array(); 28 29 /** 30 * Initializes the dynamic parameters from $INPUT 31 * 32 * @param SearchConfig $searchConfig 33 */ 34 public function __construct(SearchConfig $searchConfig) { 35 global $INPUT; 36 $this->searchConfig = $searchConfig; 37 /** @var \helper_plugin_struct_config $confHlp */ 38 $confHlp = plugin_load('helper', 'struct_config'); 39 40 if($INPUT->has(self::$PARAM_SORT)) { 41 list($colname, $sort) = $confHlp->parseSort($INPUT->str(self::$PARAM_SORT)); 42 $this->setSort($colname, $sort); 43 } 44 45 if($INPUT->has(self::$PARAM_FILTER)) { 46 foreach($INPUT->arr(self::$PARAM_FILTER) as $colcomp => $filter) { 47 list($colname, $comp, $value,) = $confHlp->parseFilterLine('AND', $colcomp . $filter); 48 $this->addFilter($colname, $comp, $value); 49 } 50 } 51 52 if($INPUT->has(self::$PARAM_OFFSET)) { 53 $this->setOffset($INPUT->int(self::$PARAM_OFFSET)); 54 } 55 } 56 57 /** 58 * Returns the full qualified name for a given column 59 * 60 * @param string|Column $column 61 * @return bool|string 62 */ 63 protected function resolveColumn($column) { 64 if(!is_a($column, '\dokuwiki\plugin\struct\meta\Column')) { 65 $column = $this->searchConfig->findColumn($column); 66 if(!$column) return false; 67 } 68 /** @var Column $column */ 69 return $column->getFullQualifiedLabel(); 70 } 71 72 /** 73 * Sets the sorting column 74 * 75 * @param string|Column $column 76 * @param bool $asc 77 */ 78 public function setSort($column, $asc = true) { 79 $column = $this->resolveColumn($column); 80 if(!$column) return; 81 $this->sort = array($column, $asc); 82 } 83 84 /** 85 * Remove the sorting column 86 */ 87 public function removeSort() { 88 $this->sort = null; 89 } 90 91 /** 92 * Set the offset 93 * 94 * @param int $offset 95 */ 96 public function setOffset($offset) { 97 $this->offset = $offset; 98 } 99 100 /** 101 * Removes the offset 102 */ 103 public function removeOffset() { 104 $this->offset = 0; 105 } 106 107 /** 108 * Adds another filter 109 * 110 * When there is a filter for that column already, the new filter overwrites it. Setting a 111 * blank value is the same as calling @see removeFilter() 112 * 113 * @param string|Column $column 114 * @param string $comp the comparator 115 * @param string $value the value to compare against 116 */ 117 public function addFilter($column, $comp, $value) { 118 $column = $this->resolveColumn($column); 119 if(!$column) return; 120 121 if(trim($value) === '') { 122 $this->removeFilter($column); 123 } else { 124 $this->filters[$column] = array($comp, $value); 125 } 126 } 127 128 /** 129 * Removes the filter for the given column 130 * 131 * @param $column 132 */ 133 public function removeFilter($column) { 134 $column = $this->resolveColumn($column); 135 if(!$column) return; 136 if(isset($this->filters[$column])) unset($this->filters[$column]); 137 } 138 139 /** 140 * Remove all filter 141 */ 142 public function clearFilters() { 143 $this->filters = array(); 144 } 145 146 /** 147 * @return array the current filters 148 */ 149 public function getFilters() { 150 return $this->filters; 151 } 152 153 /** 154 * Get the current parameters 155 * 156 * It creates a flat key value in a form that can be used to 157 * create URLs or Form parameters 158 * 159 * 160 * @return array 161 */ 162 public function getURLParameters() { 163 $params = array(); 164 if($this->offset) { 165 $params[self::$PARAM_OFFSET] = $this->offset; 166 } 167 168 if($this->sort) { 169 list($column, $asc) = $this->sort; 170 if(!$asc) $column = "^$column"; 171 $params[self::$PARAM_SORT] = $column; 172 } 173 174 if($this->filters) { 175 176 foreach($this->filters as $column => $filter) { 177 list($comp, $value) = $filter; 178 $key = self::$PARAM_FILTER . '[' . $column . $comp . ']'; 179 $params[$key] = $value; 180 } 181 } 182 183 return $params; 184 } 185 186 /** 187 * Updates the given config array with the values currently set 188 * 189 * This should only be called once at the initialization 190 * 191 * @param array $config 192 * @return array 193 */ 194 public function updateConfig($config) { 195 if($this->offset) { 196 $config['offset'] = $this->offset; 197 } 198 199 if($this->sort) { 200 list($column, $asc) = $this->sort; 201 $config['sort'] = array( 202 array($column, $asc) 203 ); 204 } 205 206 if($this->filters) { 207 if(empty($config['filter'])) $config['filter'] = array(); 208 foreach($this->filters as $column => $filter) { 209 list($comp, $value) = $filter; 210 $config['filter'][] = array($column, $comp, $value, 'AND'); 211 } 212 } 213 214 return $config; 215 } 216 217} 218