aggregator.pages.inc 17.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
// $Id$

/**
 * @file
 * User page callbacks for the aggregator module.
 */

/**
 * Menu callback; displays the most recent items gathered from any feed.
11 12 13
 *
 * @return
 *   The items HTML.
14 15
 */
function aggregator_page_last() {
16
  drupal_add_feed(url('aggregator/rss'), variable_get('site_name', 'Drupal') . ' ' . t('aggregator'));
17

18
  $items = aggregator_feed_items_load('sum');
19 20

  return _aggregator_page_list($items, arg(1));
21 22 23 24
}

/**
 * Menu callback; displays all the items captured from a particular feed.
25 26 27 28 29 30 31 32
 *
 * If there are two arguments then this function is the categorize form.
 *
 * @param $arg1
 *   If there are two arguments then $arg1 is $form_state. Otherwise, $arg1 is $feed.
 * @param $arg2
 *   If there are two arguments then $arg2 is feed.
 * @return
33
 *   The item's HTML.
34 35 36 37 38 39
 */
function aggregator_page_source($arg1, $arg2 = NULL) {
  // If there are two arguments then this function is the categorize form, and
  // $arg1 is $form_state and $arg2 is $feed. Otherwise, $arg1 is $feed.
  $feed = is_array($arg2) ? $arg2 : $arg1;
  $feed = (object)$feed;
40
  drupal_set_title($feed->title);
41
  $feed_source = theme('aggregator_feed_source', $feed);
42

43
  // It is safe to include the fid in the query because it's loaded from the
44
  // database by aggregator_feed_load.
45
  $items = aggregator_feed_items_load('source', $feed);
46

47
  return _aggregator_page_list($items, arg(3), $feed_source);
48 49 50 51
}

/**
 * Menu callback; displays all the items aggregated in a particular category.
52 53 54 55 56 57 58 59 60
 *
 * If there are two arguments then this function is called as a form.
 *
 * @param $arg1
 *   If there are two arguments then $arg1 is $form_state. Otherwise, $arg1 is $category.
 * @param $arg2
 *   If there are two arguments then $arg2 is $category.
 * @return
 *   The items HTML.
61 62 63 64 65 66
 */
function aggregator_page_category($arg1, $arg2 = NULL) {
  // If there are two arguments then we are called as a form, $arg1 is
  // $form_state and $arg2 is $category. Otherwise, $arg1 is $category.
  $category = is_array($arg2) ? $arg2 : $arg1;

67
  drupal_add_feed(url('aggregator/rss/' . $category['cid']), variable_get('site_name', 'Drupal') . ' ' . t('aggregator - @title', array('@title' => $category['title'])));
68 69

  // It is safe to include the cid in the query because it's loaded from the
70
  // database by aggregator_category_load.
71
  $items = aggregator_feed_items_load('category', $category);
72

73 74 75 76
  return _aggregator_page_list($items, arg(3));
}

/**
77
 * Load feed items
78
 *
79 80 81 82
 * @param $type
 *   The filter for the items. Possible values: 'sum', 'source', 'category'
 * @param $data
 *   Feed or category data for filtering
83 84
 * @return
 *   An array of the feed items.
85
 */
86
function aggregator_feed_items_load($type, $data = NULL) {
87
  $items = array();
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  $range_limit = 20;
  switch ($type) {
    case 'sum':
      $result = db_query_range('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', 0, $range_limit);
      break;
    case 'source':
      $result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC', array(':fid' => $data->fid), 0, $range_limit);
      break;
    case 'category':
      $result = db_query_range('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 = :cid ORDER BY timestamp DESC, i.iid DESC', array(':cid' => $data['cid']), 0, $range_limit);
      break;
  }

  foreach ($result as $item) {
    $item->categories = 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 = :iid ORDER BY c.title', array(':iid' => $item->iid))->fetchAll();
    $items[] = $item;
104
  }
105

106
  return $items;
107 108 109
}

/**
110 111 112 113 114 115 116 117 118 119 120 121
 * Prints an aggregator page listing a number of feed items.
 *
 * Various menu callbacks use this function to print their feeds.
 *
 * @param $items
 *   The items to be listed.
 * @param $op
 *   Which form should be added to the items. Only 'categorize' is now recognized.
 * @param $feed_source
 *   The feed source URL.
 * @return
 *   The items HTML.
122
 */
