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    /**
19     * Comparisons are done against the full string (including prefix/postfix)
20     *
21     * @param QueryBuilderWhere $add
22     * @param string $tablealias
23     * @param string $colname
24     * @param string $comp
25     * @param string|string[] $value
26     * @param string $op
27     */
28    public function filter(QueryBuilderWhere $add, $tablealias, $colname, $comp, $value, $op)
29    {
30        $add = $add->where($op); // open a subgroup
31        $add->where('AND', "$tablealias.$colname != ''"); // make sure the field isn't empty
32        $op = 'AND';
33
34        /** @var QueryBuilderWhere $add Where additionional queries are added to */
35        if (is_array($value)) {
36            $add = $add->where($op); // sub where group
37            $op = 'OR';
38        }
39        $QB = $add->getQB();
40        foreach ((array) $value as $item) {
41            $column = "$tablealias.$colname";
42
43            if ($this->config['prefix']) {
44                $pl = $QB->addValue($this->config['prefix']);
45                $column = "$pl || $column";
46            }
47            if ($this->config['postfix']) {
48                $pl = $QB->addValue($this->config['postfix']);
49                $column = "$column || $pl";
50            }
51
52            $pl = $QB->addValue($item);
53            $add->where($op, "$column $comp $pl");
54        }
55    }
56}
57