1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6use dokuwiki\plugin\qc\Output;
7
8/**
9 * DokuWiki Plugin qc (Action Component)
10 *
11 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12 * @author  Andreas Gohr <gohr@cosmocode.de>
13 */
14class action_plugin_qc_ajax extends ActionPlugin
15{
16    /**
17     * Registers a callback function for a given event
18     *
19     * @param EventHandler $controller DokuWiki's event controller object
20     * @return void
21     */
22    public function register(EventHandler $controller)
23    {
24        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax', []);
25    }
26
27    /**
28     * Out put the wanted HTML
29     *
30     * @param Event $event
31     * @param $param
32     */
33    public function ajax(Event $event, $param)
34    {
35        if (substr($event->data, 0, 10) != 'plugin_qc_') return;
36        $event->preventDefault();
37        $event->stopPropagation();
38        global $INPUT;
39
40        $id = cleanID($INPUT->str('id'));
41        if (blank($id)) die('no id given');
42
43        /** @var helper_plugin_qc $helper */
44        $helper = plugin_load('helper', 'qc');
45        if (!$helper->shouldShow($id)) {
46            http_status(404, 'No QC data available');
47            exit();
48        }
49
50        $out = new Output($id);
51        if ($event->data == 'plugin_qc_short') {
52            echo $out->short();
53        } elseif ($event->data == 'plugin_qc_long') {
54            echo $out->long();
55        }
56    }
57}
58