1<?php
2/**
3 * DokuWiki Plugin custombuttons (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Constantinos Xanthopoulos <conx@xanthopoulos.info>
7 */
8class action_plugin_custombuttons extends DokuWiki_Action_Plugin {
9
10    /**
11     * Registers a callback function for a given event
12     */
13    public function register(Doku_Event_Handler $controller) {
14        if ($this->loadCBData())
15            $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array());
16    }
17
18    /**
19     * Read config
20     *
21     * @return bool|mixed
22     */
23    protected function loadCBData() {
24        $file = @file_get_contents(DOKU_PLUGIN . "custombuttons/config.json");
25        if(!$file) return false;
26        return json_decode($file, true);
27    }
28
29    /**
30     * Build list of buttons
31     *
32     * @return array
33     */
34    protected function makelist() {
35        $conf = $this->loadCBData();
36
37        $buttonlist = array();
38        foreach ($conf as $button) {
39            $ico = '../../plugins/custombuttons/';
40            if (!$button['icon']) {
41                $ico .= 'genpng.php?text='. $button['label'];
42            } else {
43                $ico .= 'ico/'. $button['icon'];
44            }
45
46            if ($button['type'] == 1) {
47                $buttonlist[] = array(
48                    'type' => 'format',
49                    'title' => $button['label'],
50                    'icon' => $ico,
51                    'open' => $button['pretag'],
52                    'close' => $button['posttag']
53                );
54            } else {
55                $buttonlist[] = array(
56                    'type' => 'insert',
57                    'title' => $button['label'],
58                    'icon' => $ico,
59                    'insert' => $button['code'],
60                    'block' => true
61                );
62            }
63        }
64        return $buttonlist;
65    }
66
67    /**
68     * Add list with buttons to toolbar
69     *
70     * @param Doku_Event $event
71     * @param            $param
72     */
73    public function insert_button(Doku_Event $event, $param) {
74        $buttonlist = $this->makelist();
75
76        if ($this->getConf('usepicker')) {
77            $event->data[] = array(
78                'type' => 'picker',
79                'title' => $this->getLang('picker'),
80                'icon' => '../../plugins/custombuttons/custom.png',
81                'list' => $buttonlist
82            );
83        } else {
84            $event->data = array_merge($event->data, $buttonlist);
85        }
86
87    }
88}
89
90