xref: /dokuwiki/lib/plugins/popularity/admin.php (revision 5d8c9d422c83ef31e1acbe6e37664185196c3016)
1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\Form\Form;
5
6/**
7 * Popularity Feedback Plugin
8 *
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @author     Andreas Gohr <andi@splitbrain.org>
11 */
12class admin_plugin_popularity extends AdminPlugin
13{
14    /** @var helper_plugin_popularity */
15    protected $helper;
16    protected $sentStatus;
17
18    /**
19     * admin_plugin_popularity constructor.
20     */
21    public function __construct()
22    {
23        $this->helper = $this->loadHelper('popularity', false);
24    }
25
26    /**
27     * return prompt for admin menu
28     * @param $language
29     * @return string
30     */
31    public function getMenuText($language)
32    {
33        return $this->getLang('name');
34    }
35
36    /**
37     * return sort order for position in admin menu
38     */
39    public function getMenuSort()
40    {
41        return 2000;
42    }
43
44    /**
45     * Accessible for managers
46     */
47    public function forAdminOnly()
48    {
49        return false;
50    }
51
52
53    /**
54     * handle user request
55     */
56    public function handle()
57    {
58        global $INPUT;
59
60        //Send the data
61        if ($INPUT->has('data')) {
62            if (!checkSecurityToken()) return;
63            $this->sentStatus = $this->helper->sendData($INPUT->str('data'));
64            if ($this->sentStatus === '') {
65                //Update the last time we sent the data
66                touch($this->helper->popularityLastSubmitFile);
67            }
68            //Deal with the autosubmit option
69            $this->enableAutosubmit($INPUT->has('autosubmit'));
70        }
71    }
72
73    /**
74     * Enable or disable autosubmit
75     * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it.
76     */
77    protected function enableAutosubmit($enable)
78    {
79        if ($enable) {
80            io_saveFile($this->helper->autosubmitFile, ' ');
81        } else {
82            @unlink($this->helper->autosubmitFile);
83        }
84    }
85
86    /**
87     * Output HTML form
88     */
89    public function html()
90    {
91        global $INPUT;
92
93        if (! $INPUT->has('data')) {
94            echo $this->locale_xhtml('intro');
95            //If there was an error the last time we tried to autosubmit, warn the user
96            if ($this->helper->isAutoSubmitEnabled()) {
97                if (file_exists($this->helper->autosubmitErrorFile)) {
98                    echo $this->getLang('autosubmitError');
99                    echo io_readFile($this->helper->autosubmitErrorFile);
100                }
101            }
102            flush();
103            echo $this->buildForm('server');
104            //Print the last time the data was sent
105            $lastSent = $this->helper->lastSentTime();
106            if ($lastSent !== 0) {
107                echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent);
108            }
109        } elseif ($this->sentStatus === '') {
110            //If we successfully send the data
111            echo $this->locale_xhtml('submitted');
112        } else {
113            //If we failed to submit the data, try directly with the browser
114            echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />';
115            echo $this->getLang('submitDirectly');
116            echo $this->buildForm('browser', $INPUT->str('data'));
117        }
118    }
119
120
121    /**
122     * Build the form which presents the data to be sent
123     * @param string $submissionMode How is the data supposed to be sent? (may be: 'browser' or 'server')
124     * @param string $data   The popularity data, if it has already been computed. NULL otherwise.
125     * @return string The form, as an html string
126     */
127    protected function buildForm($submissionMode, $data = null)
128    {
129        $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script());
130        if (is_null($data)) {
131            $data = $this->helper->gatherAsString();
132        }
133
134        // The browser-submission form posts to an external server, so it must not carry
135        // our CSRF token; the server-submission form posts back to the wiki and gets one.
136        $form = new Form(['action' => $url], $submissionMode === 'browser');
137        $form->addFieldsetOpen()->attr('style', 'width: 60%;');
138
139        $form->addTextarea('data')
140            ->val($data)
141            ->useInput(false)
142            ->attrs(['readonly' => 'readonly', 'rows' => 10, 'cols' => 80])
143            ->addClass('edit');
144
145        //If we submit via the server, we give the opportunity to subscribe to the autosubmission option
146        if ($submissionMode !== 'browser') {
147            $autosubmit = $form->addCheckbox('autosubmit', $this->getLang('autosubmit'))->useInput(false);
148            if ($this->helper->isAutosubmitEnabled()) {
149                $autosubmit->attr('checked', 'checked');
150            }
151            $form->setHiddenField('do', 'admin');
152            $form->setHiddenField('page', 'popularity');
153        }
154
155        $form->addButton('submit', $this->getLang('submit'))->attr('type', 'submit');
156        $form->addFieldsetClose();
157
158        return $form->toHTML();
159    }
160}
161