xref: /plugin/struct/action/ajax.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
1914921fbSAndreas Gohr<?php
2*d6d97f60SAnna 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
10914921fbSAndreas Gohr// must be run within Dokuwiki
11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
13914921fbSAndreas Gohr
14914921fbSAndreas Gohrif (!defined('DOKU_INC')) die();
15914921fbSAndreas Gohr
16*d6d97f60SAnna Dabrowskaclass action_plugin_struct_ajax extends DokuWiki_Action_Plugin
17*d6d97f60SAnna Dabrowska{
18914921fbSAndreas Gohr
19914921fbSAndreas Gohr    /**
20914921fbSAndreas Gohr     * Registers a callback function for a given event
21914921fbSAndreas Gohr     *
22914921fbSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
23914921fbSAndreas Gohr     * @return void
24914921fbSAndreas Gohr     */
25*d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
26*d6d97f60SAnna Dabrowska    {
27914921fbSAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
28914921fbSAndreas Gohr    }
29914921fbSAndreas Gohr
30914921fbSAndreas Gohr    /**
31914921fbSAndreas Gohr     * Pass Ajax call to a type
32914921fbSAndreas Gohr     *
33914921fbSAndreas Gohr     * @param Doku_Event $event event object by reference
34914921fbSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
35914921fbSAndreas Gohr     *                           handler was registered]
36914921fbSAndreas Gohr     */
37*d6d97f60SAnna Dabrowska    public function handle_ajax(Doku_Event $event, $param)
38*d6d97f60SAnna Dabrowska    {
39914921fbSAndreas Gohr        if ($event->data != 'plugin_struct') return;
40914921fbSAndreas Gohr        $event->preventDefault();
41914921fbSAndreas Gohr        $event->stopPropagation();
429dfe4ab9SAndreas Gohr        global $conf;
43914921fbSAndreas Gohr
44914921fbSAndreas Gohr        header('Content-Type: application/json');
45914921fbSAndreas Gohr        try {
46914921fbSAndreas Gohr            $result = $this->executeTypeAjax();
47914921fbSAndreas Gohr        } catch (StructException $e) {
48914921fbSAndreas Gohr            $result = array(
499dfe4ab9SAndreas Gohr                'error' => $e->getMessage() . ' ' . basename($e->getFile()) . ':' . $e->getLine()
50914921fbSAndreas Gohr            );
519dfe4ab9SAndreas Gohr            if ($conf['allowdebug']) {
529dfe4ab9SAndreas Gohr                $result['stacktrace'] = $e->getTraceAsString();
539dfe4ab9SAndreas Gohr            }
54914921fbSAndreas Gohr            http_status(500);
55914921fbSAndreas Gohr        }
56914921fbSAndreas Gohr
57*d6d97f60SAnna Dabrowska        $json = new JSON();
58914921fbSAndreas Gohr        echo $json->encode($result);
59914921fbSAndreas Gohr    }
60914921fbSAndreas Gohr
61914921fbSAndreas Gohr    /**
62914921fbSAndreas Gohr     * Check the input variables and run the AJAX call
63914921fbSAndreas Gohr     *
64914921fbSAndreas Gohr     * @throws StructException
65914921fbSAndreas Gohr     * @return mixed
66914921fbSAndreas Gohr     */
67*d6d97f60SAnna Dabrowska    protected function executeTypeAjax()
68*d6d97f60SAnna Dabrowska    {
69914921fbSAndreas Gohr        global $INPUT;
70914921fbSAndreas Gohr
71914921fbSAndreas Gohr        $col = $INPUT->str('column');
72914921fbSAndreas Gohr        if (blank($col)) throw new StructException('No column provided');
73914921fbSAndreas Gohr        list($schema, $colname) = explode('.', $col, 2);
74914921fbSAndreas Gohr        if (blank($schema) || blank($colname)) throw new StructException('Column format is wrong');
75914921fbSAndreas Gohr
76914921fbSAndreas Gohr        $schema = new Schema($schema);
77914921fbSAndreas Gohr        if (!$schema->getId()) throw new StructException('Unknown Schema');
78914921fbSAndreas Gohr
79914921fbSAndreas Gohr        $column = $schema->findColumn($colname);
80914921fbSAndreas Gohr        if ($column === false) throw new StructException('Column not found');
81914921fbSAndreas Gohr
82914921fbSAndreas Gohr        return $column->getType()->handleAjax();
83914921fbSAndreas Gohr    }
84914921fbSAndreas Gohr}
85