1ed3de3d6SAndreas Gohr<?php 23ad9c1eaSAndreas Gohruse plugin\struct\meta\Column; 33ad9c1eaSAndreas Gohruse plugin\struct\meta\Schema; 43ad9c1eaSAndreas Gohruse plugin\struct\meta\StructException; 53ad9c1eaSAndreas Gohruse plugin\struct\meta\Value; 6ed3de3d6SAndreas Gohr 7ed3de3d6SAndreas Gohr/** 83ad9c1eaSAndreas Gohr * Allows adding a single struct field as a bureaucracy field 93ad9c1eaSAndreas Gohr * 103ad9c1eaSAndreas Gohr * This class is used when a field of the type struct_field is encountered in the 113ad9c1eaSAndreas Gohr * bureaucracy syntax. 12ed3de3d6SAndreas Gohr */ 13ed3de3d6SAndreas Gohrclass helper_plugin_struct_field extends helper_plugin_bureaucracy_field { 14ed3de3d6SAndreas Gohr 153ad9c1eaSAndreas Gohr /** @var Column */ 163ad9c1eaSAndreas Gohr public $column; 17ed3de3d6SAndreas Gohr 183ad9c1eaSAndreas Gohr /** 193ad9c1eaSAndreas Gohr * Initialize the appropriate column 203ad9c1eaSAndreas Gohr * 213ad9c1eaSAndreas Gohr * @param array $args 223ad9c1eaSAndreas Gohr */ 233ad9c1eaSAndreas Gohr public function initialize($args) { 243ad9c1eaSAndreas Gohr parent::initialize($args); 25ed3de3d6SAndreas Gohr 263ad9c1eaSAndreas Gohr // find the column 273ad9c1eaSAndreas Gohr try { 283ad9c1eaSAndreas Gohr $this->column = $this->findColumn($this->opt['label']); 293ad9c1eaSAndreas Gohr } catch(StructException $e) { 303ad9c1eaSAndreas Gohr msg(hsc($e->getMessage()), -1); 313ad9c1eaSAndreas Gohr } 323ad9c1eaSAndreas Gohr } 333ad9c1eaSAndreas Gohr 343ad9c1eaSAndreas Gohr /** 353ad9c1eaSAndreas Gohr * Validate the field 363ad9c1eaSAndreas Gohr * 373ad9c1eaSAndreas Gohr * @throws Exception 383ad9c1eaSAndreas Gohr */ 393ad9c1eaSAndreas Gohr protected function _validate() { 403ad9c1eaSAndreas Gohr parent::_validate(); // checks optional state stuff 413ad9c1eaSAndreas Gohr if(!$this->column) return; 423ad9c1eaSAndreas Gohr $this->opt['value'] = $this->column->getType()->validate($this->opt['value']); 433ad9c1eaSAndreas Gohr } 443ad9c1eaSAndreas Gohr 453ad9c1eaSAndreas Gohr /** 463ad9c1eaSAndreas Gohr * Creates the HTML for the field 473ad9c1eaSAndreas Gohr * 483ad9c1eaSAndreas Gohr * @param array $params 493ad9c1eaSAndreas Gohr * @param Doku_Form $form 503ad9c1eaSAndreas Gohr * @param int $formid 513ad9c1eaSAndreas Gohr */ 523ad9c1eaSAndreas Gohr public function renderfield($params, Doku_Form $form, $formid) { 533ad9c1eaSAndreas Gohr if(!$this->column) return; 543ad9c1eaSAndreas Gohr 553ad9c1eaSAndreas Gohr // this is what parent does 563ad9c1eaSAndreas Gohr $this->_handlePreload(); 573ad9c1eaSAndreas Gohr if(!$form->_infieldset) { 583ad9c1eaSAndreas Gohr $form->startFieldset(''); 593ad9c1eaSAndreas Gohr } 603ad9c1eaSAndreas Gohr if($this->error) { 613ad9c1eaSAndreas Gohr $params['class'] = 'bureaucracy_error'; 623ad9c1eaSAndreas Gohr } 633ad9c1eaSAndreas Gohr 643ad9c1eaSAndreas Gohr // output the field 653ad9c1eaSAndreas Gohr $value = new Value($this->column, $this->opt['value']); 66*95838d50SAndreas Gohr $field = $this->makeField($value, $params['name']); 673ad9c1eaSAndreas Gohr $form->addElement($field); 683ad9c1eaSAndreas Gohr } 693ad9c1eaSAndreas Gohr 70*95838d50SAndreas Gohr 71*95838d50SAndreas Gohr 72*95838d50SAndreas Gohr /** 73*95838d50SAndreas Gohr * Create the input field 74*95838d50SAndreas Gohr * 75*95838d50SAndreas Gohr * @param Value $field 76*95838d50SAndreas Gohr * @param String $name field's name 77*95838d50SAndreas Gohr * @return string 78*95838d50SAndreas Gohr */ 79*95838d50SAndreas Gohr protected function makeField(Value $field, $name) { 80*95838d50SAndreas Gohr $trans = hsc($field->getColumn()->getTranslatedLabel()); 81*95838d50SAndreas Gohr $hint = hsc($field->getColumn()->getTranslatedHint()); 82*95838d50SAndreas Gohr $class = $hint ? 'hashint' : ''; 83*95838d50SAndreas Gohr $lclass = $this->error ? 'bureaucracy_error' : ''; 84*95838d50SAndreas Gohr $colname = $field->getColumn()->getFullQualifiedLabel(); 85*95838d50SAndreas Gohr $required = ' <sup>*</sup>'; 86*95838d50SAndreas Gohr 87*95838d50SAndreas Gohr $input = $field->getValueEditor($name); 88*95838d50SAndreas Gohr 89*95838d50SAndreas Gohr 90*95838d50SAndreas Gohr $html = ''; 91*95838d50SAndreas Gohr $html .= "<label class=\"$lclass\" data-column=\"$colname\">"; 92*95838d50SAndreas Gohr $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>"; 93*95838d50SAndreas Gohr $html .= "<span class=\"input\">$input</span>"; 94*95838d50SAndreas Gohr $html .= '</label>'; 95*95838d50SAndreas Gohr 96*95838d50SAndreas Gohr return $html; 97*95838d50SAndreas Gohr } 98*95838d50SAndreas Gohr 993ad9c1eaSAndreas Gohr /** 1003ad9c1eaSAndreas Gohr * Tries to find the correct column and schema 1013ad9c1eaSAndreas Gohr * 1023ad9c1eaSAndreas Gohr * @throws StructException 1033ad9c1eaSAndreas Gohr * @param string $colname 1043ad9c1eaSAndreas Gohr * @return \plugin\struct\meta\Column 1053ad9c1eaSAndreas Gohr */ 1063ad9c1eaSAndreas Gohr protected function findColumn($colname) { 1073ad9c1eaSAndreas Gohr list($table, $label) = explode('.', $colname, 2); 1083ad9c1eaSAndreas Gohr if(!$table || !$label) { 1093ad9c1eaSAndreas Gohr throw new StructException('Field \'%s\' not given in schema.field form', $colname); 1103ad9c1eaSAndreas Gohr } 1113ad9c1eaSAndreas Gohr $schema = new Schema($table); 1123ad9c1eaSAndreas Gohr return $schema->findColumn($label); 1133ad9c1eaSAndreas Gohr } 1143ad9c1eaSAndreas Gohr 1153ad9c1eaSAndreas Gohr /** 1163ad9c1eaSAndreas Gohr * This ensures all language strings are still working 1173ad9c1eaSAndreas Gohr * 1183ad9c1eaSAndreas Gohr * @return string always 'bureaucracy' 1193ad9c1eaSAndreas Gohr */ 1203ad9c1eaSAndreas Gohr public function getPluginName() { 1213ad9c1eaSAndreas Gohr return 'bureaucracy'; 1223ad9c1eaSAndreas Gohr } 123ed3de3d6SAndreas Gohr 124ed3de3d6SAndreas Gohr} 125