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