1<?php
2/**
3 * @license    http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html
4 * @author     Francois <dokuplugin@merciol.fr>
5 *
6 * Plugin tiledblog: display blog in tile
7 */
8
9if (!defined ('DOKU_INC'))
10    die ();
11if (!defined ('DOKU_PLUGIN'))
12    define ('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
13require_once (DOKU_PLUGIN.'syntax.php');
14
15class syntax_plugin_tiledblog extends DokuWiki_Syntax_Plugin {
16
17    // ============================================================
18    function getInfo () {
19        return confToHash (dirname (__FILE__).'/INFO.txt');
20    }
21    function getType () { return 'substition'; }
22    function getPType () { return 'block'; }
23    function getSort () { return 299; }
24    function connectTo ($mode) {
25        $this->Lexer->addSpecialPattern ('\{\{tiledBlog.*?\}\}', $mode, 'plugin_tiledblog');
26    }
27
28    // ============================================================
29    function handle ($match, $state, $pos, Doku_Handler $handler) {
30        switch ($state) {
31        case DOKU_LEXER_SPECIAL :
32            return trim (substr ($match, 11, -2)); // "{{tiledBlog" => 11 "}}" => 2
33        }
34        return false;
35    }
36
37    // ============================================================
38    function render ($mode, Doku_Renderer $renderer, $indata) {
39        if ($mode != 'xhtml')
40            return false;
41        $data = " ".$indata." ";
42        if (preg_match_all ("#(\"[^\"]*\")* help (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0) {
43            $this->help ($renderer);
44            return true;
45        }
46        global $ID;
47        $ns = getNS ($ID);
48        // namespace
49        if (preg_match_all ("#^ >([^ ]*) .*#", strtolower ($data), $dumy) > 0) {
50            $ns = $dumy[1][0];
51            if (($ns == '*') || ($ns == ':'))
52                $ns = '';
53            elseif ($ns == '.')
54                $ns = getNS ($ID);
55            else
56                $ns = cleanID ($ns);
57        }
58        global $conf;
59        $savedir = ((!$conf['savedir'] || strpos ($conf['savedir'], '.') === 0) ? DOKU_INC : "").$conf['savedir']."/";
60        $cacheDir = $savedir."cache/tiledblog/";
61        if (!is_dir ($cacheDir)) {
62            @mkdir ($cacheDir);
63            @chmod ($cacheDir, 0775);
64        }
65        $renderer->info ['cache'] = FALSE;
66        if (preg_match_all ("#(\"[^\"]*\")* sample (\"[^\"]*\")*#", strtolower ($data), $dumy) > 0) {
67            $this->sample ($renderer, $ns, $cacheDir);
68            return true;
69        }
70        $this->tile ($renderer, $ns, $cacheDir);
71        return true;
72    }
73
74    // ============================================================
75    function sample (Doku_Renderer $renderer, $ns, $cacheDir) {
76        $filename = $cacheDir.md5($ns)."-sample.cache";
77        if (file_exists ($filename) &&
78            (time () - filemtime ($filename) < $this->getConf ('sampleDelai'))) {
79            $renderer->doc .= file_get_contents ($filename);
80            return;
81        }
82        if ($helperPlugin =& plugin_load ('helper', 'blog')) {
83            $entries = $helperPlugin->getBlog ($ns);
84            $width = $this->getConf ('iconSize');
85            $keys = array_keys ($entries);
86            $rand = array_rand ($keys);
87            $text = $this->getEntry ($entries [$keys [$rand]], $width);
88            $renderer->doc .= $text.NL;
89            file_put_contents ($filename, $text);
90        }
91    }
92
93    // ============================================================
94    function tile (Doku_Renderer $renderer, $ns, $cacheDir) {
95        $formPos = $this->getConf ('formPosition');
96        $blogPlugin =& plugin_load ('syntax', 'blog_blog');
97        $createPerm = (auth_quickaclcheck ($ns.':*') >= AUTH_CREATE);
98        if ($formPos == 'top')
99            $this->displayForm ($renderer, $blogPlugin, $ns, $createPerm);
100
101        $filename = $cacheDir.md5($ns)."-tile.cache";
102        if (file_exists ($filename) &&
103            (time () - filemtime ($filename) < $this->getConf ('sampleDelai'))) {
104            $renderer->doc .= file_get_contents ($filename);
105        } else {
106            if ($helperPlugin =& plugin_load ('helper', 'blog'))
107                $entries = $helperPlugin->getBlog ($ns);
108            $text = '
109     <div class="tiledblog">';
110                  $width = $this->getConf ('iconSize');
111                  foreach ($entries as $entry)
112                      $text .= $this->getEntry ($entry, $width);
113                  $text .= '
114     </div>';
115                  $renderer->doc .= $text.NL;
116                  file_put_contents ($filename, $text);
117        }
118
119        if ($formPos == 'bottom')
120            $this->displayForm ($renderer, $blogPlugin, $ns, $createPerm);
121    }
122
123
124    // ============================================================
125    function displayForm (Doku_Renderer $renderer, syntax_plugin_blog_blog $blogPlugin, $ns, $createPerm) {
126        if ($createPerm)
127            $renderer->doc .= $blogPlugin->_newEntryForm ($ns, "");
128        global $INFO;
129        if (! $this->isAdmin ())
130            return;
131        $renderer->doc .= '<div><form>';
132        foreach (array ('clear', 'clearAll') as $action)
133            $renderer->doc .= '<input value="'.$this->getLang ($action).'" onclick="javascript:tiledblogAjax (\''.$action.'\', \''.$ns.'\')" type="button">';
134       $renderer->doc .= '</form></div>';
135    }
136
137    // ============================================================
138    function isAdmin () {
139        global $INFO;
140        return
141            isset ($INFO ['userinfo']) &&
142            isset ($INFO ['userinfo']['grps']) &&
143            in_array (trim ($this->getConf ('adminGroup')), $INFO ['userinfo']['grps']);
144    }
145
146    // ============================================================
147    function getEntry ($entry, $width) {
148        if (auth_quickaclcheck($entry['id']) < AUTH_READ)
149            return;
150        $id    = $entry['id'];
151        $file  = wikiFN ($entry['id']);
152        if (!@file_exists($file))
153            return '';
154        $title = $entry['title'];
155        $meta  = p_get_metadata ($id);
156        $img   = $meta['relation']['firstimage'];
157        $date  = dformat ($meta['date']['created']);
158        global $_REQUEST;
159        return '
160     <div class="tileblog">
161      <a href="'.wl ($id).'">
162       <div class="image">
163        <img src="'.ml ($img, array ('cache'=>$_REQUEST['cache'], 'w'=>$width)).'" width="'.$width.'"/>
164       </div>
165       <p class="title">'.$title.'</p>
166       <p class="date">'.$date.'</p>
167      </a>
168     </div>';
169    }
170
171    // ============================================================
172    function help (Doku_Renderer $renderer) {
173        $url = "http://admin.parlenet.org/plugins/tiledblog/";
174        $renderer->doc .=
175                       '      <h1>Help TiledBlog V2.0</h1>'.NL.
176                       '      <ul>'.NL.
177                       '        <li><b>{{tiledBlog&gt;</b>namespace [help] <b>}}</b></li>'.NL.
178                       '      </ul>'.NL.
179                       '      <p><a class="urlextern" rel="nofollow" title="'.$url.'" href="'.$url.'">'.$url.'</a></p>'.NL;
180    }
181
182    // ============================================================
183} // syntax_plugin_tiledblog
184?>
185