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 26c0230d2cSAndreas Gohr /** @var bool is this a raw value only? */ 27c0230d2cSAndreas Gohr protected $rawonly = false; 28c0230d2cSAndreas Gohr 2938fa36fbSAndreas Gohr /** 3038fa36fbSAndreas Gohr * Value constructor. 3138fa36fbSAndreas Gohr * 3238fa36fbSAndreas Gohr * @param Column $column 3338fa36fbSAndreas Gohr * @param array|int|string $value 3438fa36fbSAndreas Gohr */ 3538fa36fbSAndreas Gohr public function __construct(Column $column, $value) { 3638fa36fbSAndreas Gohr $this->column = $column; 3717560ecbSAndreas Gohr $this->setValue($value); 3838fa36fbSAndreas Gohr } 3938fa36fbSAndreas Gohr 4038fa36fbSAndreas Gohr /** 4138fa36fbSAndreas Gohr * @return Column 4238fa36fbSAndreas Gohr */ 4338fa36fbSAndreas Gohr public function getColumn() { 4438fa36fbSAndreas Gohr return $this->column; 4538fa36fbSAndreas Gohr } 4638fa36fbSAndreas Gohr 4738fa36fbSAndreas Gohr /** 4838fa36fbSAndreas Gohr * @return array|int|string 4938fa36fbSAndreas Gohr */ 5038fa36fbSAndreas Gohr public function getValue() { 51c0230d2cSAndreas Gohr if($this->rawonly) { 52c0230d2cSAndreas Gohr throw new StructException('Accessing value of rawonly value forbidden'); 53c0230d2cSAndreas Gohr } 5438fa36fbSAndreas Gohr return $this->value; 5538fa36fbSAndreas Gohr } 5638fa36fbSAndreas Gohr 5738fa36fbSAndreas Gohr /** 5890421550SAndreas Gohr * Access the raw value 5990421550SAndreas Gohr * 6090421550SAndreas Gohr * @return array|string (array on multi) 6190421550SAndreas Gohr */ 6290421550SAndreas Gohr public function getRawValue() { 6390421550SAndreas Gohr return $this->rawvalue; 6490421550SAndreas Gohr } 6590421550SAndreas Gohr 6690421550SAndreas Gohr /** 675241ca30SAndreas Gohr * Access the display value 685241ca30SAndreas Gohr * 695241ca30SAndreas Gohr * @return array|string (array on multi) 705241ca30SAndreas Gohr */ 715241ca30SAndreas Gohr public function getDisplayValue() { 72c0230d2cSAndreas Gohr if($this->rawonly) { 73c0230d2cSAndreas Gohr throw new StructException('Accessing displayvalue of rawonly value forbidden'); 74c0230d2cSAndreas Gohr } 755241ca30SAndreas Gohr return $this->display; 765241ca30SAndreas Gohr } 775241ca30SAndreas Gohr 785241ca30SAndreas Gohr /** 7917560ecbSAndreas Gohr * Allows overwriting the current value 8017560ecbSAndreas Gohr * 8119065d4eSAndreas Gohr * Cleans the value(s) of empties 8219065d4eSAndreas Gohr * 8317560ecbSAndreas Gohr * @param array|int|string $value 84c0230d2cSAndreas Gohr * @param bool $israw is the passed value a raw value? turns Value into rawonly 8517560ecbSAndreas Gohr */ 86c0230d2cSAndreas Gohr public function setValue($value, $israw=false) { 87c0230d2cSAndreas Gohr $this->rawonly = $israw; 88c0230d2cSAndreas Gohr 8990421550SAndreas Gohr // treat all givens the same 9090421550SAndreas Gohr if(!is_array($value)) { 9117560ecbSAndreas Gohr $value = array($value); 9217560ecbSAndreas Gohr } 937937b821SAndreas Gohr 9490421550SAndreas Gohr // reset/init 9590421550SAndreas Gohr $this->value = array(); 9690421550SAndreas Gohr $this->rawvalue = array(); 975241ca30SAndreas Gohr $this->display = array(); 9890421550SAndreas Gohr 9919065d4eSAndreas Gohr // remove all blanks 10090421550SAndreas Gohr foreach($value as $val) { 101c0230d2cSAndreas Gohr if($israw) { 102c0230d2cSAndreas Gohr $raw = $val; 103c0230d2cSAndreas Gohr } else { 10490421550SAndreas Gohr $raw = $this->column->getType()->rawValue($val); 105c0230d2cSAndreas Gohr } 106bf83580bSMichael Grosse if('' === (string) trim($raw)) continue; 10790421550SAndreas Gohr $this->value[] = $val; 10890421550SAndreas Gohr $this->rawvalue[] = $raw; 109c0230d2cSAndreas Gohr if($israw) { 110c0230d2cSAndreas Gohr $this->display[] = $val; 111c0230d2cSAndreas Gohr } else { 1125241ca30SAndreas Gohr $this->display[] = $this->column->getType()->displayValue($val); 11390421550SAndreas Gohr } 114c0230d2cSAndreas Gohr } 1157937b821SAndreas Gohr 11690421550SAndreas Gohr // make single value again 1177937b821SAndreas Gohr if(!$this->column->isMulti()) { 11890421550SAndreas Gohr $this->value = (string) array_shift($this->value); 11990421550SAndreas Gohr $this->rawvalue = (string) array_shift($this->rawvalue); 1205241ca30SAndreas Gohr $this->display = (string) array_shift($this->display); 1217937b821SAndreas Gohr } 12219065d4eSAndreas Gohr } 1237937b821SAndreas Gohr 12490421550SAndreas Gohr /** 12590421550SAndreas Gohr * Is this empty? 12690421550SAndreas Gohr * 12790421550SAndreas Gohr * @return bool 12890421550SAndreas Gohr */ 12990421550SAndreas Gohr public function isEmpty() { 13090421550SAndreas Gohr return ($this->rawvalue === '' || $this->rawvalue === array()); 13117560ecbSAndreas Gohr } 13217560ecbSAndreas Gohr 13317560ecbSAndreas Gohr /** 13438fa36fbSAndreas Gohr * Render the value using the given renderer and mode 13538fa36fbSAndreas Gohr * 136053212b1SAndreas Gohr * automativally picks the right mechanism depending on multi or single value 137053212b1SAndreas Gohr * 13819065d4eSAndreas Gohr * values are only rendered when there is a value 13919065d4eSAndreas Gohr * 14038fa36fbSAndreas Gohr * @param \Doku_Renderer $R 14138fa36fbSAndreas Gohr * @param string $mode 142053212b1SAndreas Gohr * @return bool 14338fa36fbSAndreas Gohr */ 14438fa36fbSAndreas Gohr public function render(\Doku_Renderer $R, $mode) { 14538fa36fbSAndreas Gohr if($this->column->isMulti()) { 14619065d4eSAndreas Gohr if(count($this->value)) { 147053212b1SAndreas Gohr return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 14819065d4eSAndreas Gohr } 14938fa36fbSAndreas Gohr } else { 15019065d4eSAndreas Gohr if($this->value !== '') { 151053212b1SAndreas Gohr return $this->column->getType()->renderValue($this->value, $R, $mode); 152053212b1SAndreas Gohr } 153053212b1SAndreas Gohr } 15419065d4eSAndreas Gohr return true; 15519065d4eSAndreas Gohr } 156053212b1SAndreas Gohr 157*262c0fc6SMichael Grosse public function renderAsTagCloudLink(\Doku_Renderer $R, $mode, $page, $filter, $weight) { 158*262c0fc6SMichael Grosse $this->column->getType()->renderTagCloudLink($this->value, $R, $mode, $page, $filter, $weight); 159*262c0fc6SMichael Grosse } 160*262c0fc6SMichael Grosse 161053212b1SAndreas Gohr /** 162053212b1SAndreas Gohr * Return the value editor for this value field 163053212b1SAndreas Gohr * 164053212b1SAndreas Gohr * @param string $name The field name to use in the editor 165053212b1SAndreas Gohr * @return string The HTML for the editor 166053212b1SAndreas Gohr */ 167053212b1SAndreas Gohr public function getValueEditor($name) { 168053212b1SAndreas Gohr if($this->column->isMulti()) { 169c0230d2cSAndreas Gohr return $this->column->getType()->multiValueEditor($name, $this->rawvalue); 170053212b1SAndreas Gohr } else { 171c0230d2cSAndreas Gohr return $this->column->getType()->valueEditor($name, $this->rawvalue); 17238fa36fbSAndreas Gohr } 17338fa36fbSAndreas Gohr } 17419065d4eSAndreas Gohr 17519065d4eSAndreas Gohr /** 17619065d4eSAndreas Gohr * Filter callback to strip empty values 17719065d4eSAndreas Gohr * 17819065d4eSAndreas Gohr * @param string $input 17919065d4eSAndreas Gohr * @return bool 18019065d4eSAndreas Gohr */ 18119065d4eSAndreas Gohr public function filter($input) { 18219065d4eSAndreas Gohr return '' !== ((string) $input); 18319065d4eSAndreas Gohr } 18438fa36fbSAndreas Gohr} 185