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