xref: /plugin/farmer/action/ajax.php (revision af1c6dd8b9c1e35385ddfb7549af221f4070c191)
10a5d2da2SAndreas Gohr<?php
20a5d2da2SAndreas Gohr/**
30a5d2da2SAndreas Gohr * DokuWiki Plugin farmer (Action Component)
40a5d2da2SAndreas Gohr *
50a5d2da2SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
60a5d2da2SAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
70a5d2da2SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
80a5d2da2SAndreas Gohr */
90a5d2da2SAndreas Gohr
100a5d2da2SAndreas Gohrif(!defined('DOKU_INC')) die();
110a5d2da2SAndreas Gohr
120a5d2da2SAndreas Gohr/**
130a5d2da2SAndreas Gohr * Manage AJAX features
140a5d2da2SAndreas Gohr */
150a5d2da2SAndreas Gohrclass action_plugin_farmer_ajax extends DokuWiki_Action_Plugin {
160a5d2da2SAndreas Gohr
170a5d2da2SAndreas Gohr    /**
180a5d2da2SAndreas Gohr     * plugin should use this method to register its handlers with the DokuWiki's event controller
190a5d2da2SAndreas Gohr     *
200a5d2da2SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER
210a5d2da2SAndreas Gohr     *
220a5d2da2SAndreas Gohr     */
230a5d2da2SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
240a5d2da2SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_ajax_call');
250a5d2da2SAndreas Gohr    }
260a5d2da2SAndreas Gohr
270a5d2da2SAndreas Gohr    /**
280a5d2da2SAndreas Gohr     * handle ajax requests
290a5d2da2SAndreas Gohr     *
300a5d2da2SAndreas Gohr     * @param Doku_Event $event
310a5d2da2SAndreas Gohr     * @param $param
320a5d2da2SAndreas Gohr     */
330a5d2da2SAndreas Gohr    public function _ajax_call(Doku_Event $event, $param) {
340a5d2da2SAndreas Gohr        if(substr($event->data, 0, 13) !== 'plugin_farmer') {
350a5d2da2SAndreas Gohr            return;
360a5d2da2SAndreas Gohr        }
370a5d2da2SAndreas Gohr        //no other ajax call handlers needed
380a5d2da2SAndreas Gohr        $event->stopPropagation();
390a5d2da2SAndreas Gohr        $event->preventDefault();
400a5d2da2SAndreas Gohr
410a5d2da2SAndreas Gohr        if(substr($event->data, 14, 10) === 'getPlugins') {
420a5d2da2SAndreas Gohr            $this->get_animal_plugins($event, $param);
430a5d2da2SAndreas Gohr            return;
440a5d2da2SAndreas Gohr        }
450a5d2da2SAndreas Gohr        if(substr($event->data, 14, 10) === 'checkSetup') {
460a5d2da2SAndreas Gohr            $this->check_setup($event, $param);
470a5d2da2SAndreas Gohr        }
480a5d2da2SAndreas Gohr    }
490a5d2da2SAndreas Gohr
500a5d2da2SAndreas Gohr    /**
510a5d2da2SAndreas Gohr     * This function exists in order to provide a positive (i.e. 200) response to an ajax request to a non-existing animal.
520a5d2da2SAndreas Gohr     *
530a5d2da2SAndreas Gohr     * @param Doku_Event $event
540a5d2da2SAndreas Gohr     * @param            $param
550a5d2da2SAndreas Gohr     */
560a5d2da2SAndreas Gohr    public function check_setup(Doku_Event $event, $param) {
570a5d2da2SAndreas Gohr        $data = '';
580a5d2da2SAndreas Gohr        $json = new JSON();
590a5d2da2SAndreas Gohr        header('Content-Type: application/json');
600a5d2da2SAndreas Gohr        echo $json->encode($data);
610a5d2da2SAndreas Gohr    }
620a5d2da2SAndreas Gohr
630a5d2da2SAndreas Gohr    /**
640a5d2da2SAndreas Gohr     * @param Doku_Event $event
650a5d2da2SAndreas Gohr     * @param            $param
660a5d2da2SAndreas Gohr     */
670a5d2da2SAndreas Gohr    public function get_animal_plugins(Doku_Event $event, $param) {
680a5d2da2SAndreas Gohr        $animal = substr($event->data, 25);
690a5d2da2SAndreas Gohr        /** @var helper_plugin_farmer $helper */
700a5d2da2SAndreas Gohr        $helper = plugin_load('helper', 'farmer');
710a5d2da2SAndreas Gohr
72*af1c6dd8SAndreas Gohr        $plugins = $helper->getAnimalPluginRealState($animal);
730a5d2da2SAndreas Gohr
74*af1c6dd8SAndreas Gohr        header('Content-Type: text/html; charset=utf-8');
750a5d2da2SAndreas Gohr
76*af1c6dd8SAndreas Gohr        echo '<table>';
77*af1c6dd8SAndreas Gohr        echo '<tr>';
78*af1c6dd8SAndreas Gohr        echo '<th>' . $this->getLang('plugin') . '</th>';
79*af1c6dd8SAndreas Gohr        echo '<th>' . $this->getLang('plugin_default') . '</th>';
80*af1c6dd8SAndreas Gohr        echo '<th>' . $this->getLang('plugin_enabled') . '</th>';
81*af1c6dd8SAndreas Gohr        echo '<th>' . $this->getLang('plugin_disabled') . '</th>';
82*af1c6dd8SAndreas Gohr        echo '</tr>';
83*af1c6dd8SAndreas Gohr
84*af1c6dd8SAndreas Gohr        foreach($plugins as $plugin) {
85*af1c6dd8SAndreas Gohr            echo '<tr>';
86*af1c6dd8SAndreas Gohr            echo '<th>' . hsc($plugin['name']) . '</th>';
87*af1c6dd8SAndreas Gohr
88*af1c6dd8SAndreas Gohr            echo '<td>';
89*af1c6dd8SAndreas Gohr            $attr = array();
90*af1c6dd8SAndreas Gohr            $attr['type'] = 'radio';
91*af1c6dd8SAndreas Gohr            $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']';
92*af1c6dd8SAndreas Gohr            $attr['value'] = '-1';
93*af1c6dd8SAndreas Gohr            if($plugin['isdefault']) {
94*af1c6dd8SAndreas Gohr                $attr['checked'] = 'checked';
95*af1c6dd8SAndreas Gohr            }
96*af1c6dd8SAndreas Gohr            echo '<label>';
97*af1c6dd8SAndreas Gohr            echo '<input ' . buildAttributes($attr) . ' />';
98*af1c6dd8SAndreas Gohr            if($plugin['default']) {
99*af1c6dd8SAndreas Gohr                echo ' (' . $this->getLang('plugin_on') . ')';
100*af1c6dd8SAndreas Gohr            } else {
101*af1c6dd8SAndreas Gohr                echo ' (' . $this->getLang('plugin_off') . ')';
102*af1c6dd8SAndreas Gohr            }
103*af1c6dd8SAndreas Gohr            echo '</label>';
104*af1c6dd8SAndreas Gohr            echo '</td>';
105*af1c6dd8SAndreas Gohr
106*af1c6dd8SAndreas Gohr            echo '<td>';
107*af1c6dd8SAndreas Gohr            $attr = array();
108*af1c6dd8SAndreas Gohr            $attr['type'] = 'radio';
109*af1c6dd8SAndreas Gohr            $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']';
110*af1c6dd8SAndreas Gohr            $attr['value'] = '1';
111*af1c6dd8SAndreas Gohr            if(!$plugin['isdefault'] && $plugin['actual']) {
112*af1c6dd8SAndreas Gohr                $attr['checked'] = 'checked';
113*af1c6dd8SAndreas Gohr            }
114*af1c6dd8SAndreas Gohr            echo '<label>';
115*af1c6dd8SAndreas Gohr            echo '<input ' . buildAttributes($attr) . ' />';
116*af1c6dd8SAndreas Gohr            echo ' ' . $this->getLang('plugin_on');
117*af1c6dd8SAndreas Gohr            echo '</label>';
118*af1c6dd8SAndreas Gohr            echo '</td>';
119*af1c6dd8SAndreas Gohr
120*af1c6dd8SAndreas Gohr            echo '<td>';
121*af1c6dd8SAndreas Gohr            $attr = array();
122*af1c6dd8SAndreas Gohr            $attr['type'] = 'radio';
123*af1c6dd8SAndreas Gohr            $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']';
124*af1c6dd8SAndreas Gohr            $attr['value'] = '0';
125*af1c6dd8SAndreas Gohr            if(!$plugin['isdefault'] && !$plugin['actual']) {
126*af1c6dd8SAndreas Gohr                $attr['checked'] = 'checked';
127*af1c6dd8SAndreas Gohr            }
128*af1c6dd8SAndreas Gohr            echo '<label>';
129*af1c6dd8SAndreas Gohr            echo '<input ' . buildAttributes($attr) . ' />';
130*af1c6dd8SAndreas Gohr            echo ' ' . $this->getLang('plugin_off');
131*af1c6dd8SAndreas Gohr            echo '</label>';
132*af1c6dd8SAndreas Gohr            echo '</td>';
133*af1c6dd8SAndreas Gohr
134*af1c6dd8SAndreas Gohr            echo '</tr>';
135*af1c6dd8SAndreas Gohr        }
1360a5d2da2SAndreas Gohr    }
1370a5d2da2SAndreas Gohr
1380a5d2da2SAndreas Gohr}
1390a5d2da2SAndreas Gohr
140