/[drupal]/contributions/modules/feed_node/feed_node.module
ViewVC logotype

Contents of /contributions/modules/feed_node/feed_node.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Tue Dec 19 19:57:57 2006 UTC (2 years, 11 months ago) by dayre
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +8 -2 lines
File MIME type: text/x-php
Fixed inc file inclusion error which would prevent running both feed_node and feedfield modules at the same time, which both rely on the aggregation_api.inc file.  feed_node & feedfield module mainters are working on adding stabilty and features to this api for wider usage with other aggregation related modules (e.g. feedparser, and perhaps core aggregator).
1 <?php
2 // $Id: feed_node.module,v 1.2 2006/12/18 23:47:07 dayre Exp $
3
4 /**
5 * The purpose of this module is to provide a method for displaying aggregator feed items
6 * within a node body. One can "feed enable" a particular node type which allows
7 * the user to attach a feed to a node (title and URL). Once a feed is attached, a new feed
8 * is created within the aggregator module and will be updated according to the update rules
9 * specified within aggregator.
10 *
11 * When a feed detail (either title or url) is changed in the node edit form, the
12 * corresponding feed details for the aggregator feed will be updated as well.
13 */
14
15 // Currently, the feedfield project also uses the same aggregation_api.inc file
16 // which we need to check if already included. feed_node and feedfield are
17 // working towards adding more stability and functionality to the api for
18 // wider usage with other aggregator related modules.
19 if (!function_exists('aggregation_sync_feed')) {
20 include('aggregation_api.inc');
21 }
22
23 /**
24 * Help hook for feed_node.
25 */
26 function feed_node_help($section) {
27 switch ($section) {
28 case 'admin/modules#description':
29 $output = t('The feed node module enables a node to have an aggregator feed associated with it.');
30 break;
31 case "admin/settings/feed_node":
32 $output = '<p>' . t('The feed node module allows you to associate an aggregator feed with a particular node so that aggregator feed items can be displayed within the node body.') . '</p>';
33 break;
34 }
35
36 return $output;
37 }
38
39 /**
40 * Implementation of hook_menu().
41 */
42 function feed_node_menu($may_cache) {
43 $items = array();
44
45 $items[] = array('path' => 'admin/settings/feed_node', 'title' => t('feed node'),
46 'callback' => 'feed_node_settings',
47 'access' => user_access('administer feed node'));
48
49 return $items;
50 }
51
52 /**
53 * Implementation of hook_perm().
54 */
55 function feed_node_perm() {
56 return array('administer feed nodes');
57 }
58
59 function _feed_node_aggregator_dependency_check() {
60 if (!module_exist("aggregator")) {
61 drupal_set_message(t("You do not have the aggregator module installed. Please ensure this is enabled or feed node will not work."), "error");
62 }
63 }
64
65 /**
66 * Feed node implementation of the administrative settings hook.
67 */
68 function feed_node_settings() {
69
70 _feed_node_aggregator_dependency_check();
71
72 $form = array();
73 $form['feed_node_allowed_html_tags'] = array(
74 '#type' => 'textfield',
75 '#title' => t('Allowed HTML tags'),
76 '#size' => 80,
77 '#maxlength' => 255,
78 '#default_value' => variable_get("feed_node_allowed_html_tags", '<blockquote> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>'),
79 '#description' => t('The list of tags which are allowed in feed items, i.e., which will not be stripped out.')
80 );
81
82 $form['feed_node_teaser_length'] = array(
83 '#type' => 'textfield',
84 '#title' => t('Teaser length'),
85 '#size' => 10,
86 '#maxlength' => 5,
87 '#default_value' => variable_get('feed_node_teaser_length', 200),
88 '#description' => t('The maximum length for feed items. Feed items longer than this will be truncated.')
89 );
90
91 $form['feed_node_delete_flag'] = array(
92 '#type' => 'checkbox',
93 '#title' => t('Delete aggregator feed on node delete'),
94 '#default_value' => variable_get('feed_node_delete_flag', 1),
95 '#description' => t('Each feed enabled node is associated with a corresponding aggregator feed. If this is checked, then when the node is deleted, the corresponding aggregator feed is also deleted.')
96 );
97
98 // we could just to a standard 4.7 forms API returning of the $form variable,
99 // but the only way to get a admin/settings menu title that says "feed node" instead
100 // of "feed_node" is to specify a custom menu callback hook to this settings method, where
101 // we now have to do some more traditional form stuff... like create a corresponding
102 // submit method
103 $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
104 $form['clear'] = array('#type' => 'submit', '#value' => t('Reset to defaults'));
105
106 return drupal_get_form('feed_node_settings', $form);
107 }
108
109 function feed_node_settings_submit($form_id, $values) {
110 $op = isset($_POST['op']) ? $_POST['op'] : '';
111 $key = $values['var'];
112
113 // Exclude unnecessary elements.
114 unset($values['submit'], $values['reset'], $values['form_id']);
115
116 if ($op == t('Reset to defaults')) {
117 variable_del("feed_node_allowed_html_tags");
118 variable_del("feed_node_delete_flag");
119 variable_del("feed_node_teaser_length");
120 drupal_set_message(t('The configuration options have been reset to their default values.'));
121 }
122 else {
123 variable_set("feed_node_allowed_html_tags", $values['feed_node_allowed_html_tags']);
124 variable_set("feed_node_delete_flag", $values['feed_node_delete_flag']);
125 variable_set("feed_node_teaser_length", $values['feed_node_teaser_length']);
126 drupal_set_message(t('The configuration options have been saved.'));
127 }
128 }
129
130
131 /**
132 * Validates feed information specified in the feed node section of a node edit form. This method
133 * checks that all the mandatory aggregator feed information is present.
134 *
135 * @param $form
136 */
137 function _feed_node_validate($form) {
138
139 // if only one of feedtitle or feedurl exist, error
140 // if both exist, then numitems must exist
141 if (trim($form->feed_node['aggregator_feed']['title'])
142 && !trim($form->feed_node['aggregator_feed']['url'])) {
143 form_set_error('feed_node][aggregator_feed][url', t('If you specify a feed title, you must also specify a feed URL.'));
144 }
145 if (!trim($form->feed_node['aggregator_feed']['title'])
146 && trim($form->feed_node['aggregator_feed']['url'])) {
147 form_set_error('feed_node][aggregator_feed][title', t('If you specify a feed URL, you must also specify a feed title.'));
148 }
149
150 if (trim($form->feed_node['aggregator_feed']['title'])
151 && trim($form->feed_node['aggregator_feed']['url']) && !trim($form->feed_node['numItems'])) {
152 form_set_error('feed_node][numItems', t('If you specify a feed, you must also specify how many feed items to list.'));
153 }
154
155 // check to see that we don't have a conflict with an existing feed
156 // in the database
157 $urlfeed = aggregation_get_feed_by_url($form->feed_node['aggregator_feed']['url']);
158 $titlefeed = aggregation_get_feed_by_title($form->feed_node['aggregator_feed']['title']);
159 if ($urlfeed && $titlefeed && $urlfeed['fid'] != $titlefeed['fid']) {
160 form_set_error('feed_node][aggregator_feed][url', t('Feed conflict. One feed already exists with the specified URL while a different feed exists with the specified title. Change one or the other.'));
161 form_set_error('feed_node][aggregator_feed][title', t('Feed conflict. One feed already exists with the specified URL while a different feed exists with the specified title. Change one or the other.'));
162 }
163 }
164
165
166 /**
167 * Node API workflow hooks for insert, load, update and delete.
168 *
169 * @param $node
170 * @param $op
171 */
172 function feed_node_nodeapi(&$node, $op) {
173
174 switch ($op) {
175 case 'validate':
176 _feed_node_validate($node);
177 break;
178
179 // load the extended node attributes
180 case 'load':
181
182 // fetch the feed node
183 $feed_node = db_fetch_array(db_query('SELECT * FROM {feed_node} WHERE nid = %d', $node->nid));
184
185 // if no feed attached to the node, nothing to do...
186 if ($feed_node) {
187 // fetch the aggregator feed
188 $aggregator_feed = aggregation_get_feed_by_id($feed_node['fid']);
189
190 // fetch the aggregator items for the feed
191 $result = db_query('SELECT * FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC, iid DESC LIMIT %d', $feed_node['fid'], $feed_node['numItems']);
192 while ($data = db_fetch_array($result)) {
193 $aggregator_items[] = $data;
194 }
195
196 // tack on the feed and feed items
197 $feed_node['aggregator_feed'] = $aggregator_feed;
198 $feed_node['aggregator_items'] = $aggregator_items;
199
200 // return the additional node attributes
201 return array('feed_node' => $feed_node);
202 }
203 break;
204
205 // insert the extended feed node attributes, only if we a feed title or feed url
206 case 'insert':
207 case 'update':
208 if ($op == "update") {
209 // do a fresh insert
210 db_query('DELETE FROM {feed_node} WHERE nid = %d', $node->nid);
211 }
212
213 // if we have a feed, ensure it is synced with the aggregator feed. Syncing ensures that
214 // the feed exists in the aggregator table. If it doesn't, a new entry is created. If the
215 // title or URL matches an existing entry, then the call to aggregation_sync_feed() ensures that
216 // the aggregator feed item is updated so that the title and URL specified are updated for that
217 // feed.
218 if (trim($node->feed_node['aggregator_feed']['title']) && trim($node->feed_node['aggregator_feed']['url'])) {
219 $fid = aggregation_sync_feed($node->feed_node['aggregator_feed']['title'], $node->feed_node['aggregator_feed']['url']);
220 if ($fid != -1) {
221 db_query('INSERT INTO {feed_node} (nid, fid, numItems, displayStyle) VALUES (%d, %d, %d, %d)', $node->nid, $fid, $node->feed_node['numItems'], $node->feed_node['displayStyle']);
222 }
223 else {
224
225 }
226 }
227
228 break;
229
230 // delete the extended feed node attributes
231 case 'delete':
232 db_query('DELETE FROM {feed_node} WHERE nid = %d', $node->nid);
233
234 // if this node has an associated feed and the delete flag is set,
235 // delete the feed and feed items
236 if (aggregation_get_feed_by_id($node->feed_node['fid']) && variable_get('feed_node_delete_flag', 1)) {
237 aggregation_delete_feed($node->feed_node['fid']);
238 }
239 break;
240
241 case 'view':
242 // if we have an attached feed, display it in the body of the node
243 if ($node->feed_node) {
244 $node->body = $node->body . theme('feed_node', $node->feed_node);
245 }
246 break;
247 }
248 }
249
250 /**
251 * Hook to alter the node edit form so we can allow the user to specify feed information and
252 * configuration info.
253 *
254 * @param $form_id
255 * @param $form
256 *
257 */
258 function feed_node_form_alter($form_id, &$form) {
259
260 // We're only modifying node forms, if the type field isn't set we don't need
261 // to bother.
262 if (!isset($form['type'])) {
263 return;
264 }
265
266 $type = $form['type']['#value'];
267
268 // The feeds are enabled on a per node type basis. The variable used to store
269 // this information is named using both the name of this module, to
270 // avoid namespace conflicts, and the node, because we support multiple node
271 // types.
272 $enabled = variable_get('feed_node_'. $type, 0);
273
274 switch ($form_id) {
275
276 // Settings form allowing node types to have display aggregator feed items output.
277 case $type .'_node_settings':
278 $form['workflow']['feed_node_'. $type] = array(
279 '#type' => 'radios',
280 '#title' => t('Feed node capability'),
281 '#default_value' => $enabled,
282 '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
283 '#description' => t('Can this node type have an aggregator feed attached to it ?'),
284 );
285 break;
286
287 case $type .'_node_form':
288
289 // If the feed is enabled for this node type, we insert our control
290 // into the form.
291 if ($enabled) {
292
293 // if we have a feed, body isn't mandatory, we override the
294 // default of a mandatory body.
295 $form['body_filter']['body']['#required'] = 0;
296
297 $form['feed_node'] = array(
298 '#type' => 'fieldset',
299 '#title' => t('Feed information'),
300 '#collapsible' => TRUE,
301 '#collapsed' => TRUE,
302 '#tree' => TRUE,
303 '#weight' => 1
304 );
305
306 // fetch feed data to populate title and url form elements
307 $feed = aggregation_get_feed_by_id($form['#node']->feed_node['fid']);
308
309 $form['feed_node']['aggregator_feed']['title'] = array(
310 '#type' => 'textfield',
311 '#title' => t('Title of feed'),
312 '#default_value' => $feed['title'],
313 '#required' => FALSE,
314 '#maxlength' => 255,
315 '#tree' => TRUE,
316 '#weight' => 0,
317 );
318
319 $form['feed_node']['aggregator_feed']['url'] = array(
320 '#type' => 'textfield',
321 '#title' => t('Feed URL'),
322 '#default_value' => $feed['url'],
323 '#required' => FALSE,
324 '#maxlength' => 255,
325 '#weight' => 0,
326 );
327
328 $numItems = $form['#node']->feed_node['numItems'];
329 $form['feed_node']['numItems'] = array(
330 '#type' => 'textfield',
331 '#title' => t('Number of feed items to display'),
332 '#default_value' => $numItems ? $numItems : 10 ,
333 '#required' => FALSE,
334 '#maxlength' => 10,
335 '#weight' => 1,
336 );
337
338 $form['feed_node']['displayStyle'] = array(
339 '#type' => 'radios',
340 '#title' => t('Display style'),
341 '#default_value' => $form['#node']->feed_node['displayStyle'],
342 '#options' => array(t('Title + Teaser'), t('Title only')),
343 );
344 } // endif
345 break;
346
347 } // end switch
348
349 }
350
351
352 /**
353 * Given an aggregator item result set, returns an array of arrays
354 * containing aggregator item records with duplicate entries removed.
355 * This method is used to help cleanup the insertion of duplicate
356 * aggregator items into the aggregator_item table by the aggregator
357 * cron updates. Duplicate item entry is only seen when the number
358 * of aggregator feeds are in the hundreds and is caused by overlapping
359 * cron job updates inserting the same feed items multiple times.
360 * Duplicates are determined using a combinaton of item timestamp and
361 * an MD5 hash of the item description field.
362 *
363 * @param $result
364 * A database result set of aggregator_item rows containing at least the timestamp and description fields
365 * @return
366 * An array of aggregator_item row data with duplicates items removed.
367 */
368 function _feed_node_fetch_unique_feed_items($result) {
369
370 $item_tracker = array();
371 if (db_num_rows($result)) {
372 while ($item = db_fetch_array($result)) {
373 if ($item_tracker[$item['timestamp']] != md5($item['description'])) {
374 $item_tracker[$item['timestamp']] = md5($item['description']);
375 $items[] = $item;
376 }
377 }
378 }
379 return $items;
380 }
381
382 /**
383 * Displays the specified number of feed items for the feed with the specified feed id.
384 *
385 * @param $feed_node
386 * @return
387 */
388 function theme_feed_node($feed_node) {
389
390 $output = '<div class="feed_node">';
391
392 // output feed title and meta data
393 $feed = $feed_node['aggregator_feed'];
394 $output .= '<h3>'. t($feed['title']) .'</h3>';
395
396 if ($feed['link']) {
397 $output .= '<div class="feed-url"><em>'. t('URL:') .'</em> '. l($feed['link'], $feed['link'], array(), NULL, NULL, TRUE) ."</div>\n";
398 }
399
400 if ($feed['checked']) {
401 $updated = t('%time ago', array('%time' => format_interval(time() - $feed['checked'])));
402 }
403 else {
404 $updated = t('never');
405 }
406
407 if (user_access('administer news feeds')) {
408 $updated = l($updated, 'admin/aggregator');
409 }
410
411 $output .= '<div class="feed-updated"><em>'. t('Updated:') . "</em> $updated</div>";
412
413 // output individual feed items
414 $aggregator_items = $feed_node['aggregator_items'];
415 if ($aggregator_items) {
416 foreach ($aggregator_items as $aggregator_item) {
417 $item_outputs[] = $feed_node['displayStyle']
418 ? theme('feed_node_item_summary', $aggregator_item)
419 : theme('feed_node_item', $aggregator_item);
420 }
421 }
422 else {
423 $item_outputs[] = t("No feed items available.");
424 }
425
426 // if title only, theme as a list, else concatenate item outputs.
427 $output .= $feed_node['displayStyle'] ? theme('item_list', $item_outputs) : implode("\n", $item_outputs);
428
429 $output .= '</div>';
430
431 return $output;
432 }
433
434 /**
435 * Returns a themed summary display for an individual aggregator feed item.
436 *
437 * @param $item
438 * The item array from the aggregator module.
439 * @return
440 * A string containing the HTML fragment.
441 */
442 function theme_feed_node_item_summary($item) {
443 $output = '<a href="' .
444 check_url($item['link']) .'">' .
445 check_plain($item['title']) .
446 '</a> <span class="age">'.
447 t('%age old', array('%age' => format_interval(time() - $item['timestamp']))) .'</span>';
448 return $output ."\n";
449 }
450
451
452 /**
453 * Returns a themed detail display for an individual aggregator feed item.
454 *
455 * @param $item
456 * The item array from the aggregator module.
457 * @return
458 * A string containing the HTML fragment.
459 */
460 function theme_feed_node_item($item) {
461
462 if (date('Ymd', $item['timestamp']) == date('Ymd')) {
463 $source_date = t('%ago ago', array('%ago' => format_interval(time() - $item['timestamp'])));
464 }
465 else {
466 $source_date = format_date($item['timestamp'], 'custom', variable_get('date_format_medium', 'D, Y-m-d H:i'));
467 }
468
469 $output .= "<div class=\"feed_node_item\">\n";
470 $output .= '<h3 class="item_title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) ."</a></h3>\n";
471 $output .= "<div class=\"item_meta\"><span class=\"item_date\">$source_date</span></div>\n";
472
473 if ($item['description']) {
474 $output .= '<div class="item_description">'. _feed_node_truncate($item['description']) ."</div>\n";
475 }
476
477 // if the feed item belongs to categories, list them.
478 $result = db_query('SELECT c.title, c.cid FROM {aggregator_category_item} ci LEFT JOIN {aggregator_category} c ON ci.cid = c.cid WHERE ci.iid = %d ORDER BY c.title', $item['iid']);
479 $categories = array();
480 while ($category = db_fetch_object($result)) {
481 $categories[] = l($category->title, 'aggregator/categories/'. $category->cid);
482 }
483 if ($categories) {
484 $output .= '<div class="item_categories">'. t('Categories') .': '. implode(', ', $categories) ."</div>\n";
485 }
486
487 $output .= "</div>\n";
488
489 return $output;
490 }
491
492
493 /**
494 * Truncate function to mimic feedburner style feed content summarization. It uses the
495 * filter_xss() method to filter out any external references, then truncates the text
496 * to the specified length.
497 *
498 * @param $html
499 * The HTML fragment to summarize
500 * @param $length
501 * The maximum length the truncated fragment should be.
502 * @return
503 * The truncated HTMl fragment.
504 */
505 function _feed_node_truncate($html, $length = 200) {
506
507 // similar to feedburner summarize feature, strips out links, then truncates to approximate
508 // length.
509 $html = filter_xss($html, preg_split('/\s+|<|>/', variable_get("feed_node_allowed_html_tags", '<blockquote> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>'), -1, PREG_SPLIT_NO_EMPTY));
510
511 // zip in $length chars, then skip to the next word boundary
512 if (preg_match("/(.{" . $length . "})(.*?)\W/", $html, $m)) {
513 $html = $m[1] . $m[2] . "...";
514 }
515
516 // if still over the length due to extremely long word, chop it
517 if (strlen($html) > $length) {
518 $html = substr($html, 0, $length) . "...";
519 }
520
521 return $html;
522 }
523
524
525 ?>

  ViewVC Help
Powered by ViewVC 1.1.2