xref: /plugin/farmer/action/ajax.php (revision 0a5d2da2d569b70aa9be68458b77b2616f382e97)
1*0a5d2da2SAndreas Gohr<?php
2*0a5d2da2SAndreas Gohr/**
3*0a5d2da2SAndreas Gohr * DokuWiki Plugin farmer (Action Component)
4*0a5d2da2SAndreas Gohr *
5*0a5d2da2SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*0a5d2da2SAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
7*0a5d2da2SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
8*0a5d2da2SAndreas Gohr */
9*0a5d2da2SAndreas Gohr
10*0a5d2da2SAndreas Gohrif(!defined('DOKU_INC')) die();
11*0a5d2da2SAndreas Gohr
12*0a5d2da2SAndreas Gohr/**
13*0a5d2da2SAndreas Gohr * Manage AJAX features
14*0a5d2da2SAndreas Gohr */
15*0a5d2da2SAndreas Gohrclass action_plugin_farmer_ajax extends DokuWiki_Action_Plugin {
16*0a5d2da2SAndreas Gohr
17*0a5d2da2SAndreas Gohr    /**
18*0a5d2da2SAndreas Gohr     * plugin should use this method to register its handlers with the DokuWiki's event controller
19*0a5d2da2SAndreas Gohr     *
20*0a5d2da2SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER
21*0a5d2da2SAndreas Gohr     *
22*0a5d2da2SAndreas Gohr     */
23*0a5d2da2SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
24*0a5d2da2SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_ajax_call');
25*0a5d2da2SAndreas Gohr    }
26*0a5d2da2SAndreas Gohr
27*0a5d2da2SAndreas Gohr    /**
28*0a5d2da2SAndreas Gohr     * handle ajax requests
29*0a5d2da2SAndreas Gohr     *
30*0a5d2da2SAndreas Gohr     * @param Doku_Event $event
31*0a5d2da2SAndreas Gohr     * @param $param
32*0a5d2da2SAndreas Gohr     */
33*0a5d2da2SAndreas Gohr    public function _ajax_call(Doku_Event $event, $param) {
34*0a5d2da2SAndreas Gohr        if(substr($event->data, 0, 13) !== 'plugin_farmer') {
35*0a5d2da2SAndreas Gohr            return;
36*0a5d2da2SAndreas Gohr        }
37*0a5d2da2SAndreas Gohr        //no other ajax call handlers needed
38*0a5d2da2SAndreas Gohr        $event->stopPropagation();
39*0a5d2da2SAndreas Gohr        $event->preventDefault();
40*0a5d2da2SAndreas Gohr
41*0a5d2da2SAndreas Gohr        if(substr($event->data, 14, 10) === 'getPlugins') {
42*0a5d2da2SAndreas Gohr            $this->get_animal_plugins($event, $param);
43*0a5d2da2SAndreas Gohr            return;
44*0a5d2da2SAndreas Gohr        }
45*0a5d2da2SAndreas Gohr        if(substr($event->data, 14, 10) === 'checkSetup') {
46*0a5d2da2SAndreas Gohr            $this->check_setup($event, $param);
47*0a5d2da2SAndreas Gohr        }
48*0a5d2da2SAndreas Gohr    }
49*0a5d2da2SAndreas Gohr
50*0a5d2da2SAndreas Gohr    /**
51*0a5d2da2SAndreas Gohr     * This function exists in order to provide a positive (i.e. 200) response to an ajax request to a non-existing animal.
52*0a5d2da2SAndreas Gohr     *
53*0a5d2da2SAndreas Gohr     * @param Doku_Event $event
54*0a5d2da2SAndreas Gohr     * @param            $param
55*0a5d2da2SAndreas Gohr     */
56*0a5d2da2SAndreas Gohr    public function check_setup(Doku_Event $event, $param) {
57*0a5d2da2SAndreas Gohr        $data = '';
58*0a5d2da2SAndreas Gohr        $json = new JSON();
59*0a5d2da2SAndreas Gohr        header('Content-Type: application/json');
60*0a5d2da2SAndreas Gohr        echo $json->encode($data);
61*0a5d2da2SAndreas Gohr    }
62*0a5d2da2SAndreas Gohr
63*0a5d2da2SAndreas Gohr    /**
64*0a5d2da2SAndreas Gohr     * @param Doku_Event $event
65*0a5d2da2SAndreas Gohr     * @param            $param
66*0a5d2da2SAndreas Gohr     */
67*0a5d2da2SAndreas Gohr    public function get_animal_plugins(Doku_Event $event, $param) {
68*0a5d2da2SAndreas Gohr        $animal = substr($event->data, 25);
69*0a5d2da2SAndreas Gohr        /** @var helper_plugin_farmer $helper */
70*0a5d2da2SAndreas Gohr        $helper = plugin_load('helper', 'farmer');
71*0a5d2da2SAndreas Gohr        $allPlugins = $helper->getAllPlugins();
72*0a5d2da2SAndreas Gohr        $plugins = array();
73*0a5d2da2SAndreas Gohr
74*0a5d2da2SAndreas Gohr        // FIXME do we need to check other files as well? refer to config cascade
75*0a5d2da2SAndreas Gohr        $local = DOKU_FARMDIR . '/' . $animal . '/conf/plugins.local.php';
76*0a5d2da2SAndreas Gohr        if(file_exists($local)) include($local);
77*0a5d2da2SAndreas Gohr        $data = array($allPlugins, $plugins,);
78*0a5d2da2SAndreas Gohr
79*0a5d2da2SAndreas Gohr        //json library of DokuWiki
80*0a5d2da2SAndreas Gohr        $json = new JSON();
81*0a5d2da2SAndreas Gohr
82*0a5d2da2SAndreas Gohr        //set content type
83*0a5d2da2SAndreas Gohr        header('Content-Type: application/json');
84*0a5d2da2SAndreas Gohr        echo $json->encode($data);
85*0a5d2da2SAndreas Gohr    }
86*0a5d2da2SAndreas Gohr
87*0a5d2da2SAndreas Gohr}
88*0a5d2da2SAndreas Gohr
89