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