xref: /plugin/qc/action/ajax.php (revision 2c846d0808242a86f207429b961e3f883fd2b000)
11c845774SAndreas Gohr<?php
276bbc49cSAnna Dabrowska
32fc45e0cSsplitbrainuse dokuwiki\Extension\ActionPlugin;
42fc45e0cSsplitbrainuse dokuwiki\Extension\EventHandler;
52fc45e0cSsplitbrainuse dokuwiki\Extension\Event;
68ae469bfSAndreas Gohruse dokuwiki\plugin\qc\Output;
78ae469bfSAndreas Gohr
81c845774SAndreas Gohr/**
91c845774SAndreas Gohr * DokuWiki Plugin qc (Action Component)
101c845774SAndreas Gohr *
111c845774SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
121c845774SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
131c845774SAndreas Gohr */
142fc45e0cSsplitbrainclass action_plugin_qc_ajax extends ActionPlugin
1576bbc49cSAnna Dabrowska{
161c845774SAndreas Gohr    /**
171c845774SAndreas Gohr     * Registers a callback function for a given event
181c845774SAndreas Gohr     *
19*2c846d08Ssplitbrain     * @param EventHandler $controller DokuWiki's event controller object
201c845774SAndreas Gohr     * @return void
211c845774SAndreas Gohr     */
222fc45e0cSsplitbrain    public function register(EventHandler $controller)
2376bbc49cSAnna Dabrowska    {
242fc45e0cSsplitbrain        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax', []);
251c845774SAndreas Gohr    }
261c845774SAndreas Gohr
271c845774SAndreas Gohr    /**
281c845774SAndreas Gohr     * Out put the wanted HTML
291c845774SAndreas Gohr     *
30*2c846d08Ssplitbrain     * @param Event $event
311c845774SAndreas Gohr     * @param $param
321c845774SAndreas Gohr     */
332fc45e0cSsplitbrain    public function ajax(Event $event, $param)
3476bbc49cSAnna Dabrowska    {
351c845774SAndreas Gohr        if (substr($event->data, 0, 10) != 'plugin_qc_') return;
361c845774SAndreas Gohr        $event->preventDefault();
371c845774SAndreas Gohr        $event->stopPropagation();
381c845774SAndreas Gohr        global $INPUT;
391c845774SAndreas Gohr
401c845774SAndreas Gohr        $id = cleanID($INPUT->str('id'));
411c845774SAndreas Gohr        if (blank($id)) die('no id given');
421c845774SAndreas Gohr
4333274f74SAndreas Gohr        /** @var helper_plugin_qc $helper */
4433274f74SAndreas Gohr        $helper = plugin_load('helper', 'qc');
4533274f74SAndreas Gohr        if (!$helper->shouldShow($id)) {
4633274f74SAndreas Gohr            http_status(404, 'No QC data available');
4733274f74SAndreas Gohr            exit();
4833274f74SAndreas Gohr        }
4933274f74SAndreas Gohr
501c845774SAndreas Gohr        $out = new Output($id);
511c845774SAndreas Gohr        if ($event->data == 'plugin_qc_short') {
521c845774SAndreas Gohr            echo $out->short();
531c845774SAndreas Gohr        } elseif ($event->data == 'plugin_qc_long') {
541c845774SAndreas Gohr            echo $out->long();
551c845774SAndreas Gohr        }
561c845774SAndreas Gohr    }
571c845774SAndreas Gohr}
58