xref: /plugin/struct/action/edit.php (revision 4cd5cc28e2bf004aed676e9b1f46cc188a4b2240)
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
1087dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
1187dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
1287dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
1387dc1344SAndreas Gohr
1487dc1344SAndreas Gohr/**
1587dc1344SAndreas Gohr * Class action_plugin_struct_entry
1687dc1344SAndreas Gohr *
1787dc1344SAndreas Gohr * Handles adding struct forms to the default editor
1887dc1344SAndreas Gohr */
19d6d97f60SAnna Dabrowskaclass action_plugin_struct_edit extends DokuWiki_Action_Plugin
20d6d97f60SAnna Dabrowska{
2187dc1344SAndreas Gohr
2287dc1344SAndreas Gohr    /**
2387dc1344SAndreas Gohr     * @var string The form name we use to transfer schema data
2487dc1344SAndreas Gohr     */
2587dc1344SAndreas Gohr    protected static $VAR = 'struct_schema_data';
2687dc1344SAndreas Gohr
2787dc1344SAndreas Gohr    /**
2887dc1344SAndreas Gohr     * Registers a callback function for a given event
2987dc1344SAndreas Gohr     *
3087dc1344SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
3187dc1344SAndreas Gohr     * @return void
3287dc1344SAndreas Gohr     */
33d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
34d6d97f60SAnna Dabrowska    {
3587dc1344SAndreas Gohr        // add the struct editor to the edit form;
36748e747fSAnna Dabrowska        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handleEditform');
3787dc1344SAndreas Gohr    }
3887dc1344SAndreas Gohr
3987dc1344SAndreas Gohr    /**
4087dc1344SAndreas Gohr     * Enhance the editing form with structural data editing
4187dc1344SAndreas Gohr     *
4287dc1344SAndreas Gohr     * @param Doku_Event $event event object by reference
4387dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
4487dc1344SAndreas Gohr     *                           handler was registered]
4587dc1344SAndreas Gohr     * @return bool
4687dc1344SAndreas Gohr     */
47748e747fSAnna Dabrowska    public function handleEditform(Doku_Event $event, $param)
48d6d97f60SAnna Dabrowska    {
4987dc1344SAndreas Gohr        global $ID;
5087dc1344SAndreas Gohr
51025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
5287dc1344SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
5387dc1344SAndreas Gohr
5487dc1344SAndreas Gohr        $html = '';
5587dc1344SAndreas Gohr        foreach ($tables as $table) {
5687dc1344SAndreas Gohr            $html .= $this->createForm($table);
5787dc1344SAndreas Gohr        }
5887dc1344SAndreas Gohr
5987dc1344SAndreas Gohr        /** @var Doku_Form $form */
6087dc1344SAndreas Gohr        $form = $event->data;
6187dc1344SAndreas Gohr        $html = "<div class=\"struct_entry_form\">$html</div>";
6287dc1344SAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
6387dc1344SAndreas Gohr        $form->insertElement($pos, $html);
6487dc1344SAndreas Gohr
6587dc1344SAndreas Gohr        return true;
6687dc1344SAndreas Gohr    }
6787dc1344SAndreas Gohr
6887dc1344SAndreas Gohr    /**
6987dc1344SAndreas Gohr     * Create the form to edit schemadata
7087dc1344SAndreas Gohr     *
7187dc1344SAndreas Gohr     * @param string $tablename
7287dc1344SAndreas Gohr     * @return string The HTML for this schema's form
7387dc1344SAndreas Gohr     */
74d6d97f60SAnna Dabrowska    protected function createForm($tablename)
75d6d97f60SAnna Dabrowska    {
7687dc1344SAndreas Gohr        global $ID;
7787dc1344SAndreas Gohr        global $REV;
7887dc1344SAndreas Gohr        global $INPUT;
7987dc1344SAndreas Gohr        if (auth_quickaclcheck($ID) == AUTH_READ) return '';
8087dc1344SAndreas Gohr        if (checklock($ID)) return '';
81387ee210SAnna Dabrowska        $ts = $REV ?: time();
82*4cd5cc28SAnna Dabrowska        $schema = AccessTable::getPageAccess($tablename, $ID, $ts);
836ebbbb8eSAndreas Gohr        if (!$schema->getSchema()->isEditable()) {
846ebbbb8eSAndreas Gohr            return '';
856ebbbb8eSAndreas Gohr        }
8687dc1344SAndreas Gohr        $schemadata = $schema->getData();
8787dc1344SAndreas Gohr
8887dc1344SAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
8987dc1344SAndreas Gohr        if (isset($structdata[$tablename])) {
9087dc1344SAndreas Gohr            $postdata = $structdata[$tablename];
9187dc1344SAndreas Gohr        } else {
9287dc1344SAndreas Gohr            $postdata = array();
9387dc1344SAndreas Gohr        }
9487dc1344SAndreas Gohr
9587dc1344SAndreas Gohr        // we need a short, unique identifier to use in the cookie. this should be good enough
9687dc1344SAndreas Gohr        $schemaid = 'SRCT' . substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
9787dc1344SAndreas Gohr        $html = '<fieldset data-schema="' . $schemaid . '">';
98f632e8a0SMichael Große        $html .= '<legend>' . hsc($schema->getSchema()->getTranslatedLabel()) . '</legend>';
9987dc1344SAndreas Gohr        foreach ($schemadata as $field) {
10087dc1344SAndreas Gohr            $label = $field->getColumn()->getLabel();
10187dc1344SAndreas Gohr            if (isset($postdata[$label])) {
10287dc1344SAndreas Gohr                // posted data trumps stored data
103095f02a2SRandolf Rotta                $data = $postdata[$label];
1044844cefcSRandolf Rotta                if (is_array($data)) {
1054844cefcSRandolf Rotta                    $data = array_map("cleanText", $data);
1064844cefcSRandolf Rotta                } else {
1074844cefcSRandolf Rotta                    $data = cleanText($data);
1084844cefcSRandolf Rotta                }
109095f02a2SRandolf Rotta                $field->setValue($data, true);
11087dc1344SAndreas Gohr            }
11187dc1344SAndreas Gohr            $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]");
11287dc1344SAndreas Gohr        }
11387dc1344SAndreas Gohr        $html .= '</fieldset>';
11487dc1344SAndreas Gohr
11587dc1344SAndreas Gohr        return $html;
11687dc1344SAndreas Gohr    }
11787dc1344SAndreas Gohr
11887dc1344SAndreas Gohr    /**
11987dc1344SAndreas Gohr     * Create the input field
12087dc1344SAndreas Gohr     *
12187dc1344SAndreas Gohr     * @param Value $field
12287dc1344SAndreas Gohr     * @param String $name field's name
12387dc1344SAndreas Gohr     * @return string
12487dc1344SAndreas Gohr     */
125d6d97f60SAnna Dabrowska    public function makeField(Value $field, $name)
126d6d97f60SAnna Dabrowska    {
12787dc1344SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
12887dc1344SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
12987dc1344SAndreas Gohr        $class = $hint ? 'hashint' : '';
13087dc1344SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
13187dc1344SAndreas Gohr
132ee983135SMichael Große        $id = uniqid('struct__', false);
133ee983135SMichael Große        $input = $field->getValueEditor($name, $id);
13487dc1344SAndreas Gohr
13587dc1344SAndreas Gohr        // we keep all the custom form stuff the field might produce, but hide it
13687dc1344SAndreas Gohr        if (!$field->getColumn()->isVisibleInEditor()) {
13787dc1344SAndreas Gohr            $hide = 'style="display:none"';
13887dc1344SAndreas Gohr        } else {
13987dc1344SAndreas Gohr            $hide = '';
14087dc1344SAndreas Gohr        }
14187dc1344SAndreas Gohr
142ee983135SMichael Große        $html = '<div class="field">';
143ee983135SMichael Große        $html .= "<label $hide data-column=\"$colname\" for=\"$id\">";
14487dc1344SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
14587dc1344SAndreas Gohr        $html .= '</label>';
146ee983135SMichael Große        $html .= "<span class=\"input\">$input</span>";
147ee983135SMichael Große        $html .= '</div>';
14887dc1344SAndreas Gohr
14987dc1344SAndreas Gohr        return $html;
15087dc1344SAndreas Gohr    }
15187dc1344SAndreas Gohr}
15287dc1344SAndreas Gohr
15387dc1344SAndreas Gohr// vim:ts=4:sw=4:et:
154