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 /** 27 * Value constructor. 28 * 29 * @param Column $column 30 * @param array|int|string $value 31 */ 32 public function __construct(Column $column, $value) { 33 $this->column = $column; 34 $this->setValue($value); 35 } 36 37 /** 38 * @return Column 39 */ 40 public function getColumn() { 41 return $this->column; 42 } 43 44 /** 45 * @return array|int|string 46 */ 47 public function getValue() { 48 return $this->value; 49 } 50 51 /** 52 * Access the raw value 53 * 54 * @return array|string (array on multi) 55 */ 56 public function getRawValue() { 57 return $this->rawvalue; 58 } 59 60 /** 61 * Access the display value 62 * 63 * @return array|string (array on multi) 64 */ 65 public function getDisplayValue() { 66 return $this->display; 67 } 68 69 /** 70 * Allows overwriting the current value 71 * 72 * Cleans the value(s) of empties 73 * 74 * @param array|int|string $value 75 */ 76 public function setValue($value) { 77 // treat all givens the same 78 if(!is_array($value)) { 79 $value = array($value); 80 } 81 82 // reset/init 83 $this->value = array(); 84 $this->rawvalue = array(); 85 $this->display = array(); 86 87 // remove all blanks 88 foreach($value as $val) { 89 $val = trim($val); 90 $raw = $this->column->getType()->rawValue($val); 91 if('' === (string) $raw) continue; 92 $this->value[] = $val; 93 $this->rawvalue[] = $raw; 94 $this->display[] = $this->column->getType()->displayValue($val); 95 } 96 97 // make single value again 98 if(!$this->column->isMulti()) { 99 $this->value = (string) array_shift($this->value); 100 $this->rawvalue = (string) array_shift($this->rawvalue); 101 $this->display = (string) array_shift($this->display); 102 } 103 } 104 105 /** 106 * Is this empty? 107 * 108 * @return bool 109 */ 110 public function isEmpty() { 111 return ($this->rawvalue === '' || $this->rawvalue === array()); 112 } 113 114 /** 115 * Render the value using the given renderer and mode 116 * 117 * automativally picks the right mechanism depending on multi or single value 118 * 119 * values are only rendered when there is a value 120 * 121 * @param \Doku_Renderer $R 122 * @param string $mode 123 * @return bool 124 */ 125 public function render(\Doku_Renderer $R, $mode) { 126 if($this->column->isMulti()) { 127 if(count($this->value)) { 128 return $this->column->getType()->renderMultiValue($this->value, $R, $mode); 129 } 130 } else { 131 if($this->value !== '') { 132 return $this->column->getType()->renderValue($this->value, $R, $mode); 133 } 134 } 135 return true; 136 } 137 138 /** 139 * Return the value editor for this value field 140 * 141 * @param string $name The field name to use in the editor 142 * @return string The HTML for the editor 143 */ 144 public function getValueEditor($name) { 145 if($this->column->isMulti()) { 146 return $this->column->getType()->multiValueEditor($name, $this->value); 147 } else { 148 return $this->column->getType()->valueEditor($name, $this->value); 149 } 150 } 151 152 /** 153 * Filter callback to strip empty values 154 * 155 * @param string $input 156 * @return bool 157 */ 158 public function filter($input) { 159 return '' !== ((string) $input); 160 } 161} 162