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 msg(sprintf($this->getLang('e_required'), hsc($this->opt['label'])), -1); 62 } 63 } 64 65 $this->opt['value'] = $value; 66 return !$this->error; 67 } 68 69 /** 70 * Creates the HTML for the field 71 * 72 * @param array $params 73 * @param Doku_Form $form 74 * @param int $formid 75 */ 76 public function renderfield($params, Doku_Form $form, $formid) { 77 if(!$this->column) return; 78 79 // this is what parent does 80 $this->_handlePreload(); 81 if(!$form->_infieldset) { 82 $form->startFieldset(''); 83 } 84 if($this->error) { 85 $params['class'] = 'bureaucracy_error'; 86 } 87 88 // output the field 89 $value = new Value($this->column, $this->opt['value']); 90 if ($this->column->getType() instanceof Lookup) { 91 $value->setValue($this->opt['value'], true); 92 } 93 $field = $this->makeField($value, $params['name']); 94 $form->addElement($field); 95 } 96 97 /** 98 * Create the input field 99 * 100 * @param Value $field 101 * @param String $name field's name 102 * @return string 103 */ 104 protected function makeField(Value $field, $name) { 105 $trans = hsc($field->getColumn()->getTranslatedLabel()); 106 $hint = hsc($field->getColumn()->getTranslatedHint()); 107 $class = $hint ? 'hashint' : ''; 108 $lclass = $this->error ? 'bureaucracy_error' : ''; 109 $colname = $field->getColumn()->getFullQualifiedLabel(); 110 $required = $this->opt['optional'] ? '' : ' <sup>*</sup>'; 111 112 $id = uniqid('struct__', false); 113 $input = $field->getValueEditor($name, $id); 114 115 $html = '<div class="field">'; 116 $html .= "<label class=\"$lclass\" data-column=\"$colname\" for=\"$id\">"; 117 $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>"; 118 $html .= '</label>'; 119 $html .= "<span class=\"input\">$input</span>"; 120 $html .= '</div>'; 121 122 return $html; 123 } 124 125 /** 126 * Tries to find the correct column and schema 127 * 128 * @throws StructException 129 * @param string $colname 130 * @return \dokuwiki\plugin\struct\meta\Column 131 */ 132 protected function findColumn($colname) { 133 list($table, $label) = explode('.', $colname, 2); 134 if(!$table || !$label) { 135 throw new StructException('Field \'%s\' not given in schema.field form', $colname); 136 } 137 $schema = new Schema($table); 138 return $schema->findColumn($label); 139 } 140 141 /** 142 * This ensures all language strings are still working 143 * 144 * @return string always 'bureaucracy' 145 */ 146 public function getPluginName() { 147 return 'bureaucracy'; 148 } 149 150} 151