1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | # Localized string we find in template |
---|
15 | __("This tag's comments Atom feed"); |
---|
16 | __("This tag's entries Atom feed"); |
---|
17 | |
---|
18 | |
---|
19 | require dirname(__FILE__).'/_widgets.php'; |
---|
20 | |
---|
21 | $core->tpl->addBlock('Tags',array('tplTags','Tags')); |
---|
22 | $core->tpl->addBlock('TagsHeader',array('tplTags','TagsHeader')); |
---|
23 | $core->tpl->addBlock('TagsFooter',array('tplTags','TagsFooter')); |
---|
24 | $core->tpl->addBlock('EntryTags',array('tplTags','EntryTags')); |
---|
25 | $core->tpl->addBlock('TagIf',array('tplTags','TagIf')); |
---|
26 | $core->tpl->addValue('TagID',array('tplTags','TagID')); |
---|
27 | $core->tpl->addValue('TagCount',array('tplTags','TagCount')); |
---|
28 | $core->tpl->addValue('TagPercent',array('tplTags','TagPercent')); |
---|
29 | $core->tpl->addValue('TagRoundPercent',array('tplTags','TagRoundPercent')); |
---|
30 | $core->tpl->addValue('TagURL',array('tplTags','TagURL')); |
---|
31 | $core->tpl->addValue('TagCloudURL',array('tplTags','TagCloudURL')); |
---|
32 | $core->tpl->addValue('TagFeedURL',array('tplTags','TagFeedURL')); |
---|
33 | |
---|
34 | # Kept for backward compatibility (for now) |
---|
35 | $core->tpl->addBlock('MetaData',array('tplTags','Tags')); |
---|
36 | $core->tpl->addBlock('MetaDataHeader',array('tplTags','TagsHeader')); |
---|
37 | $core->tpl->addBlock('MetaDataFooter',array('tplTags','TagsFooter')); |
---|
38 | $core->tpl->addValue('MetaID',array('tplTags','TagID')); |
---|
39 | $core->tpl->addValue('MetaPercent',array('tplTags','TagPercent')); |
---|
40 | $core->tpl->addValue('MetaRoundPercent',array('tplTags','TagRoundPercent')); |
---|
41 | $core->tpl->addValue('MetaURL',array('tplTags','TagURL')); |
---|
42 | $core->tpl->addValue('MetaAllURL',array('tplTags','TagCloudURL')); |
---|
43 | $core->tpl->addBlock('EntryMetaData',array('tplTags','EntryTags')); |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | $core->addBehavior('templateBeforeBlock',array('behaviorsTags','templateBeforeBlock')); |
---|
48 | $core->addBehavior('publicBeforeDocument',array('behaviorsTags','addTplPath')); |
---|
49 | |
---|
50 | class behaviorsTags |
---|
51 | { |
---|
52 | public static function templateBeforeBlock($core,$b,$attr) |
---|
53 | { |
---|
54 | if (($b == 'Entries' || $b == 'Comments') && isset($attr['tag'])) |
---|
55 | { |
---|
56 | return |
---|
57 | "<?php\n". |
---|
58 | "if (!isset(\$params)) { \$params = array(); }\n". |
---|
59 | "if (!isset(\$params['from'])) { \$params['from'] = ''; }\n". |
---|
60 | "if (!isset(\$params['sql'])) { \$params['sql'] = ''; }\n". |
---|
61 | "\$params['from'] .= ', '.\$core->prefix.'meta META ';\n". |
---|
62 | "\$params['sql'] .= 'AND META.post_id = P.post_id ';\n". |
---|
63 | "\$params['sql'] .= \"AND META.meta_type = 'tag' \";\n". |
---|
64 | "\$params['sql'] .= \"AND META.meta_id = '".$core->con->escape($attr['tag'])."' \";\n". |
---|
65 | "?>\n"; |
---|
66 | } |
---|
67 | elseif (empty($attr['no_context']) && ($b == 'Entries' || $b == 'Comments')) |
---|
68 | { |
---|
69 | return |
---|
70 | '<?php if ($_ctx->exists("meta") && ($_ctx->meta->meta_type == "tag")) { '. |
---|
71 | "if (!isset(\$params)) { \$params = array(); }\n". |
---|
72 | "if (!isset(\$params['from'])) { \$params['from'] = ''; }\n". |
---|
73 | "if (!isset(\$params['sql'])) { \$params['sql'] = ''; }\n". |
---|
74 | "\$params['from'] .= ', '.\$core->prefix.'meta META ';\n". |
---|
75 | "\$params['sql'] .= 'AND META.post_id = P.post_id ';\n". |
---|
76 | "\$params['sql'] .= \"AND META.meta_type = 'tag' \";\n". |
---|
77 | "\$params['sql'] .= \"AND META.meta_id = '\".\$core->con->escape(\$_ctx->meta->meta_id).\"' \";\n". |
---|
78 | "} ?>\n"; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | public static function addTplPath($core) |
---|
83 | { |
---|
84 | $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates'); |
---|
85 | } |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | class tplTags |
---|
90 | { |
---|
91 | public static function Tags($attr,$content) |
---|
92 | { |
---|
93 | $type = isset($attr['type']) ? addslashes($attr['type']) : 'tag'; |
---|
94 | |
---|
95 | $limit = isset($attr['limit']) ? (integer) $attr['limit'] : 'null'; |
---|
96 | |
---|
97 | $sortby = 'meta_id_lower'; |
---|
98 | if (isset($attr['sortby']) && $attr['sortby'] == 'count') { |
---|
99 | $sortby = 'count'; |
---|
100 | } |
---|
101 | |
---|
102 | $order = 'asc'; |
---|
103 | if (isset($attr['order']) && $attr['order'] == 'desc') { |
---|
104 | $order = 'desc'; |
---|
105 | } |
---|
106 | |
---|
107 | $res = |
---|
108 | "<?php\n". |
---|
109 | "\$_ctx->meta = \$core->meta->computeMetaStats(\$core->meta->getMetadata(array('meta_type'=>'" |
---|
110 | .$type."','limit'=>".$limit."))); ". |
---|
111 | "\$_ctx->meta->sort('".$sortby."','".$order."'); ". |
---|
112 | '?>'; |
---|
113 | |
---|
114 | $res .= |
---|
115 | '<?php while ($_ctx->meta->fetch()) : ?>'.$content.'<?php endwhile; '. |
---|
116 | '$_ctx->meta = null; ?>'; |
---|
117 | |
---|
118 | return $res; |
---|
119 | } |
---|
120 | |
---|
121 | public static function TagsHeader($attr,$content) |
---|
122 | { |
---|
123 | return |
---|
124 | "<?php if (\$_ctx->meta->isStart()) : ?>". |
---|
125 | $content. |
---|
126 | "<?php endif; ?>"; |
---|
127 | } |
---|
128 | |
---|
129 | public static function TagsFooter($attr,$content) |
---|
130 | { |
---|
131 | return |
---|
132 | "<?php if (\$_ctx->meta->isEnd()) : ?>". |
---|
133 | $content. |
---|
134 | "<?php endif; ?>"; |
---|
135 | } |
---|
136 | |
---|
137 | public static function EntryTags($attr,$content) |
---|
138 | { |
---|
139 | $type = isset($attr['type']) ? addslashes($attr['type']) : 'tag'; |
---|
140 | |
---|
141 | $sortby = 'meta_id_lower'; |
---|
142 | if (isset($attr['sortby']) && $attr['sortby'] == 'count') { |
---|
143 | $sortby = 'count'; |
---|
144 | } |
---|
145 | |
---|
146 | $order = 'asc'; |
---|
147 | if (isset($attr['order']) && $attr['order'] == 'desc') { |
---|
148 | $order = 'desc'; |
---|
149 | } |
---|
150 | |
---|
151 | $res = |
---|
152 | "<?php\n". |
---|
153 | "\$_ctx->meta = \$core->meta->getMetaRecordset(\$_ctx->posts->post_meta,'".$type."'); ". |
---|
154 | "\$_ctx->meta->sort('".$sortby."','".$order."'); ". |
---|
155 | '?>'; |
---|
156 | |
---|
157 | $res .= |
---|
158 | '<?php while ($_ctx->meta->fetch()) : ?>'.$content.'<?php endwhile; '. |
---|
159 | '$_ctx->meta = null; ?>'; |
---|
160 | |
---|
161 | return $res; |
---|
162 | } |
---|
163 | |
---|
164 | public static function TagIf($attr,$content) |
---|
165 | { |
---|
166 | $if = array(); |
---|
167 | $operateur = isset($attr['operator']) ? dcTemplate::getOperator($attr['operator']) : '&&'; |
---|
168 | |
---|
169 | if (isset($attr['has_entries'])) { |
---|
170 | $sign = (boolean) $attr['has_entries'] ? '' : '!'; |
---|
171 | $if[] = $sign.'$_ctx->meta->count'; |
---|
172 | } |
---|
173 | |
---|
174 | if (!empty($if)) { |
---|
175 | return '<?php if('.implode(' '.$operateur.' ',$if).') : ?>'.$content.'<?php endif; ?>'; |
---|
176 | } else { |
---|
177 | return $content; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | public static function TagID($attr) |
---|
182 | { |
---|
183 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
184 | return '<?php echo '.sprintf($f,'$_ctx->meta->meta_id').'; ?>'; |
---|
185 | } |
---|
186 | |
---|
187 | public static function TagCount($attr) |
---|
188 | { |
---|
189 | return '<?php echo $_ctx->meta->count; ?>'; |
---|
190 | } |
---|
191 | |
---|
192 | public static function TagPercent($attr) |
---|
193 | { |
---|
194 | return '<?php echo $_ctx->meta->percent; ?>'; |
---|
195 | } |
---|
196 | |
---|
197 | public static function TagRoundPercent($attr) |
---|
198 | { |
---|
199 | return '<?php echo $_ctx->meta->roundpercent; ?>'; |
---|
200 | } |
---|
201 | |
---|
202 | public static function TagURL($attr) |
---|
203 | { |
---|
204 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
205 | return '<?php echo '.sprintf($f,'$core->blog->url.$core->url->getURLFor("tag",'. |
---|
206 | 'rawurlencode($_ctx->meta->meta_id))').'; ?>'; |
---|
207 | } |
---|
208 | |
---|
209 | public static function TagCloudURL($attr) |
---|
210 | { |
---|
211 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
212 | return '<?php echo '.sprintf($f,'$core->blog->url.$core->url->getURLFor("tags")').'; ?>'; |
---|
213 | } |
---|
214 | |
---|
215 | public static function TagFeedURL($attr) |
---|
216 | { |
---|
217 | $type = !empty($attr['type']) ? $attr['type'] : 'rss2'; |
---|
218 | |
---|
219 | if (!preg_match('#^(rss2|atom)$#',$type)) { |
---|
220 | $type = 'rss2'; |
---|
221 | } |
---|
222 | |
---|
223 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
224 | return '<?php echo '.sprintf($f,'$core->blog->url.$core->url->getURLFor("tag_feed",'. |
---|
225 | 'rawurlencode($_ctx->meta->meta_id)."/'.$type.'")').'; ?>'; |
---|
226 | } |
---|
227 | |
---|
228 | # Widget function |
---|
229 | public static function tagsWidget($w) |
---|
230 | { |
---|
231 | global $core; |
---|
232 | |
---|
233 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
234 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
235 | return; |
---|
236 | } |
---|
237 | |
---|
238 | $params = array('meta_type' => 'tag'); |
---|
239 | |
---|
240 | if ($w->limit !== '') { |
---|
241 | $params['limit'] = abs((integer) $w->limit); |
---|
242 | } |
---|
243 | |
---|
244 | $rs = $core->meta->computeMetaStats( |
---|
245 | $core->meta->getMetadata($params)); |
---|
246 | |
---|
247 | if ($rs->isEmpty()) { |
---|
248 | return; |
---|
249 | } |
---|
250 | |
---|
251 | $sort = $w->sortby; |
---|
252 | if (!in_array($sort,array('meta_id_lower','count'))) { |
---|
253 | $sort = 'meta_id_lower'; |
---|
254 | } |
---|
255 | |
---|
256 | $order = $w->orderby; |
---|
257 | if ($order != 'asc') { |
---|
258 | $order = 'desc'; |
---|
259 | } |
---|
260 | |
---|
261 | $rs->sort($sort,$order); |
---|
262 | |
---|
263 | $res = |
---|
264 | ($w->content_only ? '' : '<div class="tags'.($w->class ? ' '.html::escapeHTML($w->class) : '').'">'). |
---|
265 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
266 | '<ul>'; |
---|
267 | |
---|
268 | while ($rs->fetch()) |
---|
269 | { |
---|
270 | $res .= |
---|
271 | '<li><a href="'.$core->blog->url.$core->url->getURLFor('tag',rawurlencode($rs->meta_id)).'" '. |
---|
272 | 'class="tag'.$rs->roundpercent.'">'. |
---|
273 | $rs->meta_id.'</a> </li>'; |
---|
274 | } |
---|
275 | |
---|
276 | $res .= '</ul>'; |
---|
277 | |
---|
278 | if ($core->url->getBase('tags') && !is_null($w->alltagslinktitle) && $w->alltagslinktitle !== '') |
---|
279 | { |
---|
280 | $res .= |
---|
281 | '<p><strong><a href="'.$core->blog->url.$core->url->getURLFor("tags").'">'. |
---|
282 | html::escapeHTML($w->alltagslinktitle).'</a></strong></p>'; |
---|
283 | } |
---|
284 | |
---|
285 | $res .= ($w->content_only ? '' : '</div>'); |
---|
286 | |
---|
287 | return $res; |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | class urlTags extends dcUrlHandlers |
---|
292 | { |
---|
293 | public static function tag($args) |
---|
294 | { |
---|
295 | $n = self::getPageNumber($args); |
---|
296 | |
---|
297 | if ($args == '' && !$n) |
---|
298 | { |
---|
299 | self::p404(); |
---|
300 | } |
---|
301 | elseif (preg_match('%(.*?)/feed/(rss2|atom)?$%u',$args,$m)) |
---|
302 | { |
---|
303 | $type = $m[2] == 'atom' ? 'atom' : 'rss2'; |
---|
304 | $mime = 'application/xml'; |
---|
305 | $comments = !empty($m[3]); |
---|
306 | |
---|
307 | $GLOBALS['_ctx']->meta = $GLOBALS['core']->meta->computeMetaStats( |
---|
308 | $GLOBALS['core']->meta->getMetadata(array( |
---|
309 | 'meta_type' => 'tag', |
---|
310 | 'meta_id' => $m[1]))); |
---|
311 | |
---|
312 | if ($GLOBALS['_ctx']->meta->isEmpty()) { |
---|
313 | self::p404(); |
---|
314 | } |
---|
315 | else |
---|
316 | { |
---|
317 | $tpl = $type; |
---|
318 | |
---|
319 | if ($type == 'atom') { |
---|
320 | $mime = 'application/atom+xml'; |
---|
321 | } |
---|
322 | |
---|
323 | self::serveDocument($tpl.'.xml',$mime); |
---|
324 | } |
---|
325 | } |
---|
326 | else |
---|
327 | { |
---|
328 | if ($n) { |
---|
329 | $GLOBALS['_page_number'] = $n; |
---|
330 | } |
---|
331 | |
---|
332 | $GLOBALS['_ctx']->meta = $GLOBALS['core']->meta->computeMetaStats( |
---|
333 | $GLOBALS['core']->meta->getMetadata(array( |
---|
334 | 'meta_type' => 'tag', |
---|
335 | 'meta_id' => $args))); |
---|
336 | |
---|
337 | if ($GLOBALS['_ctx']->meta->isEmpty()) { |
---|
338 | self::p404(); |
---|
339 | } else { |
---|
340 | self::serveDocument('tag.html'); |
---|
341 | } |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | public static function tags($args) |
---|
346 | { |
---|
347 | self::serveDocument('tags.html'); |
---|
348 | } |
---|
349 | |
---|
350 | public static function tagFeed($args) |
---|
351 | { |
---|
352 | if (!preg_match('#^(.+)/(atom|rss2)(/comments)?$#',$args,$m)) |
---|
353 | { |
---|
354 | self::p404(); |
---|
355 | } |
---|
356 | else |
---|
357 | { |
---|
358 | $tag = $m[1]; |
---|
359 | $type = $m[2]; |
---|
360 | $comments = !empty($m[3]); |
---|
361 | |
---|
362 | $GLOBALS['_ctx']->meta = $GLOBALS['core']->meta->computeMetaStats( |
---|
363 | $GLOBALS['core']->meta->getMetadata(array( |
---|
364 | 'meta_type' => 'tag', |
---|
365 | 'meta_id' => $tag))); |
---|
366 | |
---|
367 | if ($GLOBALS['_ctx']->meta->isEmpty()) { |
---|
368 | # The specified tag does not exist. |
---|
369 | self::p404(); |
---|
370 | } |
---|
371 | else |
---|
372 | { |
---|
373 | $GLOBALS['_ctx']->feed_subtitle = ' - '.__('Tag').' - '.$GLOBALS['_ctx']->meta->meta_id; |
---|
374 | |
---|
375 | if ($type == 'atom') { |
---|
376 | $mime = 'application/atom+xml'; |
---|
377 | } else { |
---|
378 | $mime = 'application/xml'; |
---|
379 | } |
---|
380 | |
---|
381 | $tpl = $type; |
---|
382 | if ($comments) { |
---|
383 | $tpl .= '-comments'; |
---|
384 | $GLOBALS['_ctx']->nb_comment_per_page = $GLOBALS['core']->blog->settings->system->nb_comment_per_feed; |
---|
385 | } else { |
---|
386 | $GLOBALS['_ctx']->nb_entry_per_page = $GLOBALS['core']->blog->settings->system->nb_post_per_feed; |
---|
387 | $GLOBALS['_ctx']->short_feed_items = $GLOBALS['core']->blog->settings->system->short_feed_items; |
---|
388 | } |
---|
389 | $tpl .= '.xml'; |
---|
390 | |
---|
391 | self::serveDocument($tpl,$mime); |
---|
392 | } |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | ?> |
---|