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