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