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