123 124 125 126
function _aggregator_page_list($items, $op, $feed_source = '') {
  if (user_access('administer news feeds') && ($op == 'categorize')) {
    // Get form data.
    $output = aggregator_categorize_items($items, $feed_source);
127 128
  }
  else {
129
    // Assemble themed output.
130
    $output = $feed_source;
131 132
    foreach ($items as $item) {
      $output .= theme('aggregator_item', $item);
133
    }
134
    $output = theme('aggregator_wrapper', $output);
135
  }
136

137
  return $output;
138 139 140 141 142
}

/**
 * Form builder; build the page list form.
 *
143 144 145 146 147 148
 * @param $items
 *   An array of the feed items.
 * @param $feed_source
 *   The feed source URL.
 * @return
 *   The form structure.
149
 * @ingroup forms
150 151
 * @see aggregator_categorize_items_validate()
 * @see aggregator_categorize_items_submit()
152
 */
153 154 155 156
function aggregator_categorize_items($items, $feed_source = '') {
  $form['#submit'][] = 'aggregator_categorize_items_submit';
  $form['#validate'][] = 'aggregator_categorize_items_validate';
  $form['#theme'] = 'aggregator_categorize_items';
157 158 159
  $form['feed_source'] = array(
    '#value' => $feed_source,
  );
160 161 162
  $categories = array();
  $done = FALSE;
  $form['items'] = array();
163 164 165
  $form['categories'] = array(
    '#tree' => TRUE,
  );
166
  foreach ($items as $item) {
167
    $form['items'][$item->iid] = array('#markup' => theme('aggregator_item', $item));
168
    $form['categories'][$item->iid] = array();
169
    $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 = :iid', array(':iid' => $item->iid));
170
    $selected = array();
171
    foreach ($categories_result as $category) {
172 173 174 175 176
      if (!$done) {
        $categories[$category->cid] = check_plain($category->title);
      }
      if ($category->iid) {
        $selected[] = $category->cid;
177 178
      }
    }
179 180 181 182 183 184 185 186
    $done = TRUE;
    $form['categories'][$item->iid] = array(
      '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
      '#default_value' => $selected,
      '#options' => $categories,
      '#size' => 10,
      '#multiple' => TRUE
    );
187 188 189 190 191 192
  }
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));

  return $form;
}

193
/**
194
 * Validate aggregator_categorize_items() form submissions.
195
 */
196
function aggregator_categorize_items_validate($form, &$form_state) {
197 198 199 200 201
  if (!user_access('administer news feeds')) {
    form_error($form, t('You are not allowed to categorize this feed item.'));
  }
}

202
/**
203
 * Process aggregator_categorize_items() form submissions.
204
 */
205
function aggregator_categorize_items_submit($form, &$form_state) {
206 207
  if (!empty($form_state['values']['categories'])) {
    foreach ($form_state['values']['categories'] as $iid => $selection) {
208 209 210 211 212
      db_delete('aggregator_category_item')
        ->condition('iid', $iid)
        ->execute();
      $insert = db_insert('aggregator_category_item')->fields(array('iid', 'cid'));
      $has_values = FALSE;
213
      foreach ($selection as $cid) {
214 215 216 217 218 219
        if ($cid && $iid) {
          $has_values = TRUE;
          $insert->values(array(
            'iid' => $iid,
            'cid' => $cid,
          ));
220
        }
221
      }
222 223 224
      if ($has_values) {
        $insert->execute();
      }
225 226 227 228 229 230
    }
  }
  drupal_set_message(t('The categories have been saved.'));
}

/**
231
 * Theme the page list form for assigning categories.
232
 *
233 234 235 236
 * @param $form
 *   An associative array containing the structure of the form.
 * @return
 *   The output HTML.
237 238
 * @ingroup themeable
 */
239 240
function theme_aggregator_categorize_items($form) {
  $output = drupal_render($form['feed_source']);
241 242 243 244
  $rows = array();
  if ($form['items']) {
    foreach (element_children($form['items']) as $key) {
      if (is_array($form['items'][$key])) {
245 246 247 248
        $rows[] = array(
          drupal_render($form['items'][$key]),
          array('data' => drupal_render($form['categories'][$key]), 'class' => 'categorize-item'),
        );
249 250 251 252 253 254
      }
    }
  }
  $output .= theme('table', array('', t('Categorize')), $rows);
  $output .= drupal_render($form['submit']);
  $output .= drupal_render($form);
255

256
  return theme('aggregator_wrapper', $output);
257 258
}

