xref: /plugin/farmer/action/ajax.php (revision f31951d478c90dc7227cee735d34ecc8149d5ce9)
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        $allPlugins = $helper->getAllPlugins();
72        $plugins = array();
73
74        // FIXME do we need to check other files as well? refer to config cascade
75        $local = DOKU_FARMDIR . '/' . $animal . '/conf/plugins.local.php';
76        if(file_exists($local)) include($local);
77        $data = array($allPlugins, $plugins,);
78
79        //json library of DokuWiki
80        $json = new JSON();
81
82        //set content type
83        header('Content-Type: application/json');
84        echo $json->encode($data);
85    }
86
87}
88
89