xref: /plugin/struct/helper/field.php (revision 9bdb73ce4322baadc1bdddf8939a8043ff6f3ee8)
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']);
6695838d50SAndreas Gohr        $field = $this->makeField($value, $params['name']);
673ad9c1eaSAndreas Gohr        $form->addElement($field);
683ad9c1eaSAndreas Gohr    }
693ad9c1eaSAndreas Gohr
7095838d50SAndreas Gohr    /**
7195838d50SAndreas Gohr     * Create the input field
7295838d50SAndreas Gohr     *
7395838d50SAndreas Gohr     * @param Value $field
7495838d50SAndreas Gohr     * @param String $name field's name
7595838d50SAndreas Gohr     * @return string
7695838d50SAndreas Gohr     */
7795838d50SAndreas Gohr    protected function makeField(Value $field, $name) {
7895838d50SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
7995838d50SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
8095838d50SAndreas Gohr        $class = $hint ? 'hashint' : '';
8195838d50SAndreas Gohr        $lclass = $this->error ? 'bureaucracy_error' : '';
8295838d50SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
83*9bdb73ceSAndreas Gohr        $required = $this->opt['optional'] ? '' : ' <sup>*</sup>';
8495838d50SAndreas Gohr
8595838d50SAndreas Gohr        $input = $field->getValueEditor($name);
8695838d50SAndreas Gohr
8795838d50SAndreas Gohr        $html = '';
8895838d50SAndreas Gohr        $html .= "<label class=\"$lclass\" data-column=\"$colname\">";
8995838d50SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>";
9095838d50SAndreas Gohr        $html .= "<span class=\"input\">$input</span>";
9195838d50SAndreas Gohr        $html .= '</label>';
9295838d50SAndreas Gohr
9395838d50SAndreas Gohr        return $html;
9495838d50SAndreas Gohr    }
9595838d50SAndreas Gohr
963ad9c1eaSAndreas Gohr    /**
973ad9c1eaSAndreas Gohr     * Tries to find the correct column and schema
983ad9c1eaSAndreas Gohr     *
993ad9c1eaSAndreas Gohr     * @throws StructException
1003ad9c1eaSAndreas Gohr     * @param string $colname
1013ad9c1eaSAndreas Gohr     * @return \plugin\struct\meta\Column
1023ad9c1eaSAndreas Gohr     */
1033ad9c1eaSAndreas Gohr    protected function findColumn($colname) {
1043ad9c1eaSAndreas Gohr        list($table, $label) = explode('.', $colname, 2);
1053ad9c1eaSAndreas Gohr        if(!$table || !$label) {
1063ad9c1eaSAndreas Gohr            throw new StructException('Field \'%s\' not given in schema.field form', $colname);
1073ad9c1eaSAndreas Gohr        }
1083ad9c1eaSAndreas Gohr        $schema = new Schema($table);
1093ad9c1eaSAndreas Gohr        return $schema->findColumn($label);
1103ad9c1eaSAndreas Gohr    }
1113ad9c1eaSAndreas Gohr
1123ad9c1eaSAndreas Gohr    /**
1133ad9c1eaSAndreas Gohr     * This ensures all language strings are still working
1143ad9c1eaSAndreas Gohr     *
1153ad9c1eaSAndreas Gohr     * @return string always 'bureaucracy'
1163ad9c1eaSAndreas Gohr     */
1173ad9c1eaSAndreas Gohr    public function getPluginName() {
1183ad9c1eaSAndreas Gohr        return 'bureaucracy';
1193ad9c1eaSAndreas Gohr    }
120ed3de3d6SAndreas Gohr
121ed3de3d6SAndreas Gohr}
122