1ed3de3d6SAndreas Gohr<?php 2*d6d97f60SAnna Dabrowska 3ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Column; 4ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema; 5ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 6ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Value; 793ca6f4fSAndreas Gohruse dokuwiki\plugin\struct\meta\ValueValidator; 8e46eaffdSSzymon Olewniczakuse dokuwiki\plugin\struct\types\Lookup; 9ed3de3d6SAndreas Gohr 10ed3de3d6SAndreas Gohr/** 113ad9c1eaSAndreas Gohr * Allows adding a single struct field as a bureaucracy field 123ad9c1eaSAndreas Gohr * 133ad9c1eaSAndreas Gohr * This class is used when a field of the type struct_field is encountered in the 143ad9c1eaSAndreas Gohr * bureaucracy syntax. 15ed3de3d6SAndreas Gohr */ 16*d6d97f60SAnna Dabrowskaclass helper_plugin_struct_field extends helper_plugin_bureaucracy_field 17*d6d97f60SAnna Dabrowska{ 18ed3de3d6SAndreas Gohr 193ad9c1eaSAndreas Gohr /** @var Column */ 203ad9c1eaSAndreas Gohr public $column; 21ed3de3d6SAndreas Gohr 223ad9c1eaSAndreas Gohr /** 233ad9c1eaSAndreas Gohr * Initialize the appropriate column 243ad9c1eaSAndreas Gohr * 253ad9c1eaSAndreas Gohr * @param array $args 263ad9c1eaSAndreas Gohr */ 27*d6d97f60SAnna Dabrowska public function initialize($args) 28*d6d97f60SAnna Dabrowska { 2989be912bSSzymon Olewniczak $this->init($args); 30ed3de3d6SAndreas Gohr 313ad9c1eaSAndreas Gohr // find the column 323ad9c1eaSAndreas Gohr try { 333ad9c1eaSAndreas Gohr $this->column = $this->findColumn($this->opt['label']); 343ad9c1eaSAndreas Gohr } catch (StructException $e) { 353ad9c1eaSAndreas Gohr msg(hsc($e->getMessage()), -1); 363ad9c1eaSAndreas Gohr } 3789be912bSSzymon Olewniczak 3889be912bSSzymon Olewniczak $this->standardArgs($args); 393ad9c1eaSAndreas Gohr } 403ad9c1eaSAndreas Gohr 413ad9c1eaSAndreas Gohr /** 426c71c031SAndreas Gohr * Sets the value and validates it 433ad9c1eaSAndreas Gohr * 446c71c031SAndreas Gohr * @param mixed $value 456c71c031SAndreas Gohr * @return bool value was set successfully validated 463ad9c1eaSAndreas Gohr */ 47*d6d97f60SAnna Dabrowska protected function setVal($value) 48*d6d97f60SAnna Dabrowska { 496c71c031SAndreas Gohr if (!$this->column) { 506c71c031SAndreas Gohr $value = ''; 51131fd507SSzymon Olewniczak //don't validate placeholders here 52131fd507SSzymon Olewniczak } elseif ($this->replace($value) == $value) { 5393ca6f4fSAndreas Gohr $validator = new ValueValidator(); 546c71c031SAndreas Gohr $this->error = !$validator->validateValue($this->column, $value); 556c71c031SAndreas Gohr if ($this->error) { 566c71c031SAndreas Gohr foreach ($validator->getErrors() as $error) { 576c71c031SAndreas Gohr msg(hsc($error), -1); 586c71c031SAndreas Gohr } 596c71c031SAndreas Gohr } 606c71c031SAndreas Gohr } 616c71c031SAndreas Gohr 626c71c031SAndreas Gohr if ($value === array() || $value === '') { 636c71c031SAndreas Gohr if (!isset($this->opt['optional'])) { 646c71c031SAndreas Gohr $this->error = true; 652e0e9347SSzymon Olewniczak if ($this->column) { 662e0e9347SSzymon Olewniczak $label = $this->column->getTranslatedLabel(); 672e0e9347SSzymon Olewniczak } else { 682e0e9347SSzymon Olewniczak $label = $this->opt['label']; 692e0e9347SSzymon Olewniczak } 702e0e9347SSzymon Olewniczak msg(sprintf($this->getLang('e_required'), hsc($label)), -1); 716c71c031SAndreas Gohr } 726c71c031SAndreas Gohr } 736c71c031SAndreas Gohr 746c71c031SAndreas Gohr $this->opt['value'] = $value; 756c71c031SAndreas Gohr return !$this->error; 763ad9c1eaSAndreas Gohr } 773ad9c1eaSAndreas Gohr 783ad9c1eaSAndreas Gohr /** 793ad9c1eaSAndreas Gohr * Creates the HTML for the field 803ad9c1eaSAndreas Gohr * 813ad9c1eaSAndreas Gohr * @param array $params 823ad9c1eaSAndreas Gohr * @param Doku_Form $form 833ad9c1eaSAndreas Gohr * @param int $formid 843ad9c1eaSAndreas Gohr */ 85*d6d97f60SAnna Dabrowska public function renderfield($params, Doku_Form $form, $formid) 86*d6d97f60SAnna Dabrowska { 873ad9c1eaSAndreas Gohr if (!$this->column) return; 883ad9c1eaSAndreas Gohr 893ad9c1eaSAndreas Gohr // this is what parent does 903ad9c1eaSAndreas Gohr $this->_handlePreload(); 913ad9c1eaSAndreas Gohr if (!$form->_infieldset) { 923ad9c1eaSAndreas Gohr $form->startFieldset(''); 933ad9c1eaSAndreas Gohr } 943ad9c1eaSAndreas Gohr if ($this->error) { 953ad9c1eaSAndreas Gohr $params['class'] = 'bureaucracy_error'; 963ad9c1eaSAndreas Gohr } 973ad9c1eaSAndreas Gohr 983ad9c1eaSAndreas Gohr // output the field 993ad9c1eaSAndreas Gohr $value = new Value($this->column, $this->opt['value']); 100e46eaffdSSzymon Olewniczak if ($this->column->getType() instanceof Lookup) { 101987ccc7fSSzymon Olewniczak $value->setValue($this->opt['value'], true); 102987ccc7fSSzymon Olewniczak } 10395838d50SAndreas Gohr $field = $this->makeField($value, $params['name']); 1043ad9c1eaSAndreas Gohr $form->addElement($field); 1053ad9c1eaSAndreas Gohr } 1063ad9c1eaSAndreas Gohr 10795838d50SAndreas Gohr /** 10895838d50SAndreas Gohr * Create the input field 10995838d50SAndreas Gohr * 11095838d50SAndreas Gohr * @param Value $field 11195838d50SAndreas Gohr * @param String $name field's name 11295838d50SAndreas Gohr * @return string 11395838d50SAndreas Gohr */ 114*d6d97f60SAnna Dabrowska protected function makeField(Value $field, $name) 115*d6d97f60SAnna Dabrowska { 11695838d50SAndreas Gohr $trans = hsc($field->getColumn()->getTranslatedLabel()); 11795838d50SAndreas Gohr $hint = hsc($field->getColumn()->getTranslatedHint()); 11895838d50SAndreas Gohr $class = $hint ? 'hashint' : ''; 11995838d50SAndreas Gohr $lclass = $this->error ? 'bureaucracy_error' : ''; 12095838d50SAndreas Gohr $colname = $field->getColumn()->getFullQualifiedLabel(); 1219bdb73ceSAndreas Gohr $required = $this->opt['optional'] ? '' : ' <sup>*</sup>'; 12295838d50SAndreas Gohr 1235275870bSAnna Dabrowska $id = uniqid('struct__', true); 124ee983135SMichael Große $input = $field->getValueEditor($name, $id); 12595838d50SAndreas Gohr 126ee983135SMichael Große $html = '<div class="field">'; 127ee983135SMichael Große $html .= "<label class=\"$lclass\" data-column=\"$colname\" for=\"$id\">"; 12895838d50SAndreas Gohr $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>"; 12995838d50SAndreas Gohr $html .= '</label>'; 130ee983135SMichael Große $html .= "<span class=\"input\">$input</span>"; 131ee983135SMichael Große $html .= '</div>'; 13295838d50SAndreas Gohr 13395838d50SAndreas Gohr return $html; 13495838d50SAndreas Gohr } 13595838d50SAndreas Gohr 1363ad9c1eaSAndreas Gohr /** 1373ad9c1eaSAndreas Gohr * Tries to find the correct column and schema 1383ad9c1eaSAndreas Gohr * 1393ad9c1eaSAndreas Gohr * @throws StructException 1403ad9c1eaSAndreas Gohr * @param string $colname 141ba766201SAndreas Gohr * @return \dokuwiki\plugin\struct\meta\Column 1423ad9c1eaSAndreas Gohr */ 143*d6d97f60SAnna Dabrowska protected function findColumn($colname) 144*d6d97f60SAnna Dabrowska { 1453ad9c1eaSAndreas Gohr list($table, $label) = explode('.', $colname, 2); 1463ad9c1eaSAndreas Gohr if (!$table || !$label) { 1473ad9c1eaSAndreas Gohr throw new StructException('Field \'%s\' not given in schema.field form', $colname); 1483ad9c1eaSAndreas Gohr } 1493ad9c1eaSAndreas Gohr $schema = new Schema($table); 1503ad9c1eaSAndreas Gohr return $schema->findColumn($label); 1513ad9c1eaSAndreas Gohr } 1523ad9c1eaSAndreas Gohr 1533ad9c1eaSAndreas Gohr /** 1543ad9c1eaSAndreas Gohr * This ensures all language strings are still working 1553ad9c1eaSAndreas Gohr * 1563ad9c1eaSAndreas Gohr * @return string always 'bureaucracy' 1573ad9c1eaSAndreas Gohr */ 158*d6d97f60SAnna Dabrowska public function getPluginName() 159*d6d97f60SAnna Dabrowska { 1603ad9c1eaSAndreas Gohr return 'bureaucracy'; 1613ad9c1eaSAndreas Gohr } 162ed3de3d6SAndreas Gohr} 163