1<?php
2/**
3 * DokuWiki Plugin starred (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once(DOKU_PLUGIN.'syntax.php');
17
18class syntax_plugin_starred extends DokuWiki_Syntax_Plugin {
19
20    /** @inheritdoc */
21    function getType() { return 'substition'; }
22
23    /** @inheritdoc */
24    function getPType() { return 'block'; }
25
26    /** @inheritdoc */
27    function getSort() { return 155; }
28
29    /** @inheritdoc */
30    function connectTo($mode) {
31        $this->Lexer->addSpecialPattern('{{starred(?:>min)?(?:\|\d+)?}}',$mode,'plugin_starred');
32    }
33
34    /** @inheritdoc */
35    function handle($match, $state, $pos, Doku_Handler $handler){
36        preg_match('{{starred((?:>min)?)\|?(\d*)}}', $match, $matches);
37        return array('min' => $matches[1] !== '',
38                     'limit' => $matches[2]);
39    }
40
41    /** @inheritdoc */
42    function render($mode, Doku_Renderer $R, $data) {
43        if($mode != 'xhtml') return false;
44        /** @var Doku_Renderer_xhtml $R */
45        $R->info['cache'] = false;
46
47        if(!isset($_SERVER['REMOTE_USER'])){
48            $R->cdata($this->getLang('login'));
49            return true;
50        }
51
52        /** @var helper_plugin_starred $hlp */
53        $hlp = plugin_load('helper', 'starred');
54        $starred = $hlp->loadStars(null, $data['limit']);
55
56        $R->doc .= '<div class="plugin_starred">';
57        if(!count($starred)){
58            if (!$data['min']) {
59                $R->doc .= '<p>';
60                $R->cdata($this->getLang('none'));
61                $R->p_close();
62            }
63            $R->doc .= '</div>';
64            return true;
65        }
66
67        $R->doc .= '<ul>';
68        foreach($starred as $pid => $time){
69            $R->listitem_open(1);
70            $R->listcontent_open();
71            $R->internallink(':'.$pid,null,null,false,'navigation');
72            if (!$data['min']) {
73                $R->cdata(' '.dformat($time,'%f'));
74            }
75            global $ID;
76            $R->doc .= $hlp->starHtml($ID, $pid, false);
77            $R->listcontent_close();
78            $R->listitem_close();
79        }
80        $R->listu_close();
81        $R->doc .= '</div>';
82    }
83}
84
85// vim:ts=4:sw=4:et:enc=utf-8:
86