Failed Conditions
Pull Request — master (#3198)
by
unknown
03:14
created

Editor   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 198
rs 10
c 0
b 0
f 0
wmc 27
lcom 0
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
F show() 0 158 24
A addTextarea() 0 18 3
1
<?php
2
3
namespace dokuwiki\Ui;
4
5
use dokuwiki\Extension\Event;
6
use dokuwiki\Form\Form;
7
8
/**
9
 * DokuWiki Page Editor
10
 *
11
 * @package dokuwiki\Ui
12
 */
13
class Editor extends Ui
14
{
15
    /**
16
     * Display the Edit Window
17
     * preprocess edit form data
18
     *
19
     * @author   Andreas Gohr <[email protected]>
20
     *
21
     * @triggers HTML_EDIT_FORMSELECTION
22
     * @triggers HTML_EDITFORM_OUTPUT
23
     * @return void
24
     */
25
    public function show()
26
    {
27
        global $INPUT;
28
        global $ID;
29
        global $REV;
30
        global $DATE;
31
        global $PRE;
32
        global $SUF;
33
        global $INFO;
34
        global $SUM;
35
        global $lang;
36
        global $conf;
37
        global $TEXT;
38
39
        global $license;
40
41
        if ($INPUT->has('changecheck')) {
42
            $check = $INPUT->str('changecheck');
43
        } elseif (!$INFO['exists']) {
44
            // $TEXT has been loaded from page template
45
            $check = md5('');
46
        } else {
47
            $check = md5($TEXT);
48
        }
49
        $mod = (md5($TEXT) !== $check);
50
51
        $wr = $INFO['writable'] && !$INFO['locked'];
52
53
        // intro locale text (edit, rditrev, or read)
54
        if ($wr) {
55
            $intro = ($REV) ? 'editrev' : 'edit';
56
        } else {
57
            // check pseudo action 'source'
58
            if (!actionOK('source')) {
59
                msg('Command disabled: source', -1);
60
                return;
61
            }
62
            $intro = 'read';
63
        }
64
65
        // create the Editor form
66
        $form = new Form(['action' => wl($ID), 'id' => 'dw__editform']);
67
        $form->setHiddenField('id', $ID);
68
        $form->setHiddenField('rev', $REV);
69
        $form->setHiddenField('date', $DATE);
70
        $form->setHiddenField('prefix', $PRE .'.');
71
        $form->setHiddenField('suffix', $SUF);
72
        $form->setHiddenField('changecheck', $check);
73
74
        // prepare data for HTML_EDIT_FORMSELECTION event
75
        $data = array(
76
            'form' => $form,
77
            'wr'   => $wr,
78
            'media_manager' => true,
79
            'target' => ($INPUT->has('target') && $wr) ? $INPUT->str('target') : 'section',
80
            'intro_locale' => $intro,
81
        );
82
83
        if ($data['target'] !== 'section') {
84
            // Only emit event if page is writable, section edit data is valid and
85
            // edit target is not section.
86
            Event::createAndTrigger('HTML_EDIT_FORMSELECTION', $data, [$this,'addTextarea'], true);
87
        } else {
88
            $this->addTextarea($data);
89
        }
90
91
        $form->setHiddenField('target', $data['target']);
92
93
        if ($INPUT->has('hid')) {
94
            $form->setHiddenField('hid', $INPUT->str('hid'));
95
        }
96
        if ($INPUT->has('codeblockOffset')) {
97
            $form->setHiddenField('codeblockOffset', $INPUT->str('codeblockOffset'));
98
        }
99
100
        $form->addTagOpen('div')->id('wiki__editbar')->addClass('editBar');
101
102
        $form->addTagOpen('div')->id('size__ctl');
103
        $form->addTagClose('div');
104
105
        if ($wr) {
106
            // add edit buttons: save, preview, cancel
107
            $form->addTagOpen('div')->addClass('editButtons');
108
            $form->addButton('do[save]', $lang['btn_save'])->attr('type', 'submit')
109
                ->attrs(['accesskey' => 's', 'tabindex' => '4'])
110
                ->id('edbtn__save');
111
            $form->addButton('do[preview]', $lang['btn_preview'])->attr('type', 'submit')
112
                ->attrs(['accesskey' => 'p', 'tabindex' => '5'])
113
                ->id('edbtn__preview');
114
            $form->addButton('do[cancel]', $lang['btn_cancel'])->attr('type', 'submit')
115
                ->attrs(['tabindex' => '6']);
116
            $form->addTagClose('div'); // close div editButtons class
117
118
            // add a textbox for edit summary
119
            $form->addTagOpen('div')->addClass('summary');
120
            $input = $form->addTextInput('summary', $lang['summary'])
121
                ->attrs(['size' => '50', 'tabindex' => '2'])
122
                ->id('edit__summary')->addClass('edit')
123
                ->val($SUM);
124
            $input->getLabel()->attr('class', 'nowrap');
125
126
            // adds a checkbox for minor edits for logged in users
127
            if ($conf['useacl'] && $_SERVER['REMOTE_USER']) {
128
                $form->addCheckbox('minor', $lang['minoredit'])->id('edit__minoredit')->addClass('nowrap')->val('1');
129
            }
130
            $form->addTagClose('div'); // close div summary class
131
        }
132
133
        $form->addTagClose('div'); // close div editBar class
134
135
        // license note
136
        if ($wr && $conf['license']) {
137
            $form->addTagOpen('div')->addClass('license');
138
            $form->addHTML($lang['licenseok']
139
                .' <a href="'.$license[$conf['license']]['url'].'" rel="license" class="urlextern"'
140
                . ($conf['target']['extern']) ? ' target="'.$conf['target']['extern'].'">' : '>'
141
                . $license[$conf['license']]['name'].'</a>'
142
            );
143
            $form->addTagClose('div');
144
        }
145
146
        // start editor html output
147
        if ($wr) {
148
            // sets changed to true when previewed
149
            echo '<script>/*<![CDATA[*/'. DOKU_LF;
150
            echo 'textChanged = ' . ($mod ? 'true' : 'false');
151
            echo '/*!]]>*/</script>' . DOKU_LF;
152
        }
153
154
        // print intro locale text (edit, rditrev, or read.txt)
155
        if (isset($data['intro_locale'])) {
156
            echo p_locale_xhtml($data['intro_locale']);
157
        }
158
159
        echo '<div class="editBox" role="application">';
160
161
        echo '<div class="toolbar group">';
162
        echo '<div id="tool__bar" class="tool__bar">';
163
        if ($wr && $data['media_manager']) {
164
            echo '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.$INFO['namespace'].'" target="_blank">';
165
            echo $lang['mediaselect'];
166
            echo '</a>';
167
        }
168
        echo '</div>';
169
        echo '</div>';
170
171
        echo '<div id="draft__status" class="draft__status">';
172
        $draft = new \dokuwiki\Draft($ID, $INFO['client']);
173
        if ($draft->isDraftAvailable()) {
174
            echo $draft->getDraftMessage();
175
        }
176
        echo '</div>'.DOKU_LF;
177
178
        // emit HTML_EDITFORM_OUTPUT event, print the form
179
        Event::createAndTrigger('HTML_EDITFORM_OUTPUT', $form, 'html_form_output', false);
180
181
        echo '</div>'.DOKU_LF; // close div editBox class
182
    }
183
184
    /**
185
     * Display the default edit form (textarea)
186
     *
187
     * the default action for HTML_EDIT_FORMSELECTION.
188
     *
189
     * @param mixed[] $data
190
     */
191
    public function addTextarea(&$data)
192
    {
193
        global $TEXT;
194
195
        if ($data['target'] !== 'section') {
196
            msg('No editor for edit target '. hsc($pdata['target']) .' found.', -1);
0 ignored issues
show
Bug introduced by
The variable $pdata does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
197
        }
198
199
        // set textarea attributes
200
        $attr = array('tabindex' => '1');
201
        if (!$data['wr']) $attr['readonly'] = 'readonly';
202
        $attr['dir']  = 'auto';
203
        $attr['cols'] = '80';
204
        $attr['rows'] = '10';
205
206
        $data['form']->addTextarea('wikitext','')->attrs($attr)->val($TEXT)
207
                ->id('wiki__text')->addClass('edit');
208
    }
209
210
}
211