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 */ 12d6d97f60SAnna Dabrowskaclass Value 13d6d97f60SAnna Dabrowska{ 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 267717c082SMichael Große /** @var array|int|string */ 277717c082SMichael Große protected $compare = null; 287717c082SMichael 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 */ 38d6d97f60SAnna Dabrowska public function __construct(Column $column, $value) 39d6d97f60SAnna Dabrowska { 4038fa36fbSAndreas Gohr $this->column = $column; 4117560ecbSAndreas Gohr $this->setValue($value); 4238fa36fbSAndreas Gohr } 4338fa36fbSAndreas Gohr 4438fa36fbSAndreas Gohr /** 4538fa36fbSAndreas Gohr * @return Column 4638fa36fbSAndreas Gohr */ 47d6d97f60SAnna Dabrowska public function getColumn() 48d6d97f60SAnna Dabrowska { 4938fa36fbSAndreas Gohr return $this->column; 5038fa36fbSAndreas Gohr } 5138fa36fbSAndreas Gohr 5238fa36fbSAndreas Gohr /** 5338fa36fbSAndreas Gohr * @return array|int|string 5438fa36fbSAndreas Gohr */ 55d6d97f60SAnna Dabrowska public function getValue() 56d6d97f60SAnna Dabrowska { 57c0230d2cSAndreas Gohr if ($this->rawonly) { 58c0230d2cSAndreas Gohr throw new StructException('Accessing value of rawonly value forbidden'); 59c0230d2cSAndreas Gohr } 6038fa36fbSAndreas Gohr return $this->value; 6138fa36fbSAndreas Gohr } 6238fa36fbSAndreas Gohr 6338fa36fbSAndreas Gohr /** 6490421550SAndreas Gohr * Access the raw value 6590421550SAndreas Gohr * 6690421550SAndreas Gohr * @return array|string (array on multi) 6790421550SAndreas Gohr */ 68d6d97f60SAnna Dabrowska public function getRawValue() 69d6d97f60SAnna Dabrowska { 7090421550SAndreas Gohr return $this->rawvalue; 7190421550SAndreas Gohr } 7290421550SAndreas Gohr 7390421550SAndreas Gohr /** 745241ca30SAndreas Gohr * Access the display value 755241ca30SAndreas Gohr * 765241ca30SAndreas Gohr * @return array|string (array on multi) 775241ca30SAndreas Gohr */ 78d6d97f60SAnna Dabrowska public function getDisplayValue() 79d6d97f60SAnna Dabrowska { 80c0230d2cSAndreas Gohr if ($this->rawonly) { 81c0230d2cSAndreas Gohr throw new StructException('Accessing displayvalue of rawonly value forbidden'); 82c0230d2cSAndreas Gohr } 835241ca30SAndreas Gohr return $this->display; 845241ca30SAndreas Gohr } 855241ca30SAndreas Gohr 865241ca30SAndreas Gohr /** 877717c082SMichael Große * Access the compare value 887717c082SMichael Große * 897717c082SMichael Große * @return array|string (array on multi) 907717c082SMichael Große */ 91d6d97f60SAnna Dabrowska public function getCompareValue() 92d6d97f60SAnna Dabrowska { 937717c082SMichael Große if ($this->rawonly) { 947717c082SMichael Große throw new StructException('Accessing comparevalue of rawonly value forbidden'); 957717c082SMichael Große } 967717c082SMichael Große return $this->compare; 977717c082SMichael Große } 987717c082SMichael Große 997717c082SMichael Große /** 10017560ecbSAndreas Gohr * Allows overwriting the current value 10117560ecbSAndreas Gohr * 10219065d4eSAndreas Gohr * Cleans the value(s) of empties 10319065d4eSAndreas Gohr * 10417560ecbSAndreas Gohr * @param array|int|string $value 105c0230d2cSAndreas Gohr * @param bool $israw is the passed value a raw value? turns Value into rawonly 10617560ecbSAndreas Gohr */ 107d6d97f60SAnna Dabrowska public function setValue($value, $israw = false) 108d6d97f60SAnna Dabrowska { 109c0230d2cSAndreas Gohr $this->rawonly = $israw; 110c0230d2cSAndreas Gohr 11190421550SAndreas Gohr // treat all givens the same 11290421550SAndreas Gohr if (!is_array($value)) { 11317560ecbSAndreas Gohr $value = array($value); 11417560ecbSAndreas Gohr } 1157937b821SAndreas Gohr 11690421550SAndreas Gohr // reset/init 11790421550SAndreas Gohr $this->value = array(); 11890421550SAndreas Gohr $this->rawvalue = array(); 1195241ca30SAndreas Gohr $this->display = array(); 1207717c082SMichael Große $this->compare = array(); 12190421550SAndreas Gohr 12219065d4eSAndreas Gohr // remove all blanks 12390421550SAndreas Gohr foreach ($value as $val) { 124c0230d2cSAndreas Gohr if ($israw) { 125c0230d2cSAndreas Gohr $raw = $val; 126c0230d2cSAndreas Gohr } else { 12790421550SAndreas Gohr $raw = $this->column->getType()->rawValue($val); 128c0230d2cSAndreas Gohr } 1296a819106SAndreas Gohr if ('' === trim((string) $raw)) continue; 13090421550SAndreas Gohr $this->value[] = $val; 13190421550SAndreas Gohr $this->rawvalue[] = $raw; 132c0230d2cSAndreas Gohr if ($israw) { 133c0230d2cSAndreas Gohr $this->display[] = $val; 1347717c082SMichael Große $this->compare[] = $val; 135c0230d2cSAndreas Gohr } else { 1365241ca30SAndreas Gohr $this->display[] = $this->column->getType()->displayValue($val); 1377717c082SMichael Große $this->compare[] = $this->column->getType()->compareValue($val); 13890421550SAndreas Gohr } 139c0230d2cSAndreas Gohr } 1407937b821SAndreas Gohr 14190421550SAndreas Gohr // make single value again 1427937b821SAndreas Gohr if (!$this->column->isMulti()) { 14390421550SAndreas Gohr $this->value = (string)array_shift($this->value); 14490421550SAndreas Gohr $this->rawvalue = (string)array_shift($this->rawvalue); 1455241ca30SAndreas Gohr $this->display = (string)array_shift($this->display); 1467717c082SMichael Große $this->compare = (string)array_shift($this->compare); 1477937b821SAndreas Gohr } 14819065d4eSAndreas Gohr } 1497937b821SAndreas Gohr 15090421550SAndreas Gohr /** 15190421550SAndreas Gohr * Is this empty? 15290421550SAndreas Gohr * 15390421550SAndreas Gohr * @return bool 15490421550SAndreas Gohr */ 155d6d97f60SAnna Dabrowska public function isEmpty() 156d6d97f60SAnna Dabrowska { 15790421550SAndreas Gohr return ($this->rawvalue === '' || $this->rawvalue === array()); 15817560ecbSAndreas Gohr } 15917560ecbSAndreas Gohr 16017560ecbSAndreas Gohr /** 16138fa36fbSAndreas Gohr * Render the value using the given renderer and mode 16238fa36fbSAndreas Gohr * 163053212b1SAndreas Gohr * automativally picks the right mechanism depending on multi or single value 164053212b1SAndreas Gohr * 16519065d4eSAndreas Gohr * values are only rendered when there is a value 16619065d4eSAndreas Gohr * 16738fa36fbSAndreas Gohr * @param \Doku_Renderer $R 16838fa36fbSAndreas Gohr * @param string $mode 169053212b1SAndreas Gohr * @return bool 17038fa36fbSAndreas Gohr */ 171d6d97f60SAnna Dabrowska public function render(\Doku_Renderer $R, $mode) 172d6d97f60SAnna Dabrowska { 17338fa36fbSAndreas Gohr if ($this->column->isMulti()) { 17419065d4eSAndreas Gohr if (count($this->value)) { 175053212b1SAndreas Gohr return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 17619065d4eSAndreas Gohr } 17738fa36fbSAndreas Gohr } else { 17819065d4eSAndreas Gohr if ($this->value !== '') { 179053212b1SAndreas Gohr return $this->column->getType()->renderValue($this->value, $R, $mode); 180053212b1SAndreas Gohr } 181053212b1SAndreas Gohr } 18219065d4eSAndreas Gohr return true; 18319065d4eSAndreas Gohr } 184053212b1SAndreas Gohr 1859e7e1786SMichael Grosse /** 1869e7e1786SMichael Grosse * Render this value as a tag-link in a struct cloud 1879e7e1786SMichael Grosse * 1889e7e1786SMichael Grosse * @param \Doku_Renderer $R 1899e7e1786SMichael Grosse * @param string $mode 1909e7e1786SMichael Grosse * @param string $page 1919e7e1786SMichael Grosse * @param string $filterQuery 1929e7e1786SMichael Grosse * @param int $weight 1939e7e1786SMichael Grosse */ 194d6d97f60SAnna Dabrowska public function renderAsTagCloudLink(\Doku_Renderer $R, $mode, $page, $filterQuery, $weight) 195d6d97f60SAnna Dabrowska { 1965d17398fSMichael Grosse $value = is_array($this->value) ? $this->value[0] : $this->value; 1975d17398fSMichael Grosse $this->column->getType()->renderTagCloudLink($value, $R, $mode, $page, $filterQuery, $weight); 198262c0fc6SMichael Grosse } 199262c0fc6SMichael Grosse 200053212b1SAndreas Gohr /** 201053212b1SAndreas Gohr * Return the value editor for this value field 202053212b1SAndreas Gohr * 203053212b1SAndreas Gohr * @param string $name The field name to use in the editor 204053212b1SAndreas Gohr * @return string The HTML for the editor 205053212b1SAndreas Gohr */ 206d6d97f60SAnna Dabrowska public function getValueEditor($name, $id) 207d6d97f60SAnna Dabrowska { 208053212b1SAndreas Gohr if ($this->column->isMulti()) { 209ee983135SMichael Große return $this->column->getType()->multiValueEditor($name, $this->rawvalue, $id); 210053212b1SAndreas Gohr } else { 211ee983135SMichael Große return $this->column->getType()->valueEditor($name, $this->rawvalue, $id); 21238fa36fbSAndreas Gohr } 21338fa36fbSAndreas Gohr } 21419065d4eSAndreas Gohr 21519065d4eSAndreas Gohr /** 21619065d4eSAndreas Gohr * Filter callback to strip empty values 21719065d4eSAndreas Gohr * 21819065d4eSAndreas Gohr * @param string $input 21919065d4eSAndreas Gohr * @return bool 22019065d4eSAndreas Gohr */ 221d6d97f60SAnna Dabrowska public function filter($input) 222d6d97f60SAnna Dabrowska { 22319065d4eSAndreas Gohr return '' !== ((string)$input); 22419065d4eSAndreas Gohr } 225*5bc00e11SAndreas Gohr 226*5bc00e11SAndreas Gohr /** 227*5bc00e11SAndreas Gohr * Get a string representation of this value 228*5bc00e11SAndreas Gohr * 229*5bc00e11SAndreas Gohr * @return string 230*5bc00e11SAndreas Gohr */ 231*5bc00e11SAndreas Gohr public function __toString() 232*5bc00e11SAndreas Gohr { 233*5bc00e11SAndreas Gohr return '[' . $this->getColumn()->getFullQualifiedLabel() . '] ' . 234*5bc00e11SAndreas Gohr join(',', (array)$this->getRawValue()); 235*5bc00e11SAndreas Gohr } 23638fa36fbSAndreas Gohr} 237