1<?php
2/**
3 * DokuWiki Plugin twistienav for Bootstrap 3 template (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author: Paolo Maggio <maggio.p@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'action.php';
17
18class action_plugin_twistienav4bootstrap3 extends DokuWiki_Action_Plugin {
19
20//    protected $title_metadata = array();
21//    protected $exclusions     = array();
22
23    function __construct() {
24        global $conf;
25
26        // Load TwistieNav helper component
27        $this->helper = plugin_load('helper','twistienav4bootstrap3');
28
29        // Get some variables frome helper
30        $this->title_metadata = $this->helper->build_titlemetafields();
31        list($this->exclusions, $this->nsignore) = $this->helper->build_exclusions();
32
33    }
34
35    /**
36     * Register event handlers
37     */
38    public function register(Doku_Event_Handler $controller) {
39        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'populate_jsinfo', array());
40        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call', array());
41    }
42
43    /**
44     * Populate configuration settings to JSINFO
45     */
46    function populate_jsinfo(Doku_Event $event, $params) {
47        global $JSINFO, $conf, $ID;
48
49        // Store settings values in JSINFO
50        $JSINFO['conf']['start'] = $conf['start'];
51        $JSINFO['conf']['breadcrumbs'] = $conf['breadcrumbs'];
52        $JSINFO['conf']['youarehere'] = $conf['youarehere'];
53        $JSINFO['plugin_twistienav4bootstrap3']['twistiemap'] = $this->getConf('twistieMap');
54        $JSINFO['plugin_twistienav4bootstrap3']['style'] = $this->getConf('style');
55
56        // List namespaces for YOUAREHERE breadcrumbs
57        $yah_ns = array(0 => '');
58        if ($conf['youarehere'] or ($this->getConf('pageIdTrace')) or ($this->getConf('pageIdExtraTwistie'))) {
59            $parts = explode(':', $ID);
60            $count = count($parts);
61            $part = '';
62            for($i = 0; $i < $count - 1; $i++) {
63                $part .= $parts[$i].':';
64                if ($part == $conf['start']) continue; // Skip start page
65                $elements = 0;
66                // Get index of current crumb namespace
67                $idx  = cleanID(getNS($part));
68                $dir  = utf8_encodeFN(str_replace(':','/',$idx));
69                $data = array();
70                search($data,$conf['datadir'],'search_index',array('ns' => $idx),$dir);
71                // Count pages that are not in configured exclusions
72                foreach ($data as $item) {
73                    if (!in_array(noNS($item['id']), $this->exclusions)) {
74                        $elements++;
75                    }
76                }
77                // If there's at least one page that isn't excluded, prepare JSINFO data for that crumb
78                if ($elements > 0) {
79                    $yah_ns[$i+1] = $idx;
80                }
81            }
82            $JSINFO['plugin_twistienav4bootstrap3']['yah_ns'] = $yah_ns;
83        }
84
85        // List namespaces for TRACE breadcrumbs
86        $bc_ns = array();
87        if ($conf['breadcrumbs'] > 0) {
88            $crumbs = breadcrumbs();
89            // get namespaces currently in $crumbs
90            $i = -1;
91            foreach ($crumbs as $crumbId => $crumb) {
92                $i++;
93                // Don't do anything unless 'startPagesOnly' setting is off
94                //  or current breadcrumb leads to a namespace start page
95                if (($this->getConf('startPagesOnly') == 0) or (noNS($crumbId) == $conf['start'])) {
96                    $elements = 0;
97                    // Get index of current crumb namespace
98                    $idx  = cleanID(getNS($crumbId));
99                    $dir  = utf8_encodeFN(str_replace(':','/',$idx));
100                    $data = array();
101                    search($data,$conf['datadir'],'search_index',array('ns' => $idx),$dir);
102                    // Count pages that are not in configured exclusions
103                    foreach ($data as $item) {
104                        if (!in_array(noNS($item['id']), $this->exclusions)) {
105                            $elements++;
106                        }
107                    }
108                    // If there's at least one page that isn't excluded, prepare JSINFO data for that crumb
109                    if ($elements > 0) {
110                        $bc_ns[$i] = $idx;
111                    }
112                }
113            }
114            $JSINFO['plugin_twistienav4bootstrap3']['bc_ns'] = $bc_ns;
115        }
116
117        // Build 'pageIdTrace' skeleton if required
118        if (($this->getConf('pageIdTrace')) or ($this->getConf('pageIdExtraTwistie'))) {
119            $skeleton = '<span>';
120            if ($this->getConf('pageIdTrace')) {
121                $parts = explode(':', $ID);
122                $count = count($parts);
123                $part = '';
124                for($i = 1; $i < $count; $i++) {
125                    $part .= $parts[$i-1].':';
126                    if ($part == $conf['start']) continue; // Skip startpage
127                    if (isset($yah_ns[$i])) {
128                        $skeleton .= '<a href="javascript:void(0)">'.$parts[$i-1].'</a>:';
129                    } else {
130                        $skeleton .= $parts[$i-1].':';
131                    }
132                }
133                $skeleton .= end($parts);
134            } else {
135                $skeleton .= $ID;
136            }
137            if ($this->getConf('pageIdExtraTwistie')) {
138                $skeleton .= '<a href="javascript:void(0)" ';
139                $skeleton .= 'class="twistienav_extratwistie'.' '.$this->getConf('style');
140                $skeleton .= ($this->getConf('twistieMap')) ? ' twistienav_map' : '';
141                $skeleton .= '"></a>';
142            }
143            $skeleton .= '</span>';
144            $JSINFO['plugin_twistienav4bootstrap3']['pit_skeleton'] = $skeleton;
145        }
146    }
147
148    /**
149     * Ajax handler
150     */
151    function handle_ajax_call(Doku_Event $event, $params) {
152        global $conf;
153
154        $idx  = cleanID($_POST['idx']);
155
156        // Process AJAX calls from 'plugin_twistienav' or 'plugin_twistienav_pageid'
157        if (($event->data != 'plugin_twistienav4bootstrap3') && ($event->data != 'plugin_twistienav4bootstrap3_pageid') && ($event->data != 'plugin_twistienav4bootstrap3_nsindex')) return;
158        $event->preventDefault();
159        $event->stopPropagation();
160
161        // If AJAX caller is from 'pageId' we don't wan't to exclude start pages
162        if ($event->data == 'plugin_twistienav4bootstrap3_pageid') {
163            $exclusions = array_diff($this->exclusions, array($conf['start']));
164        } else {
165            $exclusions = $this->exclusions;
166        }
167        // If AJAX caller is from 'pageId' we don't wan't to exclude any pages
168        if ($event->data == 'plugin_twistienav4bootstrap3_pageid') {
169            $useexclusions = false;
170        } else {
171            $useexclusions = true;
172        }
173
174        $data = array();
175        $this->data = $this->helper->get_idx_data($idx, $useexclusions);
176        if (count($this->data) > 0) {
177            echo '<ul>';
178            foreach ($this->data as $item) {
179                echo '<li>'.$item['link'].'</li>';
180            }
181            echo '</ul>';
182        }
183    }
184
185}
186// vim: set fileencoding=utf-8 expandtab ts=4 sw=4 :
187