xref: /plugin/struct/types/Text.php (revision c26fb18b58d87886f772aa75934ca8ea1aa56c59)
1<?php
2namespace dokuwiki\plugin\struct\types;
3
4use dokuwiki\plugin\struct\meta\QueryBuilder;
5
6class Text extends AbstractMultiBaseType {
7
8    protected $config = array(
9        'prefix' => '',
10        'postfix' => '',
11    );
12
13    /**
14     * Output the stored data
15     *
16     * @param string|int $value the value stored in the database
17     * @param \Doku_Renderer $R the renderer currently used to render the data
18     * @param string $mode The mode the output is rendered in (eg. XHTML)
19     * @return bool true if $mode could be satisfied
20     */
21    public function renderValue($value, \Doku_Renderer $R, $mode) {
22        $R->cdata($this->config['prefix'] . $value . $this->config['postfix']);
23        return true;
24    }
25
26    /**
27     * Comparisons should always be done against the full string
28     *
29     * @param string $column
30     * @param string $comp
31     * @param string $value
32     * @return array
33     */
34    public function compare($column, $comp, $value) {
35        $opt = array();
36        if ($this->config['prefix']) {
37            $column = "? || $column";
38            $opt[] = $this->config['prefix'];
39        }
40        if ($this->config['postfix']) {
41            $column = "$column || ?";
42            $opt[] = $this->config['postfix'];
43        }
44
45        // this assumes knowledge about the parent implementation which is kinda bad
46        // but avoids some code duplication
47        list($sql) = parent::compare($column, $comp, $value);
48        $opt[] = $value;
49
50        return array($sql, $opt);
51    }
52
53    /**
54     * @param QueryBuilder $QB
55     * @param string $tablealias
56     * @param string $colname
57     * @param string $comp
58     * @param string $value
59     * @param string $op
60     */
61    public function filter(QueryBuilder $QB, $tablealias, $colname, $comp, $value, $op) {
62        $column = "$tablealias.$colname";
63
64        if ($this->config['prefix']) {
65            $pl = $QB->addValue($this->config['prefix']);
66            $column = "$pl || $column";
67        }
68        if ($this->config['postfix']) {
69            $pl = $QB->addValue($this->config['postfix']);
70            $column = "$column || $pl";
71        }
72
73        $pl = $QB->addValue($value);
74
75        switch($comp) {
76            case '~':
77                $comp = 'LIKE';
78                break;
79            case '!~':
80                $comp = 'NOT LIKE';
81                break;
82        }
83
84        $QB->filters()->where($op, "$column $comp $pl");
85    }
86
87}
88