xref: /plugin/struct/meta/SearchConfig.php (revision 16b7d914c0b69bf27d1ddd8a08e05beddbeedf02)
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
14*16b7d914SAndreas Gohr    /** @var int default aggregation caching (depends on last struct save) */
15*16b7d914SAndreas Gohr    static public $CACHE_DEFAULT = 1;
16*16b7d914SAndreas Gohr    /** @var int caching depends on current user */
17*16b7d914SAndreas Gohr    static public $CACHE_USER = 2;
18*16b7d914SAndreas Gohr    /** @var int caching depends on current date */
19*16b7d914SAndreas Gohr    static public $CACHE_DATE = 4;
20*16b7d914SAndreas Gohr
21668e4f8eSAndreas Gohr    /**
22668e4f8eSAndreas Gohr     * @var array hold the configuration as parsed and extended by dynamic params
23668e4f8eSAndreas Gohr     */
241a07b696SMichael Große    protected $config;
251a07b696SMichael Große
26668e4f8eSAndreas Gohr    /**
27668e4f8eSAndreas Gohr     * @var SearchConfigParameters manages dynamic parameters
28668e4f8eSAndreas Gohr     */
2900f6af48SAndreas Gohr    protected $dynamicParameters;
3000f6af48SAndreas Gohr
315511bd5bSAndreas Gohr    /**
32*16b7d914SAndreas Gohr     * @var int the cache flag to use (binary flags)
33*16b7d914SAndreas Gohr     */
34*16b7d914SAndreas Gohr    protected $cacheFlag;
35*16b7d914SAndreas Gohr
36*16b7d914SAndreas Gohr    /**
375511bd5bSAndreas Gohr     * SearchConfig constructor.
3800f6af48SAndreas Gohr     * @param array $config The parsed configuration for this search
395511bd5bSAndreas Gohr     */
405511bd5bSAndreas Gohr    public function __construct($config) {
415511bd5bSAndreas Gohr        parent::__construct();
425511bd5bSAndreas Gohr
433ad292a8SAndreas Gohr        // setup schemas and columns
440215a637SAndreas Gohr        if(!empty($config['schemas'])) foreach($config['schemas'] as $schema) {
453ad292a8SAndreas Gohr            $this->addSchema($schema[0], $schema[1]);
463ad292a8SAndreas Gohr        }
470215a637SAndreas Gohr        if(!empty($config['cols'])) foreach($config['cols'] as $col) {
483ad292a8SAndreas Gohr            $this->addColumn($col);
493ad292a8SAndreas Gohr        }
503ad292a8SAndreas Gohr
51*16b7d914SAndreas Gohr        // cache flag setting
52*16b7d914SAndreas Gohr        $this->cacheFlag = self::$CACHE_DEFAULT;
53*16b7d914SAndreas Gohr        if(!empty($config['filters'])) $this->cacheFlag = $this->determineCacheFlag($config['filters']);
54*16b7d914SAndreas Gohr
5500f6af48SAndreas Gohr        // apply dynamic paramters
5600f6af48SAndreas Gohr        $this->dynamicParameters = new SearchConfigParameters($this);
57668e4f8eSAndreas Gohr        $config = $this->dynamicParameters->updateConfig($config);
5800f6af48SAndreas Gohr
5900f6af48SAndreas Gohr        // configure search from configuration
6000f6af48SAndreas Gohr        if(!empty($config['filter'])) foreach($config['filter'] as $filter) {
615625b985SAndreas Gohr            $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]);
625511bd5bSAndreas Gohr        }
6300f6af48SAndreas Gohr
6400f6af48SAndreas Gohr        if(!empty($config['sort'])) foreach($config['sort'] as $sort) {
65aa124708SAndreas Gohr            $this->addSort($sort[0], $sort[1]);
661a07b696SMichael Große        }
671a07b696SMichael Große
681a07b696SMichael Große        if(!empty($config['limit'])) {
691a07b696SMichael Große            $this->setLimit($config['limit']);
701a07b696SMichael Große        }
7100f6af48SAndreas Gohr
7200f6af48SAndreas Gohr        if(!empty($config['offset'])) {
7300f6af48SAndreas Gohr            $this->setLimit($config['offset']);
7400f6af48SAndreas Gohr        }
75668e4f8eSAndreas Gohr
76668e4f8eSAndreas Gohr        $this->config = $config;
771a07b696SMichael Große    }
785511bd5bSAndreas Gohr
7900f6af48SAndreas Gohr    /**
80*16b7d914SAndreas Gohr     * Set the cache flag accordingly to the set filter placeholders
81*16b7d914SAndreas Gohr     *
82*16b7d914SAndreas Gohr     * @param array $filters
83*16b7d914SAndreas Gohr     * @return int
84*16b7d914SAndreas Gohr     */
85*16b7d914SAndreas Gohr    protected function determineCacheFlag($filters) {
86*16b7d914SAndreas Gohr        $flags = self::$CACHE_DEFAULT;
87*16b7d914SAndreas Gohr
88*16b7d914SAndreas Gohr        foreach($filters as $filter) {
89*16b7d914SAndreas Gohr            if(is_array($filter)) $filter = $filter[2]; // this is the format we get fro the config parser
90*16b7d914SAndreas Gohr
91*16b7d914SAndreas Gohr            if(strpos($filter, '$USER$') !== false) {
92*16b7d914SAndreas Gohr                $flags |= self::$CACHE_USER;
93*16b7d914SAndreas Gohr            } else if(strpos($filter, '$TODAY$') !== false) {
94*16b7d914SAndreas Gohr                $flags |= self::$CACHE_DATE;
95*16b7d914SAndreas Gohr            }
96*16b7d914SAndreas Gohr        }
97*16b7d914SAndreas Gohr
98*16b7d914SAndreas Gohr        return $flags;
99*16b7d914SAndreas Gohr    }
100*16b7d914SAndreas Gohr
101*16b7d914SAndreas Gohr    /**
1025625b985SAndreas Gohr     * Replaces placeholders in the given filter value by the proper value
1035625b985SAndreas Gohr     *
1045625b985SAndreas Gohr     * @param string $filter
1055625b985SAndreas Gohr     * @return string
1065625b985SAndreas Gohr     */
1075625b985SAndreas Gohr    protected function applyFilterVars($filter) {
1085625b985SAndreas Gohr        global $ID;
1095625b985SAndreas Gohr
1105625b985SAndreas Gohr        // apply inexpensive filters first
1115625b985SAndreas Gohr        $filter = str_replace(
1125625b985SAndreas Gohr            array(
1135625b985SAndreas Gohr                '$ID$',
1145625b985SAndreas Gohr                '$NS$',
1155625b985SAndreas Gohr                '$PAGE$',
1165625b985SAndreas Gohr                '$USER$',
1175625b985SAndreas Gohr                '$TODAY$'
1185625b985SAndreas Gohr            ),
1195625b985SAndreas Gohr            array(
1205625b985SAndreas Gohr                $ID,
1215625b985SAndreas Gohr                getNS($ID),
1225625b985SAndreas Gohr                noNS($ID),
1235625b985SAndreas Gohr                isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '',
1245625b985SAndreas Gohr                date('Y-m-d')
1255625b985SAndreas Gohr            ),
1265625b985SAndreas Gohr            $filter
1275625b985SAndreas Gohr        );
1285625b985SAndreas Gohr
1295625b985SAndreas Gohr        // apply struct filter
130d19ba4b1SAndreas Gohr        while(preg_match('/\$STRUCT\.(.*?)\$/', $filter, $match)) {
1315625b985SAndreas Gohr            $key = $match[1];
1325625b985SAndreas Gohr            $column = $this->findColumn($key);
133d19ba4b1SAndreas Gohr
1345625b985SAndreas Gohr            if($column) {
1355625b985SAndreas Gohr                $label = $column->getLabel();
1365625b985SAndreas Gohr                $table = $column->getTable();
1375625b985SAndreas Gohr                $schemaData = new SchemaData($table, $ID, 0);
1385625b985SAndreas Gohr                $data = $schemaData->getDataArray();
1395625b985SAndreas Gohr                $value = $data[$label];
1405625b985SAndreas Gohr                if(is_array($value)) $value = array_shift($value);
1415625b985SAndreas Gohr            } else {
1425625b985SAndreas Gohr                $value = '';
1435625b985SAndreas Gohr            }
1445625b985SAndreas Gohr            $key = preg_quote_cb($key);
145d19ba4b1SAndreas Gohr            $filter = preg_replace('/\$STRUCT\.' . $key . '\$/', $value, $filter, 1);
146d19ba4b1SAndreas Gohr
1475625b985SAndreas Gohr        }
1485625b985SAndreas Gohr
1495625b985SAndreas Gohr        return $filter;
1505625b985SAndreas Gohr    }
1515625b985SAndreas Gohr
1525625b985SAndreas Gohr    /**
153*16b7d914SAndreas Gohr     * @return int cacheflag for this search
154*16b7d914SAndreas Gohr     */
155*16b7d914SAndreas Gohr    public function getCacheFlag() {
156*16b7d914SAndreas Gohr        return $this->cacheFlag;
157*16b7d914SAndreas Gohr    }
158*16b7d914SAndreas Gohr
159*16b7d914SAndreas Gohr    /**
16000f6af48SAndreas Gohr     * Access the dynamic paramters of this search
16100f6af48SAndreas Gohr     *
162668e4f8eSAndreas Gohr     * Note: This call returns a clone of the parameters as they were initialized
16300f6af48SAndreas Gohr     *
16400f6af48SAndreas Gohr     * @return SearchConfigParameters
16500f6af48SAndreas Gohr     */
16600f6af48SAndreas Gohr    public function getDynamicParameters() {
16700f6af48SAndreas Gohr        return clone $this->dynamicParameters;
1685511bd5bSAndreas Gohr    }
1695511bd5bSAndreas Gohr
17000f6af48SAndreas Gohr    /**
17107993756SAndreas Gohr     * @return array the current config
17200f6af48SAndreas Gohr     */
17307993756SAndreas Gohr    public function getConf() {
1741a07b696SMichael Große        return $this->config;
1751a07b696SMichael Große    }
1761a07b696SMichael Große
1775511bd5bSAndreas Gohr}
178