xref: /plugin/struct/meta/Value.php (revision bfb78ce36191746735158574eab0f078874f52f2)
138fa36fbSAndreas Gohr<?php
238fa36fbSAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
438fa36fbSAndreas Gohr
538fa36fbSAndreas Gohr/**
638fa36fbSAndreas Gohr * Class Value
738fa36fbSAndreas Gohr *
838fa36fbSAndreas Gohr * Holds the value for a single "cell". That value may be an array for multi value columns
938fa36fbSAndreas Gohr *
10ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta
1138fa36fbSAndreas Gohr */
12d6d97f60SAnna Dabrowskaclass Value
13d6d97f60SAnna Dabrowska{
1438fa36fbSAndreas Gohr    /** @var Column */
1538fa36fbSAndreas Gohr    protected $column;
1638fa36fbSAndreas Gohr
1738fa36fbSAndreas Gohr    /** @var  array|int|string */
1838fa36fbSAndreas Gohr    protected $value;
1938fa36fbSAndreas Gohr
2090421550SAndreas Gohr    /** @var  array|int|string */
217234bfb1Ssplitbrain    protected $rawvalue;
2290421550SAndreas Gohr
235241ca30SAndreas Gohr    /** @var array|int|string */
247234bfb1Ssplitbrain    protected $display;
255241ca30SAndreas Gohr
267717c082SMichael Große    /** @var array|int|string */
277234bfb1Ssplitbrain    protected $compare;
287717c082SMichael Große
29c0230d2cSAndreas Gohr    /** @var bool is this a raw value only? */
30c0230d2cSAndreas Gohr    protected $rawonly = false;
31c0230d2cSAndreas Gohr
3238fa36fbSAndreas Gohr    /**
3338fa36fbSAndreas Gohr     * Value constructor.
3438fa36fbSAndreas Gohr     *
3538fa36fbSAndreas Gohr     * @param Column $column
3638fa36fbSAndreas Gohr     * @param array|int|string $value
3738fa36fbSAndreas Gohr     */
38d6d97f60SAnna Dabrowska    public function __construct(Column $column, $value)
39d6d97f60SAnna Dabrowska    {
4038fa36fbSAndreas Gohr        $this->column = $column;
4117560ecbSAndreas Gohr        $this->setValue($value);
4238fa36fbSAndreas Gohr    }
4338fa36fbSAndreas Gohr
4438fa36fbSAndreas Gohr    /**
4538fa36fbSAndreas Gohr     * @return Column
4638fa36fbSAndreas Gohr     */
47d6d97f60SAnna Dabrowska    public function getColumn()
48d6d97f60SAnna Dabrowska    {
4938fa36fbSAndreas Gohr        return $this->column;
5038fa36fbSAndreas Gohr    }
5138fa36fbSAndreas Gohr
5238fa36fbSAndreas Gohr    /**
5338fa36fbSAndreas Gohr     * @return array|int|string
5438fa36fbSAndreas Gohr     */
55d6d97f60SAnna Dabrowska    public function getValue()
56d6d97f60SAnna Dabrowska    {
57c0230d2cSAndreas Gohr        if ($this->rawonly) {
58c0230d2cSAndreas Gohr            throw new StructException('Accessing value of rawonly value forbidden');
59c0230d2cSAndreas Gohr        }
6038fa36fbSAndreas Gohr        return $this->value;
6138fa36fbSAndreas Gohr    }
6238fa36fbSAndreas Gohr
6338fa36fbSAndreas Gohr    /**
6490421550SAndreas Gohr     * Access the raw value
6590421550SAndreas Gohr     *
6690421550SAndreas Gohr     * @return array|string (array on multi)
6790421550SAndreas Gohr     */
68d6d97f60SAnna Dabrowska    public function getRawValue()
69d6d97f60SAnna Dabrowska    {
7090421550SAndreas Gohr        return $this->rawvalue;
7190421550SAndreas Gohr    }
7290421550SAndreas Gohr
7390421550SAndreas Gohr    /**
745241ca30SAndreas Gohr     * Access the display value
755241ca30SAndreas Gohr     *
765241ca30SAndreas Gohr     * @return array|string (array on multi)
775241ca30SAndreas Gohr     */
78d6d97f60SAnna Dabrowska    public function getDisplayValue()
79d6d97f60SAnna Dabrowska    {
80c0230d2cSAndreas Gohr        if ($this->rawonly) {
81c0230d2cSAndreas Gohr            throw new StructException('Accessing displayvalue of rawonly value forbidden');
82c0230d2cSAndreas Gohr        }
835241ca30SAndreas Gohr        return $this->display;
845241ca30SAndreas Gohr    }
855241ca30SAndreas Gohr
865241ca30SAndreas Gohr    /**
877717c082SMichael Große     * Access the compare value
887717c082SMichael Große     *
897717c082SMichael Große     * @return array|string (array on multi)
907717c082SMichael Große     */
91d6d97f60SAnna Dabrowska    public function getCompareValue()
92d6d97f60SAnna Dabrowska    {
937717c082SMichael Große        if ($this->rawonly) {
947717c082SMichael Große            throw new StructException('Accessing comparevalue of rawonly value forbidden');
957717c082SMichael Große        }
967717c082SMichael Große        return $this->compare;
977717c082SMichael Große    }
987717c082SMichael Große
997717c082SMichael Große    /**
10017560ecbSAndreas Gohr     * Allows overwriting the current value
10117560ecbSAndreas Gohr     *
10219065d4eSAndreas Gohr     * Cleans the value(s) of empties
10319065d4eSAndreas Gohr     *
10417560ecbSAndreas Gohr     * @param array|int|string $value
105c0230d2cSAndreas Gohr     * @param bool $israw is the passed value a raw value? turns Value into rawonly
10617560ecbSAndreas Gohr     */
107d6d97f60SAnna Dabrowska    public function setValue($value, $israw = false)
108d6d97f60SAnna Dabrowska    {
109c0230d2cSAndreas Gohr        $this->rawonly = $israw;
110c0230d2cSAndreas Gohr
11190421550SAndreas Gohr        // treat all givens the same
11290421550SAndreas Gohr        if (!is_array($value)) {
1137234bfb1Ssplitbrain            $value = [$value];
11417560ecbSAndreas Gohr        }
1157937b821SAndreas Gohr
11690421550SAndreas Gohr        // reset/init
1177234bfb1Ssplitbrain        $this->value = [];
1187234bfb1Ssplitbrain        $this->rawvalue = [];
1197234bfb1Ssplitbrain        $this->display = [];
1207234bfb1Ssplitbrain        $this->compare = [];
12190421550SAndreas Gohr
12219065d4eSAndreas Gohr        // remove all blanks
12390421550SAndreas Gohr        foreach ($value as $val) {
124c0230d2cSAndreas Gohr            if ($israw) {
125c0230d2cSAndreas Gohr                $raw = $val;
126c0230d2cSAndreas Gohr            } else {
12790421550SAndreas Gohr                $raw = $this->column->getType()->rawValue($val);
128c0230d2cSAndreas Gohr            }
1296a819106SAndreas Gohr            if ('' === trim((string)$raw)) continue;
13090421550SAndreas Gohr            $this->value[] = $val;
13190421550SAndreas Gohr            $this->rawvalue[] = $raw;
132c0230d2cSAndreas Gohr            if ($israw) {
133c0230d2cSAndreas Gohr                $this->display[] = $val;
1347717c082SMichael Große                $this->compare[] = $val;
135c0230d2cSAndreas Gohr            } else {
1365241ca30SAndreas Gohr                $this->display[] = $this->column->getType()->displayValue($val);
1377717c082SMichael Große                $this->compare[] = $this->column->getType()->compareValue($val);
13890421550SAndreas Gohr            }
139c0230d2cSAndreas Gohr        }
1407937b821SAndreas Gohr
14190421550SAndreas Gohr        // make single value again
1427937b821SAndreas Gohr        if (!$this->column->isMulti()) {
14390421550SAndreas Gohr            $this->value = (string)array_shift($this->value);
14490421550SAndreas Gohr            $this->rawvalue = (string)array_shift($this->rawvalue);
1455241ca30SAndreas Gohr            $this->display = (string)array_shift($this->display);
1467717c082SMichael Große            $this->compare = (string)array_shift($this->compare);
1477937b821SAndreas Gohr        }
14819065d4eSAndreas Gohr    }
1497937b821SAndreas Gohr
15090421550SAndreas Gohr    /**
15190421550SAndreas Gohr     * Is this empty?
15290421550SAndreas Gohr     *
15390421550SAndreas Gohr     * @return bool
15490421550SAndreas Gohr     */
155d6d97f60SAnna Dabrowska    public function isEmpty()
156d6d97f60SAnna Dabrowska    {
1577234bfb1Ssplitbrain        return ($this->rawvalue === '' || $this->rawvalue === []);
15817560ecbSAndreas Gohr    }
15917560ecbSAndreas Gohr
16017560ecbSAndreas Gohr    /**
16138fa36fbSAndreas Gohr     * Render the value using the given renderer and mode
16238fa36fbSAndreas Gohr     *
163053212b1SAndreas Gohr     * automativally picks the right mechanism depending on multi or single value
164053212b1SAndreas Gohr     *
16519065d4eSAndreas Gohr     * values are only rendered when there is a value
16619065d4eSAndreas Gohr     *
16738fa36fbSAndreas Gohr     * @param \Doku_Renderer $R
16838fa36fbSAndreas Gohr     * @param string $mode
169053212b1SAndreas Gohr     * @return bool
17038fa36fbSAndreas Gohr     */
171d6d97f60SAnna Dabrowska    public function render(\Doku_Renderer $R, $mode)
172d6d97f60SAnna Dabrowska    {
17338fa36fbSAndreas Gohr        if ($this->column->isMulti()) {
17419065d4eSAndreas Gohr            if (count($this->value)) {
175053212b1SAndreas Gohr                return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
17619065d4eSAndreas Gohr            }
1777234bfb1Ssplitbrain        } elseif ($this->value !== '') {
178053212b1SAndreas Gohr            return $this->column->getType()->renderValue($this->value, $R, $mode);
179053212b1SAndreas Gohr        }
18019065d4eSAndreas Gohr        return true;
18119065d4eSAndreas Gohr    }
182053212b1SAndreas Gohr
1839e7e1786SMichael Grosse    /**
1849e7e1786SMichael Grosse     * Render this value as a tag-link in a struct cloud
1859e7e1786SMichael Grosse     *
1869e7e1786SMichael Grosse     * @param \Doku_Renderer $R
1879e7e1786SMichael Grosse     * @param string $mode
1889e7e1786SMichael Grosse     * @param string $page
1899e7e1786SMichael Grosse     * @param string $filterQuery
1909e7e1786SMichael Grosse     * @param int $weight
191*bfb78ce3SAnna Dabrowska     * @param int $showCount
1929e7e1786SMichael Grosse     */
193*bfb78ce3SAnna Dabrowska    public function renderAsTagCloudLink(\Doku_Renderer $R, $mode, $page, $filterQuery, $weight, $showCount)
194d6d97f60SAnna Dabrowska    {
1955d17398fSMichael Grosse        $value = is_array($this->value) ? $this->value[0] : $this->value;
196*bfb78ce3SAnna Dabrowska        $this->column->getType()->renderTagCloudLink($value, $R, $mode, $page, $filterQuery, $weight, $showCount);
197262c0fc6SMichael Grosse    }
198262c0fc6SMichael Grosse
199053212b1SAndreas Gohr    /**
200053212b1SAndreas Gohr     * Return the value editor for this value field
201053212b1SAndreas Gohr     *
202053212b1SAndreas Gohr     * @param string $name The field name to use in the editor
203053212b1SAndreas Gohr     * @return string The HTML for the editor
204053212b1SAndreas Gohr     */
205d6d97f60SAnna Dabrowska    public function getValueEditor($name, $id)
206d6d97f60SAnna Dabrowska    {
207053212b1SAndreas Gohr        if ($this->column->isMulti()) {
208ee983135SMichael Große            return $this->column->getType()->multiValueEditor($name, $this->rawvalue, $id);
209053212b1SAndreas Gohr        } else {
210ee983135SMichael Große            return $this->column->getType()->valueEditor($name, $this->rawvalue, $id);
21138fa36fbSAndreas Gohr        }
21238fa36fbSAndreas Gohr    }
21319065d4eSAndreas Gohr
21419065d4eSAndreas Gohr    /**
21519065d4eSAndreas Gohr     * Filter callback to strip empty values
21619065d4eSAndreas Gohr     *
21719065d4eSAndreas Gohr     * @param string $input
21819065d4eSAndreas Gohr     * @return bool
21919065d4eSAndreas Gohr     */
220d6d97f60SAnna Dabrowska    public function filter($input)
221d6d97f60SAnna Dabrowska    {
22219065d4eSAndreas Gohr        return '' !== ((string)$input);
22319065d4eSAndreas Gohr    }
2245bc00e11SAndreas Gohr
2255bc00e11SAndreas Gohr    /**
2265bc00e11SAndreas Gohr     * Get a string representation of this value
2275bc00e11SAndreas Gohr     *
2285bc00e11SAndreas Gohr     * @return string
2295bc00e11SAndreas Gohr     */
2305bc00e11SAndreas Gohr    public function __toString()
2315bc00e11SAndreas Gohr    {
2325bc00e11SAndreas Gohr        return '[' . $this->getColumn()->getFullQualifiedLabel() . '] ' .
2337234bfb1Ssplitbrain            implode(',', (array)$this->getRawValue());
2345bc00e11SAndreas Gohr    }
23538fa36fbSAndreas Gohr}
236