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 * @param string|Column $column 106 * @param string $comp the comparator 107 * @param string $value the value to compare against 108 */ 109 public function addFilter($column, $comp, $value) { 110 $column = $this->resolveColumn($column); 111 if(!$column) return; 112 113 $this->filters[$column] = array($comp, $value); 114 } 115 116 /** 117 * Removes the filter for the given column 118 * 119 * @param $column 120 */ 121 public function removeFilter($column) { 122 $column = $this->resolveColumn($column); 123 if(!$column) return; 124 if(isset($this->filters[$column])) unset($this->filters[$column]); 125 } 126 127 /** 128 * Remove all filter 129 */ 130 public function clearFilters() { 131 $this->filters = array(); 132 } 133 134 /** 135 * Get the current parameters in a form that can be used to create URLs 136 */ 137 public function getURLParameters() { 138 $params = array(); 139 if($this->offset) { 140 $params[self::$PARAM_OFFSET] = $this->offset; 141 } 142 143 if($this->sort) { 144 list($column, $asc) = $this->sort; 145 if(!$asc) $column = "^$column"; 146 $params[self::$PARAM_SORT] = $column; 147 } 148 149 if($this->filters) { 150 $params[self::$PARAM_FILTER] = array(); 151 foreach($this->filters as $column => $filter) { 152 list($comp, $value) = $filter; 153 $params[self::$PARAM_FILTER][$column . $comp] = $value; 154 } 155 } 156 157 return $params; 158 } 159 160 /** 161 * Updates the given config array with the values currently set 162 * 163 * This should only be called once at the initialization 164 * 165 * @param array $config 166 * @return array 167 */ 168 public function updateConfig($config) { 169 if($this->offset) { 170 $config['offset'] = $this->offset; 171 } 172 173 if($this->sort) { 174 list($column, $asc) = $this->sort; 175 $config['sort'] = array( 176 array($column, $asc) 177 ); 178 } 179 180 if($this->filters) { 181 if(empty($config['filter'])) $config['filter'] = array(); 182 foreach($this->filters as $column => $filter) { 183 list($comp, $value) = $filter; 184 $config['filter'][] = array($column, $comp, $value, 'AND'); 185 } 186 } 187 188 return $config; 189 } 190 191} 192