xref: /plugin/struct/action/edit.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
187dc1344SAndreas Gohr<?php
2*d6d97f60SAnna 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 Gohr// must be run within Dokuwiki
1187dc1344SAndreas Gohrif (!defined('DOKU_INC')) die();
1287dc1344SAndreas Gohr
1387dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
1487dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
1587dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
1687dc1344SAndreas Gohr
1787dc1344SAndreas Gohr/**
1887dc1344SAndreas Gohr * Class action_plugin_struct_entry
1987dc1344SAndreas Gohr *
2087dc1344SAndreas Gohr * Handles adding struct forms to the default editor
2187dc1344SAndreas Gohr */
22*d6d97f60SAnna Dabrowskaclass action_plugin_struct_edit extends DokuWiki_Action_Plugin
23*d6d97f60SAnna Dabrowska{
2487dc1344SAndreas Gohr
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     *
3387dc1344SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
3487dc1344SAndreas Gohr     * @return void
3587dc1344SAndreas Gohr     */
36*d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
37*d6d97f60SAnna Dabrowska    {
3887dc1344SAndreas Gohr        // add the struct editor to the edit form;
3987dc1344SAndreas Gohr        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
4087dc1344SAndreas Gohr    }
4187dc1344SAndreas Gohr
4287dc1344SAndreas Gohr    /**
4387dc1344SAndreas Gohr     * Enhance the editing form with structural data editing
4487dc1344SAndreas Gohr     *
4587dc1344SAndreas Gohr     * @param Doku_Event $event event object by reference
4687dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
4787dc1344SAndreas Gohr     *                           handler was registered]
4887dc1344SAndreas Gohr     * @return bool
4987dc1344SAndreas Gohr     */
50*d6d97f60SAnna Dabrowska    public function handle_editform(Doku_Event $event, $param)
51*d6d97f60SAnna Dabrowska    {
5287dc1344SAndreas Gohr        global $ID;
5387dc1344SAndreas Gohr
54025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
5587dc1344SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
5687dc1344SAndreas Gohr
5787dc1344SAndreas Gohr        $html = '';
5887dc1344SAndreas Gohr        foreach ($tables as $table) {
5987dc1344SAndreas Gohr            $html .= $this->createForm($table);
6087dc1344SAndreas Gohr        }
6187dc1344SAndreas Gohr
6287dc1344SAndreas Gohr        /** @var Doku_Form $form */
6387dc1344SAndreas Gohr        $form = $event->data;
6487dc1344SAndreas Gohr        $html = "<div class=\"struct_entry_form\">$html</div>";
6587dc1344SAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
6687dc1344SAndreas Gohr        $form->insertElement($pos, $html);
6787dc1344SAndreas Gohr
6887dc1344SAndreas Gohr        return true;
6987dc1344SAndreas Gohr    }
7087dc1344SAndreas Gohr
7187dc1344SAndreas Gohr    /**
7287dc1344SAndreas Gohr     * Create the form to edit schemadata
7387dc1344SAndreas Gohr     *
7487dc1344SAndreas Gohr     * @param string $tablename
7587dc1344SAndreas Gohr     * @return string The HTML for this schema's form
7687dc1344SAndreas Gohr     */
77*d6d97f60SAnna Dabrowska    protected function createForm($tablename)
78*d6d97f60SAnna Dabrowska    {
7987dc1344SAndreas Gohr        global $ID;
8087dc1344SAndreas Gohr        global $REV;
8187dc1344SAndreas Gohr        global $INPUT;
8287dc1344SAndreas Gohr        if (auth_quickaclcheck($ID) == AUTH_READ) return '';
8387dc1344SAndreas Gohr        if (checklock($ID)) return '';
84387ee210SAnna Dabrowska        $ts = $REV ?: time();
8569f7ec8fSAnna Dabrowska        $schema = AccessTable::byTableName($tablename, $ID, $ts);
866ebbbb8eSAndreas Gohr        if (!$schema->getSchema()->isEditable()) {
876ebbbb8eSAndreas Gohr            return '';
886ebbbb8eSAndreas Gohr        }
8987dc1344SAndreas Gohr        $schemadata = $schema->getData();
9087dc1344SAndreas Gohr
9187dc1344SAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
9287dc1344SAndreas Gohr        if (isset($structdata[$tablename])) {
9387dc1344SAndreas Gohr            $postdata = $structdata[$tablename];
9487dc1344SAndreas Gohr        } else {
9587dc1344SAndreas Gohr            $postdata = array();
9687dc1344SAndreas Gohr        }
9787dc1344SAndreas Gohr
9887dc1344SAndreas Gohr        // we need a short, unique identifier to use in the cookie. this should be good enough
9987dc1344SAndreas Gohr        $schemaid = 'SRCT' . substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
10087dc1344SAndreas Gohr        $html = '<fieldset data-schema="' . $schemaid . '">';
101f632e8a0SMichael Große        $html .= '<legend>' . hsc($schema->getSchema()->getTranslatedLabel()) . '</legend>';
10287dc1344SAndreas Gohr        foreach ($schemadata as $field) {
10387dc1344SAndreas Gohr            $label = $field->getColumn()->getLabel();
10487dc1344SAndreas Gohr            if (isset($postdata[$label])) {
10587dc1344SAndreas Gohr                // posted data trumps stored data
106095f02a2SRandolf Rotta                $data = $postdata[$label];
1074844cefcSRandolf Rotta                if (is_array($data)) {
1084844cefcSRandolf Rotta                    $data = array_map("cleanText", $data);
1094844cefcSRandolf Rotta                } else {
1104844cefcSRandolf Rotta                    $data = cleanText($data);
1114844cefcSRandolf Rotta                }
112095f02a2SRandolf Rotta                $field->setValue($data, true);
11387dc1344SAndreas Gohr            }
11487dc1344SAndreas Gohr            $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]");
11587dc1344SAndreas Gohr        }
11687dc1344SAndreas Gohr        $html .= '</fieldset>';
11787dc1344SAndreas Gohr
11887dc1344SAndreas Gohr        return $html;
11987dc1344SAndreas Gohr    }
12087dc1344SAndreas Gohr
12187dc1344SAndreas Gohr    /**
12287dc1344SAndreas Gohr     * Create the input field
12387dc1344SAndreas Gohr     *
12487dc1344SAndreas Gohr     * @param Value $field
12587dc1344SAndreas Gohr     * @param String $name field's name
12687dc1344SAndreas Gohr     * @return string
12787dc1344SAndreas Gohr     */
128*d6d97f60SAnna Dabrowska    public function makeField(Value $field, $name)
129*d6d97f60SAnna Dabrowska    {
13087dc1344SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
13187dc1344SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
13287dc1344SAndreas Gohr        $class = $hint ? 'hashint' : '';
13387dc1344SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
13487dc1344SAndreas Gohr
135ee983135SMichael Große        $id = uniqid('struct__', false);
136ee983135SMichael Große        $input = $field->getValueEditor($name, $id);
13787dc1344SAndreas Gohr
13887dc1344SAndreas Gohr        // we keep all the custom form stuff the field might produce, but hide it
13987dc1344SAndreas Gohr        if (!$field->getColumn()->isVisibleInEditor()) {
14087dc1344SAndreas Gohr            $hide = 'style="display:none"';
14187dc1344SAndreas Gohr        } else {
14287dc1344SAndreas Gohr            $hide = '';
14387dc1344SAndreas Gohr        }
14487dc1344SAndreas Gohr
145ee983135SMichael Große        $html = '<div class="field">';
146ee983135SMichael Große        $html .= "<label $hide data-column=\"$colname\" for=\"$id\">";
14787dc1344SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
14887dc1344SAndreas Gohr        $html .= '</label>';
149ee983135SMichael Große        $html .= "<span class=\"input\">$input</span>";
150ee983135SMichael Große        $html .= '</div>';
15187dc1344SAndreas Gohr
15287dc1344SAndreas Gohr        return $html;
15387dc1344SAndreas Gohr    }
15487dc1344SAndreas Gohr}
15587dc1344SAndreas Gohr
15687dc1344SAndreas Gohr// vim:ts=4:sw=4:et:
157