1<?php
2/**
3 * DokuWiki Plugin filelisting (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Szymon Olewniczak <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_filelisting_ajax extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22       $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
23
24    }
25
26    /**
27     * Send the namespace files as html table rows
28     *
29     * @param Doku_Event $event  event object by reference
30     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
31     *                           handler was registered]
32     * @return void
33     */
34
35    public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
36        if($event->data != 'plugin_filelisting') return;
37        $event->preventDefault();
38        $event->stopPropagation();
39
40        global $INPUT;
41
42        $ns = $INPUT->str('namespace');
43        $baseNs = $INPUT->str('baseNamespace');
44        $lvl = $this->getNumberOfSubnamespaces($ns) - $this->getNumberOfSubnamespaces($baseNs);
45
46        /** @var helper_plugin_filelisting $filelisting */
47        $filelisting = $this->loadHelper('filelisting');
48
49        $elements = $filelisting->getFilesRows($ns, $lvl, $INPUT->bool('filesOnly'));
50        foreach($elements as $element) {
51            echo $element->toHTML();
52        }
53    }
54
55    /**
56     * Calculate the number of subnamespaces, the given namespace is consisting of
57     *
58     * @param string $namespace
59     * @return int
60     */
61    protected function getNumberOfSubnamespaces($namespace) {
62        $cleanedNamespace = trim($namespace, ':');
63        if ($cleanedNamespace === '') {
64            return 0;
65        }
66        return substr_count($cleanedNamespace, ':') + 1;
67    }
68
69}
70
71// vim:ts=4:sw=4:et:
72