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