1 | <?php |
---|
2 | /** |
---|
3 | * @brief widgets, a plugin for Dotclear 2 |
---|
4 | * |
---|
5 | * @package Dotclear |
---|
6 | * @subpackage Plugins |
---|
7 | * |
---|
8 | * @copyright Olivier Meunier & Association Dotclear |
---|
9 | * @copyright GPL-2.0-only |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('DC_RC_PATH')) {return;} |
---|
13 | |
---|
14 | class defaultWidgets |
---|
15 | { |
---|
16 | public static function search($w) |
---|
17 | { |
---|
18 | global $core; |
---|
19 | |
---|
20 | if ($core->blog->settings->system->no_search) { |
---|
21 | return; |
---|
22 | } |
---|
23 | |
---|
24 | if ($w->offline) { |
---|
25 | return; |
---|
26 | } |
---|
27 | |
---|
28 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
29 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
30 | return; |
---|
31 | } |
---|
32 | |
---|
33 | $value = isset($GLOBALS['_search']) ? html::escapeHTML($GLOBALS['_search']) : ''; |
---|
34 | |
---|
35 | return $w->renderDiv($w->content_only, $w->class, 'id="search"', |
---|
36 | ($w->title ? $w->renderTitle('<label for="q">' . html::escapeHTML($w->title) . '</label>') : '') . |
---|
37 | '<form action="' . $core->blog->url . '" method="get" role="search">' . |
---|
38 | '<fieldset>' . |
---|
39 | '<p><input type="text" size="10" maxlength="255" id="q" name="q" value="' . $value . '" ' . |
---|
40 | ($w->placeholder ? 'placeholder="' . html::escapeHTML($w->placeholder) . '"' : '') . '/> ' . |
---|
41 | '<input type="submit" class="submit" value="ok" /></p>' . |
---|
42 | '</fieldset>' . |
---|
43 | '</form>'); |
---|
44 | } |
---|
45 | |
---|
46 | public static function navigation($w) |
---|
47 | { |
---|
48 | global $core; |
---|
49 | |
---|
50 | if ($w->offline) { |
---|
51 | return; |
---|
52 | } |
---|
53 | |
---|
54 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
55 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
56 | return; |
---|
57 | } |
---|
58 | |
---|
59 | $res = |
---|
60 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
---|
61 | '<nav role="navigation"><ul>'; |
---|
62 | |
---|
63 | if ($core->url->type != 'default') { |
---|
64 | $res .= |
---|
65 | '<li class="topnav-home">' . |
---|
66 | '<a href="' . $core->blog->url . '">' . __('Home') . '</a>' . |
---|
67 | '</li>'; |
---|
68 | } |
---|
69 | |
---|
70 | $res .= |
---|
71 | '<li class="topnav-arch">' . |
---|
72 | '<a href="' . $core->blog->url . $core->url->getURLFor("archive") . '">' . |
---|
73 | __('Archives') . '</a></li>' . |
---|
74 | '</ul></nav>'; |
---|
75 | |
---|
76 | return $w->renderDiv($w->content_only, $w->class, 'id="topnav"', $res); |
---|
77 | } |
---|
78 | |
---|
79 | public static function categories($w) |
---|
80 | { |
---|
81 | global $core, $_ctx; |
---|
82 | |
---|
83 | if ($w->offline) { |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
88 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
89 | return; |
---|
90 | } |
---|
91 | |
---|
92 | $rs = $core->blog->getCategories(array('post_type' => 'post', 'without_empty' => !$w->with_empty)); |
---|
93 | if ($rs->isEmpty()) { |
---|
94 | return; |
---|
95 | } |
---|
96 | |
---|
97 | $res = |
---|
98 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : ''); |
---|
99 | |
---|
100 | $ref_level = $level = $rs->level - 1; |
---|
101 | while ($rs->fetch()) { |
---|
102 | $class = ''; |
---|
103 | if (($core->url->type == 'category' && $_ctx->categories instanceof record && $_ctx->categories->cat_id == $rs->cat_id) |
---|
104 | || ($core->url->type == 'post' && $_ctx->posts instanceof record && $_ctx->posts->cat_id == $rs->cat_id)) { |
---|
105 | $class = ' class="category-current"'; |
---|
106 | } |
---|
107 | |
---|
108 | if ($rs->level > $level) { |
---|
109 | $res .= str_repeat('<ul><li' . $class . '>', $rs->level - $level); |
---|
110 | } elseif ($rs->level < $level) { |
---|
111 | $res .= str_repeat('</li></ul>', -($rs->level - $level)); |
---|
112 | } |
---|
113 | |
---|
114 | if ($rs->level <= $level) { |
---|
115 | $res .= '</li><li' . $class . '>'; |
---|
116 | } |
---|
117 | |
---|
118 | $res .= |
---|
119 | '<a href="' . $core->blog->url . $core->url->getURLFor('category', $rs->cat_url) . '">' . |
---|
120 | html::escapeHTML($rs->cat_title) . '</a>' . |
---|
121 | ($w->postcount ? ' <span>(' . ($w->subcatscount ? $rs->nb_total : $rs->nb_post) . ')</span>' : ''); |
---|
122 | |
---|
123 | $level = $rs->level; |
---|
124 | } |
---|
125 | |
---|
126 | if ($ref_level - $level < 0) { |
---|
127 | $res .= str_repeat('</li></ul>', -($ref_level - $level)); |
---|
128 | } |
---|
129 | |
---|
130 | return $w->renderDiv($w->content_only, 'categories ' . $w->class, '', $res); |
---|
131 | } |
---|
132 | |
---|
133 | public static function bestof($w) |
---|
134 | { |
---|
135 | global $core, $_ctx; |
---|
136 | |
---|
137 | if ($w->offline) { |
---|
138 | return; |
---|
139 | } |
---|
140 | |
---|
141 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
142 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
143 | return; |
---|
144 | } |
---|
145 | |
---|
146 | $params = array( |
---|
147 | 'post_selected' => true, |
---|
148 | 'no_content' => true, |
---|
149 | 'order' => 'post_dt ' . strtoupper($w->orderby) |
---|
150 | ); |
---|
151 | |
---|
152 | $rs = $core->blog->getPosts($params); |
---|
153 | |
---|
154 | if ($rs->isEmpty()) { |
---|
155 | return; |
---|
156 | } |
---|
157 | |
---|
158 | $res = |
---|
159 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
---|
160 | '<ul>'; |
---|
161 | |
---|
162 | while ($rs->fetch()) { |
---|
163 | $class = ''; |
---|
164 | if ($core->url->type == 'post' && $_ctx->posts instanceof record && $_ctx->posts->post_id == $rs->post_id) { |
---|
165 | $class = ' class="post-current"'; |
---|
166 | } |
---|
167 | $res .= ' <li' . $class . '><a href="' . $rs->getURL() . '">' . html::escapeHTML($rs->post_title) . '</a></li> '; |
---|
168 | } |
---|
169 | |
---|
170 | $res .= '</ul>'; |
---|
171 | |
---|
172 | return $w->renderDiv($w->content_only, 'selected ' . $w->class, '', $res); |
---|
173 | } |
---|
174 | |
---|
175 | public static function langs($w) |
---|
176 | { |
---|
177 | global $core, $_ctx; |
---|
178 | |
---|
179 | if ($w->offline) { |
---|
180 | return; |
---|
181 | } |
---|
182 | |
---|
183 | if (($w->homeonly == 1 && $core->url->type != 'default' && $core->url->type != 'lang') || |
---|
184 | ($w->homeonly == 2 && ($core->url->type == 'default' || $core->url->type == 'lang'))) { |
---|
185 | return; |
---|
186 | } |
---|
187 | |
---|
188 | $rs = $core->blog->getLangs(); |
---|
189 | |
---|
190 | if ($rs->count() <= 1) { |
---|
191 | return; |
---|
192 | } |
---|
193 | |
---|
194 | $langs = l10n::getISOcodes(); |
---|
195 | $res = |
---|
196 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
---|
197 | '<ul>'; |
---|
198 | |
---|
199 | while ($rs->fetch()) { |
---|
200 | $l = ($_ctx->cur_lang == $rs->post_lang) ? '<strong>%s</strong>' : '%s'; |
---|
201 | |
---|
202 | $lang_name = isset($langs[$rs->post_lang]) ? $langs[$rs->post_lang] : $rs->post_lang; |
---|
203 | |
---|
204 | $res .= |
---|
205 | ' <li>' . |
---|
206 | sprintf($l, |
---|
207 | '<a href="' . $core->blog->url . $core->url->getURLFor('lang', $rs->post_lang) . '" ' . |
---|
208 | 'class="lang-' . $rs->post_lang . '">' . |
---|
209 | $lang_name . '</a>') . |
---|
210 | ' </li>'; |
---|
211 | } |
---|
212 | |
---|
213 | $res .= '</ul>'; |
---|
214 | |
---|
215 | return $w->renderDiv($w->content_only, 'langs ' . $w->class, '', $res); |
---|
216 | } |
---|
217 | |
---|
218 | public static function subscribe($w) |
---|
219 | { |
---|
220 | global $core; |
---|
221 | |
---|
222 | if ($w->offline) { |
---|
223 | return; |
---|
224 | } |
---|
225 | |
---|
226 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
227 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
228 | return; |
---|
229 | } |
---|
230 | |
---|
231 | $type = ($w->type == 'atom' || $w->type == 'rss2') ? $w->type : 'rss2'; |
---|
232 | $mime = $type == 'rss2' ? 'application/rss+xml' : 'application/atom+xml'; |
---|
233 | |
---|
234 | $p_title = __('This blog\'s entries %s feed'); |
---|
235 | $c_title = __('This blog\'s comments %s feed'); |
---|
236 | |
---|
237 | $res = |
---|
238 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
---|
239 | '<ul>'; |
---|
240 | |
---|
241 | $res .= |
---|
242 | '<li><a type="' . $mime . '" ' . |
---|
243 | 'href="' . $core->blog->url . $core->url->getURLFor('feed', $type) . '" ' . |
---|
244 | 'title="' . sprintf($p_title, ($type == 'atom' ? 'Atom' : 'RSS')) . '" class="feed">' . |
---|
245 | __('Entries feed') . '</a></li>'; |
---|
246 | |
---|
247 | if ($core->blog->settings->system->allow_comments || $core->blog->settings->system->allow_trackbacks) { |
---|
248 | $res .= |
---|
249 | '<li><a type="' . $mime . '" ' . |
---|
250 | 'href="' . $core->blog->url . $core->url->getURLFor('feed', $type . '/comments') . '" ' . |
---|
251 | 'title="' . sprintf($c_title, ($type == 'atom' ? 'Atom' : 'RSS')) . '" class="feed">' . |
---|
252 | __('Comments feed') . '</a></li>'; |
---|
253 | } |
---|
254 | |
---|
255 | $res .= '</ul>'; |
---|
256 | |
---|
257 | return $w->renderDiv($w->content_only, 'syndicate ' . $w->class, '', $res); |
---|
258 | } |
---|
259 | |
---|
260 | public static function feed($w) |
---|
261 | { |
---|
262 | global $core; |
---|
263 | |
---|
264 | if (!$w->url) { |
---|
265 | return; |
---|
266 | } |
---|
267 | |
---|
268 | if ($w->offline) { |
---|
269 | return; |
---|
270 | } |
---|
271 | |
---|
272 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
273 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
274 | return; |
---|
275 | } |
---|
276 | |
---|
277 | $limit = abs((integer) $w->limit); |
---|
278 | |
---|
279 | try { |
---|
280 | $feed = feedReader::quickParse($w->url, DC_TPL_CACHE); |
---|
281 | if ($feed == false || count($feed->items) == 0) { |
---|
282 | return; |
---|
283 | } |
---|
284 | } catch (Exception $e) { |
---|
285 | return; |
---|
286 | } |
---|
287 | |
---|
288 | $res = |
---|
289 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
---|
290 | '<ul>'; |
---|
291 | |
---|
292 | $i = 0; |
---|
293 | foreach ($feed->items as $item) { |
---|
294 | $title = isset($item->title) && strlen(trim($item->title)) ? $item->title : ''; |
---|
295 | $link = isset($item->link) && strlen(trim($item->link)) ? $item->link : ''; |
---|
296 | |
---|
297 | if (!$link && !$title) { |
---|
298 | continue; |
---|
299 | } |
---|
300 | |
---|
301 | if (!$title) { |
---|
302 | $title = substr($link, 0, 25) . '...'; |
---|
303 | } |
---|
304 | |
---|
305 | $li = $link ? '<a href="' . html::escapeHTML($item->link) . '">' . $title . '</a>' : $title; |
---|
306 | $res .= ' <li>' . $li . '</li> '; |
---|
307 | $i++; |
---|
308 | if ($i >= $limit) { |
---|
309 | break; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | $res .= '</ul>'; |
---|
314 | |
---|
315 | return $w->renderDiv($w->content_only, 'feed ' . $w->class, '', $res); |
---|
316 | } |
---|
317 | |
---|
318 | public static function text($w) |
---|
319 | { |
---|
320 | global $core; |
---|
321 | |
---|
322 | if ($w->offline) { |
---|
323 | return; |
---|
324 | } |
---|
325 | |
---|
326 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
327 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
328 | return; |
---|
329 | } |
---|
330 | |
---|
331 | $res = ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . $w->text; |
---|
332 | |
---|
333 | return $w->renderDiv($w->content_only, 'text ' . $w->class, '', $res); |
---|
334 | } |
---|
335 | |
---|
336 | public static function lastposts($w) |
---|
337 | { |
---|
338 | global $core, $_ctx; |
---|
339 | |
---|
340 | if ($w->offline) { |
---|
341 | return; |
---|
342 | } |
---|
343 | |
---|
344 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
345 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
346 | return; |
---|
347 | } |
---|
348 | |
---|
349 | $params['limit'] = abs((integer) $w->limit); |
---|
350 | $params['order'] = 'post_dt desc'; |
---|
351 | $params['no_content'] = true; |
---|
352 | |
---|
353 | if ($w->category) { |
---|
354 | if ($w->category == 'null') { |
---|
355 | $params['sql'] = ' AND P.cat_id IS NULL '; |
---|
356 | } elseif (is_numeric($w->category)) { |
---|
357 | $params['cat_id'] = (integer) $w->category; |
---|
358 | } else { |
---|
359 | $params['cat_url'] = $w->category; |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | if ($w->tag) { |
---|
364 | $params['meta_id'] = $w->tag; |
---|
365 | $rs = $core->meta->getPostsByMeta($params); |
---|
366 | } else { |
---|
367 | $rs = $core->blog->getPosts($params); |
---|
368 | } |
---|
369 | |
---|
370 | if ($rs->isEmpty()) { |
---|
371 | return; |
---|
372 | } |
---|
373 | |
---|
374 | $res = |
---|
375 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
---|
376 | '<ul>'; |
---|
377 | |
---|
378 | while ($rs->fetch()) { |
---|
379 | $class = ''; |
---|
380 | if ($core->url->type == 'post' && $_ctx->posts instanceof record && $_ctx->posts->post_id == $rs->post_id) { |
---|
381 | $class = ' class="post-current"'; |
---|
382 | } |
---|
383 | $res .= '<li' . $class . '><a href="' . $rs->getURL() . '">' . |
---|
384 | html::escapeHTML($rs->post_title) . '</a></li>'; |
---|
385 | } |
---|
386 | |
---|
387 | $res .= '</ul>'; |
---|
388 | |
---|
389 | return $w->renderDiv($w->content_only, 'lastposts ' . $w->class, '', $res); |
---|
390 | } |
---|
391 | |
---|
392 | public static function lastcomments($w) |
---|
393 | { |
---|
394 | global $core; |
---|
395 | |
---|
396 | if ($w->offline) { |
---|
397 | return; |
---|
398 | } |
---|
399 | |
---|
400 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
401 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
402 | return; |
---|
403 | } |
---|
404 | |
---|
405 | $params['limit'] = abs((integer) $w->limit); |
---|
406 | $params['order'] = 'comment_dt desc'; |
---|
407 | $rs = $core->blog->getComments($params); |
---|
408 | |
---|
409 | if ($rs->isEmpty()) { |
---|
410 | return; |
---|
411 | } |
---|
412 | |
---|
413 | $res = ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . '<ul>'; |
---|
414 | |
---|
415 | while ($rs->fetch()) { |
---|
416 | $res .= '<li class="' . |
---|
417 | ((boolean) $rs->comment_trackback ? 'last-tb' : 'last-comment') . |
---|
418 | '"><a href="' . $rs->getPostURL() . '#c' . $rs->comment_id . '">' . |
---|
419 | html::escapeHTML($rs->post_title) . ' - ' . |
---|
420 | html::escapeHTML($rs->comment_author) . |
---|
421 | '</a></li>'; |
---|
422 | } |
---|
423 | |
---|
424 | $res .= '</ul>'; |
---|
425 | |
---|
426 | return $w->renderDiv($w->content_only, 'lastcomments ' . $w->class, '', $res); |
---|
427 | } |
---|
428 | } |
---|