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

Subscribe::show()   C

Complexity

Conditions 11
Paths 72

Size

Total Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 72
nop 0
dl 0
loc 95
rs 5.9624
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 Subscribe Interface
10
 *
11
 * @package dokuwiki\Ui
12
 */
13
class Subscribe extends Ui
14
{
15
    /**
16
     * Display the subscribe form
17
     *
18
     * @author Adrian Lang <[email protected]>
19
     *
20
     * @triggers HTML_SUBSCRIBEFORM_OUTPUT
21
     * @return void
22
     */
23
    function show()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
    {
25
        global $INPUT;
26
        global $INFO;
27
        global $ID;
28
        global $lang;
29
        global $conf;
30
        $stime_days = $conf['subscribe_time'] / 60 / 60 / 24;
31
32
        // print intro
33
        echo p_locale_xhtml('subscr_form');
34
35
        // list up current subscriptions
36
        echo '<h2>'.$lang['subscr_m_current_header'].'</h2>';
37
        echo '<div class="level2">';
38
        if ($INFO['subscribed'] === false) {
39
            echo '<p>'.$lang['subscr_m_not_subscribed'].'</p>';
40
        } else {
41
            echo '<ul>';
42
            foreach ($INFO['subscribed'] as $sub) {
43
                echo '<li><div class="li">';
44
                if ($sub['target'] !== $ID) {
45
                    echo '<code class="ns">'.hsc(prettyprint_id($sub['target'])).'</code>';
46
                } else {
47
                    echo '<code class="page">'.hsc(prettyprint_id($sub['target'])).'</code>';
48
                }
49
                $sstl = sprintf($lang['subscr_style_'.$sub['style']], $stime_days);
50
                if (!$sstl) $sstl = hsc($sub['style']);
51
                echo ' ('.$sstl.') ';
52
53
                echo '<a href="'.wl(
54
                    $ID,
55
                    array(
56
                         'do'        => 'subscribe',
57
                         'sub_target'=> $sub['target'],
58
                         'sub_style' => $sub['style'],
59
                         'sub_action'=> 'unsubscribe',
60
                         'sectok'    => getSecurityToken()
61
                    )
62
                ).
63
                    '" class="unsubscribe">'.$lang['subscr_m_unsubscribe'].
64
                    '</a></div></li>';
65
            }
66
            echo '</ul>';
67
        }
68
        echo '</div>';
69
70
        // Add new subscription form
71
        echo '<h2>'.$lang['subscr_m_new_header'].'</h2>';
72
        echo '<div class="level2">';
73
        $ns      = getNS($ID).':';
74
        $targets = [
75
            $ID => '<code class="page">'.prettyprint_id($ID).'</code>',
76
            $ns => '<code class="ns">'.prettyprint_id($ns).'</code>',
77
        ];
78
        $styles = [
79
            'every'  => $lang['subscr_style_every'],
80
            'digest' => sprintf($lang['subscr_style_digest'], $stime_days),
81
            'list'   => sprintf($lang['subscr_style_list'], $stime_days),
82
        ];
83
84
        // create the form
85
        $form = new Form(['id' => 'subscribe__form']);
86
        $form->addTagOpen('div')->addClass('no');
87
        $form->setHiddenField('id', $ID);
88
        $form->setHiddenField('do', 'subscribe');
89
        $form->setHiddenField('sub_action', 'subscribe');
90
91
        $form->addFieldsetOpen($lang['subscr_m_subscribe']);
92
        $value = (array_key_exists($INPUT->post->str('sub_target'), $targets)) ?
93
                 $INPUT->str('sub_target') : key($targets);
94
        foreach ($targets as $val => $label) {
95
            $data = ($value === $val) ? ['checked' => 'checked'] : [];
96
            $form->addRadioButton('sub_target', $label)->val($val)->attrs($data);
97
        }
98
        $form->addFieldsetClose();
99
100
        $form->addFieldsetOpen($lang['subscr_m_receive']);
101
        $value = (array_key_exists($INPUT->post->str('sub_style'), $styles)) ?
102
                 $INPUT->str('sub_style') : key($styles);
103
        foreach ($styles as $val => $label) {
104
            $data = ($value === $val) ? ['checked' => 'checked'] : [];
105
            $form->addRadioButton('sub_style', $label)->val($val)->attrs($data);
106
        }
107
        $form->addFieldsetClose();
108
109
        $form->addButton('do[subscribe]', $lang['subscr_m_subscribe'])->attr('type', 'submit');
110
        $form->addTagClose('div');
111
112
        // emit HTML_SUBSCRIBEFORM_OUTPUT event
113
        Event::createAndTrigger('HTML_SUBSCRIBEFORM_OUTPUT', $form, null, false);
114
        print $form->toHTML();
115
116
        echo '</div>'.DOKU_LF;
117
    }
118
119
}
120