Failed Conditions
Pull Request — master (#3198)
by
unknown
02:54
created

Editor::show()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 159

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
nc 55302
nop 0
dl 0
loc 159
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->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
179
        Event::createAndTrigger('HTML_EDITFORM_OUTPUT', $form, null, false);
180
        echo $form->toHTML();
181
182
        echo '</div>'.DOKU_LF; // close div editBox class
183
    }
184
185
    /**
186
     * Display the default edit form (textarea)
187
     *
188
     * the default action for HTML_EDIT_FORMSELECTION.
189
     *
190
     * @param mixed[] $data
191
     */
192
    public function addTextarea(&$data)
193
    {
194
        global $TEXT;
195
196
        if ($data['target'] !== 'section') {
197
            msg('No editor for edit target '. hsc($data['target']) .' found.', -1);
198
        }
199
200
        // set textarea attributes
201
        $attr = array('tabindex' => '1');
202
        if (!$data['wr']) $attr['readonly'] = 'readonly';
203
        $attr['dir']  = 'auto';
204
        $attr['cols'] = '80';
205
        $attr['rows'] = '10';
206
207
        $data['form']->addTextarea('wikitext','')->attrs($attr)->val($TEXT)
208
                ->id('wiki__text')->addClass('edit');
209
    }
210
211
}
212