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*7717c082SMichael Große /** @var array|int|string */ 27*7717c082SMichael Große protected $compare = null; 28*7717c082SMichael Große 29c0230d2cSAndreas Gohr /** @var bool is this a raw value only? */ 30c0230d2cSAndreas Gohr protected $rawonly = false; 31c0230d2cSAndreas Gohr 3238fa36fbSAndreas Gohr /** 3338fa36fbSAndreas Gohr * Value constructor. 3438fa36fbSAndreas Gohr * 3538fa36fbSAndreas Gohr * @param Column $column 3638fa36fbSAndreas Gohr * @param array|int|string $value 3738fa36fbSAndreas Gohr */ 3838fa36fbSAndreas Gohr public function __construct(Column $column, $value) { 3938fa36fbSAndreas Gohr $this->column = $column; 4017560ecbSAndreas Gohr $this->setValue($value); 4138fa36fbSAndreas Gohr } 4238fa36fbSAndreas Gohr 4338fa36fbSAndreas Gohr /** 4438fa36fbSAndreas Gohr * @return Column 4538fa36fbSAndreas Gohr */ 4638fa36fbSAndreas Gohr public function getColumn() { 4738fa36fbSAndreas Gohr return $this->column; 4838fa36fbSAndreas Gohr } 4938fa36fbSAndreas Gohr 5038fa36fbSAndreas Gohr /** 5138fa36fbSAndreas Gohr * @return array|int|string 5238fa36fbSAndreas Gohr */ 5338fa36fbSAndreas Gohr public function getValue() { 54c0230d2cSAndreas Gohr if($this->rawonly) { 55c0230d2cSAndreas Gohr throw new StructException('Accessing value of rawonly value forbidden'); 56c0230d2cSAndreas Gohr } 5738fa36fbSAndreas Gohr return $this->value; 5838fa36fbSAndreas Gohr } 5938fa36fbSAndreas Gohr 6038fa36fbSAndreas Gohr /** 6190421550SAndreas Gohr * Access the raw value 6290421550SAndreas Gohr * 6390421550SAndreas Gohr * @return array|string (array on multi) 6490421550SAndreas Gohr */ 6590421550SAndreas Gohr public function getRawValue() { 6690421550SAndreas Gohr return $this->rawvalue; 6790421550SAndreas Gohr } 6890421550SAndreas Gohr 6990421550SAndreas Gohr /** 705241ca30SAndreas Gohr * Access the display value 715241ca30SAndreas Gohr * 725241ca30SAndreas Gohr * @return array|string (array on multi) 735241ca30SAndreas Gohr */ 745241ca30SAndreas Gohr public function getDisplayValue() { 75c0230d2cSAndreas Gohr if($this->rawonly) { 76c0230d2cSAndreas Gohr throw new StructException('Accessing displayvalue of rawonly value forbidden'); 77c0230d2cSAndreas Gohr } 785241ca30SAndreas Gohr return $this->display; 795241ca30SAndreas Gohr } 805241ca30SAndreas Gohr 815241ca30SAndreas Gohr /** 82*7717c082SMichael Große * Access the compare value 83*7717c082SMichael Große * 84*7717c082SMichael Große * @return array|string (array on multi) 85*7717c082SMichael Große */ 86*7717c082SMichael Große public function getCompareValue() { 87*7717c082SMichael Große if($this->rawonly) { 88*7717c082SMichael Große throw new StructException('Accessing comparevalue of rawonly value forbidden'); 89*7717c082SMichael Große } 90*7717c082SMichael Große return $this->compare; 91*7717c082SMichael Große } 92*7717c082SMichael Große 93*7717c082SMichael Große /** 9417560ecbSAndreas Gohr * Allows overwriting the current value 9517560ecbSAndreas Gohr * 9619065d4eSAndreas Gohr * Cleans the value(s) of empties 9719065d4eSAndreas Gohr * 9817560ecbSAndreas Gohr * @param array|int|string $value 99c0230d2cSAndreas Gohr * @param bool $israw is the passed value a raw value? turns Value into rawonly 10017560ecbSAndreas Gohr */ 101c0230d2cSAndreas Gohr public function setValue($value, $israw=false) { 102c0230d2cSAndreas Gohr $this->rawonly = $israw; 103c0230d2cSAndreas Gohr 10490421550SAndreas Gohr // treat all givens the same 10590421550SAndreas Gohr if(!is_array($value)) { 10617560ecbSAndreas Gohr $value = array($value); 10717560ecbSAndreas Gohr } 1087937b821SAndreas Gohr 10990421550SAndreas Gohr // reset/init 11090421550SAndreas Gohr $this->value = array(); 11190421550SAndreas Gohr $this->rawvalue = array(); 1125241ca30SAndreas Gohr $this->display = array(); 113*7717c082SMichael Große $this->compare = array(); 11490421550SAndreas Gohr 11519065d4eSAndreas Gohr // remove all blanks 11690421550SAndreas Gohr foreach($value as $val) { 117c0230d2cSAndreas Gohr if($israw) { 118c0230d2cSAndreas Gohr $raw = $val; 119c0230d2cSAndreas Gohr } else { 12090421550SAndreas Gohr $raw = $this->column->getType()->rawValue($val); 121c0230d2cSAndreas Gohr } 122bf83580bSMichael Grosse if('' === (string) trim($raw)) continue; 12390421550SAndreas Gohr $this->value[] = $val; 12490421550SAndreas Gohr $this->rawvalue[] = $raw; 125c0230d2cSAndreas Gohr if($israw) { 126c0230d2cSAndreas Gohr $this->display[] = $val; 127*7717c082SMichael Große $this->compare[] = $val; 128c0230d2cSAndreas Gohr } else { 1295241ca30SAndreas Gohr $this->display[] = $this->column->getType()->displayValue($val); 130*7717c082SMichael Große $this->compare[] = $this->column->getType()->compareValue($val); 13190421550SAndreas Gohr } 132c0230d2cSAndreas Gohr } 1337937b821SAndreas Gohr 13490421550SAndreas Gohr // make single value again 1357937b821SAndreas Gohr if(!$this->column->isMulti()) { 13690421550SAndreas Gohr $this->value = (string) array_shift($this->value); 13790421550SAndreas Gohr $this->rawvalue = (string) array_shift($this->rawvalue); 1385241ca30SAndreas Gohr $this->display = (string) array_shift($this->display); 139*7717c082SMichael Große $this->compare = (string) array_shift($this->compare); 1407937b821SAndreas Gohr } 14119065d4eSAndreas Gohr } 1427937b821SAndreas Gohr 14390421550SAndreas Gohr /** 14490421550SAndreas Gohr * Is this empty? 14590421550SAndreas Gohr * 14690421550SAndreas Gohr * @return bool 14790421550SAndreas Gohr */ 14890421550SAndreas Gohr public function isEmpty() { 14990421550SAndreas Gohr return ($this->rawvalue === '' || $this->rawvalue === array()); 15017560ecbSAndreas Gohr } 15117560ecbSAndreas Gohr 15217560ecbSAndreas Gohr /** 15338fa36fbSAndreas Gohr * Render the value using the given renderer and mode 15438fa36fbSAndreas Gohr * 155053212b1SAndreas Gohr * automativally picks the right mechanism depending on multi or single value 156053212b1SAndreas Gohr * 15719065d4eSAndreas Gohr * values are only rendered when there is a value 15819065d4eSAndreas Gohr * 15938fa36fbSAndreas Gohr * @param \Doku_Renderer $R 16038fa36fbSAndreas Gohr * @param string $mode 161053212b1SAndreas Gohr * @return bool 16238fa36fbSAndreas Gohr */ 16338fa36fbSAndreas Gohr public function render(\Doku_Renderer $R, $mode) { 16438fa36fbSAndreas Gohr if($this->column->isMulti()) { 16519065d4eSAndreas Gohr if(count($this->value)) { 166053212b1SAndreas Gohr return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 16719065d4eSAndreas Gohr } 16838fa36fbSAndreas Gohr } else { 16919065d4eSAndreas Gohr if($this->value !== '') { 170053212b1SAndreas Gohr return $this->column->getType()->renderValue($this->value, $R, $mode); 171053212b1SAndreas Gohr } 172053212b1SAndreas Gohr } 17319065d4eSAndreas Gohr return true; 17419065d4eSAndreas Gohr } 175053212b1SAndreas Gohr 1769e7e1786SMichael Grosse /** 1779e7e1786SMichael Grosse * Render this value as a tag-link in a struct cloud 1789e7e1786SMichael Grosse * 1799e7e1786SMichael Grosse * @param \Doku_Renderer $R 1809e7e1786SMichael Grosse * @param string $mode 1819e7e1786SMichael Grosse * @param string $page 1829e7e1786SMichael Grosse * @param string $filterQuery 1839e7e1786SMichael Grosse * @param int $weight 1849e7e1786SMichael Grosse */ 1859e7e1786SMichael Grosse public function renderAsTagCloudLink(\Doku_Renderer $R, $mode, $page, $filterQuery, $weight) { 1865d17398fSMichael Grosse $value = is_array($this->value) ? $this->value[0] : $this->value; 1875d17398fSMichael Grosse $this->column->getType()->renderTagCloudLink($value, $R, $mode, $page, $filterQuery, $weight); 188262c0fc6SMichael Grosse } 189262c0fc6SMichael Grosse 190053212b1SAndreas Gohr /** 191053212b1SAndreas Gohr * Return the value editor for this value field 192053212b1SAndreas Gohr * 193053212b1SAndreas Gohr * @param string $name The field name to use in the editor 194053212b1SAndreas Gohr * @return string The HTML for the editor 195053212b1SAndreas Gohr */ 196053212b1SAndreas Gohr public function getValueEditor($name) { 197053212b1SAndreas Gohr if($this->column->isMulti()) { 198c0230d2cSAndreas Gohr return $this->column->getType()->multiValueEditor($name, $this->rawvalue); 199053212b1SAndreas Gohr } else { 200c0230d2cSAndreas Gohr return $this->column->getType()->valueEditor($name, $this->rawvalue); 20138fa36fbSAndreas Gohr } 20238fa36fbSAndreas Gohr } 20319065d4eSAndreas Gohr 20419065d4eSAndreas Gohr /** 20519065d4eSAndreas Gohr * Filter callback to strip empty values 20619065d4eSAndreas Gohr * 20719065d4eSAndreas Gohr * @param string $input 20819065d4eSAndreas Gohr * @return bool 20919065d4eSAndreas Gohr */ 21019065d4eSAndreas Gohr public function filter($input) { 21119065d4eSAndreas Gohr return '' !== ((string) $input); 21219065d4eSAndreas Gohr } 21338fa36fbSAndreas Gohr} 214