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 = null;
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
92            //If there was an error the last time we tried to autosubmit, warn the user
93            if ($this->helper->isAutoSubmitEnabled()) {
94                if (file_exists($this->helper->autosubmitErrorFile)) {
95                    echo $this->getLang('autosubmitError');
96                    echo io_readFile($this->helper->autosubmitErrorFile);
97                }
98            }
99
100            flush();
101            echo $this->buildForm('server');
102
103            //Print the last time the data was sent
104            $lastSent = $this->helper->lastSentTime();
105            if ($lastSent !== 0) {
106                echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent);
107            }
108        } else {
109            //If we just submitted the form
110            if ($this->sentStatus === '') {
111                //If we successfully sent the data
112                echo $this->locale_xhtml('submitted');
113            } else {
114                //If we failed to submit the data, try directly with the browser
115                echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />';
116                echo $this->getLang('submitDirectly');
117                echo $this->buildForm('browser', $INPUT->str('data'));
118            }
119        }
120    }
121
122
123    /**
124     * Build the form which presents the data to be sent
125     * @param string $submissionMode How is the data supposed to be sent? (may be: 'browser' or 'server')
126     * @param string $data   The popularity data, if it has already been computed. NULL otherwise.
127     * @return string The form, as an html string
128     */
129    protected function buildForm($submissionMode, $data = null)
130    {
131        $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script());
132        if (is_null($data)) {
133            $data = $this->helper->gatherAsString();
134        }
135
136        $form = '<form method="post" action="'. $url  .'" accept-charset="utf-8">'
137            .'<fieldset style="width: 60%;">'
138            .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">'
139            .$data
140            .'</textarea><br />';
141
142        //If we submit via the server, we give the opportunity to suscribe to the autosubmission option
143        if ($submissionMode !== 'browser') {
144            $form .= '<label for="autosubmit">'
145                .'<input type="checkbox" name="autosubmit" id="autosubmit" '
146                .($this->helper->isAutosubmitEnabled() ? 'checked' : '' )
147                .'/> ' . $this->getLang('autosubmit') .'<br />'
148                .'</label>'
149                .'<input type="hidden" name="do" value="admin" />'
150                .'<input type="hidden" name="page" value="popularity" />';
151        }
152        $form .= '<button type="submit">'.$this->getLang('submit').'</button>'
153            .'</fieldset>'
154            .'</form>';
155        return $form;
156    }
157}
158