xref: /plugin/farmer/admin/delete.php (revision 1da41c8bdb9ce0b590d5b69552c4ddb4d0aef860)
124f27905SAndreas Gohr<?php
2*1da41c8bSAndreas Gohr
3*1da41c8bSAndreas Gohruse dokuwiki\Extension\AdminPlugin;
4*1da41c8bSAndreas Gohruse dokuwiki\Form\Form;
5*1da41c8bSAndreas Gohr
624f27905SAndreas Gohr/**
724f27905SAndreas Gohr * DokuWiki Plugin farmer (Admin Component)
824f27905SAndreas Gohr *
9*1da41c8bSAndreas Gohr * Information about the farm and the current instance
10*1da41c8bSAndreas Gohr *
1124f27905SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1224f27905SAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
1324f27905SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
1424f27905SAndreas Gohr */
15*1da41c8bSAndreas Gohrclass admin_plugin_farmer_delete extends AdminPlugin
16*1da41c8bSAndreas Gohr{
1724f27905SAndreas Gohr    /** @var helper_plugin_farmer */
1824f27905SAndreas Gohr    protected $helper;
1924f27905SAndreas Gohr
2024f27905SAndreas Gohr    /**
2124f27905SAndreas Gohr     * admin_plugin_farmer_info constructor.
2224f27905SAndreas Gohr     */
23*1da41c8bSAndreas Gohr    public function __construct()
24*1da41c8bSAndreas Gohr    {
2524f27905SAndreas Gohr        $this->helper = plugin_load('helper', 'farmer');
2624f27905SAndreas Gohr    }
2724f27905SAndreas Gohr
2824f27905SAndreas Gohr    /**
2924f27905SAndreas Gohr     * @return bool admin only!
3024f27905SAndreas Gohr     */
31*1da41c8bSAndreas Gohr    public function forAdminOnly()
32*1da41c8bSAndreas Gohr    {
3324f27905SAndreas Gohr        return true;
3424f27905SAndreas Gohr    }
3524f27905SAndreas Gohr
3624f27905SAndreas Gohr    /**
3724f27905SAndreas Gohr     * Should carry out any processing required by the plugin.
3824f27905SAndreas Gohr     */
39*1da41c8bSAndreas Gohr    public function handle()
40*1da41c8bSAndreas Gohr    {
4124f27905SAndreas Gohr        global $INPUT;
4224f27905SAndreas Gohr        global $ID;
4324f27905SAndreas Gohr        if (!$INPUT->has('delete')) return;
4424f27905SAndreas Gohr
4524f27905SAndreas Gohr        if ($INPUT->filter('trim')->str('delanimal') === '') {
4624f27905SAndreas Gohr            msg($this->getLang('delete_noanimal'), -1);
4724f27905SAndreas Gohr            return;
4824f27905SAndreas Gohr        }
4924f27905SAndreas Gohr
5024f27905SAndreas Gohr        if ($INPUT->str('delanimal') != $INPUT->str('confirm')) {
5124f27905SAndreas Gohr            msg($this->getLang('delete_mismatch'), -1);
5224f27905SAndreas Gohr            return;
5324f27905SAndreas Gohr        }
5424f27905SAndreas Gohr
558262a4cbSAndreas Gohr        $animaldir = DOKU_FARMDIR . $INPUT->str('delanimal');
5624f27905SAndreas Gohr
5724f27905SAndreas Gohr        if (!$this->helper->isInPath($animaldir, DOKU_FARMDIR) || !is_dir($animaldir)) {
5824f27905SAndreas Gohr            msg($this->getLang('delete_invalid'), -1);
5924f27905SAndreas Gohr            return;
6024f27905SAndreas Gohr        }
6124f27905SAndreas Gohr
6224f27905SAndreas Gohr        // let's delete it
6324f27905SAndreas Gohr        $ok = io_rmdir($animaldir, true);
6424f27905SAndreas Gohr        if ($ok) {
6524f27905SAndreas Gohr            msg($this->getLang('delete_success'), 1);
6624f27905SAndreas Gohr        } else {
6724f27905SAndreas Gohr            msg($this->getLang('delete_fail'), -1);
6824f27905SAndreas Gohr        }
6924f27905SAndreas Gohr
70*1da41c8bSAndreas Gohr        $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'delete'], true, '&');
7124f27905SAndreas Gohr        send_redirect($link);
7224f27905SAndreas Gohr    }
7324f27905SAndreas Gohr
7424f27905SAndreas Gohr    /**
7524f27905SAndreas Gohr     * Render HTML output, e.g. helpful text and a form
7624f27905SAndreas Gohr     */
77*1da41c8bSAndreas Gohr    public function html()
78*1da41c8bSAndreas Gohr    {
7924f27905SAndreas Gohr
8024f27905SAndreas Gohr        $form = new Form();
8124f27905SAndreas Gohr        $form->addFieldsetOpen($this->getLang('delete_animal'));
8224f27905SAndreas Gohr
8324f27905SAndreas Gohr        $animals = $this->helper->getAllAnimals();
8424f27905SAndreas Gohr        array_unshift($animals, '');
8543551cfcSMichael Grosse        $form->addDropdown('delanimal', $animals)->addClass('farmer_chosen_animals');
8624f27905SAndreas Gohr        $form->addTextInput('confirm', $this->getLang('delete_confirm'));
8724f27905SAndreas Gohr        $form->addButton('delete', $this->getLang('delete'));
8824f27905SAndreas Gohr        $form->addFieldsetClose();
8924f27905SAndreas Gohr        echo $form->toHTML();
9024f27905SAndreas Gohr    }
9124f27905SAndreas Gohr}
92