xref: /plugin/struct/helper/field.php (revision 6c71c03110f8317d20cb4e430b8578e403e279de)
1ed3de3d6SAndreas Gohr<?php
23ad9c1eaSAndreas Gohruse plugin\struct\meta\Column;
33ad9c1eaSAndreas Gohruse plugin\struct\meta\Schema;
43ad9c1eaSAndreas Gohruse plugin\struct\meta\StructException;
5*6c71c031SAndreas Gohruse plugin\struct\meta\Validator;
63ad9c1eaSAndreas Gohruse plugin\struct\meta\Value;
7ed3de3d6SAndreas Gohr
8ed3de3d6SAndreas Gohr/**
93ad9c1eaSAndreas Gohr * Allows adding a single struct field as a bureaucracy field
103ad9c1eaSAndreas Gohr *
113ad9c1eaSAndreas Gohr * This class is used when a field of the type struct_field is encountered in the
123ad9c1eaSAndreas Gohr * bureaucracy syntax.
13ed3de3d6SAndreas Gohr */
14ed3de3d6SAndreas Gohrclass helper_plugin_struct_field extends helper_plugin_bureaucracy_field {
15ed3de3d6SAndreas Gohr
163ad9c1eaSAndreas Gohr    /** @var  Column */
173ad9c1eaSAndreas Gohr    public $column;
18ed3de3d6SAndreas Gohr
193ad9c1eaSAndreas Gohr    /**
203ad9c1eaSAndreas Gohr     * Initialize the appropriate column
213ad9c1eaSAndreas Gohr     *
223ad9c1eaSAndreas Gohr     * @param array $args
233ad9c1eaSAndreas Gohr     */
243ad9c1eaSAndreas Gohr    public function initialize($args) {
253ad9c1eaSAndreas Gohr        parent::initialize($args);
26ed3de3d6SAndreas Gohr
273ad9c1eaSAndreas Gohr        // find the column
283ad9c1eaSAndreas Gohr        try {
293ad9c1eaSAndreas Gohr            $this->column = $this->findColumn($this->opt['label']);
303ad9c1eaSAndreas Gohr        } catch(StructException $e) {
313ad9c1eaSAndreas Gohr            msg(hsc($e->getMessage()), -1);
323ad9c1eaSAndreas Gohr        }
333ad9c1eaSAndreas Gohr    }
343ad9c1eaSAndreas Gohr
353ad9c1eaSAndreas Gohr    /**
36*6c71c031SAndreas Gohr     * Sets the value and validates it
373ad9c1eaSAndreas Gohr     *
38*6c71c031SAndreas Gohr     * @param mixed $value
39*6c71c031SAndreas Gohr     * @return bool value was set successfully validated
403ad9c1eaSAndreas Gohr     */
41*6c71c031SAndreas Gohr    protected function setVal($value) {
42*6c71c031SAndreas Gohr        if(!$this->column) {
43*6c71c031SAndreas Gohr            $value = '';
44*6c71c031SAndreas Gohr        } else {
45*6c71c031SAndreas Gohr            $validator = new Validator();
46*6c71c031SAndreas Gohr            $this->error = !$validator->validateValue($this->column, $value);
47*6c71c031SAndreas Gohr            if($this->error) {
48*6c71c031SAndreas Gohr                foreach($validator->getErrors() as $error) {
49*6c71c031SAndreas Gohr                    msg(hsc($error), -1);
50*6c71c031SAndreas Gohr                }
51*6c71c031SAndreas Gohr            }
52*6c71c031SAndreas Gohr
53*6c71c031SAndreas Gohr        }
54*6c71c031SAndreas Gohr
55*6c71c031SAndreas Gohr        if($value === array() || $value === '') {
56*6c71c031SAndreas Gohr            if(!isset($this->opt['optional'])) {
57*6c71c031SAndreas Gohr                $this->error = true;
58*6c71c031SAndreas Gohr                msg(sprintf($this->getLang('e_required'), hsc($this->opt['label'])), -1);
59*6c71c031SAndreas Gohr            }
60*6c71c031SAndreas Gohr        }
61*6c71c031SAndreas Gohr
62*6c71c031SAndreas Gohr        $this->opt['value'] = $value;
63*6c71c031SAndreas Gohr        return !$this->error;
643ad9c1eaSAndreas Gohr    }
653ad9c1eaSAndreas Gohr
663ad9c1eaSAndreas Gohr    /**
673ad9c1eaSAndreas Gohr     * Creates the HTML for the field
683ad9c1eaSAndreas Gohr     *
693ad9c1eaSAndreas Gohr     * @param array $params
703ad9c1eaSAndreas Gohr     * @param Doku_Form $form
713ad9c1eaSAndreas Gohr     * @param int $formid
723ad9c1eaSAndreas Gohr     */
733ad9c1eaSAndreas Gohr    public function renderfield($params, Doku_Form $form, $formid) {
743ad9c1eaSAndreas Gohr        if(!$this->column) return;
753ad9c1eaSAndreas Gohr
763ad9c1eaSAndreas Gohr        // this is what parent does
773ad9c1eaSAndreas Gohr        $this->_handlePreload();
783ad9c1eaSAndreas Gohr        if(!$form->_infieldset) {
793ad9c1eaSAndreas Gohr            $form->startFieldset('');
803ad9c1eaSAndreas Gohr        }
813ad9c1eaSAndreas Gohr        if($this->error) {
823ad9c1eaSAndreas Gohr            $params['class'] = 'bureaucracy_error';
833ad9c1eaSAndreas Gohr        }
843ad9c1eaSAndreas Gohr
853ad9c1eaSAndreas Gohr        // output the field
863ad9c1eaSAndreas Gohr        $value = new Value($this->column, $this->opt['value']);
8795838d50SAndreas Gohr        $field = $this->makeField($value, $params['name']);
883ad9c1eaSAndreas Gohr        $form->addElement($field);
893ad9c1eaSAndreas Gohr    }
903ad9c1eaSAndreas Gohr
9195838d50SAndreas Gohr    /**
9295838d50SAndreas Gohr     * Create the input field
9395838d50SAndreas Gohr     *
9495838d50SAndreas Gohr     * @param Value $field
9595838d50SAndreas Gohr     * @param String $name field's name
9695838d50SAndreas Gohr     * @return string
9795838d50SAndreas Gohr     */
9895838d50SAndreas Gohr    protected function makeField(Value $field, $name) {
9995838d50SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
10095838d50SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
10195838d50SAndreas Gohr        $class = $hint ? 'hashint' : '';
10295838d50SAndreas Gohr        $lclass = $this->error ? 'bureaucracy_error' : '';
10395838d50SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
1049bdb73ceSAndreas Gohr        $required = $this->opt['optional'] ? '' : ' <sup>*</sup>';
10595838d50SAndreas Gohr
10695838d50SAndreas Gohr        $input = $field->getValueEditor($name);
10795838d50SAndreas Gohr
10895838d50SAndreas Gohr        $html = '';
10995838d50SAndreas Gohr        $html .= "<label class=\"$lclass\" data-column=\"$colname\">";
11095838d50SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>";
11195838d50SAndreas Gohr        $html .= "<span class=\"input\">$input</span>";
11295838d50SAndreas Gohr        $html .= '</label>';
11395838d50SAndreas Gohr
11495838d50SAndreas Gohr        return $html;
11595838d50SAndreas Gohr    }
11695838d50SAndreas Gohr
1173ad9c1eaSAndreas Gohr    /**
1183ad9c1eaSAndreas Gohr     * Tries to find the correct column and schema
1193ad9c1eaSAndreas Gohr     *
1203ad9c1eaSAndreas Gohr     * @throws StructException
1213ad9c1eaSAndreas Gohr     * @param string $colname
1223ad9c1eaSAndreas Gohr     * @return \plugin\struct\meta\Column
1233ad9c1eaSAndreas Gohr     */
1243ad9c1eaSAndreas Gohr    protected function findColumn($colname) {
1253ad9c1eaSAndreas Gohr        list($table, $label) = explode('.', $colname, 2);
1263ad9c1eaSAndreas Gohr        if(!$table || !$label) {
1273ad9c1eaSAndreas Gohr            throw new StructException('Field \'%s\' not given in schema.field form', $colname);
1283ad9c1eaSAndreas Gohr        }
1293ad9c1eaSAndreas Gohr        $schema = new Schema($table);
1303ad9c1eaSAndreas Gohr        return $schema->findColumn($label);
1313ad9c1eaSAndreas Gohr    }
1323ad9c1eaSAndreas Gohr
1333ad9c1eaSAndreas Gohr    /**
1343ad9c1eaSAndreas Gohr     * This ensures all language strings are still working
1353ad9c1eaSAndreas Gohr     *
1363ad9c1eaSAndreas Gohr     * @return string always 'bureaucracy'
1373ad9c1eaSAndreas Gohr     */
1383ad9c1eaSAndreas Gohr    public function getPluginName() {
1393ad9c1eaSAndreas Gohr        return 'bureaucracy';
1403ad9c1eaSAndreas Gohr    }
141ed3de3d6SAndreas Gohr
142ed3de3d6SAndreas Gohr}
143