1<?php
2
3/**
4 * Bloglinks Plugin: displays a link to the previous and the next blog entry above posts in configured namespaces
5 *
6 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author   Gina Haeussge <osd@foosel.net>
8 */
9
10class action_plugin_bloglinks extends DokuWiki_Action_Plugin {
11
12    /**
13     * Register the eventhandlers.
14     */
15    function register(Doku_Event_Handler $controller) {
16        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'handle_act_render', array ());
17        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_metaheader_output', array ());
18    }
19
20    function handle_metaheader_output(Doku_Event $event, $params) {
21        global $ACT;
22
23        if ($ACT != 'show')
24            return;
25
26        $namespace = $this->_getActiveNamespace();
27        if (!$namespace)
28            return;
29
30        $relatedEntries = $this->_getRelatedEntries($namespace);
31
32        if (isset ($relatedEntries['prev'])) {
33            $event->data['link'][] = array (
34                'rel' => 'prev',
35                'href' => wl($relatedEntries['prev']['id'], '')
36            );
37        }
38        if (isset ($relatedEntries['next'])) {
39            $event->data['link'][] = array (
40                'rel' => 'next',
41                'href' => wl($relatedEntries['next']['id'], '')
42            );
43        }
44
45        return true;
46    }
47
48    function handle_act_render(Doku_Event $event, $params) {
49        global $ACT;
50
51        if ($ACT != 'show')
52            return;
53
54        $namespace = $this->_getActiveNamespace();
55        if ($namespace)
56            $this->_printLinks($this->_getRelatedEntries($namespace));
57
58        return true;
59    }
60
61    function _getActiveNamespace() {
62        global $ID;
63        global $INFO;
64
65        $pattern = $this->getConf('excluded_pages');
66        if (strlen($pattern) > 0 && preg_match($pattern, $ID)) {
67            return false;
68        }
69
70        if (!$INFO['exists'])
71            return false;
72
73        $namespaces = explode(',', $this->getConf('enabled_namespaces'));
74        foreach ($namespaces as $namespace) {
75            if (trim($namespace) && (strpos($ID, $namespace . ':') === 0)) {
76                return $namespace;
77            }
78        }
79
80        return false;
81    }
82
83    function _getRelatedEntries($namespace) {
84        global $ID;
85
86        // get the blog entries for the namespace
87        if ($my = & plugin_load('helper', 'blog'))
88            $entries = $my->getBlog($namespace);
89
90        if (!$entries)
91            return;
92
93        // normalize keys
94        $entries = array_values($entries);
95
96        // prepare search for current page
97        $meta = p_get_metadata($ID);
98        if ($my->sort == 'mdate') {
99            $date = $meta['date']['modified'];
100            if (!$date) $date = filemtime(wikiFN($ID));
101        } else {
102            $date = $meta['date']['created'];
103            if (!$date) $date = filectime(wikiFN($ID));
104        }
105        $perm = auth_quickaclcheck($ID);
106        $curPage = array (
107            'id' => $ID,
108            'title' => $meta['title'],
109            'date' => $date,
110            'user' => $meta['creator'],
111            'desc' => $meta['description']['abstract'],
112            'exists' => true,
113            'perm' => $perm,
114            'draft' => ($meta['type'] == 'draft'),
115        );
116
117        // get index of current page
118        $curIndex = array_search($curPage, $entries);
119
120        // get previous and next entries
121        if ($curIndex > 0 && $curIndex < count($entries) - 1) { // got a prev and a next
122            list ($next, $cur, $prev) = array_slice($entries, $curIndex -1, 3);
123        } else if ($curIndex == 0) { // only got a prev
124            list ($cur, $prev) = array_slice($entries, $curIndex, 2);
125        } else { // only got a next
126            list ($next, $cur) = array_slice($entries, $curIndex -1, 2);
127        }
128
129        return array('prev' => $prev, 'cur' => $cur, 'next' => $next);
130    }
131
132    /**
133     * Prints the links to the related entries
134     */
135    function _printLinks($relatedEntries) {
136        // display links
137        echo '<div id="plugin_bloglinks__links">';
138
139        foreach(array('prev', 'next') as $type) {
140            if (isset ($relatedEntries[$type])) {
141                echo '<div class="plugin_bloglinks__'.$type.'">';
142                echo '<a href="' . wl($relatedEntries[$type]['id'], '') . '" class="wikilink1" rel="'.$type.'">' . $this->_linkTemplate($relatedEntries[$type], $type) . '</a>';
143                echo '</div>';
144            }
145        }
146        echo DOKU_LF . '</div>';
147    }
148
149    function _linkTemplate($entry, $type) {
150        global $conf;
151
152        $replace = array(
153            '@@TITLE@@' => $entry['title'],
154            '@@AUTHOR@@' => $entry['user'],
155            '@@DATE@@' => date($conf['dformat'], $entry['date']),
156            '@@NAME@@' => $this->getLang($type . '_link'),
157        );
158
159        $linktext = $this->getConf($type.'_template');
160        $linktext = str_replace(array_keys($replace), array_values($replace), $linktext);
161        return $linktext;
162    }
163}
164
165//Setup VIM: ex: et ts=4 enc=utf-8 :
166