259 260 261 262 263 264 265 266
/**
 * Process variables for aggregator-wrapper.tpl.php.
 *
 * @see aggregator-wrapper.tpl.php
 */
function template_preprocess_aggregator_wrapper(&$variables) {
  $variables['pager'] = theme('pager', NULL, 20, 0);
}
267 268

/**
269
 * Process variables for aggregator-item.tpl.php.
270
 *
271
 * @see aggregator-item.tpl.php
272
 */
273 274 275 276 277 278
function template_preprocess_aggregator_item(&$variables) {
  $item = $variables['item'];

  $variables['feed_url'] = check_url($item->link);
  $variables['feed_title'] = check_plain($item->title);
  $variables['content'] = aggregator_filter_xss($item->description);
279

280 281
  $variables['source_url'] = '';
  $variables['source_title'] = '';
282
  if (isset($item->ftitle) && isset($item->fid)) {
283 284
    $variables['source_url'] = url("aggregator/sources/$item->fid");
    $variables['source_title'] = check_plain($item->ftitle);
285 286
  }
  if (date('Ymd', $item->timestamp) == date('Ymd')) {
287
    $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->timestamp)));
288 289
  }
  else {
290
    $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
291 292
  }

293 294
  $variables['categories'] = array();
  foreach ($item->categories as $category) {
295
    $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/' . $category->cid);
296 297 298 299 300 301 302 303 304
  }
}

/**
 * Menu callback; displays all the feeds used by the aggregator.
 */
function aggregator_page_sources() {
  $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');

305
  $output = '';
306
  foreach ($result as $feed) {
307
    // Most recent items:
308
    $summary_items = array();
309
    if (variable_get('aggregator_summary_items', 3)) {
310 311
      $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM {aggregator_item} i WHERE i.fid = :fid ORDER BY i.timestamp DESC', array(':fid' => $feed->fid), 0, variable_get('aggregator_summary_items', 3));
      foreach ($items as $item) {
312
        $summary_items[] = theme('aggregator_summary_item', $item);
313 314
      }
    }
315
    $feed->url = url('aggregator/sources/' . $feed->fid);
316
    $output .= theme('aggregator_summary_items', $summary_items, $feed);
317 318
  }
  $output .= theme('feed_icon', url('aggregator/opml'), t('OPML feed'));
319 320

  return theme('aggregator_wrapper', $output);
321 322 323 324 325 326 327 328
}

/**
 * Menu callback; displays all the categories used by the aggregator.
 */
function aggregator_page_categories() {
  $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');

329
  $output = '';
330
  foreach ($result as $category) {
331
    if (variable_get('aggregator_summary_items', 3)) {
332
      $summary_items = array();
333 334
      $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 = :cid ORDER BY i.timestamp DESC', array(':cid' => $category->cid), 0, variable_get('aggregator_summary_items', 3));
      foreach ($items as $item) {
335
        $summary_items[] = theme('aggregator_summary_item', $item);
336 337
      }
    }
338
    $category->url = url('aggregator/categories/' . $category->cid);
339
    $output .= theme('aggregator_summary_items', $summary_items, $category);
340 341
  }

342
  return theme('aggregator_wrapper', $output);
343 344 345 346 347 348 349
}

/**
 * Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
 */
function aggregator_page_rss() {
  $result = NULL;
350
  // arg(2) is the passed cid, only select for that category.
351
  if (arg(2)) {
352 353
    $category = db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = :cid', array(':cid' => arg(2)))->fetchObject();
    $result = db_query_range('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 = :cid ORDER BY timestamp DESC, i.iid DESC', array(':cid' => $category->cid), 0, variable_get('feed_default_items', 10));
354
  }
355
  // Or, get the default aggregator items.
356
  else {
357
    $category = NULL;
358
    $result = db_query_range('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', 0, variable_get('feed_default_items', 10));
359
  }
360

361
  $feeds = $result->fetchAll();
362 363 364 365
  return theme('aggregator_page_rss', $feeds, $category);
}

/**
366
 * Theme the RSS output.
367
 *
368 369 370 371
 * @param $feeds
 *   An array of the feeds to theme.
 * @param $category
 *   A common category, if any, for all the feeds.
372 373 374 375 376 377 378 379 380
 * @ingroup themeable
 */
