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