1<?php
2
3/** DokuWiki Plugin rating (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class syntax_plugin_rating extends DokuWiki_Syntax_Plugin {
13
14    /**
15     * What kind of syntax are we?
16     */
17    function getType() {
18        return 'protected';
19    }
20
21    /**
22     * Where to sort in?
23     */
24    function getSort() {
25        return 200;
26    }
27
28    /**
29     * Connect pattern to lexer
30     */
31    function connectTo($mode) {
32        $this->Lexer->addSpecialPattern('\\{\\{rating(?:.*?)\\}\\}', $mode, 'plugin_rating');
33    }
34
35    /**
36     * Handle the match
37     */
38    function handle($match, $state, $pos, Doku_Handler $handler) {
39        if ($state==DOKU_LEXER_SPECIAL) {
40            $options = array('lang' => null, 'startdate' => null, 'tag' => 'ul', 'score' => 'false' );
41            $match = rtrim($match,'\}');
42            $match = substr($match,8);
43            if ($match != '') {
44                $match = ltrim($match,'\|');
45                $match = explode(",", $match);
46                foreach($match as $option) {
47                    list($key, $val) = explode('=', $option);
48                    $options[$key] = $val;
49                }
50            }
51            return array($state, $options);
52        } else {
53            return array($state, '');
54        }
55    }
56
57    /**
58     * Create output
59     *
60     * @param string $format Renderer mode (supported modes: xhtml)
61     * @param Doku_Renderer $renderer The renderer
62     * @param array $data The data from the handler() function
63     * @return bool If rendering was successful.
64     */
65    function render($format, Doku_Renderer $renderer, $data) {
66        if($format == 'metadata') return false;
67        if($data[0] != DOKU_LEXER_SPECIAL) return false;
68
69        $hlp  = plugin_load('helper', 'rating');
70        $list = $hlp->best($data[1]['lang'],$data[1]['startdate'], 20);
71
72        if($data[1]['tag'] == 'ol') {
73            $renderer->listo_open();
74        } else {
75            $renderer->listu_open();
76        }
77
78        $num_items=0;
79        foreach($list as $item) {
80            if (auth_aclcheck($item['page'],'',null) < AUTH_READ) continue;
81            if (!page_exists($item['page'])) continue;
82            $num_items = $num_items +1;
83            $renderer->listitem_open(1);
84            if (strpos($item['page'],':') === false) {
85                $item['page'] = ':' . $item['page'];
86            }
87            $renderer->internallink($item['page']);
88            if ($data[1]['score'] === 'true') $renderer->cdata(' (' . $item['val'] . ')');
89
90            $renderer->listitem_close();
91            if ($num_items >= 10) break;
92        }
93
94        if($data[1]['tag'] == 'ol') {
95            $renderer->listo_close();
96        } else {
97            $renderer->listu_close();
98        }
99        return true;
100    }
101
102}
103