xref: /plugin/farmer/action/ajax.php (revision 1413d607f0785a125b817eff45e6f87e939fb0bb)
1<?php
2/**
3 * DokuWiki Plugin farmer (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <grosse@cosmocode.de>
7 * @author  Andreas Gohr <gohr@cosmocode.de>
8 */
9
10if(!defined('DOKU_INC')) die();
11
12/**
13 * Manage AJAX features
14 */
15class action_plugin_farmer_ajax extends DokuWiki_Action_Plugin {
16
17    /**
18     * plugin should use this method to register its handlers with the DokuWiki's event controller
19     *
20     * @param Doku_Event_Handler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER
21     *
22     */
23    public function register(Doku_Event_Handler $controller) {
24        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_ajax_call');
25    }
26
27    /**
28     * handle ajax requests
29     *
30     * @param Doku_Event $event
31     * @param $param
32     */
33    public function _ajax_call(Doku_Event $event, $param) {
34        if(substr($event->data, 0, 13) !== 'plugin_farmer') {
35            return;
36        }
37        //no other ajax call handlers needed
38        $event->stopPropagation();
39        $event->preventDefault();
40
41        if(substr($event->data, 14, 10) === 'getPlugins') {
42            $this->get_animal_plugins($event, $param);
43            return;
44        }
45        if(substr($event->data, 14, 10) === 'checkSetup') {
46            $this->check_setup($event, $param);
47        }
48    }
49
50    /**
51     * This function exists in order to provide a positive (i.e. 200) response to an ajax request to a non-existing animal.
52     *
53     * @param Doku_Event $event
54     * @param            $param
55     */
56    public function check_setup(Doku_Event $event, $param) {
57        $data = '';
58        $json = new JSON();
59        header('Content-Type: application/json');
60        echo $json->encode($data);
61    }
62
63    /**
64     * @param Doku_Event $event
65     * @param            $param
66     */
67    public function get_animal_plugins(Doku_Event $event, $param) {
68        $animal = substr($event->data, 25);
69        /** @var helper_plugin_farmer $helper */
70        $helper = plugin_load('helper', 'farmer');
71
72        $plugins = $helper->getAnimalPluginRealState($animal);
73
74        header('Content-Type: text/html; charset=utf-8');
75
76        echo '<table>';
77        echo '<tr>';
78        echo '<th>' . $this->getLang('plugin') . '</th>';
79        echo '<th>' . $this->getLang('plugin_default') . '</th>';
80        echo '<th>' . $this->getLang('plugin_enabled') . '</th>';
81        echo '<th>' . $this->getLang('plugin_disabled') . '</th>';
82        echo '</tr>';
83
84        foreach($plugins as $plugin) {
85            echo '<tr>';
86            echo '<th>' . hsc($plugin['name']) . '</th>';
87
88            echo '<td>';
89            $attr = array();
90            $attr['type'] = 'radio';
91            $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']';
92            $attr['value'] = '-1';
93            if($plugin['isdefault']) {
94                $attr['checked'] = 'checked';
95            }
96            echo '<label>';
97            echo '<input ' . buildAttributes($attr) . ' />';
98            if($plugin['default']) {
99                echo ' (' . $this->getLang('plugin_on') . ')';
100            } else {
101                echo ' (' . $this->getLang('plugin_off') . ')';
102            }
103            echo '</label>';
104            echo '</td>';
105
106            echo '<td>';
107            $attr = array();
108            $attr['type'] = 'radio';
109            $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']';
110            $attr['value'] = '1';
111            if(!$plugin['isdefault'] && $plugin['actual']) {
112                $attr['checked'] = 'checked';
113            }
114            echo '<label>';
115            echo '<input ' . buildAttributes($attr) . ' />';
116            echo ' ' . $this->getLang('plugin_on');
117            echo '</label>';
118            echo '</td>';
119
120            echo '<td>';
121            $attr = array();
122            $attr['type'] = 'radio';
123            $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']';
124            $attr['value'] = '0';
125            if(!$plugin['isdefault'] && !$plugin['actual']) {
126                $attr['checked'] = 'checked';
127            }
128            echo '<label>';
129            echo '<input ' . buildAttributes($attr) . ' />';
130            echo ' ' . $this->getLang('plugin_off');
131            echo '</label>';
132            echo '</td>';
133
134            echo '</tr>';
135        }
136    }
137
138}
139
140