xref: /plugin/struct/meta/Value.php (revision 19065d4ecbb4b9fcd93fee521dd3cd0307a2de3d)
138fa36fbSAndreas Gohr<?php
238fa36fbSAndreas Gohr
338fa36fbSAndreas Gohrnamespace 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 *
1038fa36fbSAndreas Gohr * @package 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
2038fa36fbSAndreas Gohr    /**
2138fa36fbSAndreas Gohr     * Value constructor.
2238fa36fbSAndreas Gohr     *
2338fa36fbSAndreas Gohr     * @param Column $column
2438fa36fbSAndreas Gohr     * @param array|int|string $value
2538fa36fbSAndreas Gohr     */
2638fa36fbSAndreas Gohr    public function __construct(Column $column, $value) {
2738fa36fbSAndreas Gohr        $this->column = $column;
2817560ecbSAndreas Gohr        $this->setValue($value);
2938fa36fbSAndreas Gohr    }
3038fa36fbSAndreas Gohr
3138fa36fbSAndreas Gohr    /**
3238fa36fbSAndreas Gohr     * @return Column
3338fa36fbSAndreas Gohr     */
3438fa36fbSAndreas Gohr    public function getColumn() {
3538fa36fbSAndreas Gohr        return $this->column;
3638fa36fbSAndreas Gohr    }
3738fa36fbSAndreas Gohr
3838fa36fbSAndreas Gohr    /**
3938fa36fbSAndreas Gohr     * @return array|int|string
4038fa36fbSAndreas Gohr     */
4138fa36fbSAndreas Gohr    public function getValue() {
4238fa36fbSAndreas Gohr        return $this->value;
4338fa36fbSAndreas Gohr    }
4438fa36fbSAndreas Gohr
4538fa36fbSAndreas Gohr    /**
4617560ecbSAndreas Gohr     * Allows overwriting the current value
4717560ecbSAndreas Gohr     *
48*19065d4eSAndreas Gohr     * Cleans the value(s) of empties
49*19065d4eSAndreas Gohr     *
5017560ecbSAndreas Gohr     * @param array|int|string $value
5117560ecbSAndreas Gohr     */
5217560ecbSAndreas Gohr    public function setValue($value) {
53*19065d4eSAndreas Gohr        if($this->column->isMulti()) {
54*19065d4eSAndreas Gohr            if(!is_array($value)) {
5517560ecbSAndreas Gohr                $value = array($value);
5617560ecbSAndreas Gohr            }
57*19065d4eSAndreas Gohr            // remove all blanks
58*19065d4eSAndreas Gohr            $value = array_map('trim', $value);
59*19065d4eSAndreas Gohr            $value = array_filter($value, array($this, 'filter'));
60*19065d4eSAndreas Gohr            $value = array_values($value); // reset keys
61*19065d4eSAndreas Gohr        } else {
62*19065d4eSAndreas Gohr            $value = trim($value);
63*19065d4eSAndreas Gohr        }
6417560ecbSAndreas Gohr        $this->value = $value;
6517560ecbSAndreas Gohr    }
6617560ecbSAndreas Gohr
6717560ecbSAndreas Gohr    /**
6838fa36fbSAndreas Gohr     * Render the value using the given renderer and mode
6938fa36fbSAndreas Gohr     *
70053212b1SAndreas Gohr     * automativally picks the right mechanism depending on multi or single value
71053212b1SAndreas Gohr     *
72*19065d4eSAndreas Gohr     * values are only rendered when there is a value
73*19065d4eSAndreas Gohr     *
7438fa36fbSAndreas Gohr     * @param \Doku_Renderer $R
7538fa36fbSAndreas Gohr     * @param string $mode
76053212b1SAndreas Gohr     * @return bool
7738fa36fbSAndreas Gohr     */
7838fa36fbSAndreas Gohr    public function render(\Doku_Renderer $R, $mode) {
7938fa36fbSAndreas Gohr        if($this->column->isMulti()) {
80*19065d4eSAndreas Gohr            if(count($this->value)) {
81053212b1SAndreas Gohr                return $this->column->getType()->renderMultiValue($this->value, $R, $mode);
82*19065d4eSAndreas Gohr            }
8338fa36fbSAndreas Gohr        } else {
84*19065d4eSAndreas Gohr            if($this->value !== '') {
85053212b1SAndreas Gohr                return $this->column->getType()->renderValue($this->value, $R, $mode);
86053212b1SAndreas Gohr            }
87053212b1SAndreas Gohr        }
88*19065d4eSAndreas Gohr        return true;
89*19065d4eSAndreas Gohr    }
90053212b1SAndreas Gohr
91053212b1SAndreas Gohr    /**
92053212b1SAndreas Gohr     * Return the value editor for this value field
93053212b1SAndreas Gohr     *
94053212b1SAndreas Gohr     * @param string $name The field name to use in the editor
95053212b1SAndreas Gohr     * @return string The HTML for the editor
96053212b1SAndreas Gohr     */
97053212b1SAndreas Gohr    public function getValueEditor($name) {
98053212b1SAndreas Gohr        if($this->column->isMulti()) {
99053212b1SAndreas Gohr            return $this->column->getType()->multiValueEditor($name, $this->value);
100053212b1SAndreas Gohr        } else {
101053212b1SAndreas Gohr            return $this->column->getType()->valueEditor($name, $this->value);
10238fa36fbSAndreas Gohr        }
10338fa36fbSAndreas Gohr    }
104*19065d4eSAndreas Gohr
105*19065d4eSAndreas Gohr    /**
106*19065d4eSAndreas Gohr     * Filter callback to strip empty values
107*19065d4eSAndreas Gohr     *
108*19065d4eSAndreas Gohr     * @param string $input
109*19065d4eSAndreas Gohr     * @return bool
110*19065d4eSAndreas Gohr     */
111*19065d4eSAndreas Gohr    public function filter($input) {
112*19065d4eSAndreas Gohr        return  '' !== ((string) $input);
113*19065d4eSAndreas Gohr    }
11438fa36fbSAndreas Gohr}
115