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