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 23*5241ca30SAndreas Gohr /** @var array|int|string */ 24*5241ca30SAndreas Gohr protected $display = null; 25*5241ca30SAndreas Gohr 2638fa36fbSAndreas Gohr /** 2738fa36fbSAndreas Gohr * Value constructor. 2838fa36fbSAndreas Gohr * 2938fa36fbSAndreas Gohr * @param Column $column 3038fa36fbSAndreas Gohr * @param array|int|string $value 3138fa36fbSAndreas Gohr */ 3238fa36fbSAndreas Gohr public function __construct(Column $column, $value) { 3338fa36fbSAndreas Gohr $this->column = $column; 3417560ecbSAndreas Gohr $this->setValue($value); 3538fa36fbSAndreas Gohr } 3638fa36fbSAndreas Gohr 3738fa36fbSAndreas Gohr /** 3838fa36fbSAndreas Gohr * @return Column 3938fa36fbSAndreas Gohr */ 4038fa36fbSAndreas Gohr public function getColumn() { 4138fa36fbSAndreas Gohr return $this->column; 4238fa36fbSAndreas Gohr } 4338fa36fbSAndreas Gohr 4438fa36fbSAndreas Gohr /** 4538fa36fbSAndreas Gohr * @return array|int|string 4638fa36fbSAndreas Gohr */ 4738fa36fbSAndreas Gohr public function getValue() { 4838fa36fbSAndreas Gohr return $this->value; 4938fa36fbSAndreas Gohr } 5038fa36fbSAndreas Gohr 5138fa36fbSAndreas Gohr /** 5290421550SAndreas Gohr * Access the raw value 5390421550SAndreas Gohr * 5490421550SAndreas Gohr * @return array|string (array on multi) 5590421550SAndreas Gohr */ 5690421550SAndreas Gohr public function getRawValue() { 5790421550SAndreas Gohr return $this->rawvalue; 5890421550SAndreas Gohr } 5990421550SAndreas Gohr 6090421550SAndreas Gohr /** 61*5241ca30SAndreas Gohr * Access the display value 62*5241ca30SAndreas Gohr * 63*5241ca30SAndreas Gohr * @return array|string (array on multi) 64*5241ca30SAndreas Gohr */ 65*5241ca30SAndreas Gohr public function getDisplayValue() { 66*5241ca30SAndreas Gohr return $this->display; 67*5241ca30SAndreas Gohr } 68*5241ca30SAndreas Gohr 69*5241ca30SAndreas Gohr /** 7017560ecbSAndreas Gohr * Allows overwriting the current value 7117560ecbSAndreas Gohr * 7219065d4eSAndreas Gohr * Cleans the value(s) of empties 7319065d4eSAndreas Gohr * 7417560ecbSAndreas Gohr * @param array|int|string $value 7517560ecbSAndreas Gohr */ 7617560ecbSAndreas Gohr public function setValue($value) { 7790421550SAndreas Gohr // treat all givens the same 7890421550SAndreas Gohr if(!is_array($value)) { 7917560ecbSAndreas Gohr $value = array($value); 8017560ecbSAndreas Gohr } 817937b821SAndreas Gohr 8290421550SAndreas Gohr // reset/init 8390421550SAndreas Gohr $this->value = array(); 8490421550SAndreas Gohr $this->rawvalue = array(); 85*5241ca30SAndreas Gohr $this->display = array(); 8690421550SAndreas Gohr 8719065d4eSAndreas Gohr // remove all blanks 8890421550SAndreas Gohr foreach($value as $val) { 8990421550SAndreas Gohr $val = trim($val); 9090421550SAndreas Gohr $raw = $this->column->getType()->rawValue($val); 9190421550SAndreas Gohr if('' === (string) $raw) continue; 9290421550SAndreas Gohr $this->value[] = $val; 9390421550SAndreas Gohr $this->rawvalue[] = $raw; 94*5241ca30SAndreas Gohr $this->display[] = $this->column->getType()->displayValue($val); 9590421550SAndreas Gohr } 967937b821SAndreas Gohr 9790421550SAndreas Gohr // make single value again 987937b821SAndreas Gohr if(!$this->column->isMulti()) { 9990421550SAndreas Gohr $this->value = (string) array_shift($this->value); 10090421550SAndreas Gohr $this->rawvalue = (string) array_shift($this->rawvalue); 101*5241ca30SAndreas Gohr $this->display = (string) array_shift($this->display); 1027937b821SAndreas Gohr } 10319065d4eSAndreas Gohr } 1047937b821SAndreas Gohr 10590421550SAndreas Gohr /** 10690421550SAndreas Gohr * Is this empty? 10790421550SAndreas Gohr * 10890421550SAndreas Gohr * @return bool 10990421550SAndreas Gohr */ 11090421550SAndreas Gohr public function isEmpty() { 11190421550SAndreas Gohr return ($this->rawvalue === '' || $this->rawvalue === array()); 11217560ecbSAndreas Gohr } 11317560ecbSAndreas Gohr 11417560ecbSAndreas Gohr /** 11538fa36fbSAndreas Gohr * Render the value using the given renderer and mode 11638fa36fbSAndreas Gohr * 117053212b1SAndreas Gohr * automativally picks the right mechanism depending on multi or single value 118053212b1SAndreas Gohr * 11919065d4eSAndreas Gohr * values are only rendered when there is a value 12019065d4eSAndreas Gohr * 12138fa36fbSAndreas Gohr * @param \Doku_Renderer $R 12238fa36fbSAndreas Gohr * @param string $mode 123053212b1SAndreas Gohr * @return bool 12438fa36fbSAndreas Gohr */ 12538fa36fbSAndreas Gohr public function render(\Doku_Renderer $R, $mode) { 12638fa36fbSAndreas Gohr if($this->column->isMulti()) { 12719065d4eSAndreas Gohr if(count($this->value)) { 128053212b1SAndreas Gohr return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 12919065d4eSAndreas Gohr } 13038fa36fbSAndreas Gohr } else { 13119065d4eSAndreas Gohr if($this->value !== '') { 132053212b1SAndreas Gohr return $this->column->getType()->renderValue($this->value, $R, $mode); 133053212b1SAndreas Gohr } 134053212b1SAndreas Gohr } 13519065d4eSAndreas Gohr return true; 13619065d4eSAndreas Gohr } 137053212b1SAndreas Gohr 138053212b1SAndreas Gohr /** 139053212b1SAndreas Gohr * Return the value editor for this value field 140053212b1SAndreas Gohr * 141053212b1SAndreas Gohr * @param string $name The field name to use in the editor 142053212b1SAndreas Gohr * @return string The HTML for the editor 143053212b1SAndreas Gohr */ 144053212b1SAndreas Gohr public function getValueEditor($name) { 145053212b1SAndreas Gohr if($this->column->isMulti()) { 146053212b1SAndreas Gohr return $this->column->getType()->multiValueEditor($name, $this->value); 147053212b1SAndreas Gohr } else { 148053212b1SAndreas Gohr return $this->column->getType()->valueEditor($name, $this->value); 14938fa36fbSAndreas Gohr } 15038fa36fbSAndreas Gohr } 15119065d4eSAndreas Gohr 15219065d4eSAndreas Gohr /** 15319065d4eSAndreas Gohr * Filter callback to strip empty values 15419065d4eSAndreas Gohr * 15519065d4eSAndreas Gohr * @param string $input 15619065d4eSAndreas Gohr * @return bool 15719065d4eSAndreas Gohr */ 15819065d4eSAndreas Gohr public function filter($input) { 15919065d4eSAndreas Gohr return '' !== ((string) $input); 16019065d4eSAndreas Gohr } 16138fa36fbSAndreas Gohr} 162