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 $val = trim($val); 102 if($israw) { 103 $raw = $val; 104 } else { 105 $raw = $this->column->getType()->rawValue($val); 106 } 107 if('' === (string) $raw) continue; 108 $this->value[] = $val; 109 $this->rawvalue[] = $raw; 110 if($israw) { 111 $this->display[] = $val; 112 } else { 113 $this->display[] = $this->column->getType()->displayValue($val); 114 } 115 } 116 117 // make single value again 118 if(!$this->column->isMulti()) { 119 $this->value = (string) array_shift($this->value); 120 $this->rawvalue = (string) array_shift($this->rawvalue); 121 $this->display = (string) array_shift($this->display); 122 } 123 } 124 125 /** 126 * Is this empty? 127 * 128 * @return bool 129 */ 130 public function isEmpty() { 131 return ($this->rawvalue === '' || $this->rawvalue === array()); 132 } 133 134 /** 135 * Render the value using the given renderer and mode 136 * 137 * automativally picks the right mechanism depending on multi or single value 138 * 139 * values are only rendered when there is a value 140 * 141 * @param \Doku_Renderer $R 142 * @param string $mode 143 * @return bool 144 */ 145 public function render(\Doku_Renderer $R, $mode) { 146 if($this->column->isMulti()) { 147 if(count($this->value)) { 148 return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 149 } 150 } else { 151 if($this->value !== '') { 152 return $this->column->getType()->renderValue($this->value, $R, $mode); 153 } 154 } 155 return true; 156 } 157 158 /** 159 * Return the value editor for this value field 160 * 161 * @param string $name The field name to use in the editor 162 * @return string The HTML for the editor 163 */ 164 public function getValueEditor($name) { 165 if($this->column->isMulti()) { 166 return $this->column->getType()->multiValueEditor($name, $this->rawvalue); 167 } else { 168 return $this->column->getType()->valueEditor($name, $this->rawvalue); 169 } 170 } 171 172 /** 173 * Filter callback to strip empty values 174 * 175 * @param string $input 176 * @return bool 177 */ 178 public function filter($input) { 179 return '' !== ((string) $input); 180 } 181} 182