xref: /plugin/struct/action/edit.php (revision 87dc1344fb5ebba89b6bf5f09fe8e70f0793e751)
1*87dc1344SAndreas Gohr<?php
2*87dc1344SAndreas Gohr/**
3*87dc1344SAndreas Gohr * DokuWiki Plugin struct (Action Component)
4*87dc1344SAndreas Gohr *
5*87dc1344SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*87dc1344SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7*87dc1344SAndreas Gohr */
8*87dc1344SAndreas Gohr
9*87dc1344SAndreas Gohr// must be run within Dokuwiki
10*87dc1344SAndreas Gohrif(!defined('DOKU_INC')) die();
11*87dc1344SAndreas Gohr
12*87dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
13*87dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
14*87dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTableData;
15*87dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\ValidationResult;
16*87dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Validator;
17*87dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
18*87dc1344SAndreas Gohr
19*87dc1344SAndreas Gohr/**
20*87dc1344SAndreas Gohr * Class action_plugin_struct_entry
21*87dc1344SAndreas Gohr *
22*87dc1344SAndreas Gohr * Handles adding struct forms to the default editor
23*87dc1344SAndreas Gohr */
24*87dc1344SAndreas Gohrclass action_plugin_struct_edit extends DokuWiki_Action_Plugin {
25*87dc1344SAndreas Gohr
26*87dc1344SAndreas Gohr    /**
27*87dc1344SAndreas Gohr     * @var string The form name we use to transfer schema data
28*87dc1344SAndreas Gohr     */
29*87dc1344SAndreas Gohr    protected static $VAR = 'struct_schema_data';
30*87dc1344SAndreas Gohr
31*87dc1344SAndreas Gohr    /**
32*87dc1344SAndreas Gohr     * Registers a callback function for a given event
33*87dc1344SAndreas Gohr     *
34*87dc1344SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
35*87dc1344SAndreas Gohr     * @return void
36*87dc1344SAndreas Gohr     */
37*87dc1344SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
38*87dc1344SAndreas Gohr        // add the struct editor to the edit form;
39*87dc1344SAndreas Gohr        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
40*87dc1344SAndreas Gohr    }
41*87dc1344SAndreas Gohr
42*87dc1344SAndreas Gohr    /**
43*87dc1344SAndreas Gohr     * Enhance the editing form with structural data editing
44*87dc1344SAndreas Gohr     *
45*87dc1344SAndreas Gohr     * @param Doku_Event $event event object by reference
46*87dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
47*87dc1344SAndreas Gohr     *                           handler was registered]
48*87dc1344SAndreas Gohr     * @return bool
49*87dc1344SAndreas Gohr     */
50*87dc1344SAndreas Gohr    public function handle_editform(Doku_Event $event, $param) {
51*87dc1344SAndreas Gohr        global $ID;
52*87dc1344SAndreas Gohr
53*87dc1344SAndreas Gohr        $assignments = new Assignments();
54*87dc1344SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
55*87dc1344SAndreas Gohr
56*87dc1344SAndreas Gohr        $html = '';
57*87dc1344SAndreas Gohr        foreach($tables as $table) {
58*87dc1344SAndreas Gohr            $html .= $this->createForm($table);
59*87dc1344SAndreas Gohr        }
60*87dc1344SAndreas Gohr
61*87dc1344SAndreas Gohr        /** @var Doku_Form $form */
62*87dc1344SAndreas Gohr        $form = $event->data;
63*87dc1344SAndreas Gohr        $html = "<div class=\"struct_entry_form\">$html</div>";
64*87dc1344SAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
65*87dc1344SAndreas Gohr        $form->insertElement($pos, $html);
66*87dc1344SAndreas Gohr
67*87dc1344SAndreas Gohr        return true;
68*87dc1344SAndreas Gohr    }
69*87dc1344SAndreas Gohr
70*87dc1344SAndreas Gohr    /**
71*87dc1344SAndreas Gohr     * Create the form to edit schemadata
72*87dc1344SAndreas Gohr     *
73*87dc1344SAndreas Gohr     * @param string $tablename
74*87dc1344SAndreas Gohr     * @return string The HTML for this schema's form
75*87dc1344SAndreas Gohr     */
76*87dc1344SAndreas Gohr    protected function createForm($tablename) {
77*87dc1344SAndreas Gohr        global $ID;
78*87dc1344SAndreas Gohr        global $REV;
79*87dc1344SAndreas Gohr        global $INPUT;
80*87dc1344SAndreas Gohr        if (auth_quickaclcheck($ID) == AUTH_READ) return '';
81*87dc1344SAndreas Gohr        if (checklock($ID)) return '';
82*87dc1344SAndreas Gohr        $schema = AccessTable::byTableName($tablename, $ID, $REV);
83*87dc1344SAndreas Gohr        $schemadata = $schema->getData();
84*87dc1344SAndreas Gohr
85*87dc1344SAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
86*87dc1344SAndreas Gohr        if(isset($structdata[$tablename])) {
87*87dc1344SAndreas Gohr            $postdata = $structdata[$tablename];
88*87dc1344SAndreas Gohr        } else {
89*87dc1344SAndreas Gohr            $postdata = array();
90*87dc1344SAndreas Gohr        }
91*87dc1344SAndreas Gohr
92*87dc1344SAndreas Gohr        // we need a short, unique identifier to use in the cookie. this should be good enough
93*87dc1344SAndreas Gohr        $schemaid = 'SRCT'.substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
94*87dc1344SAndreas Gohr        $html = '<fieldset data-schema="' . $schemaid . '">';
95*87dc1344SAndreas Gohr        $html .= '<legend>' . hsc($tablename) . '</legend>';
96*87dc1344SAndreas Gohr        foreach($schemadata as $field) {
97*87dc1344SAndreas Gohr            $label = $field->getColumn()->getLabel();
98*87dc1344SAndreas Gohr            if(isset($postdata[$label])) {
99*87dc1344SAndreas Gohr                // posted data trumps stored data
100*87dc1344SAndreas Gohr                $field->setValue($postdata[$label]);
101*87dc1344SAndreas Gohr            }
102*87dc1344SAndreas Gohr            $html .=  $this->makeField($field, self::$VAR . "[$tablename][$label]");
103*87dc1344SAndreas Gohr        }
104*87dc1344SAndreas Gohr        $html .= '</fieldset>';
105*87dc1344SAndreas Gohr
106*87dc1344SAndreas Gohr        return $html;
107*87dc1344SAndreas Gohr    }
108*87dc1344SAndreas Gohr
109*87dc1344SAndreas Gohr    /**
110*87dc1344SAndreas Gohr     * Create the input field
111*87dc1344SAndreas Gohr     *
112*87dc1344SAndreas Gohr     * @param Value $field
113*87dc1344SAndreas Gohr     * @param String $name field's name
114*87dc1344SAndreas Gohr     * @return string
115*87dc1344SAndreas Gohr     */
116*87dc1344SAndreas Gohr    public function makeField(Value $field, $name) {
117*87dc1344SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
118*87dc1344SAndreas Gohr        $hint  = hsc($field->getColumn()->getTranslatedHint());
119*87dc1344SAndreas Gohr        $class = $hint ? 'hashint' : '';
120*87dc1344SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
121*87dc1344SAndreas Gohr
122*87dc1344SAndreas Gohr        $input = $field->getValueEditor($name);
123*87dc1344SAndreas Gohr
124*87dc1344SAndreas Gohr        // we keep all the custom form stuff the field might produce, but hide it
125*87dc1344SAndreas Gohr        if(!$field->getColumn()->isVisibleInEditor()) {
126*87dc1344SAndreas Gohr            $hide = 'style="display:none"';
127*87dc1344SAndreas Gohr        } else {
128*87dc1344SAndreas Gohr            $hide = '';
129*87dc1344SAndreas Gohr        }
130*87dc1344SAndreas Gohr
131*87dc1344SAndreas Gohr        $html = '';
132*87dc1344SAndreas Gohr        $html .= "<label $hide data-column=\"$colname\">";
133*87dc1344SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
134*87dc1344SAndreas Gohr        $html .= "<span class=\"input\">$input</span>";
135*87dc1344SAndreas Gohr        $html .= '</label>';
136*87dc1344SAndreas Gohr
137*87dc1344SAndreas Gohr        return $html;
138*87dc1344SAndreas Gohr    }
139*87dc1344SAndreas Gohr}
140*87dc1344SAndreas Gohr
141*87dc1344SAndreas Gohr// vim:ts=4:sw=4:et:
142