1<?php
2/**
3 * DokuWiki Plugin scrapbook (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_scrapbook 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('TOOLBAR_DEFINE', 'AFTER', $this, 'handle_toolbar_define');
23        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call');
24    }
25
26    /**
27     * Register a new toolbar button
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    public function handle_toolbar_define(Doku_Event $event, $param) {
35
36        $event->data[] = array(
37            'type' => 'plugin_scrapbook',
38            'title' => 'insert from scrapbook',
39            'icon' => DOKU_BASE . 'lib/plugins/scrapbook/icon.png',
40        );
41    }
42
43    /**
44     * List available templates
45     *
46     * @param Doku_Event $event event object by reference
47     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
48     *                           handler was registered]
49     * @return void
50     */
51    public function handle_ajax_call(Doku_Event $event, $param) {
52        if($event->data != 'plugin_scrapbook') return;
53        $event->preventDefault();
54        $event->stopPropagation();
55        global $conf;
56        global $lang;
57
58        header('Content-Type: text/html; charset=utf-8');
59
60        $data = array();
61        search(
62            $data,
63            $conf['datadir'],
64            'search_universal',
65            array(
66                'depth' => 1,
67                'pagesonly' => true,
68                'listfiles' => true,
69                'firsthead' => true
70            ),
71            utf8_encodeFN(str_replace(':', '/', $this->getConf('ns')))
72        );
73
74        if(!$data) {
75            echo '<div>'.$lang['nothingfound'].'</div>';
76        } else foreach($data as $page) {
77            if($page['title']) {
78                $title = $page['title'];
79            } else {
80                $title = str_replace(array('_', '-'), array(' ', ' - '), noNS($page['id']));
81            }
82            echo '<button data-id="' . hsc($page['id']) . '">' . hsc($title) . '</button>';
83        }
84    }
85}
86
87// vim:ts=4:sw=4:et:
88