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