xref: /plugin/struct/meta/Value.php (revision 7717c082e7685f74d7eb1592595a7a2ef6225f8c)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5/**
6 * Class Value
7 *
8 * Holds the value for a single "cell". That value may be an array for multi value columns
9 *
10 * @package dokuwiki\plugin\struct\meta
11 */
12class Value {
13
14    /** @var Column */
15    protected $column;
16
17    /** @var  array|int|string */
18    protected $value;
19
20    /** @var  array|int|string */
21    protected $rawvalue = null;
22
23    /** @var array|int|string */
24    protected $display = null;
25
26    /** @var array|int|string */
27    protected $compare = null;
28
29    /** @var bool is this a raw value only? */
30    protected $rawonly = false;
31
32    /**
33     * Value constructor.
34     *
35     * @param Column $column
36     * @param array|int|string $value
37     */
38    public function __construct(Column $column, $value) {
39        $this->column = $column;
40        $this->setValue($value);
41    }
42
43    /**
44     * @return Column
45     */
46    public function getColumn() {
47        return $this->column;
48    }
49
50    /**
51     * @return array|int|string
52     */
53    public function getValue() {
54        if($this->rawonly) {
55            throw new StructException('Accessing value of rawonly value forbidden');
56        }
57        return $this->value;
58    }
59
60    /**
61     * Access the raw value
62     *
63     * @return array|string (array on multi)
64     */
65    public function getRawValue() {
66        return $this->rawvalue;
67    }
68
69    /**
70     * Access the display value
71     *
72     * @return array|string (array on multi)
73     */
74    public function getDisplayValue() {
75        if($this->rawonly) {
76            throw new StructException('Accessing displayvalue of rawonly value forbidden');
77        }
78        return $this->display;
79    }
80
81    /**
82     * Access the compare value
83     *
84     * @return array|string (array on multi)
85     */
86    public function getCompareValue() {
87        if($this->rawonly) {
88            throw new StructException('Accessing comparevalue of rawonly value forbidden');
89        }
90        return $this->compare;
91    }
92
93    /**
94     * Allows overwriting the current value
95     *
96     * Cleans the value(s) of empties
97     *
98     * @param array|int|string $value
99     * @param bool $israw is the passed value a raw value? turns Value into rawonly
100     */
101    public function setValue($value, $israw=false) {
102        $this->rawonly = $israw;
103
104        // treat all givens the same
105        if(!is_array($value)) {
106            $value = array($value);
107        }
108
109        // reset/init
110        $this->value = array();
111        $this->rawvalue = array();
112        $this->display = array();
113        $this->compare = array();
114
115        // remove all blanks
116        foreach($value as $val) {
117            if($israw) {
118                $raw = $val;
119            } else {
120                $raw = $this->column->getType()->rawValue($val);
121            }
122            if('' === (string) trim($raw)) continue;
123            $this->value[] = $val;
124            $this->rawvalue[] = $raw;
125            if($israw) {
126                $this->display[] = $val;
127                $this->compare[] = $val;
128            } else {
129                $this->display[] = $this->column->getType()->displayValue($val);
130                $this->compare[] = $this->column->getType()->compareValue($val);
131            }
132        }
133
134        // make single value again
135        if(!$this->column->isMulti()) {
136            $this->value = (string) array_shift($this->value);
137            $this->rawvalue = (string) array_shift($this->rawvalue);
138            $this->display = (string) array_shift($this->display);
139            $this->compare = (string) array_shift($this->compare);
140        }
141    }
142
143    /**
144     * Is this empty?
145     *
146     * @return bool
147     */
148    public function isEmpty() {
149        return ($this->rawvalue === '' || $this->rawvalue === array());
150    }
151
152    /**
153     * Render the value using the given renderer and mode
154     *
155     * automativally picks the right mechanism depending on multi or single value
156     *
157     * values are only rendered when there is a value
158     *
159     * @param \Doku_Renderer $R
160     * @param string $mode
161     * @return bool
162     */
163    public function render(\Doku_Renderer $R, $mode) {
164        if($this->column->isMulti()) {
165            if(count($this->value)) {
166                return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
167            }
168        } else {
169            if($this->value !== '') {
170                return $this->column->getType()->renderValue($this->value, $R, $mode);
171            }
172        }
173        return true;
174    }
175
176    /**
177     * Render this value as a tag-link in a struct cloud
178     *
179     * @param \Doku_Renderer $R
180     * @param string $mode
181     * @param string $page
182     * @param string $filterQuery
183     * @param int $weight
184     */
185    public function renderAsTagCloudLink(\Doku_Renderer $R, $mode, $page, $filterQuery, $weight) {
186        $value = is_array($this->value) ? $this->value[0] : $this->value;
187        $this->column->getType()->renderTagCloudLink($value, $R, $mode, $page, $filterQuery, $weight);
188    }
189
190    /**
191     * Return the value editor for this value field
192     *
193     * @param string $name The field name to use in the editor
194     * @return string The HTML for the editor
195     */
196    public function getValueEditor($name) {
197        if($this->column->isMulti()) {
198            return $this->column->getType()->multiValueEditor($name, $this->rawvalue);
199        } else {
200            return $this->column->getType()->valueEditor($name, $this->rawvalue);
201        }
202    }
203
204    /**
205     * Filter callback to strip empty values
206     *
207     * @param string $input
208     * @return bool
209     */
210    public function filter($input) {
211        return '' !== ((string) $input);
212    }
213}
214