1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5/** 6 * Class Value 7 * 8 * Holds the value for a single "cell". That value may be an array for multi value columns 9 * 10 * @package dokuwiki\plugin\struct\meta 11 */ 12class Value { 13 14 /** @var Column */ 15 protected $column; 16 17 /** @var array|int|string */ 18 protected $value; 19 20 /** @var array|int|string */ 21 protected $rawvalue = null; 22 23 /** @var array|int|string */ 24 protected $display = null; 25 26 /** @var bool is this a raw value only? */ 27 protected $rawonly = false; 28 29 /** 30 * Value constructor. 31 * 32 * @param Column $column 33 * @param array|int|string $value 34 */ 35 public function __construct(Column $column, $value) { 36 $this->column = $column; 37 $this->setValue($value); 38 } 39 40 /** 41 * @return Column 42 */ 43 public function getColumn() { 44 return $this->column; 45 } 46 47 /** 48 * @return array|int|string 49 */ 50 public function getValue() { 51 if($this->rawonly) { 52 throw new StructException('Accessing value of rawonly value forbidden'); 53 } 54 return $this->value; 55 } 56 57 /** 58 * Access the raw value 59 * 60 * @return array|string (array on multi) 61 */ 62 public function getRawValue() { 63 return $this->rawvalue; 64 } 65 66 /** 67 * Access the display value 68 * 69 * @return array|string (array on multi) 70 */ 71 public function getDisplayValue() { 72 if($this->rawonly) { 73 throw new StructException('Accessing displayvalue of rawonly value forbidden'); 74 } 75 return $this->display; 76 } 77 78 /** 79 * Allows overwriting the current value 80 * 81 * Cleans the value(s) of empties 82 * 83 * @param array|int|string $value 84 * @param bool $israw is the passed value a raw value? turns Value into rawonly 85 */ 86 public function setValue($value, $israw=false) { 87 $this->rawonly = $israw; 88 89 // treat all givens the same 90 if(!is_array($value)) { 91 $value = array($value); 92 } 93 94 // reset/init 95 $this->value = array(); 96 $this->rawvalue = array(); 97 $this->display = array(); 98 99 // remove all blanks 100 foreach($value as $val) { 101 if($israw) { 102 $raw = $val; 103 } else { 104 $raw = $this->column->getType()->rawValue($val); 105 } 106 if('' === (string) trim($raw)) continue; 107 $this->value[] = $val; 108 $this->rawvalue[] = $raw; 109 if($israw) { 110 $this->display[] = $val; 111 } else { 112 $this->display[] = $this->column->getType()->displayValue($val); 113 } 114 } 115 116 // make single value again 117 if(!$this->column->isMulti()) { 118 $this->value = (string) array_shift($this->value); 119 $this->rawvalue = (string) array_shift($this->rawvalue); 120 $this->display = (string) array_shift($this->display); 121 } 122 } 123 124 /** 125 * Is this empty? 126 * 127 * @return bool 128 */ 129 public function isEmpty() { 130 return ($this->rawvalue === '' || $this->rawvalue === array()); 131 } 132 133 /** 134 * Render the value using the given renderer and mode 135 * 136 * automativally picks the right mechanism depending on multi or single value 137 * 138 * values are only rendered when there is a value 139 * 140 * @param \Doku_Renderer $R 141 * @param string $mode 142 * @return bool 143 */ 144 public function render(\Doku_Renderer $R, $mode) { 145 if($this->column->isMulti()) { 146 if(count($this->value)) { 147 return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 148 } 149 } else { 150 if($this->value !== '') { 151 return $this->column->getType()->renderValue($this->value, $R, $mode); 152 } 153 } 154 return true; 155 } 156 157 /** 158 * Return the value editor for this value field 159 * 160 * @param string $name The field name to use in the editor 161 * @return string The HTML for the editor 162 */ 163 public function getValueEditor($name) { 164 if($this->column->isMulti()) { 165 return $this->column->getType()->multiValueEditor($name, $this->rawvalue); 166 } else { 167 return $this->column->getType()->valueEditor($name, $this->rawvalue); 168 } 169 } 170 171 /** 172 * Filter callback to strip empty values 173 * 174 * @param string $input 175 * @return bool 176 */ 177 public function filter($input) { 178 return '' !== ((string) $input); 179 } 180} 181