xref: /plugin/struct/meta/Value.php (revision c0230d2cff12393e50d4f6e8a795895769b395a3)
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 */
1238fa36fbSAndreas Gohrclass Value {
1338fa36fbSAndreas Gohr
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 */
2190421550SAndreas Gohr    protected $rawvalue = null;
2290421550SAndreas Gohr
235241ca30SAndreas Gohr    /** @var array|int|string */
245241ca30SAndreas Gohr    protected $display = null;
255241ca30SAndreas Gohr
26*c0230d2cSAndreas Gohr    /** @var bool is this a raw value only? */
27*c0230d2cSAndreas Gohr    protected $rawonly = false;
28*c0230d2cSAndreas Gohr
2938fa36fbSAndreas Gohr    /**
3038fa36fbSAndreas Gohr     * Value constructor.
3138fa36fbSAndreas Gohr     *
3238fa36fbSAndreas Gohr     * @param Column $column
3338fa36fbSAndreas Gohr     * @param array|int|string $value
3438fa36fbSAndreas Gohr     */
3538fa36fbSAndreas Gohr    public function __construct(Column $column, $value) {
3638fa36fbSAndreas Gohr        $this->column = $column;
3717560ecbSAndreas Gohr        $this->setValue($value);
3838fa36fbSAndreas Gohr    }
3938fa36fbSAndreas Gohr
4038fa36fbSAndreas Gohr    /**
4138fa36fbSAndreas Gohr     * @return Column
4238fa36fbSAndreas Gohr     */
4338fa36fbSAndreas Gohr    public function getColumn() {
4438fa36fbSAndreas Gohr        return $this->column;
4538fa36fbSAndreas Gohr    }
4638fa36fbSAndreas Gohr
4738fa36fbSAndreas Gohr    /**
4838fa36fbSAndreas Gohr     * @return array|int|string
4938fa36fbSAndreas Gohr     */
5038fa36fbSAndreas Gohr    public function getValue() {
51*c0230d2cSAndreas Gohr        if($this->rawonly) {
52*c0230d2cSAndreas Gohr            throw new StructException('Accessing value of rawonly value forbidden');
53*c0230d2cSAndreas Gohr        }
5438fa36fbSAndreas Gohr        return $this->value;
5538fa36fbSAndreas Gohr    }
5638fa36fbSAndreas Gohr
5738fa36fbSAndreas Gohr    /**
5890421550SAndreas Gohr     * Access the raw value
5990421550SAndreas Gohr     *
6090421550SAndreas Gohr     * @return array|string (array on multi)
6190421550SAndreas Gohr     */
6290421550SAndreas Gohr    public function getRawValue() {
6390421550SAndreas Gohr        return $this->rawvalue;
6490421550SAndreas Gohr    }
6590421550SAndreas Gohr
6690421550SAndreas Gohr    /**
675241ca30SAndreas Gohr     * Access the display value
685241ca30SAndreas Gohr     *
695241ca30SAndreas Gohr     * @return array|string (array on multi)
705241ca30SAndreas Gohr     */
715241ca30SAndreas Gohr    public function getDisplayValue() {
72*c0230d2cSAndreas Gohr        if($this->rawonly) {
73*c0230d2cSAndreas Gohr            throw new StructException('Accessing displayvalue of rawonly value forbidden');
74*c0230d2cSAndreas Gohr        }
755241ca30SAndreas Gohr        return $this->display;
765241ca30SAndreas Gohr    }
775241ca30SAndreas Gohr
785241ca30SAndreas Gohr    /**
7917560ecbSAndreas Gohr     * Allows overwriting the current value
8017560ecbSAndreas Gohr     *
8119065d4eSAndreas Gohr     * Cleans the value(s) of empties
8219065d4eSAndreas Gohr     *
8317560ecbSAndreas Gohr     * @param array|int|string $value
84*c0230d2cSAndreas Gohr     * @param bool $israw is the passed value a raw value? turns Value into rawonly
8517560ecbSAndreas Gohr     */
86*c0230d2cSAndreas Gohr    public function setValue($value, $israw=false) {
87*c0230d2cSAndreas Gohr        $this->rawonly = $israw;
88*c0230d2cSAndreas Gohr
8990421550SAndreas Gohr        // treat all givens the same
9090421550SAndreas Gohr        if(!is_array($value)) {
9117560ecbSAndreas Gohr            $value = array($value);
9217560ecbSAndreas Gohr        }
937937b821SAndreas Gohr
9490421550SAndreas Gohr        // reset/init
9590421550SAndreas Gohr        $this->value = array();
9690421550SAndreas Gohr        $this->rawvalue = array();
975241ca30SAndreas Gohr        $this->display = array();
9890421550SAndreas Gohr
9919065d4eSAndreas Gohr        // remove all blanks
10090421550SAndreas Gohr        foreach($value as $val) {
10190421550SAndreas Gohr            $val = trim($val);
102*c0230d2cSAndreas Gohr            if($israw) {
103*c0230d2cSAndreas Gohr                $raw = $val;
104*c0230d2cSAndreas Gohr            } else {
10590421550SAndreas Gohr                $raw = $this->column->getType()->rawValue($val);
106*c0230d2cSAndreas Gohr            }
10790421550SAndreas Gohr            if('' === (string) $raw) continue;
10890421550SAndreas Gohr            $this->value[] = $val;
10990421550SAndreas Gohr            $this->rawvalue[] = $raw;
110*c0230d2cSAndreas Gohr            if($israw) {
111*c0230d2cSAndreas Gohr                $this->display[] = $val;
112*c0230d2cSAndreas Gohr            } else {
1135241ca30SAndreas Gohr                $this->display[] = $this->column->getType()->displayValue($val);
11490421550SAndreas Gohr            }
115*c0230d2cSAndreas Gohr        }
1167937b821SAndreas Gohr
11790421550SAndreas Gohr        // make single value again
1187937b821SAndreas Gohr        if(!$this->column->isMulti()) {
11990421550SAndreas Gohr            $this->value = (string) array_shift($this->value);
12090421550SAndreas Gohr            $this->rawvalue = (string) array_shift($this->rawvalue);
1215241ca30SAndreas Gohr            $this->display = (string) array_shift($this->display);
1227937b821SAndreas Gohr        }
12319065d4eSAndreas Gohr    }
1247937b821SAndreas Gohr
12590421550SAndreas Gohr    /**
12690421550SAndreas Gohr     * Is this empty?
12790421550SAndreas Gohr     *
12890421550SAndreas Gohr     * @return bool
12990421550SAndreas Gohr     */
13090421550SAndreas Gohr    public function isEmpty() {
13190421550SAndreas Gohr        return ($this->rawvalue === '' || $this->rawvalue === array());
13217560ecbSAndreas Gohr    }
13317560ecbSAndreas Gohr
13417560ecbSAndreas Gohr    /**
13538fa36fbSAndreas Gohr     * Render the value using the given renderer and mode
13638fa36fbSAndreas Gohr     *
137053212b1SAndreas Gohr     * automativally picks the right mechanism depending on multi or single value
138053212b1SAndreas Gohr     *
13919065d4eSAndreas Gohr     * values are only rendered when there is a value
14019065d4eSAndreas Gohr     *
14138fa36fbSAndreas Gohr     * @param \Doku_Renderer $R
14238fa36fbSAndreas Gohr     * @param string $mode
143053212b1SAndreas Gohr     * @return bool
14438fa36fbSAndreas Gohr     */
14538fa36fbSAndreas Gohr    public function render(\Doku_Renderer $R, $mode) {
14638fa36fbSAndreas Gohr        if($this->column->isMulti()) {
14719065d4eSAndreas Gohr            if(count($this->value)) {
148053212b1SAndreas Gohr                return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
14919065d4eSAndreas Gohr            }
15038fa36fbSAndreas Gohr        } else {
15119065d4eSAndreas Gohr            if($this->value !== '') {
152053212b1SAndreas Gohr                return $this->column->getType()->renderValue($this->value, $R, $mode);
153053212b1SAndreas Gohr            }
154053212b1SAndreas Gohr        }
15519065d4eSAndreas Gohr        return true;
15619065d4eSAndreas Gohr    }
157053212b1SAndreas Gohr
158053212b1SAndreas Gohr    /**
159053212b1SAndreas Gohr     * Return the value editor for this value field
160053212b1SAndreas Gohr     *
161053212b1SAndreas Gohr     * @param string $name The field name to use in the editor
162053212b1SAndreas Gohr     * @return string The HTML for the editor
163053212b1SAndreas Gohr     */
164053212b1SAndreas Gohr    public function getValueEditor($name) {
165053212b1SAndreas Gohr        if($this->column->isMulti()) {
166*c0230d2cSAndreas Gohr            return $this->column->getType()->multiValueEditor($name, $this->rawvalue);
167053212b1SAndreas Gohr        } else {
168*c0230d2cSAndreas Gohr            return $this->column->getType()->valueEditor($name, $this->rawvalue);
16938fa36fbSAndreas Gohr        }
17038fa36fbSAndreas Gohr    }
17119065d4eSAndreas Gohr
17219065d4eSAndreas Gohr    /**
17319065d4eSAndreas Gohr     * Filter callback to strip empty values
17419065d4eSAndreas Gohr     *
17519065d4eSAndreas Gohr     * @param string $input
17619065d4eSAndreas Gohr     * @return bool
17719065d4eSAndreas Gohr     */
17819065d4eSAndreas Gohr    public function filter($input) {
17919065d4eSAndreas Gohr        return '' !== ((string) $input);
18019065d4eSAndreas Gohr    }
18138fa36fbSAndreas Gohr}
182