xref: /plugin/struct/types/Text.php (revision b8cff1df68ab0306a2e009c448378d9530ee06b2)
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 QueryBuilderWhere $add
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(QueryBuilderWhere $add, $tablealias, $colname, $comp, $value, $op) {
38        /** @var QueryBuilderWhere $add Where additionional queries are added to */
39        if(is_array($value)) {
40            $add = $add->where($op); // sub where group
41            $op = 'OR';
42        }
43        $QB = $add->getQB();
44        foreach((array) $value as $item) {
45            $column = "$tablealias.$colname";
46
47            if($this->config['prefix']) {
48                $pl = $QB->addValue($this->config['prefix']);
49                $column = "$pl || $column";
50            }
51            if($this->config['postfix']) {
52                $pl = $QB->addValue($this->config['postfix']);
53                $column = "$column || $pl";
54            }
55
56            $pl = $QB->addValue($item);
57            $add->where($op, "$column $comp $pl");
58        }
59    }
60
61}
62