xref: /plugin/struct/action/edit.php (revision 025cb9da4274aac00be48202c8eb49058f2dd283)
187dc1344SAndreas Gohr<?php
287dc1344SAndreas Gohr/**
387dc1344SAndreas Gohr * DokuWiki Plugin struct (Action Component)
487dc1344SAndreas Gohr *
587dc1344SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
687dc1344SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
787dc1344SAndreas Gohr */
887dc1344SAndreas Gohr
987dc1344SAndreas Gohr// must be run within Dokuwiki
1087dc1344SAndreas Gohrif(!defined('DOKU_INC')) die();
1187dc1344SAndreas Gohr
1287dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
1387dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
1487dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
1587dc1344SAndreas Gohr
1687dc1344SAndreas Gohr/**
1787dc1344SAndreas Gohr * Class action_plugin_struct_entry
1887dc1344SAndreas Gohr *
1987dc1344SAndreas Gohr * Handles adding struct forms to the default editor
2087dc1344SAndreas Gohr */
2187dc1344SAndreas Gohrclass action_plugin_struct_edit extends DokuWiki_Action_Plugin {
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     */
3487dc1344SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
3587dc1344SAndreas Gohr        // add the struct editor to the edit form;
3687dc1344SAndreas Gohr        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
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     */
4787dc1344SAndreas Gohr    public function handle_editform(Doku_Event $event, $param) {
4887dc1344SAndreas Gohr        global $ID;
4987dc1344SAndreas Gohr
50*025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
5187dc1344SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
5287dc1344SAndreas Gohr
5387dc1344SAndreas Gohr        $html = '';
5487dc1344SAndreas Gohr        foreach($tables as $table) {
5587dc1344SAndreas Gohr            $html .= $this->createForm($table);
5687dc1344SAndreas Gohr        }
5787dc1344SAndreas Gohr
5887dc1344SAndreas Gohr        /** @var Doku_Form $form */
5987dc1344SAndreas Gohr        $form = $event->data;
6087dc1344SAndreas Gohr        $html = "<div class=\"struct_entry_form\">$html</div>";
6187dc1344SAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
6287dc1344SAndreas Gohr        $form->insertElement($pos, $html);
6387dc1344SAndreas Gohr
6487dc1344SAndreas Gohr        return true;
6587dc1344SAndreas Gohr    }
6687dc1344SAndreas Gohr
6787dc1344SAndreas Gohr    /**
6887dc1344SAndreas Gohr     * Create the form to edit schemadata
6987dc1344SAndreas Gohr     *
7087dc1344SAndreas Gohr     * @param string $tablename
7187dc1344SAndreas Gohr     * @return string The HTML for this schema's form
7287dc1344SAndreas Gohr     */
7387dc1344SAndreas Gohr    protected function createForm($tablename) {
7487dc1344SAndreas Gohr        global $ID;
7587dc1344SAndreas Gohr        global $REV;
7687dc1344SAndreas Gohr        global $INPUT;
7787dc1344SAndreas Gohr        if(auth_quickaclcheck($ID) == AUTH_READ) return '';
7887dc1344SAndreas Gohr        if(checklock($ID)) return '';
7987dc1344SAndreas Gohr        $schema = AccessTable::byTableName($tablename, $ID, $REV);
806ebbbb8eSAndreas Gohr        if(!$schema->getSchema()->isEditable()) {
816ebbbb8eSAndreas Gohr            return '';
826ebbbb8eSAndreas Gohr        }
8387dc1344SAndreas Gohr        $schemadata = $schema->getData();
8487dc1344SAndreas Gohr
8587dc1344SAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
8687dc1344SAndreas Gohr        if(isset($structdata[$tablename])) {
8787dc1344SAndreas Gohr            $postdata = $structdata[$tablename];
8887dc1344SAndreas Gohr        } else {
8987dc1344SAndreas Gohr            $postdata = array();
9087dc1344SAndreas Gohr        }
9187dc1344SAndreas Gohr
9287dc1344SAndreas Gohr        // we need a short, unique identifier to use in the cookie. this should be good enough
9387dc1344SAndreas Gohr        $schemaid = 'SRCT' . substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
9487dc1344SAndreas Gohr        $html = '<fieldset data-schema="' . $schemaid . '">';
9587dc1344SAndreas Gohr        $html .= '<legend>' . hsc($tablename) . '</legend>';
9687dc1344SAndreas Gohr        foreach($schemadata as $field) {
9787dc1344SAndreas Gohr            $label = $field->getColumn()->getLabel();
9887dc1344SAndreas Gohr            if(isset($postdata[$label])) {
9987dc1344SAndreas Gohr                // posted data trumps stored data
1004bb1f072SMichael Grosse                $field->setValue($postdata[$label], true);
10187dc1344SAndreas Gohr            }
10287dc1344SAndreas Gohr            $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]");
10387dc1344SAndreas Gohr        }
10487dc1344SAndreas Gohr        $html .= '</fieldset>';
10587dc1344SAndreas Gohr
10687dc1344SAndreas Gohr        return $html;
10787dc1344SAndreas Gohr    }
10887dc1344SAndreas Gohr
10987dc1344SAndreas Gohr    /**
11087dc1344SAndreas Gohr     * Create the input field
11187dc1344SAndreas Gohr     *
11287dc1344SAndreas Gohr     * @param Value $field
11387dc1344SAndreas Gohr     * @param String $name field's name
11487dc1344SAndreas Gohr     * @return string
11587dc1344SAndreas Gohr     */
11687dc1344SAndreas Gohr    public function makeField(Value $field, $name) {
11787dc1344SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
11887dc1344SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
11987dc1344SAndreas Gohr        $class = $hint ? 'hashint' : '';
12087dc1344SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
12187dc1344SAndreas Gohr
12287dc1344SAndreas Gohr        $input = $field->getValueEditor($name);
12387dc1344SAndreas Gohr
12487dc1344SAndreas Gohr        // we keep all the custom form stuff the field might produce, but hide it
12587dc1344SAndreas Gohr        if(!$field->getColumn()->isVisibleInEditor()) {
12687dc1344SAndreas Gohr            $hide = 'style="display:none"';
12787dc1344SAndreas Gohr        } else {
12887dc1344SAndreas Gohr            $hide = '';
12987dc1344SAndreas Gohr        }
13087dc1344SAndreas Gohr
13187dc1344SAndreas Gohr        $html = '';
13287dc1344SAndreas Gohr        $html .= "<label $hide data-column=\"$colname\">";
13387dc1344SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
13487dc1344SAndreas Gohr        $html .= "<span class=\"input\">$input</span>";
13587dc1344SAndreas Gohr        $html .= '</label>';
13687dc1344SAndreas Gohr
13787dc1344SAndreas Gohr        return $html;
13887dc1344SAndreas Gohr    }
13987dc1344SAndreas Gohr}
14087dc1344SAndreas Gohr
14187dc1344SAndreas Gohr// vim:ts=4:sw=4:et:
142