Dotclear

source: plugins/feedEntries/_public.php @ 1006

Revision 1006, 9.6 KB checked in by pep, 14 years ago (diff)

feedEntries : initial import in DC Lab

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of feedEntries, a plugin for Dotclear 2.
5#
6# Copyright (c) 2008 Pep and contributors
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
14$core->tpl->addBlock('Feed',                 array('feedEntriesTemplates','Feed'));
15$core->tpl->addValue('FeedTitle',            array('feedEntriesTemplates','FeedTitle'));
16$core->tpl->addValue('FeedURL',              array('feedEntriesTemplates','FeedURL'));
17$core->tpl->addValue('FeedDescription',      array('feedEntriesTemplates','FeedDescription'));
18$core->tpl->addBlock('FeedEntries',          array('feedEntriesTemplates','FeedEntries'));
19$core->tpl->addBlock('FeedEntriesHeader',    array('feedEntriesTemplates','FeedEntriesHeader'));
20$core->tpl->addBlock('FeedEntriesFooter',    array('feedEntriesTemplates','FeedEntriesFooter'));
21$core->tpl->addBlock('FeedEntryIf',          array('feedEntriesTemplates','FeedEntryIf'));
22$core->tpl->addValue('FeedEntryIfFirst',     array('feedEntriesTemplates','FeedEntryIfFirst'));
23$core->tpl->addValue('FeedEntryIfOdd',       array('feedEntriesTemplates','FeedEntryIfOdd'));
24$core->tpl->addValue('FeedEntryTitle',       array('feedEntriesTemplates','FeedEntryTitle'));
25$core->tpl->addValue('FeedEntryURL',         array('feedEntriesTemplates','FeedEntryURL'));
26$core->tpl->addValue('FeedEntryAuthor',      array('feedEntriesTemplates','FeedEntryAuthor'));
27$core->tpl->addValue('FeedEntrySummary',     array('feedEntriesTemplates','FeedEntrySummary'));
28$core->tpl->addValue('FeedEntryExcerpt',     array('feedEntriesTemplates','FeedEntryExcerpt'));
29$core->tpl->addValue('FeedEntryContent',     array('feedEntriesTemplates','FeedEntryContent'));
30$core->tpl->addValue('FeedEntryPubdate',     array('feedEntriesTemplates','FeedEntryPubdate'));
31
32class feedEntriesTemplates
33{
34     /**
35      * Start a feed block
36      * <tpl:Feed source="url"></tpl:Feed>
37      *
38      * Attribute(s) :
39      * - source = URL of the feed to fetch and render (required)
40      *
41      */
42     public static function Feed($attr,$content)
43     {
44          if (empty($attr['source'])) {
45               return;
46          }
47         
48          if (strpos($attr['source'],'/') === 0) {
49               $attr['source'] = http::getHost().$attr['source'];
50          }
51         
52          return
53               '<?php'."\n".
54               '$_ctx->feed = feedReader::quickParse("'.$attr['source'].'",DC_TPL_CACHE); '."\n".
55               'if ($_ctx->feed !== null) : ?>'."\n".
56               $content."\n".
57               '<?php unset($_ctx->feed); '."\n".
58               'endif; ?>'."\n";
59     }
60     
61     /**
62      * Display the title of the current feed
63      * {{tpl:FeedTitle}}
64      *
65      */
66     public static function FeedTitle($attr)
67     {
68          $f = $GLOBALS['core']->tpl->getFilters($attr);
69          return '<?php echo '.sprintf($f,'$_ctx->feed->title').'; ?>';
70     }
71     
72     /**
73      * Display the source URL of the current feed
74      * {{tpl:FeedURL}}
75      *
76      */
77     public static function FeedURL($attr)
78     {
79          $f = $GLOBALS['core']->tpl->getFilters($attr);
80          return '<?php echo '.sprintf($f,'$_ctx->feed->link').'; ?>';
81     }
82     
83     /**
84      * Display the description of the current feed
85      * {{tpl:FeedDescription}}
86      *
87      */
88     public static function FeedDescription($attr)
89     {
90          $f = $GLOBALS['core']->tpl->getFilters($attr);
91          return '<?php echo '.sprintf($f,'$_ctx->feed->description').'; ?>';
92     }
93     
94     /**
95      * Start the loop to process each entry in the current feed
96      * <tpl:FeedEntries lastn="nb"></tpl:FeedEntries>
97      *
98      * Attribute(s) :
99      * - lastn = Number of entries to show (optional, default to 10)
100      *
101      */
102     public static function FeedEntries($attr,$content)
103     {
104          $lastn = 10;
105          if (isset($attr['lastn'])) {
106               $lastn = abs((integer) $attr['lastn'])+0;
107          }
108         
109          return
110               '<?php'."\n".
111               'if (count($_ctx->feed->items)) : '."\n".
112               '$nb_feed_items = min(count($_ctx->feed->items),'.$lastn.');'."\n".
113               'for ($_ctx->feed_idx = 0; $_ctx->feed_idx < $nb_feed_items; $_ctx->feed_idx++) : ?>'."\n".
114               $content."\n".
115               '<?php endfor;'."\n".
116               'unset($_ctx->feed_idx,$nb_feed_items); '."\n".
117               'endif; ?>'."\n";
118     }
119     
120     /**
121      * Display a block at the start of the entries loop
122      * <tpl:FeedEntriesHeader></tpl:FeedEntriesHeader>
123      *
124      */
125     public static function FeedEntriesHeader($attr,$content)
126     {
127          return
128          "<?php if (\$_ctx->feed_idx == 0) : ?>".
129          $content.
130          "<?php endif; ?>";
131     }
132     
133     /**
134      * Display a block at the end of the entries loop
135      * <tpl:FeedEntriesFooter></tpl:FeedEntriesFooter>
136      *
137      */
138     public static function FeedEntriesFooter($attr,$content)
139     {
140          return
141          "<?php if (\$_ctx->feed_idx == ($nb_feed_items - 1)) : ?>".
142          $content.
143          "<?php endif; ?>";
144     }
145
146     /**
147      * Display a block only if some conditions are matched.
148      * <tpl:FeedEntryIf></tpl:FeedEntryIf>
149      *
150      * Attribute(s) :
151      * - operator (optional) = logical operator used to compute multiple conditions
152      * - first (optional)    = test if the current entry is the first in set
153      * - odd (optional)      = test if the current entry has an odd index in set
154      * - extended (optional) = test if the current entry has a complete (non-empty) "description" property
155      *
156      */
157     public static function FeedEntryIf($attr,$content)
158     {
159          $if = array();
160         
161          $operator = isset($attr['operator']) ? $GLOBALS['core']->tpl->getOperator($attr['operator']) : '&&' ;
162         
163          if (isset($attr['first'])) {
164               $sign = (boolean) $attr['first'] ? '=' : '!';
165               $if[] = '$_ctx->feed_idx '.$sign.'= 0';
166          }
167         
168          if (isset($attr['odd'])) {
169               $sign = (boolean) $attr['odd'] ? '=' : '!';
170               $if[] = '($_ctx->feed_idx+1)%2 '.$sign.'= 1';
171          }
172         
173          if (isset($attr['extended'])) {
174               $sign = (boolean) $attr['extended'] ? '' : '!';
175               $if[] = $sign.'dcFeedEntries::isExtended()';
176          }
177
178          if (!empty($if)) {
179               return '<?php if('.implode(' '.$operator.' ',$if).') : ?>'.$content.'<?php endif; ?>';
180          } else {
181               return $content;
182          }         
183     }
184
185     /**
186      * Return a special class if the current entry is the first of the collection
187      * {{tpl:FeedEntryIfFirst}}
188      *
189      */
190     public static function FeedEntryIfFirst($attr)
191     {
192          $ret = isset($attr['return']) ? $attr['return'] : 'first';
193          $ret = html::escapeHTML($ret);
194         
195          return
196          '<?php if ($_ctx->feed_idx == 0) { '.
197          "echo '".addslashes($ret)."'; } ?>";
198     }
199
200     /**
201      * Return a special class if the current entry has an odd index in the collection
202      * {{tpl:FeedEntryIfOdd}}
203      *
204      */
205     public static function FeedEntryIfOdd($attr)
206     {
207          $ret = isset($attr['return']) ? $attr['return'] : 'odd';
208          $ret = html::escapeHTML($ret);
209         
210          return
211          '<?php if (($_ctx->feed_idx+1)%2 == 1) { '.
212          "echo '".addslashes($ret)."'; } ?>";
213     }
214     
215     /**
216      * Display the title of the current entry
217      * {{tpl:FeedEntryTitle}}
218      *
219      */
220     public static function FeedEntryTitle($attr)
221     {
222          $f = $GLOBALS['core']->tpl->getFilters($attr);
223          return '<?php echo '.sprintf($f,'$_ctx->feed->items[$_ctx->feed_idx]->title').'; ?>';
224     }
225
226     /**
227      * Display the source URL of the current entry
228      * {{tpl:FeedEntryURL}}
229      *
230      */
231     public static function FeedEntryURL($attr)
232     {
233          $f = $GLOBALS['core']->tpl->getFilters($attr);
234          return '<?php echo '.sprintf($f,'$_ctx->feed->items[$_ctx->feed_idx]->link').'; ?>';
235     }
236
237     /**
238      * Display the author of the current entry
239      * {{tpl:FeedEntryAuthor}}
240      *
241      */
242     public static function FeedEntryAuthor($attr)
243     {
244          $f = $GLOBALS['core']->tpl->getFilters($attr);
245          return '<?php echo '.sprintf($f,'$_ctx->feed->items[$_ctx->feed_idx]->creator').'; ?>';
246     }
247
248     /**
249      * Display the summary of the current entry.
250      * {{tpl:FeedEntrySummary}}
251      *
252      */
253     public static function FeedEntrySummary($attr)
254     {
255          $f = $GLOBALS['core']->tpl->getFilters($attr);
256          return '<?php echo '.sprintf($f,'$_ctx->feed->items[$_ctx->feed_idx]->description').'; ?>';
257     }
258
259     /**
260      * Display an excerpt of the current entry.
261      * {{tpl:FeedEntryExcerpt}}
262      *
263      */
264     public static function FeedEntryExcerpt($attr)
265     {
266          $f = $GLOBALS['core']->tpl->getFilters($attr);
267          return '<?php echo '.sprintf($f,'dcFeedEntries::getExcerpt()').'; ?>';
268     }
269     
270     /**
271      * Display the full content of the current entry
272      * {{tpl:FeedEntryContent}}
273      *
274      */
275     public static function FeedEntryContent($attr)
276     {
277          $f = $GLOBALS['core']->tpl->getFilters($attr);
278          return '<?php echo '.sprintf($f,'$_ctx->feed->items[$_ctx->feed_idx]->content').'; ?>';
279     }
280
281     /**
282      * Display the publication date and/or time of the current entry
283      * {{tpl:FeedEntryPubdate format="strftime"}}
284      *
285      * Attribute(s) :
286      * - format = Format string compatible with PHP strftime()
287      *            (optional, default to the date_format setting of the running blog)
288      *
289      */
290     public static function FeedEntryPubdate($attr)
291     {
292          $fmt = $GLOBALS['core']->blog->settings->date_format;
293          if (!empty($attr['format'])) {
294               $fmt = $attr['format'];
295          }
296          $f = $GLOBALS['core']->tpl->getFilters($attr);
297          return '<?php echo '.sprintf($f,'dt::str("'.$fmt.'",$_ctx->feed->items[$_ctx->feed_idx]->TS,$core->blog->settings->blog_timezone)').'; ?>';
298     }
299}
300
301class dcFeedEntries
302{
303     /**
304      * Get an excerpt from a feed entry.
305      * Returns the "description" property as is if available, or a filtered version of the "content" property.
306      * By "filtered" we mean clean from any HTML markup.
307      *
308      * @return     string    The text to be used as an excerpt
309      *
310      */
311     public static function getExcerpt()
312     {
313          global $core,$_ctx;
314         
315          if (!$_ctx->feed || is_null($_ctx->feed_idx)) {
316               return;
317          }
318         
319          if ($_ctx->feed->items[$_ctx->feed_idx]->description) {
320               return $_ctx->feed->items[$_ctx->feed_idx]->description;
321          }
322          else {
323               return html::clean($_ctx->feed->items[$_ctx->feed_idx]->content);
324          }
325     }
326     
327     /**
328      * Check if the current feed entry has a non-empty description property
329      *
330      * @return     boolean   True if the "description" property isn't empty, false elsewise
331      */
332     public static function isExtended()
333     {
334          global $core,$_ctx;
335         
336          if (!$_ctx->feed || is_null($_ctx->feed_idx)) {
337               return false;
338          }
339         
340          return ($_ctx->feed->items[$_ctx->feed_idx]->description != '');
341     }
342}
343?>
Note: See TracBrowser for help on using the repository browser.

Sites map