/[drupal]/contributions/modules/feedparser/feedaggregator_node.module
ViewVC logotype

Contents of /contributions/modules/feedparser/feedaggregator_node.module

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


Revision 1.42 - (show annotations) (download) (as text)
Thu Jan 4 01:57:46 2007 UTC (2 years, 10 months ago) by budda
Branch: MAIN
CVS Tags: DRUPAL-5--0-1-dev, HEAD
Branch point for: DRUPAL-5, DRUPAL-4-7
Changes since 1.41: +4 -3 lines
File MIME type: text/x-php
Improved node creation process to allow calling of Drupal nodeapi function.
1 <?php
2 // $Id: feedaggregator_node.module,v 1.41 2007/01/01 23:52:09 budda Exp $
3 /**
4 * FeedAggregator Node
5 * @description: Generates 1st class nodes from aggregator feed items.
6 *
7 * @author: Mike Carter <mike @ www.ixis.co.uk/contact>
8 */
9
10 function feedaggregator_node_help($section){
11 switch($section){
12 case 'node/add#aggregator-item':
13 return t('A news item.');
14 case "admin/modules#description":
15 return t('<strong>Feed Processor</strong>: Creates nodes from feed items');
16 }
17 }
18
19
20 function feedaggregator_node_node_info() {
21 return array('aggregator-item' => array('name' => t('aggregator item'), 'base' => 'feedaggregator_node'));
22 }
23
24
25 function feedaggregator_node_perm() {
26 return array('edit aggregator items');
27 }
28
29
30 function feedaggregator_node_access($op, $node) {
31 global $user;
32
33 if ($op == 'create') {
34 // Only users with permission to do so may create this node type.
35 return user_access('administer news feeds');
36 }
37
38 // Users who create a node may edit or delete it later, assuming they have the
39 // necessary permissions.
40 if ($op == 'update' || $op == 'delete') {
41 if (user_access('edit aggregator items') && ($user->uid == $node->uid)) {
42 return TRUE;
43 }
44 }
45 }
46
47
48 /**
49 * Implementation of hook_menu().
50 */
51 function feedaggregator_node_menu($may_cache) {
52 global $user;
53 $items = array();
54
55 if ($may_cache) {
56 $items[] = array('path' => 'node/add/aggregator-item', 'title' => t('aggregator-item'),
57 'access' => user_access('edit aggregator items'));
58 }
59
60 return $items;
61 }
62
63
64 function feedaggregator_node_form(&$node) {
65 $form['title'] = array(
66 '#type' => 'textfield',
67 '#title' => t('Title'),
68 '#required' => TRUE,
69 '#default_value' => $node->title,
70 '#weight' => -5,
71 '#description' => t('A short descriptive title for the story.')
72 );
73
74 $form['body_filter']['body'] = array(
75 '#type' => 'textarea',
76 '#title' => t('Description'),
77 '#default_value' => $node->body,
78 '#rows' => '10',
79 '#required' => FALSE
80 );
81 $form['body_filter']['filter'] = filter_form($node->format);
82
83 $form['link'] = array(
84 '#type' => 'textfield',
85 '#title' => t('Link'),
86 '#required' => TRUE,
87 '#default_value' => $node->link,
88 '#weight' => -4,
89 '#description' => t('The fully-qualified URL to the complete story.')
90 );
91
92 return $form;
93 }
94
95
96 function feedaggregator_node_link($type, $node = 0, $teaser = FALSE) {
97 $links = array();
98
99 if ($type == 'node' && $node->type == 'aggregator-item') {
100 if (!$teaser && variable_get('feedmanager_showfullstory', TRUE)) {
101 // Make the full story url in to a clickable link when viewing the node
102 $attributes['target'] = variable_get('feedmanager_target', 'default');
103 if($rel = variable_get('feedmanager_rel', false)) $attributes['rel'] = $rel;
104
105 $links[] = l(t('full story'), $node->link, $attributes);
106 }
107 }
108
109 return $links;
110 }
111
112
113 function feedaggregator_node_update($node) {
114 db_query("UPDATE {aggregator_node} SET link = '%s' WHERE nid = %d", $node->link, $node->nid);
115 }
116
117
118 function feedaggregator_node_load($node) {
119 $result = db_query('SELECT af.title as feed_name, af.url as feed_url, af.image as feed_image, an.fid, an.iid, an.link FROM {aggregator_node} an LEFT JOIN {aggregator_feed} af ON af.fid = an.fid WHERE nid = %d', $node->nid);
120 $additions = db_fetch_object($result);
121
122 return $additions;
123 }
124
125
126 function feedaggregator_node_delete(&$node) {
127 db_query('DELETE FROM {aggregator_node} WHERE nid = %d', $node->nid);
128 }
129
130
131 function feedaggregator_node_insert($node) {
132 db_query("INSERT INTO {aggregator_node} SET iid = %d, nid = %d, fid = %d, link = '%s'", $node->iid, $node->nid, $node->fid, $node->link);
133 }
134
135
136 function feedaggregator_node_parsername() {
137 return array('aggregator-item');
138 }
139
140 function feedaggregator_get_vocabulary() {
141 return db_result(db_query("SELECT v.vid FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'aggregator-item'));
142 }
143
144 function feedaggregator_node_feedapi($feed, $op, $item = NULL) {
145 global $user;
146 static $cat_names = array();
147
148 switch($op) {
149 case 'processor_name':
150 return array('feedaggregator_node' => t('Nodes'));
151
152 case 'item_count':
153 $total_items = db_result(db_query('SELECT COUNT(an.nid) FROM {node} n JOIN {aggregator_node} an ON n.nid = an.nid WHERE an.fid = %d AND n.status = 1', $feed['fid']));
154 return array('item_count' => $total_items);
155
156 case 'item_save':
157 if($feed['processor'] == 'feedaggregator_node') {
158
159 $node['link'] = $item->get_link();
160
161 // Check if the item already exists
162 $duplicate = db_fetch_array(db_query("SELECT nid FROM {aggregator_node} WHERE link = '%s'", $node['link']));
163 if($duplicate) {
164 $old_node = (array)node_load(array('nid' => $duplicate['nid']));
165
166 // Should this feed honor updating of existing items?
167 if(!$feed['update_items'] || (
168 strcasecmp($old_node['body'], $item->get_description()) == 0
169 && strcasecmp($old_node['title'], $item->get_title()) == 0
170 && strcasecmp($old_node['link'], $item->get_link()) == 0
171 )) {
172 return FALSE;
173 }
174
175 $node['nid'] = $old_node['nid'];
176 $node['created'] = $old_node['created'];
177 } else {
178 // Set nodes default options
179 $options = variable_get('node_options_aggregator-item', array('status', 'promote'));
180 foreach($options as $option) {
181 $node[$option] = 1;
182 }
183
184 $item_time = $item->get_date('U');
185 $node['created'] = $item_time ? $item_time : time();
186 $node->date = format_date($node['created'], 'custom', 'Y-m-d H:i:s O');
187 }
188
189 $title = $item->get_title();
190
191 $node += array(
192 'type' => 'aggregator-item',
193 'changed' => time(),
194 'comment' => variable_get('comment_aggregator-item', 2),
195 'title' => parse_entities($title),
196 'teaser' => $item->get_description() ? node_teaser($item->get_description(), $feed['format']) : '',
197 'body' => $item->get_description(),
198 'fid' => $feed['fid'],
199 'iid' => $item->data['iid'],
200 'taxonomy' => $feed['taxonomy'],
201 'format' => $feed['format']
202 );
203
204 // Assign the author as specified in this feeds settings
205 $account = user_load(array('name' => $feed['author']));
206 $node['uid'] = $account->uid;
207
208 // Convert Node array in to an object
209 $node = (object)$node;
210
211 if($feed['autotaxonomy'] && is_array($item->get_categories())) {
212 // Find the vocabulary associated with this node type
213 $vocab = feedaggregator_get_vocabulary();
214
215 // Process any feed item category tags
216 if($vocab) {
217 foreach ($item->get_categories() as $category_name) {
218 if (trim($category_name) != '') {
219
220 // See if the term is already defined
221 if (!isset($cat_names[$category_name])) {
222 $cat_names[$category_name] = module_invoke('taxonomy', 'get_term_by_name', $category_name);
223 }
224
225 // If there is no existing term, create a new one
226 if (count($cat_names[$category_name]) == 0) {
227 $term = array();
228 $term['name'] = check_plain($category_name);
229 $term['description'] = '';
230 $term['vid'] = $vocab;
231 $term['weight'] = 0;
232 module_invoke('taxonomy', 'save_term', $term);
233
234 // Assign the new term to our new node
235 $node->taxonomy[] = $term['tid'];
236 $cat_names[$category_name][0]->tid = $term['tid'];
237 }
238 else {
239 // Use the existing category term in the database
240 if (!in_array($cat_names[$category_name][0]->tid, $node->taxonomy)) {
241 $node->taxonomy[] = $cat_names[$category_name][0]->tid;
242 }
243 }
244 }
245 }
246 }
247 }
248
249 node_object_prepare($node);
250 $node = node_submit($node);
251 node_save($node);
252 return array('item_save' => TRUE);
253 }
254 break;
255
256 case 'expire_items':
257 // Unpublish any old nodes
258 $result = db_query(db_rewrite_sql("UPDATE {node} n JOIN {aggregator_node} an ON n.nid = an.nid SET n.status = 0 WHERE an.fid = %d AND n.changed < %d"), $feed['fid'], time() - $feed['expires']);
259 break;
260
261 case 'remove':
262 $result = db_query("SELECT nid FROM {aggregator_node} WHERE fid = %d", $feed['fid']);
263 while($node = db_fetch_object($result)) {
264 node_delete($node->nid);
265 }
266
267 break;
268 }
269 }
270
271
272 function feedaggregator_node_form_alter($form_id, &$form) {
273 switch($form_id) {
274 case 'feedmanager_form_feed_edit':
275 // Show extra node options if the processor is 'nodes'.
276 if($form['advanced']['processor']['#value'] == 'feedaggregator_node') {
277 $feed = feedmanager_get_feed($form['fid']['#value']);
278 $form['advanced']['author'] = array('#type' => 'textfield', '#title' => t('Authored by'), '#maxlength' => 60, '#autocomplete_path' => 'user/autocomplete', '#default_value' => $feed['author'] ? $feed['author'] : '', '#weight' => -1, '#description' => t('Assigns created nodes to a user. Leave blank for %anonymous.', array('%anonymous' => theme('placeholder', variable_get('anonymous', 'Anonymous')))));
279
280 // AutoTaxonomy
281 if(feedaggregator_get_vocabulary()) {
282 $form['advanced']['autotaxonomy'] = array(
283 '#type' => 'checkbox',
284 '#title' => t('Use feed category tags'),
285 '#description' => t('Some feeds provide categories defined by the author. These can be added to your taxonomy, and the feed items tagged with the terms.'),
286 '#default_value' => $edit['autotaxonomy'] ? $edit['autotaxonomy'] : FALSE,
287 );
288 } else {
289 $form['advanced']['autotaxonomy'] = array(
290 '#value' => t('You can automatically use tags defined in this feed to categories your nodes as they are created. To do this you need to %url to the %type content-type.', array('%url' => l(t('create a vocabulary and assign it'), 'admin/taxonomy'), '%type' => theme('placeholder', 'aggregator-item')))
291 );
292 }
293
294 // Define what Filters to pass aggregator items through
295 $form['advanced']['format'] = filter_form($feed['format'], 0, array('advanced', 'format'));
296
297
298 $form['advanced']['format']['#collapsed'] = FALSE;
299 $form['advanced']['format']['#collapsible'] = FALSE;
300 }
301 break;
302 }
303 }
304
305
306 /*
307 * Lists all items in the feed
308 */
309 function feedaggregator_node_list_items($feed, $limit = NULL) {
310 $per_page = $limit ? $limit : variable_get('default_nodes_main', 10);
311 $result = pager_query(db_rewrite_sql('SELECT n.nid, n.sticky, n.created FROM {node} n JOIN {aggregator_node} an ON an.nid = n.nid WHERE an.fid = %d AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC'), $per_page, $feed['fid'], NULL, $feed['fid']);
312
313 if (db_num_rows($result)) {
314 drupal_add_link(array('rel' => 'alternate',
315 'type' => 'application/rss+xml',
316 'title' => t('RSS'),
317 'href' => url('rss.xml', NULL, NULL, TRUE)));
318 $output = '';
319 while ($node = db_fetch_object($result)) {
320 $output .= node_view(node_load($node->nid), 1);
321 }
322
323 $output .= theme('pager', NULL, $per_page, $feed['fid']);
324 }
325
326 return $output;
327 }

  ViewVC Help
Powered by ViewVC 1.1.2