xref: /plugin/struct/action/edit.php (revision 5e29103a15bd9873f422f66a6a5239b6aec4651e)
187dc1344SAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
387dc1344SAndreas Gohr/**
487dc1344SAndreas Gohr * DokuWiki Plugin struct (Action Component)
587dc1344SAndreas Gohr *
687dc1344SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
787dc1344SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
887dc1344SAndreas Gohr */
987dc1344SAndreas Gohr
107234bfb1Ssplitbrainuse dokuwiki\Extension\ActionPlugin;
117234bfb1Ssplitbrainuse dokuwiki\Extension\EventHandler;
127234bfb1Ssplitbrainuse dokuwiki\Extension\Event;
13c8ecffd7SMichael Großeuse dokuwiki\Form\Form;
1487dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
1587dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
1687dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
1787dc1344SAndreas Gohr
1887dc1344SAndreas Gohr/**
1987dc1344SAndreas Gohr * Class action_plugin_struct_entry
2087dc1344SAndreas Gohr *
2187dc1344SAndreas Gohr * Handles adding struct forms to the default editor
2287dc1344SAndreas Gohr */
237234bfb1Ssplitbrainclass action_plugin_struct_edit extends ActionPlugin
24d6d97f60SAnna Dabrowska{
2587dc1344SAndreas Gohr    /**
2687dc1344SAndreas Gohr     * @var string The form name we use to transfer schema data
2787dc1344SAndreas Gohr     */
2887dc1344SAndreas Gohr    protected static $VAR = 'struct_schema_data';
2987dc1344SAndreas Gohr
3087dc1344SAndreas Gohr    /**
3187dc1344SAndreas Gohr     * Registers a callback function for a given event
3287dc1344SAndreas Gohr     *
33*5e29103aSannda     * @param EventHandler $controller DokuWiki's event controller object
3487dc1344SAndreas Gohr     * @return void
3587dc1344SAndreas Gohr     */
367234bfb1Ssplitbrain    public function register(EventHandler $controller)
37d6d97f60SAnna Dabrowska    {
3887dc1344SAndreas Gohr        // add the struct editor to the edit form;
39748e747fSAnna Dabrowska        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handleEditform');
40c8ecffd7SMichael Große        $controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'addFromData');
41c8ecffd7SMichael Große    }
42c8ecffd7SMichael Große
43c8ecffd7SMichael Große    /**
44c8ecffd7SMichael Große     * Adds the html for the struct editors to the edit from
45c8ecffd7SMichael Große     *
46c8ecffd7SMichael Große     * Handles the FORM_EDIT_OUTPUT event
47c8ecffd7SMichael Große     *
48c8ecffd7SMichael Große     * @return bool
49c8ecffd7SMichael Große     */
507234bfb1Ssplitbrain    public function addFromData(Event $event, $_param)
51c8ecffd7SMichael Große    {
52c8ecffd7SMichael Große        $html = $this->getEditorHtml();
53c8ecffd7SMichael Große
54c8ecffd7SMichael Große        /** @var Form $form */
55c8ecffd7SMichael Große        $form = $event->data;
56c8ecffd7SMichael Große        $pos = $form->findPositionByAttribute('id', 'wiki__editbar'); // insert the form before the main buttons
57c8ecffd7SMichael Große        $form->addHTML($html, $pos);
58c8ecffd7SMichael Große
59c8ecffd7SMichael Große        return true;
6087dc1344SAndreas Gohr    }
6187dc1344SAndreas Gohr
6287dc1344SAndreas Gohr    /**
6387dc1344SAndreas Gohr     * Enhance the editing form with structural data editing
6487dc1344SAndreas Gohr     *
65c8ecffd7SMichael Große     * TODO: Remove this after HTML_EDITFORM_OUTPUT is no longer released in DokuWiki stable
66c8ecffd7SMichael Große     *
67*5e29103aSannda     * @param Event $event event object by reference
6887dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
6987dc1344SAndreas Gohr     *                           handler was registered]
7087dc1344SAndreas Gohr     * @return bool
7187dc1344SAndreas Gohr     */
727234bfb1Ssplitbrain    public function handleEditform(Event $event, $param)
73d6d97f60SAnna Dabrowska    {
74c8ecffd7SMichael Große        $html = $this->getEditorHtml();
75c8ecffd7SMichael Große
76c8ecffd7SMichael Große        /** @var Doku_Form $form */
77c8ecffd7SMichael Große        $form = $event->data;
78c8ecffd7SMichael Große        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
79c8ecffd7SMichael Große        $form->insertElement($pos, $html);
80c8ecffd7SMichael Große
81c8ecffd7SMichael Große        return true;
82c8ecffd7SMichael Große    }
83c8ecffd7SMichael Große
84c8ecffd7SMichael Große    /**
85c8ecffd7SMichael Große     * @return string
86c8ecffd7SMichael Große     */
87c8ecffd7SMichael Große    private function getEditorHtml()
88c8ecffd7SMichael Große    {
8987dc1344SAndreas Gohr        global $ID;
9087dc1344SAndreas Gohr
91025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
9287dc1344SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
9387dc1344SAndreas Gohr
9487dc1344SAndreas Gohr        $html = '';
9587dc1344SAndreas Gohr        foreach ($tables as $table) {
9687dc1344SAndreas Gohr            $html .= $this->createForm($table);
9787dc1344SAndreas Gohr        }
9887dc1344SAndreas Gohr
99c8ecffd7SMichael Große        return "<div class=\"struct_entry_form\">$html</div>";
10087dc1344SAndreas Gohr    }
10187dc1344SAndreas Gohr
10287dc1344SAndreas Gohr    /**
10387dc1344SAndreas Gohr     * Create the form to edit schemadata
10487dc1344SAndreas Gohr     *
10587dc1344SAndreas Gohr     * @param string $tablename
10687dc1344SAndreas Gohr     * @return string The HTML for this schema's form
10787dc1344SAndreas Gohr     */
108d6d97f60SAnna Dabrowska    protected function createForm($tablename)
109d6d97f60SAnna Dabrowska    {
11087dc1344SAndreas Gohr        global $ID;
11187dc1344SAndreas Gohr        global $REV;
11287dc1344SAndreas Gohr        global $INPUT;
11387dc1344SAndreas Gohr        if (auth_quickaclcheck($ID) == AUTH_READ) return '';
11487dc1344SAndreas Gohr        if (checklock($ID)) return '';
115387ee210SAnna Dabrowska        $ts = $REV ?: time();
1164cd5cc28SAnna Dabrowska        $schema = AccessTable::getPageAccess($tablename, $ID, $ts);
1176ebbbb8eSAndreas Gohr        if (!$schema->getSchema()->isEditable()) {
1186ebbbb8eSAndreas Gohr            return '';
1196ebbbb8eSAndreas Gohr        }
12087dc1344SAndreas Gohr        $schemadata = $schema->getData();
12187dc1344SAndreas Gohr
12287dc1344SAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
12387dc1344SAndreas Gohr        if (isset($structdata[$tablename])) {
12487dc1344SAndreas Gohr            $postdata = $structdata[$tablename];
12587dc1344SAndreas Gohr        } else {
1267234bfb1Ssplitbrain            $postdata = [];
12787dc1344SAndreas Gohr        }
12887dc1344SAndreas Gohr
12987dc1344SAndreas Gohr        // we need a short, unique identifier to use in the cookie. this should be good enough
1307234bfb1Ssplitbrain        $schemaid = 'SRCT' . substr(str_replace(['+', '/'], '', base64_encode(sha1($tablename, true))), 0, 5);
13187dc1344SAndreas Gohr        $html = '<fieldset data-schema="' . $schemaid . '">';
132f632e8a0SMichael Große        $html .= '<legend>' . hsc($schema->getSchema()->getTranslatedLabel()) . '</legend>';
13387dc1344SAndreas Gohr        foreach ($schemadata as $field) {
13487dc1344SAndreas Gohr            $label = $field->getColumn()->getLabel();
13587dc1344SAndreas Gohr            if (isset($postdata[$label])) {
13687dc1344SAndreas Gohr                // posted data trumps stored data
137095f02a2SRandolf Rotta                $data = $postdata[$label];
1384844cefcSRandolf Rotta                if (is_array($data)) {
1394844cefcSRandolf Rotta                    $data = array_map("cleanText", $data);
1404844cefcSRandolf Rotta                } else {
1414844cefcSRandolf Rotta                    $data = cleanText($data);
1424844cefcSRandolf Rotta                }
143095f02a2SRandolf Rotta                $field->setValue($data, true);
14487dc1344SAndreas Gohr            }
14587dc1344SAndreas Gohr            $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]");
14687dc1344SAndreas Gohr        }
14787dc1344SAndreas Gohr        $html .= '</fieldset>';
14887dc1344SAndreas Gohr
14987dc1344SAndreas Gohr        return $html;
15087dc1344SAndreas Gohr    }
15187dc1344SAndreas Gohr
15287dc1344SAndreas Gohr    /**
15387dc1344SAndreas Gohr     * Create the input field
15487dc1344SAndreas Gohr     *
15587dc1344SAndreas Gohr     * @param Value $field
15687dc1344SAndreas Gohr     * @param String $name field's name
15787dc1344SAndreas Gohr     * @return string
15887dc1344SAndreas Gohr     */
159d6d97f60SAnna Dabrowska    public function makeField(Value $field, $name)
160d6d97f60SAnna Dabrowska    {
16187dc1344SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
16287dc1344SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
16387dc1344SAndreas Gohr        $class = $hint ? 'hashint' : '';
16487dc1344SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
16587dc1344SAndreas Gohr
166ee983135SMichael Große        $id = uniqid('struct__', false);
167ee983135SMichael Große        $input = $field->getValueEditor($name, $id);
16887dc1344SAndreas Gohr
16987dc1344SAndreas Gohr        // we keep all the custom form stuff the field might produce, but hide it
17087dc1344SAndreas Gohr        if (!$field->getColumn()->isVisibleInEditor()) {
17187dc1344SAndreas Gohr            $hide = 'style="display:none"';
17287dc1344SAndreas Gohr        } else {
17387dc1344SAndreas Gohr            $hide = '';
17487dc1344SAndreas Gohr        }
17587dc1344SAndreas Gohr
176ee983135SMichael Große        $html = '<div class="field">';
177ee983135SMichael Große        $html .= "<label $hide data-column=\"$colname\" for=\"$id\">";
17887dc1344SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
17987dc1344SAndreas Gohr        $html .= '</label>';
180ee983135SMichael Große        $html .= "<span class=\"input\">$input</span>";
181ee983135SMichael Große        $html .= '</div>';
18287dc1344SAndreas Gohr
18387dc1344SAndreas Gohr        return $html;
18487dc1344SAndreas Gohr    }
18587dc1344SAndreas Gohr}
18687dc1344SAndreas Gohr
18787dc1344SAndreas Gohr// vim:ts=4:sw=4:et:
188