xref: /plugin/farmer/admin/delete.php (revision 1da41c8bdb9ce0b590d5b69552c4ddb4d0aef860)
1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\Form\Form;
5
6/**
7 * DokuWiki Plugin farmer (Admin Component)
8 *
9 * Information about the farm and the current instance
10 *
11 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12 * @author  Michael Große <grosse@cosmocode.de>
13 * @author  Andreas Gohr <gohr@cosmocode.de>
14 */
15class admin_plugin_farmer_delete extends AdminPlugin
16{
17    /** @var helper_plugin_farmer */
18    protected $helper;
19
20    /**
21     * admin_plugin_farmer_info constructor.
22     */
23    public function __construct()
24    {
25        $this->helper = plugin_load('helper', 'farmer');
26    }
27
28    /**
29     * @return bool admin only!
30     */
31    public function forAdminOnly()
32    {
33        return true;
34    }
35
36    /**
37     * Should carry out any processing required by the plugin.
38     */
39    public function handle()
40    {
41        global $INPUT;
42        global $ID;
43        if (!$INPUT->has('delete')) return;
44
45        if ($INPUT->filter('trim')->str('delanimal') === '') {
46            msg($this->getLang('delete_noanimal'), -1);
47            return;
48        }
49
50        if ($INPUT->str('delanimal') != $INPUT->str('confirm')) {
51            msg($this->getLang('delete_mismatch'), -1);
52            return;
53        }
54
55        $animaldir = DOKU_FARMDIR . $INPUT->str('delanimal');
56
57        if (!$this->helper->isInPath($animaldir, DOKU_FARMDIR) || !is_dir($animaldir)) {
58            msg($this->getLang('delete_invalid'), -1);
59            return;
60        }
61
62        // let's delete it
63        $ok = io_rmdir($animaldir, true);
64        if ($ok) {
65            msg($this->getLang('delete_success'), 1);
66        } else {
67            msg($this->getLang('delete_fail'), -1);
68        }
69
70        $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'delete'], true, '&');
71        send_redirect($link);
72    }
73
74    /**
75     * Render HTML output, e.g. helpful text and a form
76     */
77    public function html()
78    {
79
80        $form = new Form();
81        $form->addFieldsetOpen($this->getLang('delete_animal'));
82
83        $animals = $this->helper->getAllAnimals();
84        array_unshift($animals, '');
85        $form->addDropdown('delanimal', $animals)->addClass('farmer_chosen_animals');
86        $form->addTextInput('confirm', $this->getLang('delete_confirm'));
87        $form->addButton('delete', $this->getLang('delete'));
88        $form->addFieldsetClose();
89        echo $form->toHTML();
90    }
91}
92