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