xref: /plugin/struct/action/edit.php (revision c8ecffd7150191bafc0bcbbb6c04db8146f82840)
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
10*c8ecffd7SMichael Großeuse dokuwiki\Form\Form;
1187dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
1287dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
1387dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
1487dc1344SAndreas Gohr
1587dc1344SAndreas Gohr/**
1687dc1344SAndreas Gohr * Class action_plugin_struct_entry
1787dc1344SAndreas Gohr *
1887dc1344SAndreas Gohr * Handles adding struct forms to the default editor
1987dc1344SAndreas Gohr */
20d6d97f60SAnna Dabrowskaclass action_plugin_struct_edit extends DokuWiki_Action_Plugin
21d6d97f60SAnna Dabrowska{
2287dc1344SAndreas Gohr
2387dc1344SAndreas Gohr    /**
2487dc1344SAndreas Gohr     * @var string The form name we use to transfer schema data
2587dc1344SAndreas Gohr     */
2687dc1344SAndreas Gohr    protected static $VAR = 'struct_schema_data';
2787dc1344SAndreas Gohr
2887dc1344SAndreas Gohr    /**
2987dc1344SAndreas Gohr     * Registers a callback function for a given event
3087dc1344SAndreas Gohr     *
3187dc1344SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
3287dc1344SAndreas Gohr     * @return void
3387dc1344SAndreas Gohr     */
34d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
35d6d97f60SAnna Dabrowska    {
3687dc1344SAndreas Gohr        // add the struct editor to the edit form;
37748e747fSAnna Dabrowska        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handleEditform');
38*c8ecffd7SMichael Große        $controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'addFromData');
39*c8ecffd7SMichael Große    }
40*c8ecffd7SMichael Große
41*c8ecffd7SMichael Große    /**
42*c8ecffd7SMichael Große     * Adds the html for the struct editors to the edit from
43*c8ecffd7SMichael Große     *
44*c8ecffd7SMichael Große     * Handles the FORM_EDIT_OUTPUT event
45*c8ecffd7SMichael Große     *
46*c8ecffd7SMichael Große     * @return bool
47*c8ecffd7SMichael Große     */
48*c8ecffd7SMichael Große    public function addFromData(Doku_Event $event, $_param)
49*c8ecffd7SMichael Große    {
50*c8ecffd7SMichael Große        $html = $this->getEditorHtml();
51*c8ecffd7SMichael Große
52*c8ecffd7SMichael Große        /** @var Form $form */
53*c8ecffd7SMichael Große        $form = $event->data;
54*c8ecffd7SMichael Große        $pos = $form->findPositionByAttribute( 'id', 'wiki__editbar'); // insert the form before the main buttons
55*c8ecffd7SMichael Große        $form->addHTML($html, $pos);
56*c8ecffd7SMichael Große
57*c8ecffd7SMichael Große        return true;
5887dc1344SAndreas Gohr    }
5987dc1344SAndreas Gohr
6087dc1344SAndreas Gohr    /**
6187dc1344SAndreas Gohr     * Enhance the editing form with structural data editing
6287dc1344SAndreas Gohr     *
63*c8ecffd7SMichael Große     * TODO: Remove this after HTML_EDITFORM_OUTPUT is no longer released in DokuWiki stable
64*c8ecffd7SMichael Große     *
6587dc1344SAndreas Gohr     * @param Doku_Event $event event object by reference
6687dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
6787dc1344SAndreas Gohr     *                           handler was registered]
6887dc1344SAndreas Gohr     * @return bool
6987dc1344SAndreas Gohr     */
70748e747fSAnna Dabrowska    public function handleEditform(Doku_Event $event, $param)
71d6d97f60SAnna Dabrowska    {
72*c8ecffd7SMichael Große        $html = $this->getEditorHtml();
73*c8ecffd7SMichael Große
74*c8ecffd7SMichael Große        /** @var Doku_Form $form */
75*c8ecffd7SMichael Große        $form = $event->data;
76*c8ecffd7SMichael Große        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
77*c8ecffd7SMichael Große        $form->insertElement($pos, $html);
78*c8ecffd7SMichael Große
79*c8ecffd7SMichael Große        return true;
80*c8ecffd7SMichael Große    }
81*c8ecffd7SMichael Große
82*c8ecffd7SMichael Große    /**
83*c8ecffd7SMichael Große     * @return string
84*c8ecffd7SMichael Große     */
85*c8ecffd7SMichael Große    private function getEditorHtml()
86*c8ecffd7SMichael Große    {
8787dc1344SAndreas Gohr        global $ID;
8887dc1344SAndreas Gohr
89025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
9087dc1344SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
9187dc1344SAndreas Gohr
9287dc1344SAndreas Gohr        $html = '';
9387dc1344SAndreas Gohr        foreach ($tables as $table) {
9487dc1344SAndreas Gohr            $html .= $this->createForm($table);
9587dc1344SAndreas Gohr        }
9687dc1344SAndreas Gohr
97*c8ecffd7SMichael Große        return "<div class=\"struct_entry_form\">$html</div>";
9887dc1344SAndreas Gohr    }
9987dc1344SAndreas Gohr
10087dc1344SAndreas Gohr    /**
10187dc1344SAndreas Gohr     * Create the form to edit schemadata
10287dc1344SAndreas Gohr     *
10387dc1344SAndreas Gohr     * @param string $tablename
10487dc1344SAndreas Gohr     * @return string The HTML for this schema's form
10587dc1344SAndreas Gohr     */
106d6d97f60SAnna Dabrowska    protected function createForm($tablename)
107d6d97f60SAnna Dabrowska    {
10887dc1344SAndreas Gohr        global $ID;
10987dc1344SAndreas Gohr        global $REV;
11087dc1344SAndreas Gohr        global $INPUT;
11187dc1344SAndreas Gohr        if (auth_quickaclcheck($ID) == AUTH_READ) return '';
11287dc1344SAndreas Gohr        if (checklock($ID)) return '';
113387ee210SAnna Dabrowska        $ts = $REV ?: time();
1144cd5cc28SAnna Dabrowska        $schema = AccessTable::getPageAccess($tablename, $ID, $ts);
1156ebbbb8eSAndreas Gohr        if (!$schema->getSchema()->isEditable()) {
1166ebbbb8eSAndreas Gohr            return '';
1176ebbbb8eSAndreas Gohr        }
11887dc1344SAndreas Gohr        $schemadata = $schema->getData();
11987dc1344SAndreas Gohr
12087dc1344SAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
12187dc1344SAndreas Gohr        if (isset($structdata[$tablename])) {
12287dc1344SAndreas Gohr            $postdata = $structdata[$tablename];
12387dc1344SAndreas Gohr        } else {
12487dc1344SAndreas Gohr            $postdata = array();
12587dc1344SAndreas Gohr        }
12687dc1344SAndreas Gohr
12787dc1344SAndreas Gohr        // we need a short, unique identifier to use in the cookie. this should be good enough
12887dc1344SAndreas Gohr        $schemaid = 'SRCT' . substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
12987dc1344SAndreas Gohr        $html = '<fieldset data-schema="' . $schemaid . '">';
130f632e8a0SMichael Große        $html .= '<legend>' . hsc($schema->getSchema()->getTranslatedLabel()) . '</legend>';
13187dc1344SAndreas Gohr        foreach ($schemadata as $field) {
13287dc1344SAndreas Gohr            $label = $field->getColumn()->getLabel();
13387dc1344SAndreas Gohr            if (isset($postdata[$label])) {
13487dc1344SAndreas Gohr                // posted data trumps stored data
135095f02a2SRandolf Rotta                $data = $postdata[$label];
1364844cefcSRandolf Rotta                if (is_array($data)) {
1374844cefcSRandolf Rotta                    $data = array_map("cleanText", $data);
1384844cefcSRandolf Rotta                } else {
1394844cefcSRandolf Rotta                    $data = cleanText($data);
1404844cefcSRandolf Rotta                }
141095f02a2SRandolf Rotta                $field->setValue($data, true);
14287dc1344SAndreas Gohr            }
14387dc1344SAndreas Gohr            $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]");
14487dc1344SAndreas Gohr        }
14587dc1344SAndreas Gohr        $html .= '</fieldset>';
14687dc1344SAndreas Gohr
14787dc1344SAndreas Gohr        return $html;
14887dc1344SAndreas Gohr    }
14987dc1344SAndreas Gohr
15087dc1344SAndreas Gohr    /**
15187dc1344SAndreas Gohr     * Create the input field
15287dc1344SAndreas Gohr     *
15387dc1344SAndreas Gohr     * @param Value $field
15487dc1344SAndreas Gohr     * @param String $name field's name
15587dc1344SAndreas Gohr     * @return string
15687dc1344SAndreas Gohr     */
157d6d97f60SAnna Dabrowska    public function makeField(Value $field, $name)
158d6d97f60SAnna Dabrowska    {
15987dc1344SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
16087dc1344SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
16187dc1344SAndreas Gohr        $class = $hint ? 'hashint' : '';
16287dc1344SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
16387dc1344SAndreas Gohr
164ee983135SMichael Große        $id = uniqid('struct__', false);
165ee983135SMichael Große        $input = $field->getValueEditor($name, $id);
16687dc1344SAndreas Gohr
16787dc1344SAndreas Gohr        // we keep all the custom form stuff the field might produce, but hide it
16887dc1344SAndreas Gohr        if (!$field->getColumn()->isVisibleInEditor()) {
16987dc1344SAndreas Gohr            $hide = 'style="display:none"';
17087dc1344SAndreas Gohr        } else {
17187dc1344SAndreas Gohr            $hide = '';
17287dc1344SAndreas Gohr        }
17387dc1344SAndreas Gohr
174ee983135SMichael Große        $html = '<div class="field">';
175ee983135SMichael Große        $html .= "<label $hide data-column=\"$colname\" for=\"$id\">";
17687dc1344SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
17787dc1344SAndreas Gohr        $html .= '</label>';
178ee983135SMichael Große        $html .= "<span class=\"input\">$input</span>";
179ee983135SMichael Große        $html .= '</div>';
18087dc1344SAndreas Gohr
18187dc1344SAndreas Gohr        return $html;
18287dc1344SAndreas Gohr    }
18387dc1344SAndreas Gohr}
18487dc1344SAndreas Gohr
18587dc1344SAndreas Gohr// vim:ts=4:sw=4:et:
186