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