1<?php 2 3namespace plugin\struct\meta; 4 5/** 6 * Class SearchConfig 7 * 8 * The same as @see Search but can be initialized by a configuration array 9 * 10 * @package plugin\struct\meta 11 */ 12class SearchConfig extends Search { 13 14 /** 15 * @var array hold the configuration as parsed and extended by dynamic params 16 */ 17 protected $config; 18 19 /** 20 * @var SearchConfigParameters manages dynamic parameters 21 */ 22 protected $dynamicParameters; 23 24 /** 25 * SearchConfig constructor. 26 * @param array $config The parsed configuration for this search 27 */ 28 public function __construct($config) { 29 parent::__construct(); 30 31 // setup schemas and columns 32 if(!empty($config['schemas'])) foreach($config['schemas'] as $schema) { 33 $this->addSchema($schema[0], $schema[1]); 34 } 35 if(!empty($config['cols'])) foreach($config['cols'] as $col) { 36 $this->addColumn($col); 37 } 38 39 // apply dynamic paramters 40 $this->dynamicParameters = new SearchConfigParameters($this); 41 $config = $this->dynamicParameters->updateConfig($config); 42 43 // configure search from configuration 44 if(!empty($config['filter'])) foreach($config['filter'] as $filter) { 45 $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]); 46 } 47 48 if(!empty($config['sort'])) foreach($config['sort'] as $sort) { 49 $this->addSort($sort[0], $sort[1]); 50 } 51 52 if(!empty($config['limit'])) { 53 $this->setLimit($config['limit']); 54 } 55 56 if(!empty($config['offset'])) { 57 $this->setLimit($config['offset']); 58 } 59 60 $this->config = $config; 61 } 62 63 /** 64 * Replaces placeholders in the given filter value by the proper value 65 * 66 * @param string $filter 67 * @return string 68 */ 69 protected function applyFilterVars($filter) { 70 global $ID; 71 72 // apply inexpensive filters first 73 $filter = str_replace( 74 array( 75 '$ID$', 76 '$NS$', 77 '$PAGE$', 78 '$USER$', 79 '$TODAY$' 80 ), 81 array( 82 $ID, 83 getNS($ID), 84 noNS($ID), 85 isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '', 86 date('Y-m-d') 87 ), 88 $filter 89 ); 90 91 // apply struct filter 92 while(preg_match('/\$STRUCT\.(.*?)\$/', $filter, $matches)) { 93 foreach($matches as $match) { 94 $key = $match[1]; 95 $column = $this->findColumn($key); 96 if($column) { 97 $label = $column->getLabel(); 98 $table = $column->getTable(); 99 $schemaData = new SchemaData($table, $ID, 0); 100 $data = $schemaData->getDataArray(); 101 $value = $data[$label]; 102 if(is_array($value)) $value = array_shift($value); 103 } else { 104 $value = ''; 105 } 106 $key = preg_quote_cb($key); 107 $filter = preg_replace('/\$STRUCT\.'.$key.'\$/', $value, 1); 108 } 109 } 110 111 return $filter; 112 } 113 114 /** 115 * Access the dynamic paramters of this search 116 * 117 * Note: This call returns a clone of the parameters as they were initialized 118 * 119 * @return SearchConfigParameters 120 */ 121 public function getDynamicParameters() { 122 return clone $this->dynamicParameters; 123 } 124 125 /** 126 * @return array the current config 127 */ 128 public function getConf() { 129 return $this->config; 130 } 131 132} 133