function theme_aggregator_page_rss($feeds, $category = NULL) {
  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');

  $items = '';
  $feed_length = variable_get('feed_item_length', 'teaser');
  foreach ($feeds as $feed) {
    switch ($feed_length) {
381
      case 'teaser':
382 383
        $teaser = node_teaser($feed->description);
        if ($teaser != $feed->description) {
384
          $teaser .= '<p><a href="' . check_url($feed->link) . '">' . t('read more') . "</a></p>\n";
385
        }
386
        $feed->description = $teaser;
387 388
        break;
      case 'title':
389
        $feed->description = '';
390 391
        break;
    }
392
    $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
393 394
  }

395
  $site_name = variable_get('site_name', 'Drupal');
396
  $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE));
397
  $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));
398

399
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
400
  $output .= "<rss version=\"2.0\">\n";
401
  $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
402 403 404 405 406 407 408
  $output .= "</rss>\n";

  print $output;
}

/**
 * Menu callback; generates an OPML representation of all feeds.
409 410 411 412 413
 *
 * @param $cid
 *   If set, feeds are exported only from a category with this ID. Otherwise, all feeds are exported.
 * @return
 *   The output XML.
414 415 416
 */
function aggregator_page_opml($cid = NULL) {
  if ($cid) {
417
    $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 = :cid ORDER BY title', array(':cid' => $cid));
418 419 420 421
  }
  else {
    $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
  }
422

423
  $feeds = $result->fetchAll();
424 425 426 427
  return theme('aggregator_page_opml', $feeds);
}

/**
428
 * Theme the OPML feed output.
429
 *
430 431
 * @param $feeds
 *   An array of the feeds to theme.
432 433 434 435
 * @ingroup themeable
 */
function theme_aggregator_page_opml($feeds) {
  drupal_set_header('Content-Type: text/xml; charset=utf-8');
436

437
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
438 439
  $output .= "<opml version=\"1.1\">\n";
  $output .= "<head>\n";
440 441
  $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) . "</title>\n";
  $output .= '<dateModified>' . gmdate('r') . "</dateModified>\n";
442 443
  $output .= "</head>\n";
  $output .= "<body>\n";
444
  foreach ($feeds as $feed) {
445
    $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="' . check_url($feed->url) . "\" />\n";
446 447 448 449 450 451 452 453
  }
  $output .= "</body>\n";
  $output .= "</opml>\n";

  print $output;
}

/**
454
 * Process variables for aggregator-summary-items.tpl.php.
455
 *
456 457 458 459 460 461 462 463 464 465 466 467
 * @see aggregator-summary-item.tpl.php
 */
function template_preprocess_aggregator_summary_items(&$variables) {
  $variables['title'] = check_plain($variables['source']->title);
  $variables['summary_list'] = theme('item_list', $variables['summary_items']);
  $variables['source_url'] = $variables['source']->url;
}

/**
 * Process variables for aggregator-summary-item.tpl.php.
 *
 * @see aggregator-summary-item.tpl.php
468
 */
469 470 471 472 473
function template_preprocess_aggregator_summary_item(&$variables) {
  $item = $variables['item'];

  $variables['feed_url'] = check_url($item->link);
  $variables['feed_title'] = check_plain($item->title);
474
  $variables['feed_age'] = t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->timestamp)));
475 476 477

  $variables['source_url'] = '';
  $variables['source_title'] = '';
478
  if (!empty($item->feed_link)) {
479 480
    $variables['source_url'] = check_url($item->feed_link);
    $variables['source_title'] = check_plain($item->feed_title);
481 482 483 484
  }
}

/**
485
 * Process variables for aggregator-feed-source.tpl.php.
486
 *
487
 * @see aggregator-feed-source.tpl.php
488
 */
489 490
function template_preprocess_aggregator_feed_source(&$variables) {
  $feed = $variables['feed'];
491

492 493 494 495
  $variables['source_icon'] = theme('feed_icon', $feed->url, t('!title feed', array('!title' => $feed->title)));
  $variables['source_image'] = $feed->image;
  $variables['source_description'] = aggregator_filter_xss($feed->description);
  $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
496 497

  if ($feed->checked) {
498
    $variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked)));
499 500
  }
  else {
501
    $variables['last_checked'] = t('never');
502 503 504
  }

  if (user_access('administer news feeds')) {
505
    $variables['last_checked'] = l($variables['last_checked'], 'admin/content/aggregator');
506 507
  }
}