xref: /plugin/struct/types/Text.php (revision 4e427bd54e8ef843ff97ba884a78700c185fd6bc)
1<?php
2namespace dokuwiki\plugin\struct\types;
3
4use dokuwiki\plugin\struct\meta\QueryBuilder;
5use dokuwiki\plugin\struct\meta\QueryBuilderWhere;
6
7class Text extends AbstractMultiBaseType {
8
9    protected $config = array(
10        'prefix' => '',
11        'postfix' => '',
12    );
13
14    /**
15     * Output the stored data
16     *
17     * @param string|int $value the value stored in the database
18     * @param \Doku_Renderer $R the renderer currently used to render the data
19     * @param string $mode The mode the output is rendered in (eg. XHTML)
20     * @return bool true if $mode could be satisfied
21     */
22    public function renderValue($value, \Doku_Renderer $R, $mode) {
23        $R->cdata($this->config['prefix'] . $value . $this->config['postfix']);
24        return true;
25    }
26
27    /**
28     * Comparisons are done against the full string (including prefix/postfix)
29     *
30     * @param QueryBuilder $QB
31     * @param string $tablealias
32     * @param string $colname
33     * @param string $comp
34     * @param string|string[] $value
35     * @param string $op
36     */
37    public function filter(QueryBuilder $QB, $tablealias, $colname, $comp, $value, $op) {
38        /** @var QueryBuilderWhere $add Where additionional queries are added to */
39        if(is_array($value)) {
40            $add = $QB->filters()->where($op); // sub where group
41            $op = 'OR';
42        } else {
43            $add = $QB->filters(); // main where clause
44        }
45        foreach((array) $value as $item) {
46            $column = "$tablealias.$colname";
47
48            if($this->config['prefix']) {
49                $pl = $QB->addValue($this->config['prefix']);
50                $column = "$pl || $column";
51            }
52            if($this->config['postfix']) {
53                $pl = $QB->addValue($this->config['postfix']);
54                $column = "$column || $pl";
55            }
56
57            $pl = $QB->addValue($item);
58            $add->where($op, "$column $comp $pl");
59        }
60    }
61
62}
63