1<?php
2
3namespace dokuwiki\plugin\struct\types;
4
5use dokuwiki\plugin\struct\meta\QueryBuilderWhere;
6
7/**
8 * Class TraitFilterPrefix
9 *
10 * This implements a filter function for Types that use pre- or post fixes. It makes sure
11 * given values are checked against the pre/postfixed values from the database
12 *
13 * @package dokuwiki\plugin\struct\types
14 */
15trait TraitFilterPrefix
16{
17    /**
18     * Comparisons are done against the full string (including prefix/postfix)
19     *
20     * @param QueryBuilderWhere $add
21     * @param string $tablealias
22     * @param string $colname
23     * @param string $comp
24     * @param string|string[] $value
25     * @param string $op
26     */
27    public function filter(QueryBuilderWhere $add, $tablealias, $colname, $comp, $value, $op)
28    {
29        /** @var QueryBuilderWhere $add Where additionional queries are added to */
30        if (is_array($value)) {
31            $add = $add->where($op); // sub where group
32            $op = 'OR';
33        }
34        $QB = $add->getQB();
35        foreach ((array)$value as $item) {
36            $column = "$tablealias.$colname";
37
38            if ($this->config['prefix']) {
39                $pl = $QB->addValue($this->config['prefix']);
40                $column = "$pl || $column";
41            }
42            if ($this->config['postfix']) {
43                $pl = $QB->addValue($this->config['postfix']);
44                $column = "$column || $pl";
45            }
46
47            $pl = $QB->addValue($item);
48            $add->where($op, "$column $comp $pl");
49        }
50    }
51}
52