xref: /plugin/struct/helper/field.php (revision 2e0e93472f7d9eecb79acdab8510ebaf447d1a29)
1ed3de3d6SAndreas Gohr<?php
2ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Column;
3ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
4ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
5ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
693ca6f4fSAndreas Gohruse dokuwiki\plugin\struct\meta\ValueValidator;
7e46eaffdSSzymon Olewniczakuse dokuwiki\plugin\struct\types\Lookup;
8ed3de3d6SAndreas Gohr
9ed3de3d6SAndreas Gohr/**
103ad9c1eaSAndreas Gohr * Allows adding a single struct field as a bureaucracy field
113ad9c1eaSAndreas Gohr *
123ad9c1eaSAndreas Gohr * This class is used when a field of the type struct_field is encountered in the
133ad9c1eaSAndreas Gohr * bureaucracy syntax.
14ed3de3d6SAndreas Gohr */
15ed3de3d6SAndreas Gohrclass helper_plugin_struct_field extends helper_plugin_bureaucracy_field {
16ed3de3d6SAndreas Gohr
173ad9c1eaSAndreas Gohr    /** @var  Column */
183ad9c1eaSAndreas Gohr    public $column;
19ed3de3d6SAndreas Gohr
203ad9c1eaSAndreas Gohr    /**
213ad9c1eaSAndreas Gohr     * Initialize the appropriate column
223ad9c1eaSAndreas Gohr     *
233ad9c1eaSAndreas Gohr     * @param array $args
243ad9c1eaSAndreas Gohr     */
253ad9c1eaSAndreas Gohr    public function initialize($args) {
2689be912bSSzymon Olewniczak        $this->init($args);
27ed3de3d6SAndreas Gohr
283ad9c1eaSAndreas Gohr        // find the column
293ad9c1eaSAndreas Gohr        try {
303ad9c1eaSAndreas Gohr            $this->column = $this->findColumn($this->opt['label']);
313ad9c1eaSAndreas Gohr        } catch(StructException $e) {
323ad9c1eaSAndreas Gohr            msg(hsc($e->getMessage()), -1);
333ad9c1eaSAndreas Gohr        }
3489be912bSSzymon Olewniczak
3589be912bSSzymon Olewniczak        $this->standardArgs($args);
363ad9c1eaSAndreas Gohr    }
373ad9c1eaSAndreas Gohr
383ad9c1eaSAndreas Gohr    /**
396c71c031SAndreas Gohr     * Sets the value and validates it
403ad9c1eaSAndreas Gohr     *
416c71c031SAndreas Gohr     * @param mixed $value
426c71c031SAndreas Gohr     * @return bool value was set successfully validated
433ad9c1eaSAndreas Gohr     */
446c71c031SAndreas Gohr    protected function setVal($value) {
456c71c031SAndreas Gohr        if(!$this->column) {
466c71c031SAndreas Gohr            $value = '';
47131fd507SSzymon Olewniczak        //don't validate placeholders here
48131fd507SSzymon Olewniczak        } elseif($this->replace($value) == $value) {
4993ca6f4fSAndreas Gohr            $validator = new ValueValidator();
506c71c031SAndreas Gohr            $this->error = !$validator->validateValue($this->column, $value);
516c71c031SAndreas Gohr            if($this->error) {
526c71c031SAndreas Gohr                foreach($validator->getErrors() as $error) {
536c71c031SAndreas Gohr                    msg(hsc($error), -1);
546c71c031SAndreas Gohr                }
556c71c031SAndreas Gohr            }
566c71c031SAndreas Gohr        }
576c71c031SAndreas Gohr
586c71c031SAndreas Gohr        if($value === array() || $value === '') {
596c71c031SAndreas Gohr            if(!isset($this->opt['optional'])) {
606c71c031SAndreas Gohr                $this->error = true;
61*2e0e9347SSzymon Olewniczak                if ($this->column) {
62*2e0e9347SSzymon Olewniczak                    $label = $this->column->getTranslatedLabel();
63*2e0e9347SSzymon Olewniczak                } else {
64*2e0e9347SSzymon Olewniczak                    $label = $this->opt['label'];
65*2e0e9347SSzymon Olewniczak                }
66*2e0e9347SSzymon Olewniczak                msg(sprintf($this->getLang('e_required'), hsc($label)), -1);
676c71c031SAndreas Gohr            }
686c71c031SAndreas Gohr        }
696c71c031SAndreas Gohr
706c71c031SAndreas Gohr        $this->opt['value'] = $value;
716c71c031SAndreas Gohr        return !$this->error;
723ad9c1eaSAndreas Gohr    }
733ad9c1eaSAndreas Gohr
743ad9c1eaSAndreas Gohr    /**
753ad9c1eaSAndreas Gohr     * Creates the HTML for the field
763ad9c1eaSAndreas Gohr     *
773ad9c1eaSAndreas Gohr     * @param array $params
783ad9c1eaSAndreas Gohr     * @param Doku_Form $form
793ad9c1eaSAndreas Gohr     * @param int $formid
803ad9c1eaSAndreas Gohr     */
813ad9c1eaSAndreas Gohr    public function renderfield($params, Doku_Form $form, $formid) {
823ad9c1eaSAndreas Gohr        if(!$this->column) return;
833ad9c1eaSAndreas Gohr
843ad9c1eaSAndreas Gohr        // this is what parent does
853ad9c1eaSAndreas Gohr        $this->_handlePreload();
863ad9c1eaSAndreas Gohr        if(!$form->_infieldset) {
873ad9c1eaSAndreas Gohr            $form->startFieldset('');
883ad9c1eaSAndreas Gohr        }
893ad9c1eaSAndreas Gohr        if($this->error) {
903ad9c1eaSAndreas Gohr            $params['class'] = 'bureaucracy_error';
913ad9c1eaSAndreas Gohr        }
923ad9c1eaSAndreas Gohr
933ad9c1eaSAndreas Gohr        // output the field
943ad9c1eaSAndreas Gohr        $value = new Value($this->column, $this->opt['value']);
95e46eaffdSSzymon Olewniczak        if ($this->column->getType() instanceof Lookup) {
96987ccc7fSSzymon Olewniczak            $value->setValue($this->opt['value'], true);
97987ccc7fSSzymon Olewniczak        }
9895838d50SAndreas Gohr        $field = $this->makeField($value, $params['name']);
993ad9c1eaSAndreas Gohr        $form->addElement($field);
1003ad9c1eaSAndreas Gohr    }
1013ad9c1eaSAndreas Gohr
10295838d50SAndreas Gohr    /**
10395838d50SAndreas Gohr     * Create the input field
10495838d50SAndreas Gohr     *
10595838d50SAndreas Gohr     * @param Value $field
10695838d50SAndreas Gohr     * @param String $name field's name
10795838d50SAndreas Gohr     * @return string
10895838d50SAndreas Gohr     */
10995838d50SAndreas Gohr    protected function makeField(Value $field, $name) {
11095838d50SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
11195838d50SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
11295838d50SAndreas Gohr        $class = $hint ? 'hashint' : '';
11395838d50SAndreas Gohr        $lclass = $this->error ? 'bureaucracy_error' : '';
11495838d50SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
1159bdb73ceSAndreas Gohr        $required = $this->opt['optional'] ? '' : ' <sup>*</sup>';
11695838d50SAndreas Gohr
1175275870bSAnna Dabrowska        $id = uniqid('struct__', true);
118ee983135SMichael Große        $input = $field->getValueEditor($name, $id);
11995838d50SAndreas Gohr
120ee983135SMichael Große        $html = '<div class="field">';
121ee983135SMichael Große        $html .= "<label class=\"$lclass\" data-column=\"$colname\" for=\"$id\">";
12295838d50SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>";
12395838d50SAndreas Gohr        $html .= '</label>';
124ee983135SMichael Große        $html .= "<span class=\"input\">$input</span>";
125ee983135SMichael Große        $html .= '</div>';
12695838d50SAndreas Gohr
12795838d50SAndreas Gohr        return $html;
12895838d50SAndreas Gohr    }
12995838d50SAndreas Gohr
1303ad9c1eaSAndreas Gohr    /**
1313ad9c1eaSAndreas Gohr     * Tries to find the correct column and schema
1323ad9c1eaSAndreas Gohr     *
1333ad9c1eaSAndreas Gohr     * @throws StructException
1343ad9c1eaSAndreas Gohr     * @param string $colname
135ba766201SAndreas Gohr     * @return \dokuwiki\plugin\struct\meta\Column
1363ad9c1eaSAndreas Gohr     */
1373ad9c1eaSAndreas Gohr    protected function findColumn($colname) {
1383ad9c1eaSAndreas Gohr        list($table, $label) = explode('.', $colname, 2);
1393ad9c1eaSAndreas Gohr        if(!$table || !$label) {
1403ad9c1eaSAndreas Gohr            throw new StructException('Field \'%s\' not given in schema.field form', $colname);
1413ad9c1eaSAndreas Gohr        }
1423ad9c1eaSAndreas Gohr        $schema = new Schema($table);
1433ad9c1eaSAndreas Gohr        return $schema->findColumn($label);
1443ad9c1eaSAndreas Gohr    }
1453ad9c1eaSAndreas Gohr
1463ad9c1eaSAndreas Gohr    /**
1473ad9c1eaSAndreas Gohr     * This ensures all language strings are still working
1483ad9c1eaSAndreas Gohr     *
1493ad9c1eaSAndreas Gohr     * @return string always 'bureaucracy'
1503ad9c1eaSAndreas Gohr     */
1513ad9c1eaSAndreas Gohr    public function getPluginName() {
1523ad9c1eaSAndreas Gohr        return 'bureaucracy';
1533ad9c1eaSAndreas Gohr    }
154ed3de3d6SAndreas Gohr
155ed3de3d6SAndreas Gohr}
156