1<?php
2/**
3 * popoutviewer Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once(DOKU_PLUGIN.'syntax.php');
15
16class syntax_plugin_tipoftheday_totd extends DokuWiki_Syntax_Plugin {
17
18    private $hasSections = array();
19
20    function getType() { return 'substition'; }
21    function getPType() { return 'block'; }
22    function getSort() { return 302; }
23
24    function connectTo($mode) {
25        $this->Lexer->addSpecialPattern('{{totd>.+?}}', $mode, 'plugin_totd_totd');
26    }
27
28    function handle($match, $state, $pos, Doku_Handler $handler) {
29
30        $match = substr($match, 2, -2); // strip markup
31        list($match, $flags) = explode('&', $match, 2);
32
33        // break the pattern up into its parts
34        list($mode, $page, $sect) = preg_split('/>|#/u', $match, 3);
35
36        $return = array($page, explode('&', $flags));
37        return $return;
38    }
39
40    function render($mode, Doku_Renderer $renderer, $data) {
41        global $ID;
42
43        if ( $mode == 'xhtml' ) {
44            $renderer->nocache();
45
46            list($page, $flags) = $data;
47            if ( !is_Array($flags) ) {
48                $flags = array($flags);
49            }
50
51            $sections = $this->_getSections($page);
52
53            // Check for given totd section
54            if ( empty($_REQUEST['totd']) ) {
55                $section = $sections[array_rand($sections)];
56            } else {
57                $section = $_REQUEST['totd'];
58            }
59
60            if ( empty($section) ) $section = $sections[0];
61
62            $this->hasSections[] = cleanID($section); // Prevent selecting the same section again
63
64            $helper = plugin_load('helper', 'include');
65            $ins = $helper->_get_instructions($page, cleanID($section), $mode, $renderer->lastlevel, $flags);
66
67            $renderer->doc .= '<div id="totd_plugin">';
68            $renderer->doc .= '<div class="totd-header">';
69            $renderer->doc .= '<h1>Tip of the Day</h1>';
70            $renderer->doc .= '</div>';
71            $renderer->doc .= '<div class="totd-content">';
72
73            $renderer->doc .= p_render($mode, $ins, $myINFO);
74
75            $current = array_search($section, $sections);
76
77            // Index for next and previous entry
78            $prev = ($current == 0 ? count($sections) : $current) -1;
79            $next = ($current >= count($sections)-1 ? 0 : $current +1);
80
81            $renderer->doc .= '</div>';
82            $renderer->doc .= '<div class="totd-footer">';
83            $renderer->doc .= tpl_link(wl($ID, array('totd' => $sections[$prev])), "&lt;", 'title="previous" onclick="totd_loadnew(\'' . $page . (count($flags)>0 ? '%26' . implode('%26', $flags) : '') . '\', \'' . $sections[$prev] . '\'); return false;"', true );
84            $renderer->doc .= tpl_link(wl($ID, array('totd' => $sections[$next])), "&gt;", 'title="next" onclick="totd_loadnew(\'' . $page . (count($flags)>0 ? '%26' . implode('%26', $flags) : '') . '\', \'' . $sections[$next] . '\'); return false;"', true );
85            $renderer->doc .= '</div>';
86            $renderer->doc .= '</div>';
87
88            return true;
89        }
90
91        return false;
92    }
93
94    /**
95     * Get a section including its subsections
96     */
97    function _getSections($id) {
98
99        $headers = array();
100        $instructions = p_cached_instructions(wikiFN($id));
101        if ( !is_array($instructions) ) return array();
102
103        foreach ($instructions as $ins) {
104            if ($ins[0] == 'header') {
105                if ( in_array(cleanID($ins[1][0]), $this->hasSections) ) { continue; }
106                $headers[] = $ins[1][0]; // section name
107            }
108        }
109
110        return $headers;
111    }
112}
113// vim:ts=4:sw=4:et:enc=utf-8:
114