aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Ui/Editor.php
blob: 065f1a4f70e2a8a93828568b9674a09e652612cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php

namespace dokuwiki\Ui;

use dokuwiki\Draft;
use dokuwiki\Extension\Event;
use dokuwiki\Form\Form;

/**
 * DokuWiki Page Editor
 *
 * @package dokuwiki\Ui
 */
class Editor extends Ui
{
    /**
     * Display the Edit Window
     * preprocess edit form data
     *
     * @return void
     * @author   Andreas Gohr <andi@splitbrain.org>
     *
     * @triggers EDIT_FORM_ADDTEXTAREA
     */
    public function show()
    {
        global $INPUT;
        global $ID;
        global $REV;
        global $DATE;
        global $PRE;
        global $SUF;
        global $INFO;
        global $SUM;
        global $lang;
        global $conf;
        global $TEXT;

        global $license;

        if ($INPUT->has('changecheck')) {
            $check = $INPUT->str('changecheck');
        } elseif (!$INFO['exists']) {
            // $TEXT has been loaded from page template
            $check = md5('');
        } else {
            $check = md5($TEXT);
        }
        $mod = (md5($TEXT) !== $check);

        $wr = $INFO['writable'] && !$INFO['locked'];

        // intro locale text (edit, rditrev, or read)
        if ($wr) {
            $intro = ($REV) ? 'editrev' : 'edit';
        } else {
            // check pseudo action 'source'
            if (!actionOK('source')) {
                msg('Command disabled: source', -1);
                return;
            }
            $intro = 'read';
        }

        // create the Editor form
        $form = new Form(['id' => 'dw__editform']);
        $form->setHiddenField('id', $ID);
        $form->setHiddenField('rev', $REV);
        $form->setHiddenField('date', $DATE);
        $form->setHiddenField('prefix', $PRE . '.');
        $form->setHiddenField('suffix', $SUF);
        $form->setHiddenField('changecheck', $check);

        // prepare data for EDIT_FORM_ALTERNATE event
        $data = [
            'form' => $form,
            'wr' => $wr,
            'media_manager' => true,
            'target' => ($INPUT->has('target') && $wr) ? $INPUT->str('target') : 'section',
            'intro_locale' => $intro
        ];

        if ($data['target'] !== 'section') {
            // Only emit event if page is writable, section edit data is valid and
            // edit target is not section.
            Event::createAndTrigger('EDIT_FORM_ADDTEXTAREA', $data, [$this, 'addTextarea'], true);
        } else {
            $this->addTextarea($data);
        }

        $form->setHiddenField('target', $data['target']);

        if ($INPUT->has('hid')) {
            $form->setHiddenField('hid', $INPUT->str('hid'));
        }
        if ($INPUT->has('codeblockOffset')) {
            $form->setHiddenField('codeblockOffset', $INPUT->str('codeblockOffset'));
        }

        $form->addTagOpen('div')->id('wiki__editbar')->addClass('editBar');

        $form->addTagOpen('div')->id('size__ctl');
        $form->addTagClose('div');

        if ($wr) {
            // add edit buttons: save, preview, cancel
            $form->addTagOpen('div')->addClass('editButtons');
            $form->addButton('do[save]', $lang['btn_save'])->attr('type', 'submit')
                ->attrs(['accesskey' => 's', 'tabindex' => '4'])
                ->id('edbtn__save');
            $form->addButton('do[preview]', $lang['btn_preview'])->attr('type', 'submit')
                ->attrs(['accesskey' => 'p', 'tabindex' => '5'])
                ->id('edbtn__preview');
            $form->addButton('do[cancel]', $lang['btn_cancel'])->attr('type', 'submit')
                ->attrs(['tabindex' => '6']);
            $form->addTagClose('div'); // close div editButtons class

            // add a textbox for edit summary
            $form->addTagOpen('div')->addClass('summary');
            $input = $form->addTextInput('summary', $lang['summary'])
                ->attrs(['size' => '50', 'tabindex' => '2'])
                ->id('edit__summary')->addClass('edit')
                ->val($SUM);
            $input->getLabel()->attr('class', 'nowrap');

            // adds a checkbox for minor edits for logged in users
            if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
                $form->addHTML(' ');
                $form->addCheckbox('minor', $lang['minoredit'])->id('edit__minoredit')->addClass('nowrap')->val('1');
            }
            $form->addTagClose('div'); // close div summary class
        }

        $form->addTagClose('div'); // close div editBar class

        // license note
        if ($wr && $conf['license']) {
            $attr = [
                'href' => $license[$conf['license']]['url'],
                'rel' => 'license',
                'class' => 'urlextern',
                'target' => $conf['target']['extern'] ?: ''
            ];
            $form->addTagOpen('div')->addClass('license');
            $form->addHTML($lang['licenseok']
                . ' <a ' . buildAttributes($attr, true) . '>' . $license[$conf['license']]['name'] . '</a>');
            $form->addTagClose('div');
        }

        // start editor html output
        if ($wr) {
            // sets changed to true when previewed
            tpl_inlineScript('textChanged = ' . ($mod ? 'true' : 'false') . ';');
        }

        // print intro locale text (edit, rditrev, or read.txt)
        if (isset($data['intro_locale'])) {
            echo p_locale_xhtml($data['intro_locale']);
        }

        echo '<div class="editBox" role="application">';

        echo '<div class="toolbar group">';
        echo '<div id="tool__bar" class="tool__bar">';
        if ($wr && $data['media_manager']) {
            echo '<a href="' . DOKU_BASE . 'lib/exe/mediamanager.php?ns=' . $INFO['namespace'] . '" target="_blank">';
            echo $lang['mediaselect'];
            echo '</a>';
        }
        echo '</div>';
        echo '</div>';

        echo '<div id="draft__status" class="draft__status">';
        $draft = new Draft($ID, $INFO['client']);
        if ($draft->isDraftAvailable()) {
            echo $draft->getDraftMessage();
        }
        echo '</div>';

        echo $form->toHTML('Edit');

        echo '</div>'; // close div editBox class
    }

    /**
     * Display the default edit form (textarea)
     *
     * the default action for EDIT_FORM_ADDTEXTAREA
     *
     * @param array{wr: bool, media_manager: bool, target: string, intro_locale: string, form: Form} $data
     */
    public function addTextarea(&$data)
    {
        global $TEXT;

        if ($data['target'] !== 'section') {
            msg('No editor for edit target ' . hsc($data['target']) . ' found.', -1);
        }

        // set textarea attributes
        $attr = ['tabindex' => '1'];
        if (!$data['wr']) $attr['readonly'] = 'readonly';
        $attr['dir'] = 'auto';
        $attr['cols'] = '80';
        $attr['rows'] = '10';

        $data['form']->addTextarea('wikitext', '')->attrs($attr)->val($TEXT)
            ->id('wiki__text')->addClass('edit');
    }
}