Dotclear

source: plugins/blogroll/_public.php @ 2778:fe7e025273c4

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

Add "offline" option on widgets, closes #1945

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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14require dirname(__FILE__).'/_widgets.php';
15
16# Blogroll template functions
17$core->tpl->addValue('Blogroll',array('tplBlogroll','blogroll'));
18$core->tpl->addValue('BlogrollXbelLink',array('tplBlogroll','blogrollXbelLink'));
19
20$core->url->register('xbel','xbel','^xbel(?:/?)$',array('urlBlogroll','xbel'));
21
22class tplBlogroll
23{
24     public static function blogroll($attr)
25     {
26          $category='<h3>%s</h3>';
27          $block='<ul>%s</ul>';
28          $item='<li%2$s>%1$s</li>';
29
30          if (isset($attr['category'])) {
31               $category = addslashes($attr['category']);
32          }
33
34          if (isset($attr['block'])) {
35               $block = addslashes($attr['block']);
36          }
37
38          if (isset($attr['item'])) {
39               $item = addslashes($attr['item']);
40          }
41
42          $only_cat = 'null';
43          if (!empty($attr['only_category'])) {
44               $only_cat = "'".addslashes($attr['only_category'])."'";
45          }
46
47          return
48          '<?php '.
49          "echo tplBlogroll::getList('".$category."','".$block."','".$item."',".$only_cat."); ".
50          '?>';
51     }
52
53     public static function blogrollXbelLink($attr)
54     {
55          $f = $GLOBALS['core']->tpl->getFilters($attr);
56          return '<?php echo '.sprintf($f,'$core->blog->url.$core->url->getURLFor("xbel")').'; ?>';
57     }
58
59     public static function getList($cat_title='<h3>%s</h3>',$block='<ul>%s</ul>',$item='<li>%s</li>',$category=null)
60     {
61          $blogroll = new dcBlogroll($GLOBALS['core']->blog);
62
63          try {
64               $links = $blogroll->getLinks();
65          } catch (Exception $e) {
66               return false;
67          }
68
69          $res = '';
70
71          $hierarchy = $blogroll->getLinksHierarchy($links);
72
73          if ($category) {
74               if (!isset($hierarchy[$category])) {
75                    return '';
76               }
77               $hierarchy = array($hierarchy[$category]);
78          }
79
80          foreach ($hierarchy as $k => $v)
81          {
82               if ($k != '') {
83                    $res .= sprintf($cat_title,html::escapeHTML($k))."\n";
84               }
85
86               $res .= self::getLinksList($v,$block,$item);
87          }
88
89          return $res;
90     }
91
92     private static function getLinksList($links,$block='<ul>%s</ul>',$item='<li%2$s>%1$s</li>')
93     {
94          $list = '';
95
96          # Find current link item if any
97          $current = -1;
98          $current_size = 0;
99          $self_uri = http::getSelfURI();
100
101          foreach ($links as $k => $v)
102          {
103               if (!preg_match('$^([a-z][a-z0-9.+-]+://)$',$v['link_href'])) {
104                    $url = http::concatURL($self_uri,$v['link_href']);
105                    if (strlen($url) > $current_size && preg_match('/^'.preg_quote($url,'/').'/',$self_uri)) {
106                         $current = $k;
107                         $current_size = strlen($url);
108                    }
109               }
110          }
111
112          foreach ($links as $k => $v)
113          {
114               $title = $v['link_title'];
115               $href  = $v['link_href'];
116               $desc = $v['link_desc'];
117               $lang  = $v['link_lang'];
118               $xfn = $v['link_xfn'];
119
120               $link =
121               '<a href="'.html::escapeHTML($href).'"'.
122               ((!$lang) ? '' : ' hreflang="'.html::escapeHTML($lang).'"').
123               ((!$desc) ? '' : ' title="'.html::escapeHTML($desc).'"').
124               ((!$xfn) ? '' : ' rel="'.html::escapeHTML($xfn).'"').
125               '>'.
126               html::escapeHTML($title).
127               '</a>';
128
129               $current_class = $current == $k ? ' class="active"' : '';
130
131               $list .= sprintf($item,$link,$current_class)."\n";
132          }
133
134          return sprintf($block,$list)."\n";
135     }
136
137     # Widget function
138     public static function linksWidget($w)
139     {
140          global $core;
141
142          if ($w->offline)
143               return;
144
145          if (($w->homeonly == 1 && $core->url->type != 'default') ||
146               ($w->homeonly == 2 && $core->url->type == 'default')) {
147               return;
148          }
149
150          $links = self::getList($w->renderSubtitle('',false),'<ul>%s</ul>','<li%2$s>%1$s</li>',$w->category);
151
152          if (empty($links)) {
153               return;
154          }
155
156          return $w->renderDiv($w->content_only,'links '.$w->class,'',
157               ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '').
158               $links);
159     }
160}
161
162class urlBlogroll extends dcUrlHandlers
163{
164     public static function xbel($args)
165     {
166          $blogroll = new dcBlogroll($GLOBALS['core']->blog);
167
168          try {
169               $links = $blogroll->getLinks();
170          } catch (Exception $e) {
171               self::p404();
172               return;
173          }
174
175          if ($args) {
176               self::p404();
177               return;
178          }
179
180          http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);
181
182          header('Content-Type: text/xml; charset=UTF-8');
183
184          echo
185          '<?xml version="1.0" encoding="UTF-8"?>'."\n".
186          '<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange '.
187          'Language 1.0//EN//XML"'."\n".
188          '"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">'."\n".
189          '<xbel version="1.0">'."\n".
190          '<title>'.html::escapeHTML($GLOBALS['core']->blog->name)." blogroll</title>\n";
191
192          $i = 1;
193          foreach ($blogroll->getLinksHierarchy($links) as $cat_title => $links)
194          {
195               if ($cat_title != '') {
196                    echo
197                    '<folder>'."\n".
198                    "<title>".html::escapeHTML($cat_title)."</title>\n";
199               }
200
201               foreach ($links as $k => $v)
202               {
203                    $lang = $v['link_lang'] ? ' xml:lang="'.$v['link_lang'].'"' : '';
204
205                    echo
206                    '<bookmark href="'.$v['link_href'].'"'.$lang.'>'."\n".
207                    '<title>'.html::escapeHTML($v['link_title'])."</title>\n";
208
209                    if ($v['link_desc']) {
210                         echo '<desc>'.html::escapeHTML($v['link_desc'])."</desc>\n";
211                    }
212
213                    if ($v['link_xfn']) {
214                         echo
215                         "<info>\n".
216                         '<metadata owner="http://gmpg.org/xfn/">'.$v['link_xfn']."</metadata>\n".
217                         "</info>\n";
218                    }
219
220                    echo
221                    "</bookmark>\n";
222               }
223
224               if ($cat_title != '') {
225                    echo "</folder>\n";
226               }
227
228               $i++;
229          }
230
231          echo
232          '</xbel>';
233     }
234}
Note: See TracBrowser for help on using the repository browser.

Sites map