1<?php
2/**
3 * DokuWiki Plugin DocNavigation (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Gerrit Uitslag <klapinklapin@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12require_once(DOKU_INC.'inc/parser/xhtml.php');
13
14/**
15 * Add documentation navigation elements around page
16 */
17class action_plugin_docnavigation extends DokuWiki_Action_Plugin {
18
19    /**
20     * Register the events
21     *
22     * @param Doku_Event_Handler $controller
23     */
24    public function register(Doku_Event_Handler $controller) {
25        $controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, '_addtopnavigation');
26    }
27
28    /**
29     * Add navigation bar to top of content
30     *
31     * @param Doku_Event $event
32     * @param            $param
33     */
34    public function _addtopnavigation(Doku_Event &$event, $param) {
35        global $ACT;
36
37        if($event->data[0] != 'xhtml' || !in_array($ACT, array('show', 'preview'))) return;
38
39        $event->data[1] = $this->getNavbar($linktoToC = false)
40                        . $event->data[1]
41                        . $this->getNavbar($linktoToC = true);
42    }
43
44    /**
45     * Return html of navigation elements
46     *
47     * @param bool $linktoToC add referer to ToC
48     * @return string
49     */
50    private function getNavbar($linktoToC = true) {
51        global $ID;
52        global $ACT;
53        $data = array();
54        if($ACT == 'preview') {
55            // the RENDERER_CONTENT_POSTPROCESS event is triggered just after rendering the instruction,
56            // so syntax instance will exists
57            /** @var syntax_plugin_docnavigation_pagenav $pagenav */
58            $pagenav = plugin_load('syntax', 'docnavigation_pagenav');
59            if($pagenav) {
60                $data = $pagenav->data[$ID];
61            }
62        } else {
63            $data = p_get_metadata($ID, 'docnavigation');
64        }
65
66        $out = '';
67        if(!empty($data)) {
68            /** @var Doku_Renderer_xhtml $Renderer */
69            static $Renderer = null;
70            if(is_null($Renderer)){
71                $Renderer = p_get_renderer('xhtml');
72            }
73
74            if($linktoToC) {
75                $out .= '<div class="clearer"></div>'.DOKU_LF;
76            }
77
78            $out .= '<div class="docnavbar'.($linktoToC ? ' showtoc' : '').'">'.DOKU_LF.DOKU_TAB.'<div class="leftnav">';
79            if($data['previous'][0]) {
80                $title = $this->getTitle($data['previous'], $Renderer);
81                $out .= '← '.$Renderer->internallink($data['previous'][0], $title, null, true);
82            }
83            $out .= '&nbsp;</div>'.DOKU_LF;
84
85            if($linktoToC) {
86                $out .= DOKU_TAB.'<div class="centernav">';
87                if($data['toc'][0]) {
88                    $title = $this->getTitle($data['toc'], $Renderer);
89                    $out .= $Renderer->internallink($data['toc'][0], $title, null, true);
90                }
91                $out .= '&nbsp;</div>'.DOKU_LF;
92            }
93
94            $out .= DOKU_TAB.'<div class="rightnav">&nbsp;';
95            if($data['next'][0]) {
96                $title = $this->getTitle($data['next'], $Renderer);
97                $out .= $Renderer->internallink($data['next'][0], $title, null, true).' →';
98            }
99            $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
100        }
101        return $out;
102    }
103
104    /**
105     * Build nice url title, if no title given use original link with original not cleaned id
106     *
107     * @param array $link
108     * @param Doku_Renderer_xhtml $Renderer
109     * @return string
110     */
111    protected function getTitle($link, $Renderer) {
112        if($link[1] === null) {
113            $defaulttitle = $Renderer->_simpleTitle($link[2]);
114            return $Renderer->_getLinkTitle(null, $defaulttitle, $link[0]);
115        }
116        return $link[1];
117    }
118}
119
120// vim:ts=4:sw=4:et:
121