1<?php
2/**
3 * Dynamic Archive Plugin: dynamically displays
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Esther Brunner <wikidesign@gmail.com>
7 */
8
9/**
10 * All DokuWiki plugins to extend the parser/rendering mechanism
11 * need to inherit from this class
12 */
13class syntax_plugin_blog_autoarchive extends DokuWiki_Syntax_Plugin {
14
15    function getType() { return 'substition'; }
16    function getPType() { return 'block'; }
17    function getSort() { return 309; }
18
19    function connectTo($mode) {
20        $this->Lexer->addSpecialPattern('\{\{autoarchive>.*?\}\}', $mode, 'plugin_blog_autoarchive');
21    }
22
23    function handle($match, $state, $pos, Doku_Handler $handler) {
24        global $ID;
25
26        $match = substr($match, 14, -2); // strip {{autoarchive> from start and }} from end
27        list($match, $flags) = explode('?', $match, 2);
28        $flags = explode('&', $flags);
29        list($ns, $refine) = explode(' ', $match, 2);
30
31        if ($ns == '') $ns = cleanID($this->getConf('namespace'));
32        elseif (($ns == '*') || ($ns == ':')) $ns = '';
33        elseif ($ns == '.') $ns = getNS($ID);
34        else $ns = cleanID($ns);
35
36        return array($ns, $flags, $refine, $pos);
37    }
38
39    function render($mode, Doku_Renderer $renderer, $data) {
40        list($ns, $flags, $refine, $pos) = $data;
41        if ($mode != 'xhtml') return false;
42
43        // no caching for dynamic content
44        $renderer->nocache();
45
46        // get the blog entries for our namespace
47        if ($my = plugin_load('helper', 'blog')) $entries = $my->getBlog($ns);
48
49        // use tag refinements?
50        if ($refine) {
51            if (plugin_isdisabled('tag') || (!$tag = plugin_load('helper', 'tag'))) {
52                msg($this->getLang('missing_tagplugin'), -1);
53            } else {
54                $entries = $tag->tagRefine($entries, $refine);
55            }
56        }
57
58        if (!$entries) return true; // nothing to display
59
60        // what to display
61        if(preg_match('/^\d\d\d\d-\d\d$/',$_REQUEST['blogarchive'])){
62            $now = $_REQUEST['blogarchive'];
63        }else{
64            $now = strftime('%Y-%m'); // current month
65        }
66        list($y,$m) = explode('-',$now);
67
68        // display the archive overview
69        $cnt = $this->_buildTimeChooser($renderer, $entries, $now);
70
71        $renderer->header($this->_posts($cnt,$m,$y),2,$pos);
72        $renderer->section_open(2);
73
74        // let Pagelist Plugin do the work for us
75        if (plugin_isdisabled('pagelist')
76                || (!$pagelist = plugin_load('helper', 'pagelist'))) {
77            msg($this->getLang('missing_pagelistplugin'), -1);
78            return false;
79        }
80        $pagelist->setFlags($flags);
81        $pagelist->startList();
82        foreach ($entries as $entry) {
83            $date = strftime('%Y-%m',$entry['date']);
84            // entry in the right date range?
85            if($date < $now || $date > $now) continue;
86            $pagelist->addPage($entry);
87        }
88        $renderer->doc .= $pagelist->finishList();
89
90        $renderer->section_close();
91        return true;
92
93    }
94
95    /**
96     * Creates a list of monthly archive links
97     *
98     * @param object reference $R - the XHTML renderer
99     * @param array reference $entries - all entries metadata
100     * @param string $now - currently selected month ('YYYY-MM')
101     * @return int - number of posts for selected month
102     */
103    function _buildTimeChooser(&$R, &$entries, $now){
104        global $ID;
105
106        // get the months where posts exist
107        $months = array();
108        foreach($entries as $entry){
109            $y = date('Y',$entry['date']);
110            $m = date('m',$entry['date']);
111            if(isset($months[$y][$m])) {
112                $months[$y][$m]++;
113            }else{
114                $months[$y][$m] = 1;
115            }
116        }
117
118        $ret = 0;
119        // output
120        $R->doc .= '<div class="autoarchive_selector">';
121        foreach($months as $y => $mdata){
122            $R->listu_open();
123            $R->listitem_open(1);
124            $R->listcontent_open();
125            $R->doc .= $y.'<span>:</span>';
126            $R->listcontent_close();
127            ksort($mdata);
128            foreach($mdata as $m => $cnt){
129                $R->listu_open();
130                $R->listitem_open(2);
131                $R->listcontent_open();
132                if("$y-$m" == $now) $R->doc .= '<span class="cur">';
133                $R->doc .= '<a href="'.wl($ID,array('blogarchive'=>"$y-$m")).'" class="wikilink1" title="'.$this->_posts($cnt,$m,$y).'">';
134                $R->doc .= $this->getLang('month_'.$m);
135                $R->doc .= '</a>';
136                if("$y-$m" == $now){
137                    $R->doc .= '</span>';
138                    $ret = $cnt;
139                }
140                $R->listcontent_close();
141                $R->listitem_close();
142                $R->listu_close();
143            }
144            $R->listitem_close();
145            $R->listu_close();
146        }
147        $R->doc .='</div>';
148        return $ret;
149    }
150
151    function _posts($num,$month,$year){
152        return sprintf($this->getLang('autoarchive'),
153                       $num, $this->getLang("month_$month"),
154                       $year);
155    }
156}
157// vim:ts=4:sw=4:et:enc=utf-8:
158