Dotclear

source: plugins/widgets/_widgets_functions.php @ 2667:ef63c4e390be

Revision 2667:ef63c4e390be, 9.7 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Add widget class on every widget, fixes #1901

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

Sites map