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 * 4819065d4eSAndreas Gohr * Cleans the value(s) of empties 4919065d4eSAndreas Gohr * 5017560ecbSAndreas Gohr * @param array|int|string $value 5117560ecbSAndreas Gohr */ 5217560ecbSAndreas Gohr public function setValue($value) { 53*7937b821SAndreas Gohr if($this->column->isMulti() && !is_array($value)) { 5417560ecbSAndreas Gohr $value = array($value); 5517560ecbSAndreas Gohr } 56*7937b821SAndreas Gohr 57*7937b821SAndreas Gohr if(is_array($value)) { 5819065d4eSAndreas Gohr // remove all blanks 5919065d4eSAndreas Gohr $value = array_map('trim', $value); 6019065d4eSAndreas Gohr $value = array_filter($value, array($this, 'filter')); 6119065d4eSAndreas Gohr $value = array_values($value); // reset keys 62*7937b821SAndreas Gohr 63*7937b821SAndreas Gohr if(!$this->column->isMulti()) { 64*7937b821SAndreas Gohr $value = (string) array_shift($value); 65*7937b821SAndreas Gohr } 6619065d4eSAndreas Gohr } else { 6719065d4eSAndreas Gohr $value = trim($value); 6819065d4eSAndreas Gohr } 69*7937b821SAndreas Gohr 7017560ecbSAndreas Gohr $this->value = $value; 7117560ecbSAndreas Gohr } 7217560ecbSAndreas Gohr 7317560ecbSAndreas Gohr /** 7438fa36fbSAndreas Gohr * Render the value using the given renderer and mode 7538fa36fbSAndreas Gohr * 76053212b1SAndreas Gohr * automativally picks the right mechanism depending on multi or single value 77053212b1SAndreas Gohr * 7819065d4eSAndreas Gohr * values are only rendered when there is a value 7919065d4eSAndreas Gohr * 8038fa36fbSAndreas Gohr * @param \Doku_Renderer $R 8138fa36fbSAndreas Gohr * @param string $mode 82053212b1SAndreas Gohr * @return bool 8338fa36fbSAndreas Gohr */ 8438fa36fbSAndreas Gohr public function render(\Doku_Renderer $R, $mode) { 8538fa36fbSAndreas Gohr if($this->column->isMulti()) { 8619065d4eSAndreas Gohr if(count($this->value)) { 87053212b1SAndreas Gohr return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 8819065d4eSAndreas Gohr } 8938fa36fbSAndreas Gohr } else { 9019065d4eSAndreas Gohr if($this->value !== '') { 91053212b1SAndreas Gohr return $this->column->getType()->renderValue($this->value, $R, $mode); 92053212b1SAndreas Gohr } 93053212b1SAndreas Gohr } 9419065d4eSAndreas Gohr return true; 9519065d4eSAndreas Gohr } 96053212b1SAndreas Gohr 97053212b1SAndreas Gohr /** 98053212b1SAndreas Gohr * Return the value editor for this value field 99053212b1SAndreas Gohr * 100053212b1SAndreas Gohr * @param string $name The field name to use in the editor 101053212b1SAndreas Gohr * @return string The HTML for the editor 102053212b1SAndreas Gohr */ 103053212b1SAndreas Gohr public function getValueEditor($name) { 104053212b1SAndreas Gohr if($this->column->isMulti()) { 105053212b1SAndreas Gohr return $this->column->getType()->multiValueEditor($name, $this->value); 106053212b1SAndreas Gohr } else { 107053212b1SAndreas Gohr return $this->column->getType()->valueEditor($name, $this->value); 10838fa36fbSAndreas Gohr } 10938fa36fbSAndreas Gohr } 11019065d4eSAndreas Gohr 11119065d4eSAndreas Gohr /** 11219065d4eSAndreas Gohr * Filter callback to strip empty values 11319065d4eSAndreas Gohr * 11419065d4eSAndreas Gohr * @param string $input 11519065d4eSAndreas Gohr * @return bool 11619065d4eSAndreas Gohr */ 11719065d4eSAndreas Gohr public function filter($input) { 11819065d4eSAndreas Gohr return '' !== ((string) $input); 11919065d4eSAndreas Gohr } 12038fa36fbSAndreas Gohr} 121