source: sipes/cord/modules/aggregator/aggregator.pages.inc @ b354002

stableversion-3.0
Last change on this file since b354002 was b354002, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio del cord

  • Propiedad mode establecida a 100755
File size: 17.2 KB
Línea 
1<?php
2
3/**
4 * @file
5 * User page callbacks for the aggregator module.
6 */
7
8/**
9 * Menu callback; displays the most recent items gathered from any feed.
10 *
11 * @return
12 *   The items HTML.
13 */
14function aggregator_page_last() {
15  drupal_add_feed(url('aggregator/rss'), variable_get('site_name', 'Drupal') .' '. t('aggregator'));
16
17  $items = aggregator_feed_items_load('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_item} i INNER JOIN {aggregator_feed} f ON i.fid = f.fid ORDER BY i.timestamp DESC, i.iid DESC');
18
19  return _aggregator_page_list($items, arg(1));
20}
21
22/**
23 * Menu callback; displays all the items captured from a particular feed.
24 *
25 * If there are two arguments then this function is the categorize form.
26 *
27 * @param $arg1
28 *   If there are two arguments then $arg1 is $form_state. Otherwise, $arg1 is $feed.
29 * @param $arg2
30 *   If there are two arguments then $arg2 is feed.
31 * @return
32 *   The items HTML.
33 */
34function aggregator_page_source($arg1, $arg2 = NULL) {
35  // If there are two arguments then this function is the categorize form, and
36  // $arg1 is $form_state and $arg2 is $feed. Otherwise, $arg1 is $feed.
37  $feed = is_array($arg2) ? $arg2 : $arg1;
38  $feed = (object)$feed;
39  drupal_set_title(check_plain($feed->title));
40  $feed_source = theme('aggregator_feed_source', $feed);
41
42  // It is safe to include the fid in the query because it's loaded from the
43  // database by aggregator_feed_load.
44  $items = aggregator_feed_items_load('SELECT * FROM {aggregator_item} WHERE fid = '. $feed->fid .' ORDER BY timestamp DESC, iid DESC');
45
46  return _aggregator_page_list($items, arg(3), $feed_source);
47}
48
49/**
50 * Menu callback; displays all the items aggregated in a particular category.
51 *
52 * If there are two arguments then this function is called as a form.
53 *
54 * @param $arg1
55 *   If there are two arguments then $arg1 is $form_state. Otherwise, $arg1 is $category.
56 * @param $arg2
57 *   If there are two arguments then $arg2 is $category.
58 * @return
59 *   The items HTML.
60 */
61function aggregator_page_category($arg1, $arg2 = NULL) {
62  // If there are two arguments then we are called as a form, $arg1 is
63  // $form_state and $arg2 is $category. Otherwise, $arg1 is $category.
64  $category = is_array($arg2) ? $arg2 : $arg1;
65
66  drupal_add_feed(url('aggregator/rss/'. $category['cid']), variable_get('site_name', 'Drupal') .' '. t('aggregator - @title', array('@title' => $category['title'])));
67
68  // It is safe to include the cid in the query because it's loaded from the
69  // database by aggregator_category_load.
70  $items = aggregator_feed_items_load('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_category_item} c LEFT JOIN {aggregator_item} i ON c.iid = i.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE cid = '. $category['cid'] .' ORDER BY timestamp DESC, i.iid DESC');
71
72  return _aggregator_page_list($items, arg(3));
73}
74
75/**
76 * Load feed items by passing a SQL query.
77 *
78 * @param $sql
79 *   The query to be executed.
80 * @return
81 *   An array of the feed items.
82 */
83function aggregator_feed_items_load($sql) {
84  $items = array();
85  if (isset($sql)) {
86    $result = pager_query($sql, 20);
87    while ($item = db_fetch_object($result)) {
88      $result_category = 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);
89      $item->categories = array();
90      while ($item_categories = db_fetch_object($result_category)) {
91        $item->categories[] = $item_categories;
92      }
93      $items[$item->iid] = $item;
94    }
95  }
96  return $items;
97}
98
99/**
100 * Prints an aggregator page listing a number of feed items.
101 *
102 * Various menu callbacks use this function to print their feeds.
103 *
104 * @param $items
105 *   The items to be listed.
106 * @param $op
107 *   Which form should be added to the items. Only 'categorize' is now recognized.
108 * @param $feed_source
109 *   The feed source URL.
110 * @return
111 *   The items HTML.
112 */
113function _aggregator_page_list($items, $op, $feed_source = '') {
114  if (user_access('administer news feeds') && ($op == 'categorize')) {
115    // Get form data.
116    $output = aggregator_categorize_items($items, $feed_source);
117  }
118  else {
119    // Assemble themed output.
120    $output = $feed_source;
121    foreach ($items as $item) {
122      $output .= theme('aggregator_item', $item);
123    }
124    $output = theme('aggregator_wrapper', $output);
125  }
126  return $output;
127}
128
129/**
130 * Form builder; build the page list form.
131 *
132 * @param $items
133 *   An array of the feed items.
134 * @param $feed_source
135 *   The feed source URL.
136 * @return
137 *   The form structure.
138 * @ingroup forms
139 * @see aggregator_categorize_items_validate()
140 * @see aggregator_categorize_items_submit()
141 */
142function aggregator_categorize_items($items, $feed_source = '') {
143  $form['#submit'][] = 'aggregator_categorize_items_submit';
144  $form['#validate'][] = 'aggregator_categorize_items_validate';
145  $form['#theme'] = 'aggregator_categorize_items';
146  $form['feed_source'] = array('#value' => $feed_source);
147  $categories = array();
148  $done = FALSE;
149  $form['items'] = array();
150  $form['categories'] = array('#tree' => TRUE);
151  foreach ($items as $item) {
152    $form['items'][$item->iid] = array('#value' => theme('aggregator_item', $item));
153    $form['categories'][$item->iid] = array();
154    $categories_result = db_query('SELECT c.cid, c.title, ci.iid FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid AND ci.iid = %d', $item->iid);
155    $selected = array();
156    while ($category = db_fetch_object($categories_result)) {
157      if (!$done) {
158        $categories[$category->cid] = check_plain($category->title);
159      }
160      if ($category->iid) {
161        $selected[] = $category->cid;
162      }
163    }
164    $done = TRUE;
165    $form['categories'][$item->iid] = array(
166      '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
167      '#default_value' => $selected,
168      '#options' => $categories,
169      '#size' => 10,
170      '#multiple' => TRUE
171    );
172  }
173  $form['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
174
175  return $form;
176}
177
178/**
179 * Validate aggregator_categorize_items form submissions.
180 */
181function aggregator_categorize_items_validate($form, &$form_state) {
182  if (!user_access('administer news feeds')) {
183    form_error($form, t('You are not allowed to categorize this feed item.'));
184  }
185}
186
187/**
188 * Process aggregator_categorize_items form submissions.
189 */
190function aggregator_categorize_items_submit($form, &$form_state) {
191  if (!empty($form_state['values']['categories'])) {
192    foreach ($form_state['values']['categories'] as $iid => $selection) {
193      db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d', $iid);
194      foreach ($selection as $cid) {
195        if ($cid) {
196          db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES (%d, %d)', $cid, $iid);
197        }
198      }
199    }
200  }
201  drupal_set_message(t('The categories have been saved.'));
202}
203
204/**
205 * Theme the page list form for assigning categories.
206 *
207 * @param $form
208 *   An associative array containing the structure of the form.
209 * @return
210 *   The output HTML.
211 * @ingroup themeable
212 */
213function theme_aggregator_categorize_items($form) {
214  $output = drupal_render($form['feed_source']);
215  $rows = array();
216  if ($form['items']) {
217    foreach (element_children($form['items']) as $key) {
218      if (is_array($form['items'][$key])) {
219        $rows[] = array(
220          drupal_render($form['items'][$key]),
221          array('data' => drupal_render($form['categories'][$key]), 'class' => 'categorize-item'),
222        );
223      }
224    }
225  }
226  $output .= theme('table', array('', t('Categorize')), $rows);
227  $output .= drupal_render($form['submit']);
228  $output .= drupal_render($form);
229  return theme('aggregator_wrapper', $output);
230}
231
232/**
233 * Process variables for aggregator-wrapper.tpl.php.
234 *
235 * @see aggregator-wrapper.tpl.php
236 */
237function template_preprocess_aggregator_wrapper(&$variables) {
238  $variables['pager'] = theme('pager', NULL, 20, 0);
239}
240
241/**
242 * Process variables for aggregator-item.tpl.php.
243 *
244 * @see aggregator-item.tpl.php
245 */
246function template_preprocess_aggregator_item(&$variables) {
247  $item = $variables['item'];
248
249  $variables['feed_url'] = check_url($item->link);
250  $variables['feed_title'] = check_plain($item->title);
251  $variables['content'] = aggregator_filter_xss($item->description);
252
253  $variables['source_url'] = '';
254  $variables['source_title'] = '';
255  if (isset($item->ftitle) && isset($item->fid)) {
256    $variables['source_url'] = url("aggregator/sources/$item->fid");
257    $variables['source_title'] = check_plain($item->ftitle);
258  }
259  if (date('Ymd', $item->timestamp) == date('Ymd')) {
260    $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(time() - $item->timestamp)));
261  }
262  else {
263    $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
264  }
265
266  $variables['categories'] = array();
267  foreach ($item->categories as $category) {
268    $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/'. $category->cid);
269  }
270}
271
272/**
273 * Menu callback; displays all the feeds used by the aggregator.
274 */
275function aggregator_page_sources() {
276  $result = db_query('SELECT f.fid, f.title, f.description, f.image, MAX(i.timestamp) AS last FROM {aggregator_feed} f LEFT JOIN {aggregator_item} i ON f.fid = i.fid GROUP BY f.fid, f.title, f.description, f.image ORDER BY last DESC, f.title');
277
278  $output = '';
279  while ($feed = db_fetch_object($result)) {
280    // Most recent items:
281    $summary_items = array();
282    if (variable_get('aggregator_summary_items', 3)) {
283      $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM {aggregator_item} i WHERE i.fid = %d ORDER BY i.timestamp DESC', $feed->fid, 0, variable_get('aggregator_summary_items', 3));
284      while ($item = db_fetch_object($items)) {
285        $summary_items[] = theme('aggregator_summary_item', $item);
286      }
287    }
288    $feed->url = url('aggregator/sources/'. $feed->fid);
289    $output .= theme('aggregator_summary_items', $summary_items, $feed);
290  }
291  $output .= theme('feed_icon', url('aggregator/opml'), t('OPML feed'));
292
293  return theme('aggregator_wrapper', $output);
294}
295
296/**
297 * Menu callback; displays all the categories used by the aggregator.
298 */
299function aggregator_page_categories() {
300  $result = db_query('SELECT c.cid, c.title, c.description FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid LEFT JOIN {aggregator_item} i ON ci.iid = i.iid GROUP BY c.cid, c.title, c.description');
301
302  $output = '';
303  while ($category = db_fetch_object($result)) {
304    if (variable_get('aggregator_summary_items', 3)) {
305      $summary_items = array();
306      $items = db_query_range('SELECT i.title, i.timestamp, i.link, f.title as feed_title, f.link as feed_link FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON i.iid = ci.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE ci.cid = %d ORDER BY i.timestamp DESC', $category->cid, 0, variable_get('aggregator_summary_items', 3));
307      while ($item = db_fetch_object($items)) {
308        $summary_items[] = theme('aggregator_summary_item', $item);
309      }
310    }
311    $category->url = url('aggregator/categories/'. $category->cid);
312    $output .= theme('aggregator_summary_items', $summary_items, $category);
313  }
314
315  return theme('aggregator_wrapper', $output);
316}
317
318/**
319 * Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
320 */
321function aggregator_page_rss() {
322  $result = NULL;
323  // arg(2) is the passed cid, only select for that category
324  if (arg(2)) {
325    $category = db_fetch_object(db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = %d', arg(2)));
326    $sql = 'SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_category_item} c LEFT JOIN {aggregator_item} i ON c.iid = i.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE cid = %d ORDER BY timestamp DESC, i.iid DESC';
327    $result = db_query_range($sql, $category->cid, 0, variable_get('feed_default_items', 10));
328  }
329  // or, get the default aggregator items
330  else {
331    $category = NULL;
332    $sql = 'SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_item} i INNER JOIN {aggregator_feed} f ON i.fid = f.fid ORDER BY i.timestamp DESC, i.iid DESC';
333    $result = db_query_range($sql, 0, variable_get('feed_default_items', 10));
334  }
335
336  $feeds = array();
337  while ($item = db_fetch_object($result)) {
338    $feeds[] = $item;
339  }
340  return theme('aggregator_page_rss', $feeds, $category);
341}
342
343/**
344 * Theme the RSS output.
345 *
346 * @param $feeds
347 *   An array of the feeds to theme.
348 * @param $category
349 *   A common category, if any, for all the feeds.
350 * @ingroup themeable
351 */
352function theme_aggregator_page_rss($feeds, $category = NULL) {
353  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
354
355  $items = '';
356  $feed_length = variable_get('feed_item_length', 'teaser');
357  foreach ($feeds as $feed) {
358    switch ($feed_length) {
359      case 'teaser':
360        $teaser = node_teaser($feed->description);
361        if ($teaser != $feed->description) {
362          $teaser .= '<p><a href="'. check_url($feed->link) .'">'. t('read more') ."</a></p>\n";
363        }
364        $feed->description = $teaser;
365        break;
366      case 'title':
367        $feed->description = '';
368        break;
369    }
370    $items .= format_rss_item($feed->ftitle .': '. $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
371  }
372
373  $site_name = variable_get('site_name', 'Drupal');
374  $url = url((isset($category) ? 'aggregator/categories/'. $category->cid : 'aggregator'), array('absolute' => TRUE));
375  $description = isset($category) ? t('@site_name - aggregated feeds in category @title', array('@site_name' => $site_name, '@title' => $category->title)) : t('@site_name - aggregated feeds', array('@site_name' => $site_name));
376
377  $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
378  $output .= "<rss version=\"2.0\">\n";
379  $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
380  $output .= "</rss>\n";
381
382  print $output;
383}
384
385/**
386 * Menu callback; generates an OPML representation of all feeds.
387 *
388 * @param $cid
389 *   If set, feeds are exported only from a category with this ID. Otherwise, all feeds are exported.
390 * @return
391 *   The output XML.
392 */
393function aggregator_page_opml($cid = NULL) {
394  if ($cid) {
395    $result = db_query('SELECT f.title, f.url FROM {aggregator_feed} f LEFT JOIN {aggregator_category_feed} c on f.fid = c.fid WHERE c.cid = %d ORDER BY title', $cid);
396  }
397  else {
398    $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
399  }
400
401  $feeds = array();
402  while ($item = db_fetch_object($result)) {
403    $feeds[] = $item;
404  }
405
406  return theme('aggregator_page_opml', $feeds);
407}
408
409/**
410 * Theme the OPML feed output.
411 *
412 * @param $feeds
413 *   An array of the feeds to theme.
414 * @ingroup themeable
415 */
416function theme_aggregator_page_opml($feeds) {
417
418  drupal_set_header('Content-Type: text/xml; charset=utf-8');
419
420  $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
421  $output .= "<opml version=\"1.1\">\n";
422  $output .= "<head>\n";
423  $output .= '<title>'. check_plain(variable_get('site_name', 'Drupal')) ."</title>\n";
424  $output .= '<dateModified>'. gmdate('r') ."</dateModified>\n";
425  $output .= "</head>\n";
426  $output .= "<body>\n";
427  foreach ($feeds as $feed) {
428    $output .= '<outline text="'. check_plain($feed->title) .'" xmlUrl="'. check_url($feed->url) ."\" />\n";
429  }
430  $output .= "</body>\n";
431  $output .= "</opml>\n";
432
433  print $output;
434}
435
436/**
437 * Process variables for aggregator-summary-items.tpl.php.
438 *
439 * @see aggregator-summary-item.tpl.php
440 */
441function template_preprocess_aggregator_summary_items(&$variables) {
442  $variables['title'] = check_plain($variables['source']->title);
443  $variables['summary_list'] = theme('item_list', $variables['summary_items']);
444  $variables['source_url'] = $variables['source']->url;
445}
446
447/**
448 * Process variables for aggregator-summary-item.tpl.php.
449 *
450 * @see aggregator-summary-item.tpl.php
451 */
452function template_preprocess_aggregator_summary_item(&$variables) {
453  $item = $variables['item'];
454
455  $variables['feed_url'] = check_url($item->link);
456  $variables['feed_title'] = check_plain($item->title);
457  $variables['feed_age'] = t('%age old', array('%age' => format_interval(time() - $item->timestamp)));
458
459  $variables['source_url'] = '';
460  $variables['source_title'] = '';
461  if (!empty($item->feed_link)) {
462    $variables['source_url'] = check_url($item->feed_link);
463    $variables['source_title'] = check_plain($item->feed_title);
464  }
465}
466
467/**
468 * Process variables for aggregator-feed-source.tpl.php.
469 *
470 * @see aggregator-feed-source.tpl.php
471 */
472function template_preprocess_aggregator_feed_source(&$variables) {
473  $feed = $variables['feed'];
474
475  $variables['source_icon'] = theme('feed_icon', $feed->url, t('!title feed', array('!title' => $feed->title)));
476  $variables['source_image'] = $feed->image;
477  $variables['source_description'] = aggregator_filter_xss($feed->description);
478  $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
479
480  if ($feed->checked) {
481    $variables['last_checked'] = t('@time ago', array('@time' => format_interval(time() - $feed->checked)));
482  }
483  else {
484    $variables['last_checked'] = t('never');
485  }
486
487  if (user_access('administer news feeds')) {
488    $variables['last_checked'] = l($variables['last_checked'], 'admin/content/aggregator');
489  }
490}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.