xref: /plugin/struct/action/edit.php (revision 87dc1344fb5ebba89b6bf5f09fe8e70f0793e751)
1<?php
2/**
3 * DokuWiki Plugin struct (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12use dokuwiki\plugin\struct\meta\AccessTable;
13use dokuwiki\plugin\struct\meta\Assignments;
14use dokuwiki\plugin\struct\meta\AccessTableData;
15use dokuwiki\plugin\struct\meta\ValidationResult;
16use dokuwiki\plugin\struct\meta\Validator;
17use dokuwiki\plugin\struct\meta\Value;
18
19/**
20 * Class action_plugin_struct_entry
21 *
22 * Handles adding struct forms to the default editor
23 */
24class action_plugin_struct_edit extends DokuWiki_Action_Plugin {
25
26    /**
27     * @var string The form name we use to transfer schema data
28     */
29    protected static $VAR = 'struct_schema_data';
30
31    /**
32     * Registers a callback function for a given event
33     *
34     * @param Doku_Event_Handler $controller DokuWiki's event controller object
35     * @return void
36     */
37    public function register(Doku_Event_Handler $controller) {
38        // add the struct editor to the edit form;
39        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
40    }
41
42    /**
43     * Enhance the editing form with structural data editing
44     *
45     * @param Doku_Event $event event object by reference
46     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
47     *                           handler was registered]
48     * @return bool
49     */
50    public function handle_editform(Doku_Event $event, $param) {
51        global $ID;
52
53        $assignments = new Assignments();
54        $tables = $assignments->getPageAssignments($ID);
55
56        $html = '';
57        foreach($tables as $table) {
58            $html .= $this->createForm($table);
59        }
60
61        /** @var Doku_Form $form */
62        $form = $event->data;
63        $html = "<div class=\"struct_entry_form\">$html</div>";
64        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
65        $form->insertElement($pos, $html);
66
67        return true;
68    }
69
70    /**
71     * Create the form to edit schemadata
72     *
73     * @param string $tablename
74     * @return string The HTML for this schema's form
75     */
76    protected function createForm($tablename) {
77        global $ID;
78        global $REV;
79        global $INPUT;
80        if (auth_quickaclcheck($ID) == AUTH_READ) return '';
81        if (checklock($ID)) return '';
82        $schema = AccessTable::byTableName($tablename, $ID, $REV);
83        $schemadata = $schema->getData();
84
85        $structdata = $INPUT->arr(self::$VAR);
86        if(isset($structdata[$tablename])) {
87            $postdata = $structdata[$tablename];
88        } else {
89            $postdata = array();
90        }
91
92        // we need a short, unique identifier to use in the cookie. this should be good enough
93        $schemaid = 'SRCT'.substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
94        $html = '<fieldset data-schema="' . $schemaid . '">';
95        $html .= '<legend>' . hsc($tablename) . '</legend>';
96        foreach($schemadata as $field) {
97            $label = $field->getColumn()->getLabel();
98            if(isset($postdata[$label])) {
99                // posted data trumps stored data
100                $field->setValue($postdata[$label]);
101            }
102            $html .=  $this->makeField($field, self::$VAR . "[$tablename][$label]");
103        }
104        $html .= '</fieldset>';
105
106        return $html;
107    }
108
109    /**
110     * Create the input field
111     *
112     * @param Value $field
113     * @param String $name field's name
114     * @return string
115     */
116    public function makeField(Value $field, $name) {
117        $trans = hsc($field->getColumn()->getTranslatedLabel());
118        $hint  = hsc($field->getColumn()->getTranslatedHint());
119        $class = $hint ? 'hashint' : '';
120        $colname = $field->getColumn()->getFullQualifiedLabel();
121
122        $input = $field->getValueEditor($name);
123
124        // we keep all the custom form stuff the field might produce, but hide it
125        if(!$field->getColumn()->isVisibleInEditor()) {
126            $hide = 'style="display:none"';
127        } else {
128            $hide = '';
129        }
130
131        $html = '';
132        $html .= "<label $hide data-column=\"$colname\">";
133        $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
134        $html .= "<span class=\"input\">$input</span>";
135        $html .= '</label>';
136
137        return $html;
138    }
139}
140
141// vim:ts=4:sw=4:et:
142