xref: /plugin/struct/types/LongText.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
1<?php
2
3namespace dokuwiki\plugin\struct\types;
4
5class LongText extends AbstractMultiBaseType
6{
7    use TraitFilterPrefix;
8
9    protected $config = ['prefix' => '', 'postfix' => '', 'rows' => '5', 'cols' => '50'];
10
11
12    /**
13     * Output the stored data
14     *
15     * @param string|int $value the value stored in the database
16     * @param \Doku_Renderer $R the renderer currently used to render the data
17     * @param string $mode The mode the output is rendered in (eg. XHTML)
18     * @return bool true if $mode could be satisfied
19     */
20    public function renderValue($value, \Doku_Renderer $R, $mode)
21    {
22        if ($mode === 'xhtml') {
23            $valueWithBR = nl2br(hsc($value));
24            $R->doc .= hsc($this->config['prefix']) . $valueWithBR . hsc($this->config['postfix']);
25        } else {
26            $R->cdata($this->config['prefix'] . $value . $this->config['postfix']);
27        }
28        return true;
29    }
30
31    /**
32     * Clean line endings
33     *
34     * @param int|string $rawvalue
35     * @return int|string
36     */
37    public function validate($rawvalue)
38    {
39        $rawvalue = rtrim($rawvalue);
40        $rawvalue = cleanText($rawvalue);
41        return $rawvalue;
42    }
43
44    /**
45     * Use a text area for input
46     *
47     * @param string $name
48     * @param string $rawvalue
49     * @param string $htmlID
50     *
51     * @return string
52     */
53    public function valueEditor($name, $rawvalue, $htmlID)
54    {
55        $rawvalue = formText($rawvalue);
56        $params = ['name' => $name, 'class' => 'struct_' . strtolower($this->getClass()), 'id' => $htmlID, 'rows' => $this->config['rows'], 'cols' => $this->config['cols']];
57        $attributes = buildAttributes($params, true);
58
59        return "<textarea $attributes>$rawvalue</textarea>";
60    }
61}
62