1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Michael Klier <chi@chimeric.de>
5 */
6
7/**
8 * Class action_plugin_blogtng_entry
9 */
10class action_plugin_blogtng_entry extends DokuWiki_Action_Plugin{
11
12    /** @var helper_plugin_blogtng_entry */
13    var $entryhelper = null;
14    /** @var helper_plugin_blogtng_comments */
15    var $commenthelper = null;
16
17    function __construct() {
18        $this->entryhelper = plugin_load('helper', 'blogtng_entry');
19        $this->commenthelper = plugin_load('helper', 'blogtng_comments');
20    }
21
22    /**
23     * Registers a callback function for a given event
24     *
25     * @param Doku_Event_Handler $controller
26     */
27    function register(Doku_Event_Handler $controller) {
28        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'replaceUsualPageViewWithBlogView', array());
29        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addNextPrevNavigationToMetadata', array ());
30    }
31
32    /**
33     * Intercept the usual page display and replace it with a
34     * blog template controlled one.
35     *
36     * @param Doku_Event $event  event object by reference
37     * @param array      $param  empty array as passed to register_hook()
38     * @return bool
39     */
40    function replaceUsualPageViewWithBlogView(Doku_Event $event, $param) {
41        global $ID;
42        if($event->data != 'show') return false;
43
44        $pid = md5($ID);
45        $this->entryhelper->load_by_pid($pid);
46        if($this->entryhelper->get_blog() == '') return true;
47
48        // we can handle it
49        $event->preventDefault();
50
51        $this->commenthelper->setPid($pid);
52        $this->entryhelper->tpl_content($this->entryhelper->entry['blog'], 'entry');
53        return true;
54    }
55
56    /**
57     * Add next and prev meta headers for navigating through
58     * blog posts
59     *
60     * @param Doku_Event $event  event object by reference
61     * @param array      $param  empty array as passed to register_hook()
62     * @return bool
63     */
64    function addNextPrevNavigationToMetadata(Doku_Event $event, $param) {
65        global $ACT, $ID;
66
67        if ($ACT != 'show')
68            return false;
69
70        $pid = md5($ID);
71        $this->entryhelper->load_by_pid($pid);
72        if($this->entryhelper->get_blog() == '') return true;
73
74        $relatedentries = $this->entryhelper->getAdjacentLinks($ID);
75        if (isset ($relatedentries['prev'])) {
76            $event->data['link'][] = array (
77                'rel' => 'prev',
78                'href' => wl($relatedentries['prev']['page'])
79            );
80        }
81        if (isset ($relatedentries['next'])) {
82            $event->data['link'][] = array (
83                'rel' => 'next',
84                'href' => wl($relatedentries['next']['page'])
85            );
86        }
87
88        return true;
89    }
90}
91