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