xref: /plugin/struct/meta/Value.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
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 */
12*d6d97f60SAnna Dabrowskaclass Value
13*d6d97f60SAnna Dabrowska{
1438fa36fbSAndreas Gohr
1538fa36fbSAndreas Gohr    /** @var Column */
1638fa36fbSAndreas Gohr    protected $column;
1738fa36fbSAndreas Gohr
1838fa36fbSAndreas Gohr    /** @var  array|int|string */
1938fa36fbSAndreas Gohr    protected $value;
2038fa36fbSAndreas Gohr
2190421550SAndreas Gohr    /** @var  array|int|string */
2290421550SAndreas Gohr    protected $rawvalue = null;
2390421550SAndreas Gohr
245241ca30SAndreas Gohr    /** @var array|int|string */
255241ca30SAndreas Gohr    protected $display = null;
265241ca30SAndreas Gohr
277717c082SMichael Große    /** @var array|int|string */
287717c082SMichael Große    protected $compare = null;
297717c082SMichael Große
30c0230d2cSAndreas Gohr    /** @var bool is this a raw value only? */
31c0230d2cSAndreas Gohr    protected $rawonly = false;
32c0230d2cSAndreas Gohr
3338fa36fbSAndreas Gohr    /**
3438fa36fbSAndreas Gohr     * Value constructor.
3538fa36fbSAndreas Gohr     *
3638fa36fbSAndreas Gohr     * @param Column $column
3738fa36fbSAndreas Gohr     * @param array|int|string $value
3838fa36fbSAndreas Gohr     */
39*d6d97f60SAnna Dabrowska    public function __construct(Column $column, $value)
40*d6d97f60SAnna Dabrowska    {
4138fa36fbSAndreas Gohr        $this->column = $column;
4217560ecbSAndreas Gohr        $this->setValue($value);
4338fa36fbSAndreas Gohr    }
4438fa36fbSAndreas Gohr
4538fa36fbSAndreas Gohr    /**
4638fa36fbSAndreas Gohr     * @return Column
4738fa36fbSAndreas Gohr     */
48*d6d97f60SAnna Dabrowska    public function getColumn()
49*d6d97f60SAnna Dabrowska    {
5038fa36fbSAndreas Gohr        return $this->column;
5138fa36fbSAndreas Gohr    }
5238fa36fbSAndreas Gohr
5338fa36fbSAndreas Gohr    /**
5438fa36fbSAndreas Gohr     * @return array|int|string
5538fa36fbSAndreas Gohr     */
56*d6d97f60SAnna Dabrowska    public function getValue()
57*d6d97f60SAnna Dabrowska    {
58c0230d2cSAndreas Gohr        if ($this->rawonly) {
59c0230d2cSAndreas Gohr            throw new StructException('Accessing value of rawonly value forbidden');
60c0230d2cSAndreas Gohr        }
6138fa36fbSAndreas Gohr        return $this->value;
6238fa36fbSAndreas Gohr    }
6338fa36fbSAndreas Gohr
6438fa36fbSAndreas Gohr    /**
6590421550SAndreas Gohr     * Access the raw value
6690421550SAndreas Gohr     *
6790421550SAndreas Gohr     * @return array|string (array on multi)
6890421550SAndreas Gohr     */
69*d6d97f60SAnna Dabrowska    public function getRawValue()
70*d6d97f60SAnna Dabrowska    {
7190421550SAndreas Gohr        return $this->rawvalue;
7290421550SAndreas Gohr    }
7390421550SAndreas Gohr
7490421550SAndreas Gohr    /**
755241ca30SAndreas Gohr     * Access the display value
765241ca30SAndreas Gohr     *
775241ca30SAndreas Gohr     * @return array|string (array on multi)
785241ca30SAndreas Gohr     */
79*d6d97f60SAnna Dabrowska    public function getDisplayValue()
80*d6d97f60SAnna Dabrowska    {
81c0230d2cSAndreas Gohr        if ($this->rawonly) {
82c0230d2cSAndreas Gohr            throw new StructException('Accessing displayvalue of rawonly value forbidden');
83c0230d2cSAndreas Gohr        }
845241ca30SAndreas Gohr        return $this->display;
855241ca30SAndreas Gohr    }
865241ca30SAndreas Gohr
875241ca30SAndreas Gohr    /**
887717c082SMichael Große     * Access the compare value
897717c082SMichael Große     *
907717c082SMichael Große     * @return array|string (array on multi)
917717c082SMichael Große     */
92*d6d97f60SAnna Dabrowska    public function getCompareValue()
93*d6d97f60SAnna Dabrowska    {
947717c082SMichael Große        if ($this->rawonly) {
957717c082SMichael Große            throw new StructException('Accessing comparevalue of rawonly value forbidden');
967717c082SMichael Große        }
977717c082SMichael Große        return $this->compare;
987717c082SMichael Große    }
997717c082SMichael Große
1007717c082SMichael Große    /**
10117560ecbSAndreas Gohr     * Allows overwriting the current value
10217560ecbSAndreas Gohr     *
10319065d4eSAndreas Gohr     * Cleans the value(s) of empties
10419065d4eSAndreas Gohr     *
10517560ecbSAndreas Gohr     * @param array|int|string $value
106c0230d2cSAndreas Gohr     * @param bool $israw is the passed value a raw value? turns Value into rawonly
10717560ecbSAndreas Gohr     */
108*d6d97f60SAnna Dabrowska    public function setValue($value, $israw = false)
109*d6d97f60SAnna Dabrowska    {
110c0230d2cSAndreas Gohr        $this->rawonly = $israw;
111c0230d2cSAndreas Gohr
11290421550SAndreas Gohr        // treat all givens the same
11390421550SAndreas Gohr        if (!is_array($value)) {
11417560ecbSAndreas Gohr            $value = array($value);
11517560ecbSAndreas Gohr        }
1167937b821SAndreas Gohr
11790421550SAndreas Gohr        // reset/init
11890421550SAndreas Gohr        $this->value = array();
11990421550SAndreas Gohr        $this->rawvalue = array();
1205241ca30SAndreas Gohr        $this->display = array();
1217717c082SMichael Große        $this->compare = array();
12290421550SAndreas Gohr
12319065d4eSAndreas Gohr        // remove all blanks
12490421550SAndreas Gohr        foreach ($value as $val) {
125c0230d2cSAndreas Gohr            if ($israw) {
126c0230d2cSAndreas Gohr                $raw = $val;
127c0230d2cSAndreas Gohr            } else {
12890421550SAndreas Gohr                $raw = $this->column->getType()->rawValue($val);
129c0230d2cSAndreas Gohr            }
130bf83580bSMichael Grosse            if ('' === (string) trim($raw)) continue;
13190421550SAndreas Gohr            $this->value[] = $val;
13290421550SAndreas Gohr            $this->rawvalue[] = $raw;
133c0230d2cSAndreas Gohr            if ($israw) {
134c0230d2cSAndreas Gohr                $this->display[] = $val;
1357717c082SMichael Große                $this->compare[] = $val;
136c0230d2cSAndreas Gohr            } else {
1375241ca30SAndreas Gohr                $this->display[] = $this->column->getType()->displayValue($val);
1387717c082SMichael Große                $this->compare[] = $this->column->getType()->compareValue($val);
13990421550SAndreas Gohr            }
140c0230d2cSAndreas Gohr        }
1417937b821SAndreas Gohr
14290421550SAndreas Gohr        // make single value again
1437937b821SAndreas Gohr        if (!$this->column->isMulti()) {
14490421550SAndreas Gohr            $this->value = (string) array_shift($this->value);
14590421550SAndreas Gohr            $this->rawvalue = (string) array_shift($this->rawvalue);
1465241ca30SAndreas Gohr            $this->display = (string) array_shift($this->display);
1477717c082SMichael Große            $this->compare = (string) array_shift($this->compare);
1487937b821SAndreas Gohr        }
14919065d4eSAndreas Gohr    }
1507937b821SAndreas Gohr
15190421550SAndreas Gohr    /**
15290421550SAndreas Gohr     * Is this empty?
15390421550SAndreas Gohr     *
15490421550SAndreas Gohr     * @return bool
15590421550SAndreas Gohr     */
156*d6d97f60SAnna Dabrowska    public function isEmpty()
157*d6d97f60SAnna Dabrowska    {
15890421550SAndreas Gohr        return ($this->rawvalue === '' || $this->rawvalue === array());
15917560ecbSAndreas Gohr    }
16017560ecbSAndreas Gohr
16117560ecbSAndreas Gohr    /**
16238fa36fbSAndreas Gohr     * Render the value using the given renderer and mode
16338fa36fbSAndreas Gohr     *
164053212b1SAndreas Gohr     * automativally picks the right mechanism depending on multi or single value
165053212b1SAndreas Gohr     *
16619065d4eSAndreas Gohr     * values are only rendered when there is a value
16719065d4eSAndreas Gohr     *
16838fa36fbSAndreas Gohr     * @param \Doku_Renderer $R
16938fa36fbSAndreas Gohr     * @param string $mode
170053212b1SAndreas Gohr     * @return bool
17138fa36fbSAndreas Gohr     */
172*d6d97f60SAnna Dabrowska    public function render(\Doku_Renderer $R, $mode)
173*d6d97f60SAnna Dabrowska    {
17438fa36fbSAndreas Gohr        if ($this->column->isMulti()) {
17519065d4eSAndreas Gohr            if (count($this->value)) {
176053212b1SAndreas Gohr                return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
17719065d4eSAndreas Gohr            }
17838fa36fbSAndreas Gohr        } else {
17919065d4eSAndreas Gohr            if ($this->value !== '') {
180053212b1SAndreas Gohr                return $this->column->getType()->renderValue($this->value, $R, $mode);
181053212b1SAndreas Gohr            }
182053212b1SAndreas Gohr        }
18319065d4eSAndreas Gohr        return true;
18419065d4eSAndreas Gohr    }
185053212b1SAndreas Gohr
1869e7e1786SMichael Grosse    /**
1879e7e1786SMichael Grosse     * Render this value as a tag-link in a struct cloud
1889e7e1786SMichael Grosse     *
1899e7e1786SMichael Grosse     * @param \Doku_Renderer $R
1909e7e1786SMichael Grosse     * @param string $mode
1919e7e1786SMichael Grosse     * @param string $page
1929e7e1786SMichael Grosse     * @param string $filterQuery
1939e7e1786SMichael Grosse     * @param int $weight
1949e7e1786SMichael Grosse     */
195*d6d97f60SAnna Dabrowska    public function renderAsTagCloudLink(\Doku_Renderer $R, $mode, $page, $filterQuery, $weight)
196*d6d97f60SAnna Dabrowska    {
1975d17398fSMichael Grosse        $value = is_array($this->value) ? $this->value[0] : $this->value;
1985d17398fSMichael Grosse        $this->column->getType()->renderTagCloudLink($value, $R, $mode, $page, $filterQuery, $weight);
199262c0fc6SMichael Grosse    }
200262c0fc6SMichael Grosse
201053212b1SAndreas Gohr    /**
202053212b1SAndreas Gohr     * Return the value editor for this value field
203053212b1SAndreas Gohr     *
204053212b1SAndreas Gohr     * @param string $name The field name to use in the editor
205053212b1SAndreas Gohr     * @return string The HTML for the editor
206053212b1SAndreas Gohr     */
207*d6d97f60SAnna Dabrowska    public function getValueEditor($name, $id)
208*d6d97f60SAnna Dabrowska    {
209053212b1SAndreas Gohr        if ($this->column->isMulti()) {
210ee983135SMichael Große            return $this->column->getType()->multiValueEditor($name, $this->rawvalue, $id);
211053212b1SAndreas Gohr        } else {
212ee983135SMichael Große            return $this->column->getType()->valueEditor($name, $this->rawvalue, $id);
21338fa36fbSAndreas Gohr        }
21438fa36fbSAndreas Gohr    }
21519065d4eSAndreas Gohr
21619065d4eSAndreas Gohr    /**
21719065d4eSAndreas Gohr     * Filter callback to strip empty values
21819065d4eSAndreas Gohr     *
21919065d4eSAndreas Gohr     * @param string $input
22019065d4eSAndreas Gohr     * @return bool
22119065d4eSAndreas Gohr     */
222*d6d97f60SAnna Dabrowska    public function filter($input)
223*d6d97f60SAnna Dabrowska    {
22419065d4eSAndreas Gohr        return '' !== ((string) $input);
22519065d4eSAndreas Gohr    }
22638fa36fbSAndreas Gohr}
227