xref: /plugin/struct/meta/SearchConfig.php (revision 5625b9852e4b32987787d47facc1d75903f74f2d)
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) {
45*5625b985SAndreas 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    /**
64*5625b985SAndreas Gohr     * Replaces placeholders in the given filter value by the proper value
65*5625b985SAndreas Gohr     *
66*5625b985SAndreas Gohr     * @param string $filter
67*5625b985SAndreas Gohr     * @return string
68*5625b985SAndreas Gohr     */
69*5625b985SAndreas Gohr    protected function applyFilterVars($filter) {
70*5625b985SAndreas Gohr        global $ID;
71*5625b985SAndreas Gohr
72*5625b985SAndreas Gohr        // apply inexpensive filters first
73*5625b985SAndreas Gohr        $filter = str_replace(
74*5625b985SAndreas Gohr            array(
75*5625b985SAndreas Gohr                '$ID$',
76*5625b985SAndreas Gohr                '$NS$',
77*5625b985SAndreas Gohr                '$PAGE$',
78*5625b985SAndreas Gohr                '$USER$',
79*5625b985SAndreas Gohr                '$TODAY$'
80*5625b985SAndreas Gohr            ),
81*5625b985SAndreas Gohr            array(
82*5625b985SAndreas Gohr                $ID,
83*5625b985SAndreas Gohr                getNS($ID),
84*5625b985SAndreas Gohr                noNS($ID),
85*5625b985SAndreas Gohr                isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '',
86*5625b985SAndreas Gohr                date('Y-m-d')
87*5625b985SAndreas Gohr            ),
88*5625b985SAndreas Gohr            $filter
89*5625b985SAndreas Gohr        );
90*5625b985SAndreas Gohr
91*5625b985SAndreas Gohr        // apply struct filter
92*5625b985SAndreas Gohr        while(preg_match('/\$STRUCT\.(.*?)\$/', $filter, $matches)) {
93*5625b985SAndreas Gohr            foreach($matches as $match) {
94*5625b985SAndreas Gohr                $key = $match[1];
95*5625b985SAndreas Gohr                $column = $this->findColumn($key);
96*5625b985SAndreas Gohr                if($column) {
97*5625b985SAndreas Gohr                    $label = $column->getLabel();
98*5625b985SAndreas Gohr                    $table = $column->getTable();
99*5625b985SAndreas Gohr                    $schemaData = new SchemaData($table, $ID, 0);
100*5625b985SAndreas Gohr                    $data = $schemaData->getDataArray();
101*5625b985SAndreas Gohr                    $value = $data[$label];
102*5625b985SAndreas Gohr                    if(is_array($value)) $value = array_shift($value);
103*5625b985SAndreas Gohr                } else {
104*5625b985SAndreas Gohr                    $value = '';
105*5625b985SAndreas Gohr                }
106*5625b985SAndreas Gohr                $key = preg_quote_cb($key);
107*5625b985SAndreas Gohr                $filter = preg_replace('/\$STRUCT\.'.$key.'\$/', $value, 1);
108*5625b985SAndreas Gohr            }
109*5625b985SAndreas Gohr        }
110*5625b985SAndreas Gohr
111*5625b985SAndreas Gohr        return $filter;
112*5625b985SAndreas Gohr    }
113*5625b985SAndreas Gohr
114*5625b985SAndreas 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