xref: /plugin/farmer/admin/plugins.php (revision 415bff3c069d0105b4bdcab89111282183fce317)
1bc461538SMichael Große<?php
2bc461538SMichael Große/**
3bc461538SMichael Große * Plugin Skeleton: Displays "Hello World!"
4bc461538SMichael Große *
5bc461538SMichael Große * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6bc461538SMichael Große * @author     Christopher Smith <chris@jalakai.co.uk>
7bc461538SMichael Große */
8bc461538SMichael Große
9bc461538SMichael Große
10bc461538SMichael Große/**
11bc461538SMichael Große * All DokuWiki plugins to extend the admin function
12bc461538SMichael Große * need to inherit from this class
13bc461538SMichael Große */
14bc461538SMichael Großeclass admin_plugin_farmer_plugins extends DokuWiki_Admin_Plugin {
15bc461538SMichael Große
16bc461538SMichael Große    /**
17bc461538SMichael Große     * handle user request
18bc461538SMichael Große     */
190b96e6d7SMichael Große    public function handle() {
20bc461538SMichael Große
21bc461538SMichael Große        if (!file_exists(DOKU_INC . 'inc/preload.php')) {
22bc461538SMichael Große            global $ID;
23bc461538SMichael Große            $get = $_GET;
24bc461538SMichael Große            if(isset($get['id'])) unset($get['id']);
25bc461538SMichael Große            $get['page'] = 'farmer_foo';
26bc461538SMichael Große            $self = wl($ID, $get, false, '&');
27bc461538SMichael Große            send_redirect($self);
28bc461538SMichael Große        }
29bc461538SMichael Große
300b96e6d7SMichael Große        if (isset($_REQUEST['farmer_submitBulk'])) {
310b96e6d7SMichael Große            /** @var helper_plugin_farmer $helper */
320b96e6d7SMichael Große            $helper = plugin_load('helper', 'farmer');
330b96e6d7SMichael Große            $animals = $helper->getAllAnimals();
340b96e6d7SMichael Große            $plugin = $_REQUEST['farmer__bulkPluginSelect'];
350b96e6d7SMichael Große            foreach ($animals as $animal) {
360b96e6d7SMichael Große                $pluginConf = file(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
370b96e6d7SMichael Große                if ($_REQUEST['farmer_submitBulk'] === 'activate') {
380b96e6d7SMichael Große                    foreach ($pluginConf as $key => $line) {
390b96e6d7SMichael Große                        if (strpos($line, '$plugins[' . $plugin . ']') !== FALSE) {
400b96e6d7SMichael Große                            array_splice($pluginConf, $key, 1);
410b96e6d7SMichael Große                            break; // the plugin was deactivated and the deactivation is now removed
42bc461538SMichael Große                        }
43bc461538SMichael Große                    }
440b96e6d7SMichael Große                } else {
450b96e6d7SMichael Große                    $pluginIsActive = true;
460b96e6d7SMichael Große                    foreach ($pluginConf as $key => $line) {
470b96e6d7SMichael Große                        if (strpos($line, '$plugins[' . $plugin . ']') !== FALSE) {
480b96e6d7SMichael Große                            $pluginIsActive = false;
490b96e6d7SMichael Große                            break; // the plugin is already deactivated;
50bc461538SMichael Große                        }
51bc461538SMichael Große                    }
520b96e6d7SMichael Große                    if ($pluginIsActive) {
530b96e6d7SMichael Große                        $pluginConf[] = '$plugins[' . $plugin . '] = 0';
54bc461538SMichael Große                    }
550b96e6d7SMichael Große                }
560b96e6d7SMichael Große                io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', implode('\n',$pluginConf));
570b96e6d7SMichael Große                touch(DOKU_FARMDIR . $animal . '/conf/local.php');
580b96e6d7SMichael Große            }
590b96e6d7SMichael Große        }
60bc461538SMichael Große    }
61bc461538SMichael Große
62bc461538SMichael Große    /**
63bc461538SMichael Große     * output appropriate html
64bc461538SMichael Große     */
650b96e6d7SMichael Große    public function html() {
66bc461538SMichael Große        echo '<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>';
67bc461538SMichael Große        echo '<link href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.min.css" type="text/css" rel="stylesheet" />';
68bc461538SMichael Große
690b96e6d7SMichael Große        $switchForm =  new \dokuwiki\Form\Form();
700b96e6d7SMichael Große        $switchForm->addFieldsetOpen('edit a single animal or all at once?');
710b96e6d7SMichael Große        $switchForm->addRadioButton('bulkSingleSwitch', 'bulk edit all animals')->id('farmer__bulk')->attr('type','radio')->addClass('block');
720b96e6d7SMichael Große        $switchForm->addRadioButton('bulkSingleSwitch', 'edit a single animal')->id('farmer__single')->attr('type','radio')->addClass('block');
730b96e6d7SMichael Große        $switchForm->addFieldsetClose();
740b96e6d7SMichael Große        echo $switchForm->toHTML();
75bc461538SMichael Große
760b96e6d7SMichael Große        /** @var helper_plugin_farmer $helper */
770b96e6d7SMichael Große        $helper = plugin_load('helper', 'farmer');
780b96e6d7SMichael Große        $plugins = $helper->getAllPlugins();
790b96e6d7SMichael Große
800b96e6d7SMichael Große        $bulkForm = new \dokuwiki\Form\Form();
810b96e6d7SMichael Große        $bulkForm->id('farmer__bulkForm');
82*415bff3cSMichael Große        $bulkForm->addFieldsetOpen('Activate or deactivate a plugin in all animals');
830b96e6d7SMichael Große        $bulkForm->addHTML('bulk');
840b96e6d7SMichael Große        $bulkForm->addTagOpen('select')->id('farmer__bulkPluginSelect')->attr('name','farmer__bulkPlugin');
850b96e6d7SMichael Große        $bulkForm->addTagOpen('option')->attr('selected', 'selected')->attr('disabled', 'disabled')->attr('hidden', 'hidden')->attr('value', "");
860b96e6d7SMichael Große        $bulkForm->addTagClose('option');
870b96e6d7SMichael Große        foreach ($plugins as $plugin) {
880b96e6d7SMichael Große            $bulkForm->addTagOpen('option')->attr('value', $plugin);
890b96e6d7SMichael Große            $bulkForm->addHTML($plugin);
900b96e6d7SMichael Große            $bulkForm->addTagClose('option');
910b96e6d7SMichael Große        }
920b96e6d7SMichael Große        $bulkForm->addTagClose('select');
930b96e6d7SMichael Große        $bulkForm->addButton('farmer__submitBulk','Activate')->attr('value','activate')->attr('type','submit')->attr('disabled','disabled')->addClass('bulkButton');
940b96e6d7SMichael Große        $bulkForm->addButton('farmer__submitBulk','Deactivate')->attr('value','deactivate')->attr('type','submit')->attr('disabled','disabled')->addClass('bulkButton');
95*415bff3cSMichael Große        $bulkForm->addFieldsetClose();
960b96e6d7SMichael Große        echo $bulkForm->toHTML();
970b96e6d7SMichael Große
980b96e6d7SMichael Große        $singleForm = new \dokuwiki\Form\Form();
990b96e6d7SMichael Große        $singleForm->id('farmer__singlePluginForm');
100*415bff3cSMichael Große        $singleForm->addFieldsetOpen('edit the plugins of a specific animal');
101*415bff3cSMichael Große        $singleForm->addTagOpen('select')->id('farmer__animalSelect')->attr('name', 'plugin_farmer[selectedAnimal]');
1020b96e6d7SMichael Große        $singleForm->addTagOpen('option')->attr('selected', 'selected')->attr('disabled', 'disabled')->attr('hidden', 'hidden')->attr('value', "");
1030b96e6d7SMichael Große        $singleForm->addTagClose('option');
1040b96e6d7SMichael Große        $animals = $helper->getAllAnimals();
1050b96e6d7SMichael Große        foreach ($animals as $animal) {
1060b96e6d7SMichael Große            $singleForm->addTagOpen('option');
1070b96e6d7SMichael Große            $singleForm->addHTML($animal);
1080b96e6d7SMichael Große            $singleForm->addTagClose('option');
1090b96e6d7SMichael Große        }
1100b96e6d7SMichael Große        $singleForm->addTagClose('select');
111*415bff3cSMichael Große        $singleForm->addButton('plugin_farmer[submit_type]','Submit')->attr('type','submit')->val('updateSingleAnimal');
112*415bff3cSMichael Große        $singleForm->addButton('farmer__reset','Reset')->attr('type','reset');
113*415bff3cSMichael Große        $singleForm->addTagOpen('div')->id('farmer__animalPlugins');
114*415bff3cSMichael Große        $singleForm->addTagClose('div');
115*415bff3cSMichael Große        $switchForm->addFieldsetClose();
116*415bff3cSMichael Große        $singleForm->addButton('plugin_farmer[submit_type]','Submit')->attr('type','submit')->val('updateSingleAnimal');
117*415bff3cSMichael Große        $singleForm->addButton('farmer__reset','Reset')->attr('type','reset');
1180b96e6d7SMichael Große        echo $singleForm->toHTML();
119bc461538SMichael Große    }
120bc461538SMichael Große
121bc461538SMichael Große    public function getMenuText() {
122bc461538SMichael Große        return 'Farmer: Change animal plugins';
123bc461538SMichael Große    }
124bc461538SMichael Große
125bc461538SMichael Große    public function getMenuSort() {
126bc461538SMichael Große        return 42;
127bc461538SMichael Große    }
128bc461538SMichael Große
129bc461538SMichael Große}
130bc461538SMichael Große
131