Dotclear

source: plugins/widgets/_widgets_functions.php @ 2778:fe7e025273c4

Revision 2778:fe7e025273c4, 10.0 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Add "offline" option on widgets, closes #1945

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

Sites map