xref: /plugin/struct/helper/field.php (revision 3ad9c1eaefd634b03b03b85327536e38c6df025a)
1ed3de3d6SAndreas Gohr<?php
2*3ad9c1eaSAndreas Gohruse plugin\struct\meta\Column;
3*3ad9c1eaSAndreas Gohruse plugin\struct\meta\Schema;
4*3ad9c1eaSAndreas Gohruse plugin\struct\meta\StructException;
5*3ad9c1eaSAndreas Gohruse plugin\struct\meta\Value;
6ed3de3d6SAndreas Gohr
7ed3de3d6SAndreas Gohr/**
8*3ad9c1eaSAndreas Gohr * Allows adding a single struct field as a bureaucracy field
9*3ad9c1eaSAndreas Gohr *
10*3ad9c1eaSAndreas Gohr * This class is used when a field of the type struct_field is encountered in the
11*3ad9c1eaSAndreas Gohr * bureaucracy syntax.
12ed3de3d6SAndreas Gohr */
13ed3de3d6SAndreas Gohrclass helper_plugin_struct_field extends helper_plugin_bureaucracy_field {
14ed3de3d6SAndreas Gohr
15*3ad9c1eaSAndreas Gohr    /** @var  Column */
16*3ad9c1eaSAndreas Gohr    public $column;
17ed3de3d6SAndreas Gohr
18*3ad9c1eaSAndreas Gohr    /**
19*3ad9c1eaSAndreas Gohr     * Initialize the appropriate column
20*3ad9c1eaSAndreas Gohr     *
21*3ad9c1eaSAndreas Gohr     * @param array $args
22*3ad9c1eaSAndreas Gohr     */
23*3ad9c1eaSAndreas Gohr    public function initialize($args) {
24*3ad9c1eaSAndreas Gohr        parent::initialize($args);
25ed3de3d6SAndreas Gohr
26*3ad9c1eaSAndreas Gohr        // find the column
27*3ad9c1eaSAndreas Gohr        try {
28*3ad9c1eaSAndreas Gohr            $this->column = $this->findColumn($this->opt['label']);
29*3ad9c1eaSAndreas Gohr        } catch(StructException $e) {
30*3ad9c1eaSAndreas Gohr            msg(hsc($e->getMessage()), -1);
31*3ad9c1eaSAndreas Gohr        }
32*3ad9c1eaSAndreas Gohr    }
33*3ad9c1eaSAndreas Gohr
34*3ad9c1eaSAndreas Gohr    /**
35*3ad9c1eaSAndreas Gohr     * Validate the field
36*3ad9c1eaSAndreas Gohr     *
37*3ad9c1eaSAndreas Gohr     * @throws Exception
38*3ad9c1eaSAndreas Gohr     */
39*3ad9c1eaSAndreas Gohr    protected function _validate() {
40*3ad9c1eaSAndreas Gohr        parent::_validate(); // checks optional state stuff
41*3ad9c1eaSAndreas Gohr        if(!$this->column) return;
42*3ad9c1eaSAndreas Gohr        $this->opt['value'] = $this->column->getType()->validate($this->opt['value']);
43*3ad9c1eaSAndreas Gohr    }
44*3ad9c1eaSAndreas Gohr
45*3ad9c1eaSAndreas Gohr    /**
46*3ad9c1eaSAndreas Gohr     * Creates the HTML for the field
47*3ad9c1eaSAndreas Gohr     *
48*3ad9c1eaSAndreas Gohr     * @param array $params
49*3ad9c1eaSAndreas Gohr     * @param Doku_Form $form
50*3ad9c1eaSAndreas Gohr     * @param int $formid
51*3ad9c1eaSAndreas Gohr     */
52*3ad9c1eaSAndreas Gohr    public function renderfield($params, Doku_Form $form, $formid) {
53*3ad9c1eaSAndreas Gohr        if(!$this->column) return;
54*3ad9c1eaSAndreas Gohr
55*3ad9c1eaSAndreas Gohr        // this is what parent does
56*3ad9c1eaSAndreas Gohr        $this->_handlePreload();
57*3ad9c1eaSAndreas Gohr        if(!$form->_infieldset) {
58*3ad9c1eaSAndreas Gohr            $form->startFieldset('');
59*3ad9c1eaSAndreas Gohr        }
60*3ad9c1eaSAndreas Gohr        if($this->error) {
61*3ad9c1eaSAndreas Gohr            $params['class'] = 'bureaucracy_error';
62*3ad9c1eaSAndreas Gohr        }
63*3ad9c1eaSAndreas Gohr
64*3ad9c1eaSAndreas Gohr        // output the field
65*3ad9c1eaSAndreas Gohr        $value = new Value($this->column, $this->opt['value']);
66*3ad9c1eaSAndreas Gohr        $field = action_plugin_struct_entry::makeField($value, $params['name']);
67*3ad9c1eaSAndreas Gohr        $form->addElement($field);
68*3ad9c1eaSAndreas Gohr    }
69*3ad9c1eaSAndreas Gohr
70*3ad9c1eaSAndreas Gohr    /**
71*3ad9c1eaSAndreas Gohr     * Tries to find the correct column and schema
72*3ad9c1eaSAndreas Gohr     *
73*3ad9c1eaSAndreas Gohr     * @throws StructException
74*3ad9c1eaSAndreas Gohr     * @param string $colname
75*3ad9c1eaSAndreas Gohr     * @return \plugin\struct\meta\Column
76*3ad9c1eaSAndreas Gohr     */
77*3ad9c1eaSAndreas Gohr    protected function findColumn($colname) {
78*3ad9c1eaSAndreas Gohr        list($table, $label) = explode('.', $colname, 2);
79*3ad9c1eaSAndreas Gohr        if(!$table || !$label) {
80*3ad9c1eaSAndreas Gohr            throw new StructException('Field \'%s\' not given in schema.field form', $colname);
81*3ad9c1eaSAndreas Gohr        }
82*3ad9c1eaSAndreas Gohr        $schema = new Schema($table);
83*3ad9c1eaSAndreas Gohr        return $schema->findColumn($label);
84*3ad9c1eaSAndreas Gohr    }
85*3ad9c1eaSAndreas Gohr
86*3ad9c1eaSAndreas Gohr    /**
87*3ad9c1eaSAndreas Gohr     * This ensures all language strings are still working
88*3ad9c1eaSAndreas Gohr     *
89*3ad9c1eaSAndreas Gohr     * @return string always 'bureaucracy'
90*3ad9c1eaSAndreas Gohr     */
91*3ad9c1eaSAndreas Gohr    public function getPluginName() {
92*3ad9c1eaSAndreas Gohr        return 'bureaucracy';
93*3ad9c1eaSAndreas Gohr    }
94ed3de3d6SAndreas Gohr
95ed3de3d6SAndreas Gohr}
96