xref: /plugin/struct/meta/SearchConfig.php (revision ed3de3d67c65b9b61c3e1eeabb225a2664ee001d)
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    /** @var int default aggregation caching (depends on last struct save) */
15    static public $CACHE_DEFAULT = 1;
16    /** @var int caching depends on current user */
17    static public $CACHE_USER = 2;
18    /** @var int caching depends on current date */
19    static public $CACHE_DATE = 4;
20
21    /**
22     * @var array hold the configuration as parsed and extended by dynamic params
23     */
24    protected $config;
25
26    /**
27     * @var SearchConfigParameters manages dynamic parameters
28     */
29    protected $dynamicParameters;
30
31    /**
32     * @var int the cache flag to use (binary flags)
33     */
34    protected $cacheFlag;
35
36    /**
37     * SearchConfig constructor.
38     * @param array $config The parsed configuration for this search
39     */
40    public function __construct($config) {
41        parent::__construct();
42
43        // setup schemas and columns
44        if(!empty($config['schemas'])) foreach($config['schemas'] as $schema) {
45            $this->addSchema($schema[0], $schema[1]);
46        }
47        if(!empty($config['cols'])) foreach($config['cols'] as $col) {
48            $this->addColumn($col);
49        }
50
51        // cache flag setting
52        $this->cacheFlag = self::$CACHE_DEFAULT;
53        if(!empty($config['filters'])) $this->cacheFlag = $this->determineCacheFlag($config['filters']);
54
55        // apply dynamic paramters
56        $this->dynamicParameters = new SearchConfigParameters($this);
57        $config = $this->dynamicParameters->updateConfig($config);
58
59        // configure search from configuration
60        if(!empty($config['filter'])) foreach($config['filter'] as $filter) {
61            $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]);
62        }
63
64        if(!empty($config['sort'])) foreach($config['sort'] as $sort) {
65            $this->addSort($sort[0], $sort[1]);
66        }
67
68        if(!empty($config['limit'])) {
69            $this->setLimit($config['limit']);
70        }
71
72        if(!empty($config['offset'])) {
73            $this->setLimit($config['offset']);
74        }
75
76        $this->config = $config;
77    }
78
79    /**
80     * Set the cache flag accordingly to the set filter placeholders
81     *
82     * @param array $filters
83     * @return int
84     */
85    protected function determineCacheFlag($filters) {
86        $flags = self::$CACHE_DEFAULT;
87
88        foreach($filters as $filter) {
89            if(is_array($filter)) $filter = $filter[2]; // this is the format we get fro the config parser
90
91            if(strpos($filter, '$USER$') !== false) {
92                $flags |= self::$CACHE_USER;
93            } else if(strpos($filter, '$TODAY$') !== false) {
94                $flags |= self::$CACHE_DATE;
95            }
96        }
97
98        return $flags;
99    }
100
101    /**
102     * Replaces placeholders in the given filter value by the proper value
103     *
104     * @param string $filter
105     * @return string
106     */
107    protected function applyFilterVars($filter) {
108        global $ID;
109
110        // apply inexpensive filters first
111        $filter = str_replace(
112            array(
113                '$ID$',
114                '$NS$',
115                '$PAGE$',
116                '$USER$',
117                '$TODAY$'
118            ),
119            array(
120                $ID,
121                getNS($ID),
122                noNS($ID),
123                isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '',
124                date('Y-m-d')
125            ),
126            $filter
127        );
128
129        // apply struct filter
130        while(preg_match('/\$STRUCT\.(.*?)\$/', $filter, $match)) {
131            $key = $match[1];
132            $column = $this->findColumn($key);
133
134            if($column) {
135                $label = $column->getLabel();
136                $table = $column->getTable();
137                $schemaData = new SchemaData($table, $ID, 0);
138                $data = $schemaData->getDataArray();
139                $value = $data[$label];
140                if(is_array($value)) $value = array_shift($value);
141            } else {
142                $value = '';
143            }
144            $key = preg_quote_cb($key);
145            $filter = preg_replace('/\$STRUCT\.' . $key . '\$/', $value, $filter, 1);
146
147        }
148
149        return $filter;
150    }
151
152    /**
153     * @return int cacheflag for this search
154     */
155    public function getCacheFlag() {
156        return $this->cacheFlag;
157    }
158
159    /**
160     * Access the dynamic paramters of this search
161     *
162     * Note: This call returns a clone of the parameters as they were initialized
163     *
164     * @return SearchConfigParameters
165     */
166    public function getDynamicParameters() {
167        return clone $this->dynamicParameters;
168    }
169
170    /**
171     * @return array the current config
172     */
173    public function getConf() {
174        return $this->config;
175    }
176
177}
178