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    /** @inheritdoc */
29    public function showInMenu()
30    {
31        return false;
32    }
33
34    /** @inheritdoc */
35    public function handle()
36    {
37        global $INPUT;
38        global $ID;
39        if (!$INPUT->has('delete')) return;
40
41        if ($INPUT->filter('trim')->str('delanimal') === '') {
42            msg($this->getLang('delete_noanimal'), -1);
43            return;
44        }
45
46        if ($INPUT->str('delanimal') != $INPUT->str('confirm')) {
47            msg($this->getLang('delete_mismatch'), -1);
48            return;
49        }
50
51        $animaldir = DOKU_FARMDIR . $INPUT->str('delanimal');
52
53        if (!$this->helper->isInPath($animaldir, DOKU_FARMDIR) || !is_dir($animaldir)) {
54            msg($this->getLang('delete_invalid'), -1);
55            return;
56        }
57
58        // let's delete it
59        $ok = io_rmdir($animaldir, true);
60        if ($ok) {
61            msg($this->getLang('delete_success'), 1);
62        } else {
63            msg($this->getLang('delete_fail'), -1);
64        }
65
66        $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'delete'], true, '&');
67        send_redirect($link);
68    }
69
70    /** @inheritdoc */
71    public function html()
72    {
73
74        $form = new Form();
75        $form->addFieldsetOpen($this->getLang('delete_animal'));
76
77        $animals = $this->helper->getAllAnimals();
78        array_unshift($animals, '');
79        $form->addDropdown('delanimal', $animals)->addClass('farmer_chosen_animals');
80        $form->addTextInput('confirm', $this->getLang('delete_confirm'));
81        $form->addButton('delete', $this->getLang('delete'));
82        $form->addFieldsetClose();
83        echo $form->toHTML();
84    }
85}
86