xref: /dokuwiki/lib/plugins/popularity/admin.php (revision 29fc53cff0d7cb2855f0786f6337d882aa13d1d6)
15faeb1e6SAndreas Gohr<?php
25faeb1e6SAndreas Gohr/**
35faeb1e6SAndreas Gohr * Popularity Feedback Plugin
45faeb1e6SAndreas Gohr *
55faeb1e6SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
65faeb1e6SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
75faeb1e6SAndreas Gohr */
8*29fc53cfSAndreas Gohrclass admin_plugin_popularity extends DokuWiki_Admin_Plugin
9*29fc53cfSAndreas Gohr{
1038479cbbSDominik Eckelmann
11*29fc53cfSAndreas Gohr    /** @var helper_plugin_popularity */
123dc2d50cSAndreas Gohr    protected $helper;
133dc2d50cSAndreas Gohr    protected $sentStatus = null;
145faeb1e6SAndreas Gohr
15*29fc53cfSAndreas Gohr    /**
16*29fc53cfSAndreas Gohr     * admin_plugin_popularity constructor.
17*29fc53cfSAndreas Gohr     */
18*29fc53cfSAndreas Gohr    public function __construct()
19*29fc53cfSAndreas Gohr    {
2098be6429SGuillaume Turri        $this->helper = $this->loadHelper('popularity', false);
215faeb1e6SAndreas Gohr    }
225faeb1e6SAndreas Gohr
235faeb1e6SAndreas Gohr    /**
245faeb1e6SAndreas Gohr     * return prompt for admin menu
253dc2d50cSAndreas Gohr     * @param $language
263dc2d50cSAndreas Gohr     * @return string
275faeb1e6SAndreas Gohr     */
28*29fc53cfSAndreas Gohr    public function getMenuText($language)
29*29fc53cfSAndreas Gohr    {
305faeb1e6SAndreas Gohr        return $this->getLang('name');
315faeb1e6SAndreas Gohr    }
325faeb1e6SAndreas Gohr
335faeb1e6SAndreas Gohr    /**
345faeb1e6SAndreas Gohr     * return sort order for position in admin menu
355faeb1e6SAndreas Gohr     */
36*29fc53cfSAndreas Gohr    public function getMenuSort()
37*29fc53cfSAndreas Gohr    {
385faeb1e6SAndreas Gohr        return 2000;
395faeb1e6SAndreas Gohr    }
405faeb1e6SAndreas Gohr
415faeb1e6SAndreas Gohr    /**
421bda8618SAndreas Gohr     * Accessible for managers
431bda8618SAndreas Gohr     */
44*29fc53cfSAndreas Gohr    public function forAdminOnly()
45*29fc53cfSAndreas Gohr    {
461bda8618SAndreas Gohr        return false;
471bda8618SAndreas Gohr    }
481bda8618SAndreas Gohr
491bda8618SAndreas Gohr
501bda8618SAndreas Gohr    /**
515faeb1e6SAndreas Gohr     * handle user request
525faeb1e6SAndreas Gohr     */
53*29fc53cfSAndreas Gohr    public function handle()
54*29fc53cfSAndreas Gohr    {
55f21e024aSHakan Sandell        global $INPUT;
56f21e024aSHakan Sandell
5798be6429SGuillaume Turri        //Send the data
58f21e024aSHakan Sandell        if ($INPUT->has('data')) {
59f21e024aSHakan Sandell            $this->sentStatus = $this->helper->sendData($INPUT->str('data'));
605827ba0bSGuillaume Turri            if ($this->sentStatus === '') {
615827ba0bSGuillaume Turri                //Update the last time we sent the data
625827ba0bSGuillaume Turri                touch($this->helper->popularityLastSubmitFile);
635827ba0bSGuillaume Turri            }
6498be6429SGuillaume Turri            //Deal with the autosubmit option
65*29fc53cfSAndreas Gohr            $this->enableAutosubmit($INPUT->has('autosubmit'));
6698be6429SGuillaume Turri        }
6798be6429SGuillaume Turri    }
6898be6429SGuillaume Turri
6998be6429SGuillaume Turri    /**
7098be6429SGuillaume Turri     * Enable or disable autosubmit
7198be6429SGuillaume Turri     * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it.
7298be6429SGuillaume Turri     */
73*29fc53cfSAndreas Gohr    protected function enableAutosubmit($enable)
74*29fc53cfSAndreas Gohr    {
7598be6429SGuillaume Turri        if ($enable) {
7698be6429SGuillaume Turri            io_saveFile($this->helper->autosubmitFile, ' ');
7798be6429SGuillaume Turri        } else {
7898be6429SGuillaume Turri            @unlink($this->helper->autosubmitFile);
7998be6429SGuillaume Turri        }
805faeb1e6SAndreas Gohr    }
815faeb1e6SAndreas Gohr
825faeb1e6SAndreas Gohr    /**
831bda8618SAndreas Gohr     * Output HTML form
845faeb1e6SAndreas Gohr     */
85*29fc53cfSAndreas Gohr    public function html()
86*29fc53cfSAndreas Gohr    {
87f21e024aSHakan Sandell        global $INPUT;
88f21e024aSHakan Sandell
89f21e024aSHakan Sandell        if (! $INPUT->has('data')) {
905faeb1e6SAndreas Gohr            echo $this->locale_xhtml('intro');
915faeb1e6SAndreas Gohr
9298be6429SGuillaume Turri            //If there was an error the last time we tried to autosubmit, warn the user
9398be6429SGuillaume Turri            if ($this->helper->isAutoSubmitEnabled()) {
9479e79377SAndreas Gohr                if (file_exists($this->helper->autosubmitErrorFile)) {
9598be6429SGuillaume Turri                    echo $this->getLang('autosubmitError');
9698be6429SGuillaume Turri                    echo io_readFile($this->helper->autosubmitErrorFile);
9798be6429SGuillaume Turri                }
9898be6429SGuillaume Turri            }
9998be6429SGuillaume Turri
1005faeb1e6SAndreas Gohr            flush();
10198be6429SGuillaume Turri            echo $this->buildForm('server');
1025827ba0bSGuillaume Turri
1035827ba0bSGuillaume Turri            //Print the last time the data was sent
1045827ba0bSGuillaume Turri            $lastSent = $this->helper->lastSentTime();
1055827ba0bSGuillaume Turri            if ($lastSent !== 0) {
106a375d5e5SGuillaume Turri                echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent);
1075827ba0bSGuillaume Turri            }
1085faeb1e6SAndreas Gohr        } else {
10998be6429SGuillaume Turri            //If we just submitted the form
11098be6429SGuillaume Turri            if ($this->sentStatus === '') {
11198be6429SGuillaume Turri                //If we successfully sent the data
11298be6429SGuillaume Turri                echo $this->locale_xhtml('submitted');
11398be6429SGuillaume Turri            } else {
11498be6429SGuillaume Turri                //If we failed to submit the data, try directly with the browser
11598be6429SGuillaume Turri                echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />';
11698be6429SGuillaume Turri                echo $this->getLang('submitDirectly');
117f21e024aSHakan Sandell                echo $this->buildForm('browser', $INPUT->str('data'));
1185faeb1e6SAndreas Gohr            }
1195faeb1e6SAndreas Gohr        }
1205faeb1e6SAndreas Gohr    }
1215faeb1e6SAndreas Gohr
1225faeb1e6SAndreas Gohr
1235faeb1e6SAndreas Gohr    /**
12498be6429SGuillaume Turri     * Build the form which presents the data to be sent
12538479cbbSDominik Eckelmann     * @param string $submissionMode How is the data supposed to be sent? (may be: 'browser' or 'server')
12698be6429SGuillaume Turri     * @param string $data   The popularity data, if it has already been computed. NULL otherwise.
12738479cbbSDominik Eckelmann     * @return string The form, as an html string
1285faeb1e6SAndreas Gohr     */
129*29fc53cfSAndreas Gohr    protected function buildForm($submissionMode, $data = null)
130*29fc53cfSAndreas Gohr    {
13198be6429SGuillaume Turri        $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script());
13298be6429SGuillaume Turri        if (is_null($data)) {
13398be6429SGuillaume Turri            $data = $this->helper->gatherAsString();
1345faeb1e6SAndreas Gohr        }
1355faeb1e6SAndreas Gohr
13698be6429SGuillaume Turri        $form = '<form method="post" action="'. $url  .'" accept-charset="utf-8">'
13798be6429SGuillaume Turri            .'<fieldset style="width: 60%;">'
13898be6429SGuillaume Turri            .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">'
13998be6429SGuillaume Turri            .$data
14098be6429SGuillaume Turri            .'</textarea><br />';
14198be6429SGuillaume Turri
14298be6429SGuillaume Turri        //If we submit via the server, we give the opportunity to suscribe to the autosubmission option
14398be6429SGuillaume Turri        if ($submissionMode !== 'browser') {
14498be6429SGuillaume Turri            $form .= '<label for="autosubmit">'
14598be6429SGuillaume Turri                .'<input type="checkbox" name="autosubmit" id="autosubmit" '
14698be6429SGuillaume Turri                .($this->helper->isAutosubmitEnabled() ? 'checked' : '' )
14798be6429SGuillaume Turri                .'/> ' . $this->getLang('autosubmit') .'<br />'
14898be6429SGuillaume Turri                .'</label>'
1496cd259d7SAnika Henke                .'<input type="hidden" name="do" value="admin" />'
1506cd259d7SAnika Henke                .'<input type="hidden" name="page" value="popularity" />';
1515faeb1e6SAndreas Gohr        }
152ae614416SAnika Henke        $form .= '<button type="submit">'.$this->getLang('submit').'</button>'
15398be6429SGuillaume Turri            .'</fieldset>'
15498be6429SGuillaume Turri            .'</form>';
15598be6429SGuillaume Turri        return $form;
1565faeb1e6SAndreas Gohr    }
1575faeb1e6SAndreas Gohr}
158