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