1<?php
2/**
3 * DokuWiki Plugin miniblog
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Esther Brunner <wikidesign@gmail.com> (upstream)
7 * @author  Robert Rackl <wiki@doogie.de> (upstream)
8 * @author  lainme <lainme993@gmail.com>
9 */
10
11// must be run within Dokuwiki
12if (!defined('DOKU_INC')) die();
13
14class syntax_plugin_miniblog_entry extends DokuWiki_Syntax_Plugin {
15    public function getType() {
16        return 'substition';
17    }
18
19    public function getSort() {
20        return 380;
21    }
22
23    public function getPType() {
24        return 'block';
25    }
26
27    public function connectTo($mode) {
28        $this->Lexer->addSpecialPattern('<miniblog.*>', $mode, 'plugin_miniblog_entry');
29    }
30
31    public function handle($match, $state, $pos, Doku_Handler $handler){
32        return trim(mb_substr($match, 10, -1));
33    }
34
35    public function render($mode, Doku_Renderer $renderer, $data) {
36        global $INPUT;
37        global $INFO;
38
39        if ($mode != 'xhtml') return false;
40
41        // disable cache and toc
42        $renderer->info['cache'] = false;
43        $INFO['prependTOC'] = false;
44
45        $entries = plugin_load('helper', 'miniblog_entry')->entry_list('blog');
46        $num = 5; // display 5 entries per page
47
48        // slice
49        $page = $num*$INPUT->int('page', 0); // index of first entry in current page
50        $less = (($page > 0) ? max(0, ($page-$num)/$num) : -1); // previous page
51        $more = ((count($entries) > $page+$num) ? ($page+$num)/$num : -1); // next page
52        $entries = array_slice($entries, $page, $num);
53
54        // comment count
55        $source = 'count.js';
56        $option = array('disqus_shortname' => $this->getConf('shortname'));
57        $renderer->doc .= plugin_load('helper', 'miniblog_comment')->comment_script($source, $option);
58
59        // contents
60        if ($data == 'twentyfifteen') {
61            $this->render_twentyfifteen($renderer, $entries, $less, $more);
62        } else {
63            $this->render_default($renderer, $entries, $less, $more);
64        }
65        return true;
66    }
67
68    function render_default($renderer, $entries, $less, $more) {
69        global $ID;
70
71        // show entries
72        foreach ($entries as $entry) {
73            list($head, $content) = plugin_load('helper', 'miniblog_entry')->entry_content($entry['id']);
74
75            $renderer->doc .= '<h1><a href="'.wl($entry['id']).'">'.$head.'</a></h1>';
76            $renderer->doc .= '<p class="miniblog_info">';
77            $renderer->doc .= dformat($entry['date']).' · '.$entry['user'].' · <a href="'.wl($entry['id'],'',true).'#disqus_thread"></a>';
78            $renderer->doc .= '</p>';
79            $renderer->doc .= html_secedit($content, false); // no section edit button
80        }
81
82        // pagination
83        if ($less !== -1) {
84            $renderer->doc .= '<p class="medialeft"><a href="'.wl($ID, 'page='.$less).'" class="wikilink1">'.$this->getLang('newer').'</a></p>';
85        }
86        if ($more !== -1) {
87            $renderer->doc .= '<p class="mediaright"><a href="'.wl($ID, 'page='.$more).'" class="wikilink1">'.$this->getLang('older').'</a></p>';
88        }
89    }
90
91    function render_twentyfifteen($renderer, $entries, $less, $more) {
92        global $ID;
93
94        // show entries
95        foreach ($entries as $entry) {
96            list($head, $content) = plugin_load('helper', 'miniblog_entry')->entry_content($entry['id']);
97
98            $renderer->doc .= '<article class="entry">';
99            $renderer->doc .= '<div class="entry-content">';
100            $renderer->doc .= '<h1><a href="'.wl($entry['id']).'">'.$head.'</a></h1>';
101            $renderer->doc .= html_secedit($content, false); // no section edit button
102            $renderer->doc .= '</div>';
103            $renderer->doc .= '<footer class="entry-footer">';
104            $renderer->doc .= '<span class="entry-date">'.dformat($entry['date']).'</span>';
105            $renderer->doc .= '<span class="entry-author">'.$entry['user'].'</span>';
106            $renderer->doc .= '<span class="entry-comment"><a href="'.wl($entry['id'],'',true).'#disqus_thread"></a></span>';
107            $renderer->doc .= '</footer>';
108            $renderer->doc .= '</article>';
109        }
110
111        // pagination
112        $renderer->doc .= '<article class="entry entry-navigation"><div class="entry-content">';
113        if ($less !== -1) {
114            $renderer->doc .= '<p class="medialeft"><a href="'.wl($ID, 'page='.$less).'" class="wikilink1">'.$this->getLang('newer').'</a></p>';
115        }
116        if ($more !== -1) {
117            $renderer->doc .= '<p class="mediaright"><a href="'.wl($ID, 'page='.$more).'" class="wikilink1">'.$this->getLang('older').'</a></p>';
118        }
119        $renderer->doc .= '</div></article>';
120    }
121}
122