1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\Form\Form;
6
7/**
8 * DokuWiki Subscribe Interface
9 *
10 * @package dokuwiki\Ui
11 */
12class Subscribe extends Ui
13{
14    /**
15     * Display the subscribe form
16     *
17     * @return void
18     * @author Adrian Lang <lang@cosmocode.de>
19     *
20     */
21    public function show()
22    {
23        global $INPUT;
24        global $INFO;
25        global $ID;
26        global $lang;
27        global $conf;
28        $stime_days = $conf['subscribe_time'] / 60 / 60 / 24;
29
30        // print intro
31        echo p_locale_xhtml('subscr_form');
32
33        // list up current subscriptions
34        echo '<h2>' . $lang['subscr_m_current_header'] . '</h2>';
35        echo '<div class="level2">';
36        if ($INFO['subscribed'] === false) {
37            echo '<p>' . $lang['subscr_m_not_subscribed'] . '</p>';
38        } else {
39            echo '<ul>';
40            foreach ($INFO['subscribed'] as $sub) {
41                echo '<li><div class="li">';
42                if ($sub['target'] !== $ID) {
43                    echo '<code class="ns">' . hsc(prettyprint_id($sub['target'])) . '</code>';
44                } else {
45                    echo '<code class="page">' . hsc(prettyprint_id($sub['target'])) . '</code>';
46                }
47                $sstl = sprintf($lang['subscr_style_' . $sub['style']], $stime_days);
48                if (!$sstl) $sstl = hsc($sub['style']);
49                echo ' (' . $sstl . ') ';
50
51                echo '<a href="' . wl(
52                    $ID,
53                    [
54                            'do' => 'subscribe',
55                            'sub_target' => $sub['target'],
56                            'sub_style' => $sub['style'],
57                            'sub_action' => 'unsubscribe',
58                            'sectok' => getSecurityToken()
59                        ]
60                ) .
61                    '" class="unsubscribe">' . $lang['subscr_m_unsubscribe'] .
62                    '</a></div></li>';
63            }
64            echo '</ul>';
65        }
66        echo '</div>';
67
68        // Add new subscription form
69        echo '<h2>' . $lang['subscr_m_new_header'] . '</h2>';
70        echo '<div class="level2">';
71        $ns = getNS($ID) . ':';
72        $targets = [
73            $ID => '<code class="page">' . prettyprint_id($ID) . '</code>',
74            $ns => '<code class="ns">' . prettyprint_id($ns) . '</code>',
75        ];
76        $styles = [
77            'every' => $lang['subscr_style_every'],
78            'digest' => sprintf($lang['subscr_style_digest'], $stime_days),
79            'list' => sprintf($lang['subscr_style_list'], $stime_days),
80        ];
81
82        // create the form
83        $form = new Form(['id' => 'subscribe__form']);
84        $form->addTagOpen('div')->addClass('no');
85        $form->setHiddenField('id', $ID);
86        $form->setHiddenField('do', 'subscribe');
87        $form->setHiddenField('sub_action', 'subscribe');
88
89        $form->addFieldsetOpen($lang['subscr_m_subscribe']);
90
91        $value = (array_key_exists($INPUT->post->str('sub_target'), $targets)) ?
92            $INPUT->str('sub_target') : key($targets);
93        foreach ($targets as $val => $label) {
94            $data = ($value === $val) ? ['checked' => 'checked'] : [];
95            $form->addRadioButton('sub_target', $label)->val($val)->attrs($data);
96        }
97        $form->addFieldsetClose();
98
99        $form->addFieldsetOpen($lang['subscr_m_receive']);
100
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        echo $form->toHTML('Subscribe');
113
114        echo '</div>';
115    }
116}
117