xref: /dokuwiki/lib/plugins/popularity/admin.php (revision 844d8067502e44b64cfc7120b159fbb5f5f9b3e2)
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 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11/**
12 * All DokuWiki plugins to extend the admin function
13 * need to inherit from this class
14 */
15class admin_plugin_popularity extends DokuWiki_Admin_Plugin {
16    var $version;
17
18    /**
19     * @var helper_plugin_popularity
20     */
21    var $helper;
22    var $sentStatus = null;
23
24    function admin_plugin_popularity(){
25        $this->helper = $this->loadHelper('popularity', false);
26
27        $pluginInfo = $this->getInfo();
28        $this->version = $pluginInfo['date'];
29    }
30
31    /**
32     * return prompt for admin menu
33     */
34    function getMenuText($language) {
35        return $this->getLang('name');
36    }
37
38    /**
39     * return sort order for position in admin menu
40     */
41    function getMenuSort() {
42        return 2000;
43    }
44
45    /**
46     * Accessible for managers
47     */
48    function forAdminOnly() {
49        return false;
50    }
51
52
53    /**
54     * handle user request
55     */
56    function handle() {
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    function _enableAutosubmit( $enable ){
76        if ( $enable ){
77            io_saveFile( $this->helper->autosubmitFile, ' ');
78        } else {
79            @unlink($this->helper->autosubmitFile);
80        }
81    }
82
83    /**
84     * Output HTML form
85     */
86    function html() {
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    function buildForm($submissionMode, $data = null){
130        $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script());
131        if ( is_null($data) ){
132            $data = $this->helper->gatherAsString();
133        }
134
135        $form = '<form method="post" action="'. $url  .'" accept-charset="utf-8">'
136            .'<fieldset style="width: 60%;">'
137            .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">'
138            .$data
139            .'</textarea><br />';
140
141        //If we submit via the server, we give the opportunity to suscribe to the autosubmission option
142        if ( $submissionMode !== 'browser' ){
143            $form .= '<label for="autosubmit">'
144                .'<input type="checkbox" name="autosubmit" id="autosubmit" '
145                .($this->helper->isAutosubmitEnabled() ? 'checked' : '' )
146                .'/> ' . $this->getLang('autosubmit') .'<br />'
147                .'</label>'
148                .'<input type="hidden" name="do" value="admin" />'
149                .'<input type="hidden" name="page" value="popularity" />';
150        }
151        $form .= '<input type="submit" class="button" value="'.$this->getLang('submit').'"/>'
152            .'</fieldset>'
153            .'</form>';
154        return $form;
155    }
156}
157