1<?php 2 3use dokuwiki\plugin\struct\meta\Column; 4use dokuwiki\plugin\struct\meta\Schema; 5use dokuwiki\plugin\struct\meta\StructException; 6use dokuwiki\plugin\struct\meta\Value; 7use dokuwiki\plugin\struct\meta\ValueValidator; 8use dokuwiki\plugin\struct\types\Lookup; 9use dokuwiki\plugin\struct\types\Page; 10 11/** 12 * Allows adding a single struct field as a bureaucracy field 13 * 14 * This class is used when a field of the type struct_field is encountered in the 15 * bureaucracy syntax. 16 */ 17class helper_plugin_struct_field extends helper_plugin_bureaucracy_field 18{ 19 20 /** @var Column */ 21 public $column; 22 23 /** 24 * Initialize the appropriate column 25 * 26 * @param array $args 27 */ 28 public function initialize($args) 29 { 30 $this->init($args); 31 32 // find the column 33 try { 34 $this->column = $this->findColumn($this->opt['label']); 35 } catch (StructException $e) { 36 msg(hsc($e->getMessage()), -1); 37 } 38 39 $this->standardArgs($args); 40 } 41 42 /** 43 * Sets the value and validates it 44 * 45 * @param mixed $value 46 * @return bool value was set successfully validated 47 */ 48 protected function setVal($value) 49 { 50 if (!$this->column) { 51 $value = ''; 52 //don't validate placeholders here 53 } elseif ($this->replace($value) == $value) { 54 $validator = new ValueValidator(); 55 $this->error = !$validator->validateValue($this->column, $value); 56 if ($this->error) { 57 foreach ($validator->getErrors() as $error) { 58 msg(hsc($error), -1); 59 } 60 } 61 } 62 63 if ($value === array() || $value === '') { 64 if (!isset($this->opt['optional'])) { 65 $this->error = true; 66 if ($this->column) { 67 $label = $this->column->getTranslatedLabel(); 68 } else { 69 $label = $this->opt['label']; 70 } 71 msg(sprintf($this->getLang('e_required'), hsc($label)), -1); 72 } 73 } 74 75 $this->opt['value'] = $value; 76 return !$this->error; 77 } 78 79 /** 80 * Creates the HTML for the field 81 * 82 * @param array $params 83 * @param Doku_Form $form 84 * @param int $formid 85 */ 86 public function renderfield($params, Doku_Form $form, $formid) 87 { 88 if (!$this->column) return; 89 90 // this is what parent does 91 $this->_handlePreload(); 92 if (!$form->_infieldset) { 93 $form->startFieldset(''); 94 } 95 if ($this->error) { 96 $params['class'] = 'bureaucracy_error'; 97 } 98 99 // output the field 100 $value = $this->createValue(); 101 $field = $this->makeField($value, $params['name']); 102 $form->addElement($field); 103 } 104 105 /** 106 * Returns a Value object for the current column. 107 * Special handling for Page and Lookup literal form values. 108 * 109 * @return Value 110 */ 111 protected function createValue() 112 { 113 $preparedValue = $this->opt['value']; 114 115 // page fields might need to be JSON encoded depending on usetitles config 116 if ( 117 $this->column->getType() instanceof Page 118 && $this->column->getType()->getConfig()['usetitles'] 119 ) { 120 $preparedValue = json_encode([$this->opt['value'], null]); 121 } 122 123 $value = new Value($this->column, $preparedValue); 124 125 // no way to pass $israw parameter to constructor, so re-set the Lookup value 126 if ($this->column->getType() instanceof Lookup) { 127 $value->setValue($preparedValue, true); 128 } 129 130 return $value; 131 } 132 133 /** 134 * Create the input field 135 * 136 * @param Value $field 137 * @param String $name field's name 138 * @return string 139 */ 140 protected function makeField(Value $field, $name) 141 { 142 $trans = hsc($field->getColumn()->getTranslatedLabel()); 143 $hint = hsc($field->getColumn()->getTranslatedHint()); 144 $class = $hint ? 'hashint' : ''; 145 $lclass = $this->error ? 'bureaucracy_error' : ''; 146 $colname = $field->getColumn()->getFullQualifiedLabel(); 147 $required = $this->opt['optional'] ? '' : ' <sup>*</sup>'; 148 149 $id = uniqid('struct__', true); 150 $input = $field->getValueEditor($name, $id); 151 152 $html = '<div class="field">'; 153 $html .= "<label class=\"$lclass\" data-column=\"$colname\" for=\"$id\">"; 154 $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>"; 155 $html .= '</label>'; 156 $html .= "<span class=\"input\">$input</span>"; 157 $html .= '</div>'; 158 159 return $html; 160 } 161 162 /** 163 * Tries to find the correct column and schema 164 * 165 * @throws StructException 166 * @param string $colname 167 * @return \dokuwiki\plugin\struct\meta\Column 168 */ 169 protected function findColumn($colname) 170 { 171 list($table, $label) = explode('.', $colname, 2); 172 if (!$table || !$label) { 173 throw new StructException('Field \'%s\' not given in schema.field form', $colname); 174 } 175 $schema = new Schema($table); 176 return $schema->findColumn($label); 177 } 178 179 /** 180 * This ensures all language strings are still working 181 * 182 * @return string always 'bureaucracy' 183 */ 184 public function getPluginName() 185 { 186 return 'bureaucracy'; 187 } 188} 189