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

Editor::show()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 158

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
nc 55302
nop 0
dl 0
loc 158
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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(['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'] && $INPUT->server->str('REMOTE_USER')) {
128
                $form->addHTML(' ');
129
                $form->addCheckbox('minor', $lang['minoredit'])->id('edit__minoredit')->addClass('nowrap')->val('1');
130
            }
131
            $form->addTagClose('div'); // close div summary class
132
        }
133
134
        $form->addTagClose('div'); // close div editBar class
135
136
        // license note
137
        if ($wr && $conf['license']) {
138
            $form->addTagOpen('div')->addClass('license');
139
            $form->addHTML($lang['licenseok']
140
                .' <a href="'.$license[$conf['license']]['url'].'" rel="license" class="urlextern"'
141
                . ($conf['target']['extern']) ? ' target="'.$conf['target']['extern'].'">' : '>'
142
                . $license[$conf['license']]['name'].'</a>'
143
            );
144
            $form->addTagClose('div');
145
        }
146
147
        // start editor html output
148
        if ($wr) {
149
            // sets changed to true when previewed
150
            echo '<script>/*<![CDATA[*/'.'textChanged = '. ($mod ? 'true' : 'false') .'/*!]]>*/</script>';
151
        }
152
153
        // print intro locale text (edit, rditrev, or read.txt)
154
        if (isset($data['intro_locale'])) {
155
            echo p_locale_xhtml($data['intro_locale']);
156
        }
157
158
        echo '<div class="editBox" role="application">';
159
160
        echo '<div class="toolbar group">';
161
        echo '<div id="tool__bar" class="tool__bar">';
162
        if ($wr && $data['media_manager']) {
163
            echo '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.$INFO['namespace'].'" target="_blank">';
164
            echo $lang['mediaselect'];
165
            echo '</a>';
166
        }
167
        echo '</div>';
168
        echo '</div>';
169
170
        echo '<div id="draft__status" class="draft__status">';
171
        $draft = new \dokuwiki\Draft($ID, $INFO['client']);
172
        if ($draft->isDraftAvailable()) {
173
            echo $draft->getDraftMessage();
174
        }
175
        echo '</div>';
176
177
        // emit HTML_EDITFORM_OUTPUT event
178
        Event::createAndTrigger('HTML_EDITFORM_OUTPUT', $form, null, false);
179
        echo $form->toHTML();
180
181
        echo '</div>'; // 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($data['target']) .' found.', -1);
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