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