xref: /dokuwiki/lib/plugins/popularity/admin.php (revision 8553d24d33ab5f260c6e19959de764dd8472d438)
15faeb1e6SAndreas Gohr<?php
2*8553d24dSAndreas Gohr
3*8553d24dSAndreas Gohruse dokuwiki\Extension\AdminPlugin;
45faeb1e6SAndreas Gohr/**
55faeb1e6SAndreas Gohr * Popularity Feedback Plugin
65faeb1e6SAndreas Gohr *
75faeb1e6SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
85faeb1e6SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
95faeb1e6SAndreas Gohr */
10*8553d24dSAndreas Gohrclass admin_plugin_popularity extends AdminPlugin
1129fc53cfSAndreas Gohr{
1238479cbbSDominik Eckelmann
1329fc53cfSAndreas Gohr    /** @var helper_plugin_popularity */
143dc2d50cSAndreas Gohr    protected $helper;
1554cc7aa4SAndreas Gohr    protected $sentStatus;
165faeb1e6SAndreas Gohr
1729fc53cfSAndreas Gohr    /**
1829fc53cfSAndreas Gohr     * admin_plugin_popularity constructor.
1929fc53cfSAndreas Gohr     */
2029fc53cfSAndreas Gohr    public function __construct()
2129fc53cfSAndreas Gohr    {
2298be6429SGuillaume Turri        $this->helper = $this->loadHelper('popularity', false);
235faeb1e6SAndreas Gohr    }
245faeb1e6SAndreas Gohr
255faeb1e6SAndreas Gohr    /**
265faeb1e6SAndreas Gohr     * return prompt for admin menu
273dc2d50cSAndreas Gohr     * @param $language
283dc2d50cSAndreas Gohr     * @return string
295faeb1e6SAndreas Gohr     */
3029fc53cfSAndreas Gohr    public function getMenuText($language)
3129fc53cfSAndreas Gohr    {
325faeb1e6SAndreas Gohr        return $this->getLang('name');
335faeb1e6SAndreas Gohr    }
345faeb1e6SAndreas Gohr
355faeb1e6SAndreas Gohr    /**
365faeb1e6SAndreas Gohr     * return sort order for position in admin menu
375faeb1e6SAndreas Gohr     */
3829fc53cfSAndreas Gohr    public function getMenuSort()
3929fc53cfSAndreas Gohr    {
405faeb1e6SAndreas Gohr        return 2000;
415faeb1e6SAndreas Gohr    }
425faeb1e6SAndreas Gohr
435faeb1e6SAndreas Gohr    /**
441bda8618SAndreas Gohr     * Accessible for managers
451bda8618SAndreas Gohr     */
4629fc53cfSAndreas Gohr    public function forAdminOnly()
4729fc53cfSAndreas Gohr    {
481bda8618SAndreas Gohr        return false;
491bda8618SAndreas Gohr    }
501bda8618SAndreas Gohr
511bda8618SAndreas Gohr
521bda8618SAndreas Gohr    /**
535faeb1e6SAndreas Gohr     * handle user request
545faeb1e6SAndreas Gohr     */
5529fc53cfSAndreas Gohr    public function handle()
5629fc53cfSAndreas Gohr    {
57f21e024aSHakan Sandell        global $INPUT;
58f21e024aSHakan Sandell
5998be6429SGuillaume Turri        //Send the data
60f21e024aSHakan Sandell        if ($INPUT->has('data')) {
61f21e024aSHakan Sandell            $this->sentStatus = $this->helper->sendData($INPUT->str('data'));
625827ba0bSGuillaume Turri            if ($this->sentStatus === '') {
635827ba0bSGuillaume Turri                //Update the last time we sent the data
645827ba0bSGuillaume Turri                touch($this->helper->popularityLastSubmitFile);
655827ba0bSGuillaume Turri            }
6698be6429SGuillaume Turri            //Deal with the autosubmit option
6729fc53cfSAndreas Gohr            $this->enableAutosubmit($INPUT->has('autosubmit'));
6898be6429SGuillaume Turri        }
6998be6429SGuillaume Turri    }
7098be6429SGuillaume Turri
7198be6429SGuillaume Turri    /**
7298be6429SGuillaume Turri     * Enable or disable autosubmit
7398be6429SGuillaume Turri     * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it.
7498be6429SGuillaume Turri     */
7529fc53cfSAndreas Gohr    protected function enableAutosubmit($enable)
7629fc53cfSAndreas Gohr    {
7798be6429SGuillaume Turri        if ($enable) {
7898be6429SGuillaume Turri            io_saveFile($this->helper->autosubmitFile, ' ');
7998be6429SGuillaume Turri        } else {
8098be6429SGuillaume Turri            @unlink($this->helper->autosubmitFile);
8198be6429SGuillaume Turri        }
825faeb1e6SAndreas Gohr    }
835faeb1e6SAndreas Gohr
845faeb1e6SAndreas Gohr    /**
851bda8618SAndreas Gohr     * Output HTML form
865faeb1e6SAndreas Gohr     */
8729fc53cfSAndreas Gohr    public function html()
8829fc53cfSAndreas Gohr    {
89f21e024aSHakan Sandell        global $INPUT;
90f21e024aSHakan Sandell
91f21e024aSHakan Sandell        if (! $INPUT->has('data')) {
925faeb1e6SAndreas Gohr            echo $this->locale_xhtml('intro');
9398be6429SGuillaume Turri            //If there was an error the last time we tried to autosubmit, warn the user
9498be6429SGuillaume Turri            if ($this->helper->isAutoSubmitEnabled()) {
9579e79377SAndreas Gohr                if (file_exists($this->helper->autosubmitErrorFile)) {
9698be6429SGuillaume Turri                    echo $this->getLang('autosubmitError');
9798be6429SGuillaume Turri                    echo io_readFile($this->helper->autosubmitErrorFile);
9898be6429SGuillaume Turri                }
9998be6429SGuillaume Turri            }
1005faeb1e6SAndreas Gohr            flush();
10198be6429SGuillaume Turri            echo $this->buildForm('server');
1025827ba0bSGuillaume Turri            //Print the last time the data was sent
1035827ba0bSGuillaume Turri            $lastSent = $this->helper->lastSentTime();
1045827ba0bSGuillaume Turri            if ($lastSent !== 0) {
105a375d5e5SGuillaume Turri                echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent);
1065827ba0bSGuillaume Turri            }
10754cc7aa4SAndreas Gohr        } elseif ($this->sentStatus === '') {
10854cc7aa4SAndreas Gohr            //If we successfully send the data
10998be6429SGuillaume Turri            echo $this->locale_xhtml('submitted');
11098be6429SGuillaume Turri        } else {
11198be6429SGuillaume Turri            //If we failed to submit the data, try directly with the browser
11298be6429SGuillaume Turri            echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />';
11398be6429SGuillaume Turri            echo $this->getLang('submitDirectly');
114f21e024aSHakan Sandell            echo $this->buildForm('browser', $INPUT->str('data'));
1155faeb1e6SAndreas Gohr        }
1165faeb1e6SAndreas Gohr    }
1175faeb1e6SAndreas Gohr
1185faeb1e6SAndreas Gohr
1195faeb1e6SAndreas Gohr    /**
12098be6429SGuillaume Turri     * Build the form which presents the data to be sent
12138479cbbSDominik Eckelmann     * @param string $submissionMode How is the data supposed to be sent? (may be: 'browser' or 'server')
12298be6429SGuillaume Turri     * @param string $data   The popularity data, if it has already been computed. NULL otherwise.
12338479cbbSDominik Eckelmann     * @return string The form, as an html string
1245faeb1e6SAndreas Gohr     */
12529fc53cfSAndreas Gohr    protected function buildForm($submissionMode, $data = null)
12629fc53cfSAndreas Gohr    {
12798be6429SGuillaume Turri        $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script());
12898be6429SGuillaume Turri        if (is_null($data)) {
12998be6429SGuillaume Turri            $data = $this->helper->gatherAsString();
1305faeb1e6SAndreas Gohr        }
1315faeb1e6SAndreas Gohr
13298be6429SGuillaume Turri        $form = '<form method="post" action="'. $url  .'" accept-charset="utf-8">'
13398be6429SGuillaume Turri            .'<fieldset style="width: 60%;">'
13498be6429SGuillaume Turri            .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">'
13598be6429SGuillaume Turri            .$data
13698be6429SGuillaume Turri            .'</textarea><br />';
13798be6429SGuillaume Turri
13898be6429SGuillaume Turri        //If we submit via the server, we give the opportunity to suscribe to the autosubmission option
13998be6429SGuillaume Turri        if ($submissionMode !== 'browser') {
14098be6429SGuillaume Turri            $form .= '<label for="autosubmit">'
14198be6429SGuillaume Turri                .'<input type="checkbox" name="autosubmit" id="autosubmit" '
14298be6429SGuillaume Turri                .($this->helper->isAutosubmitEnabled() ? 'checked' : '' )
14398be6429SGuillaume Turri                .'/> ' . $this->getLang('autosubmit') .'<br />'
14498be6429SGuillaume Turri                .'</label>'
1456cd259d7SAnika Henke                .'<input type="hidden" name="do" value="admin" />'
1466cd259d7SAnika Henke                .'<input type="hidden" name="page" value="popularity" />';
1475faeb1e6SAndreas Gohr        }
148ae614416SAnika Henke        $form .= '<button type="submit">'.$this->getLang('submit').'</button>'
14998be6429SGuillaume Turri            .'</fieldset>'
15098be6429SGuillaume Turri            .'</form>';
15198be6429SGuillaume Turri        return $form;
1525faeb1e6SAndreas Gohr    }
1535faeb1e6SAndreas Gohr}
154