15511bd5bSAndreas Gohr<?php 25511bd5bSAndreas Gohr 35511bd5bSAndreas Gohrnamespace plugin\struct\meta; 45511bd5bSAndreas Gohr 55511bd5bSAndreas Gohr/** 65511bd5bSAndreas Gohr * Class SearchConfig 75511bd5bSAndreas Gohr * 85511bd5bSAndreas Gohr * The same as @see Search but can be initialized by a configuration array 95511bd5bSAndreas Gohr * 105511bd5bSAndreas Gohr * @package plugin\struct\meta 115511bd5bSAndreas Gohr */ 125511bd5bSAndreas Gohrclass SearchConfig extends Search { 135511bd5bSAndreas Gohr 14668e4f8eSAndreas Gohr /** 15668e4f8eSAndreas Gohr * @var array hold the configuration as parsed and extended by dynamic params 16668e4f8eSAndreas Gohr */ 171a07b696SMichael Große protected $config; 181a07b696SMichael Große 19668e4f8eSAndreas Gohr /** 20668e4f8eSAndreas Gohr * @var SearchConfigParameters manages dynamic parameters 21668e4f8eSAndreas Gohr */ 2200f6af48SAndreas Gohr protected $dynamicParameters; 2300f6af48SAndreas Gohr 245511bd5bSAndreas Gohr /** 255511bd5bSAndreas Gohr * SearchConfig constructor. 2600f6af48SAndreas Gohr * @param array $config The parsed configuration for this search 275511bd5bSAndreas Gohr */ 285511bd5bSAndreas Gohr public function __construct($config) { 295511bd5bSAndreas Gohr parent::__construct(); 305511bd5bSAndreas Gohr 313ad292a8SAndreas Gohr // setup schemas and columns 320215a637SAndreas Gohr if(!empty($config['schemas'])) foreach($config['schemas'] as $schema) { 333ad292a8SAndreas Gohr $this->addSchema($schema[0], $schema[1]); 343ad292a8SAndreas Gohr } 350215a637SAndreas Gohr if(!empty($config['cols'])) foreach($config['cols'] as $col) { 363ad292a8SAndreas Gohr $this->addColumn($col); 373ad292a8SAndreas Gohr } 383ad292a8SAndreas Gohr 3900f6af48SAndreas Gohr // apply dynamic paramters 4000f6af48SAndreas Gohr $this->dynamicParameters = new SearchConfigParameters($this); 41668e4f8eSAndreas Gohr $config = $this->dynamicParameters->updateConfig($config); 4200f6af48SAndreas Gohr 4300f6af48SAndreas Gohr // configure search from configuration 4400f6af48SAndreas Gohr if(!empty($config['filter'])) foreach($config['filter'] as $filter) { 455625b985SAndreas Gohr $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]); 465511bd5bSAndreas Gohr } 4700f6af48SAndreas Gohr 4800f6af48SAndreas Gohr if(!empty($config['sort'])) foreach($config['sort'] as $sort) { 49aa124708SAndreas Gohr $this->addSort($sort[0], $sort[1]); 501a07b696SMichael Große } 511a07b696SMichael Große 521a07b696SMichael Große if(!empty($config['limit'])) { 531a07b696SMichael Große $this->setLimit($config['limit']); 541a07b696SMichael Große } 5500f6af48SAndreas Gohr 5600f6af48SAndreas Gohr if(!empty($config['offset'])) { 5700f6af48SAndreas Gohr $this->setLimit($config['offset']); 5800f6af48SAndreas Gohr } 59668e4f8eSAndreas Gohr 60668e4f8eSAndreas Gohr $this->config = $config; 611a07b696SMichael Große } 625511bd5bSAndreas Gohr 6300f6af48SAndreas Gohr /** 645625b985SAndreas Gohr * Replaces placeholders in the given filter value by the proper value 655625b985SAndreas Gohr * 665625b985SAndreas Gohr * @param string $filter 675625b985SAndreas Gohr * @return string 685625b985SAndreas Gohr */ 695625b985SAndreas Gohr protected function applyFilterVars($filter) { 705625b985SAndreas Gohr global $ID; 715625b985SAndreas Gohr 725625b985SAndreas Gohr // apply inexpensive filters first 735625b985SAndreas Gohr $filter = str_replace( 745625b985SAndreas Gohr array( 755625b985SAndreas Gohr '$ID$', 765625b985SAndreas Gohr '$NS$', 775625b985SAndreas Gohr '$PAGE$', 785625b985SAndreas Gohr '$USER$', 795625b985SAndreas Gohr '$TODAY$' 805625b985SAndreas Gohr ), 815625b985SAndreas Gohr array( 825625b985SAndreas Gohr $ID, 835625b985SAndreas Gohr getNS($ID), 845625b985SAndreas Gohr noNS($ID), 855625b985SAndreas Gohr isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '', 865625b985SAndreas Gohr date('Y-m-d') 875625b985SAndreas Gohr ), 885625b985SAndreas Gohr $filter 895625b985SAndreas Gohr ); 905625b985SAndreas Gohr 915625b985SAndreas Gohr // apply struct filter 92*d19ba4b1SAndreas Gohr while(preg_match('/\$STRUCT\.(.*?)\$/', $filter, $match)) { 935625b985SAndreas Gohr $key = $match[1]; 945625b985SAndreas Gohr $column = $this->findColumn($key); 95*d19ba4b1SAndreas Gohr 965625b985SAndreas Gohr if($column) { 975625b985SAndreas Gohr $label = $column->getLabel(); 985625b985SAndreas Gohr $table = $column->getTable(); 995625b985SAndreas Gohr $schemaData = new SchemaData($table, $ID, 0); 1005625b985SAndreas Gohr $data = $schemaData->getDataArray(); 1015625b985SAndreas Gohr $value = $data[$label]; 1025625b985SAndreas Gohr if(is_array($value)) $value = array_shift($value); 1035625b985SAndreas Gohr } else { 1045625b985SAndreas Gohr $value = ''; 1055625b985SAndreas Gohr } 1065625b985SAndreas Gohr $key = preg_quote_cb($key); 107*d19ba4b1SAndreas Gohr $filter = preg_replace('/\$STRUCT\.' . $key . '\$/', $value, $filter, 1); 108*d19ba4b1SAndreas Gohr 1095625b985SAndreas Gohr } 1105625b985SAndreas Gohr 1115625b985SAndreas Gohr return $filter; 1125625b985SAndreas Gohr } 1135625b985SAndreas Gohr 1145625b985SAndreas Gohr /** 11500f6af48SAndreas Gohr * Access the dynamic paramters of this search 11600f6af48SAndreas Gohr * 117668e4f8eSAndreas Gohr * Note: This call returns a clone of the parameters as they were initialized 11800f6af48SAndreas Gohr * 11900f6af48SAndreas Gohr * @return SearchConfigParameters 12000f6af48SAndreas Gohr */ 12100f6af48SAndreas Gohr public function getDynamicParameters() { 12200f6af48SAndreas Gohr return clone $this->dynamicParameters; 1235511bd5bSAndreas Gohr } 1245511bd5bSAndreas Gohr 12500f6af48SAndreas Gohr /** 12607993756SAndreas Gohr * @return array the current config 12700f6af48SAndreas Gohr */ 12807993756SAndreas Gohr public function getConf() { 1291a07b696SMichael Große return $this->config; 1301a07b696SMichael Große } 1311a07b696SMichael Große 1325511bd5bSAndreas Gohr} 133