xref: /plugin/struct/action/ajax.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
1914921fbSAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
3914921fbSAndreas Gohr/**
4914921fbSAndreas Gohr * DokuWiki Plugin struct (Action Component)
5914921fbSAndreas Gohr *
6914921fbSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7914921fbSAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8914921fbSAndreas Gohr */
9914921fbSAndreas Gohr
10*7234bfb1Ssplitbrainuse dokuwiki\Extension\ActionPlugin;
11*7234bfb1Ssplitbrainuse dokuwiki\Extension\EventHandler;
12*7234bfb1Ssplitbrainuse dokuwiki\Extension\Event;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
15914921fbSAndreas Gohr
16*7234bfb1Ssplitbrainclass action_plugin_struct_ajax extends ActionPlugin
17d6d97f60SAnna Dabrowska{
18914921fbSAndreas Gohr    /**
19914921fbSAndreas Gohr     * Registers a callback function for a given event
20914921fbSAndreas Gohr     *
21914921fbSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
22914921fbSAndreas Gohr     * @return void
23914921fbSAndreas Gohr     */
24*7234bfb1Ssplitbrain    public function register(EventHandler $controller)
25d6d97f60SAnna Dabrowska    {
26748e747fSAnna Dabrowska        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
27914921fbSAndreas Gohr    }
28914921fbSAndreas Gohr
29914921fbSAndreas Gohr    /**
30914921fbSAndreas Gohr     * Pass Ajax call to a type
31914921fbSAndreas Gohr     *
32914921fbSAndreas Gohr     * @param Doku_Event $event event object by reference
33914921fbSAndreas Gohr     */
34*7234bfb1Ssplitbrain    public function handleAjax(Event $event)
35d6d97f60SAnna Dabrowska    {
36914921fbSAndreas Gohr        if ($event->data != 'plugin_struct') return;
37914921fbSAndreas Gohr        $event->preventDefault();
38914921fbSAndreas Gohr        $event->stopPropagation();
399dfe4ab9SAndreas Gohr        global $conf;
40914921fbSAndreas Gohr
41914921fbSAndreas Gohr        header('Content-Type: application/json');
42914921fbSAndreas Gohr        try {
43914921fbSAndreas Gohr            $result = $this->executeTypeAjax();
44914921fbSAndreas Gohr        } catch (StructException $e) {
45*7234bfb1Ssplitbrain            $result = ['error' => $e->getMessage() . ' ' . basename($e->getFile()) . ':' . $e->getLine()];
469dfe4ab9SAndreas Gohr            if ($conf['allowdebug']) {
479dfe4ab9SAndreas Gohr                $result['stacktrace'] = $e->getTraceAsString();
489dfe4ab9SAndreas Gohr            }
49914921fbSAndreas Gohr            http_status(500);
50914921fbSAndreas Gohr        }
51914921fbSAndreas Gohr
5217dda596SAnna Dabrowska        echo json_encode($result);
53914921fbSAndreas Gohr    }
54914921fbSAndreas Gohr
55914921fbSAndreas Gohr    /**
56914921fbSAndreas Gohr     * Check the input variables and run the AJAX call
57914921fbSAndreas Gohr     *
58914921fbSAndreas Gohr     * @return mixed
590549dcc5SAndreas Gohr     * @throws StructException
60914921fbSAndreas Gohr     */
61d6d97f60SAnna Dabrowska    protected function executeTypeAjax()
62d6d97f60SAnna Dabrowska    {
63914921fbSAndreas Gohr        global $INPUT;
64914921fbSAndreas Gohr
65914921fbSAndreas Gohr        $col = $INPUT->str('column');
66914921fbSAndreas Gohr        if (blank($col)) throw new StructException('No column provided');
67*7234bfb1Ssplitbrain        [$schema, $colname] = explode('.', $col, 2);
68914921fbSAndreas Gohr        if (blank($schema) || blank($colname)) throw new StructException('Column format is wrong');
69914921fbSAndreas Gohr
70914921fbSAndreas Gohr        $schema = new Schema($schema);
71914921fbSAndreas Gohr        if (!$schema->getId()) throw new StructException('Unknown Schema');
72914921fbSAndreas Gohr
73914921fbSAndreas Gohr        $column = $schema->findColumn($colname);
74914921fbSAndreas Gohr        if ($column === false) throw new StructException('Column not found');
75914921fbSAndreas Gohr
76914921fbSAndreas Gohr        return $column->getType()->handleAjax();
77914921fbSAndreas Gohr    }
78914921fbSAndreas Gohr}
79