aggregator.pages.inc 18.1 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
 * @param $feed
 *   The feed for which to display all items.
28 29
 *
 * @return
30
 *   The rendered list of items for a feed.
31
 */
32
function aggregator_page_source($feed) {
33
  drupal_set_title($feed->title);
34
  $feed_source = theme('aggregator_feed_source', array('feed' => $feed));
35

36
  // It is safe to include the fid in the query because it's loaded from the
37
  // database by aggregator_feed_load.
38
  $items = aggregator_feed_items_load('source', $feed);
39

40
  return _aggregator_page_list($items, arg(3), $feed_source);
41 42 43
}

/**
44
 * Menu callback; displays a form with all items captured from a feed.
45
 *
46 47
 * @param $feed
 *   The feed for which to list all the aggregated items.
48 49
 *
 * @return
50 51 52
 *   The rendered list of items for a feed.
 *
 * @see aggregator_page_source()
53
 */
54 55 56
function aggregator_page_source_form($form, $form_state, $feed) {
  return aggregator_page_source($feed);
}
57

58 59 60 61 62 63 64 65 66 67
/**
 * Menu callback; displays all the items aggregated in a particular category.
 *
 * @param $category
 *   The category for which to list all the aggregated items.
 *
 * @return
*   The rendered list of items for a category.
 */
function aggregator_page_category($category) {
68
  drupal_add_feed(url('aggregator/rss/' . $category['cid']), variable_get('site_name', 'Drupal') . ' ' . t('aggregator - @title', array('@title' => $category['title'])));
69 70

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

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

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/**
 * Menu callback; displays a form containing items aggregated in a category.
 *
 * @param $category
 *   The category for which to list all the aggregated items.
 *
 * @return
*   The rendered list of items for a category.
 *
 * @see aggregator_page_category()
 */
function aggregator_page_category_form($form, $form_state, $category) {
  return aggregator_page_category($category);
}

92
/**
93
 * Load feed items
94
 *
95 96 97 98
 * @param $type
 *   The filter for the items. Possible values: 'sum', 'source', 'category'
 * @param $data
 *   Feed or category data for filtering
99 100
 * @return
 *   An array of the feed items.
101
 */
102
function aggregator_feed_items_load($type, $data = NULL) {
103
  $items = array();
104 105 106 107 108 109
  $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':
110
      $result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC', 0, $range_limit, array(':fid' => $data->fid));
111 112
      break;
    case 'category':
113
      $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', 0, $range_limit, array(':cid' => $data['cid']));
114 115 116 117 118 119
      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;
120
  }
121

122
  return $items;
123 124 125
}

/**
126 127 128 129 130 131 132 133 134 135 136 137
 * 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.
138
 */
139 140 141 142
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);
143 144
  }
  else {
145
    // Assemble themed output.
146
    $output = $feed_source;
147
    foreach ($items as $item) {
148
      $output .= theme('aggregator_item', array('item' => $item));
149
    }
150
    $output = theme('aggregator_wrapper', array('content' => $output));
151
  }
152

153
  return $output;
154 155 156 157 158
}

/**
 * Form builder; build the page list form.
 *
159 160 161 162 163 164
 * @param $items
 *   An array of the feed items.
 * @param $feed_source
 *   The feed source URL.
 * @return
 *   The form structure.
165
 * @ingroup forms
166
 * @see aggregator_categorize_items_submit()
167
 */
168 169 170
function aggregator_categorize_items($items, $feed_source = '') {
  $form['#submit'][] = 'aggregator_categorize_items_submit';
  $form['#theme'] = 'aggregator_categorize_items';
171 172 173
  $form['feed_source'] = array(
    '#value' => $feed_source,
  );
174 175 176
  $categories = array();
  $done = FALSE;
  $form['items'] = array();
177 178 179
  $form['categories'] = array(
    '#tree' => TRUE,
  );
180
  foreach ($items as $item) {
181
    $form['items'][$item->iid] = array('#markup' => theme('aggregator_item', array('item' => $item)));
182
    $form['categories'][$item->iid] = array();
183
    $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));
184
    $selected = array();
185
    foreach ($categories_result as $category) {
186 187 188 189 190
      if (!$done) {
        $categories[$category->cid] = check_plain($category->title);
      }
      if ($category->iid) {
        $selected[] = $category->cid;
191 192
      }
    }
193 194 195 196 197 198 199 200
    $done = TRUE;
    $form['categories'][$item->iid] = array(
      '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
      '#default_value' => $selected,
      '#options' => $categories,
      '#size' => 10,
      '#multiple' => TRUE
    );
201
  }
202 203
  $form['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('form-actions')));
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
204 205 206 207

  return $form;
}

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

/**
237
 * Returns HTML for the aggregator page list form for assigning categories.
238
 *
239 240
 * @param $variables
 *   An associative array containing:
241
 *   - form: A render element representing the form.
242
 *
243 244
 * @ingroup themeable
 */
245 246 247
function theme_aggregator_categorize_items($variables) {
  $form = $variables['form'];

248
  $output = drupal_render($form['feed_source']);
249
  $rows = array();
250
  if (!empty($form['items'])) {
251
    foreach (element_children($form['items']) as $key) {
252 253
      $rows[] = array(
        drupal_render($form['items'][$key]),
254
        array('data' => drupal_render($form['categories'][$key]), 'class' => array('categorize-item')),
255
      );
256 257
    }
  }
258
  $output .= theme('table', array('header' => array('', t('Categorize')), 'rows' => $rows));
259
  $output .= drupal_render($form['submit']);
260
  $output .= drupal_render_children($form);
261

262
  return theme('aggregator_wrapper', array('content' => $output));
263 264
}

265 266 267 268 269 270
/**
 * Process variables for aggregator-wrapper.tpl.php.
 *
 * @see aggregator-wrapper.tpl.php
 */
function template_preprocess_aggregator_wrapper(&$variables) {
271
  $variables['pager'] = theme('pager', array('tags' => NULL));
272
}
273 274

/**
275
 * Process variables for aggregator-item.tpl.php.
276
 *
277
 * @see aggregator-item.tpl.php
278
 */
279 280 281 282 283 284
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);
285

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

299 300
  $variables['categories'] = array();
  foreach ($item->categories as $category) {
301
    $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/' . $category->cid);
302 303 304 305 306 307 308 309 310
  }
}

/**
 * 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');

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

326
  return theme('aggregator_wrapper', array('content' => $output));
327 328 329 330 331 332 333 334
}

/**
 * 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');

335
  $output = '';
336
  foreach ($result as $category) {
337
    if (variable_get('aggregator_summary_items', 3)) {
338
      $summary_items = array();
339
      $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', 0, variable_get('aggregator_summary_items', 3), array(':cid' => $category->cid));
340
      foreach ($items as $item) {
341
        $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
342 343
      }
    }
344
    $category->url = url('aggregator/categories/' . $category->cid);
345
    $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $category));
346 347
  }

348
  return theme('aggregator_wrapper', array('content' => $output));
349 350 351 352 353 354 355
}

/**
 * Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
 */
function aggregator_page_rss() {
  $result = NULL;
356
  // arg(2) is the passed cid, only select for that category.
357
  if (arg(2)) {
358
    $category = db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = :cid', array(':cid' => arg(2)))->fetchObject();
359
    $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', 0, variable_get('feed_default_items', 10), array(':cid' => $category->cid));
360
  }
361
  // Or, get the default aggregator items.
362
  else {
363
    $category = NULL;
364
    $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));
365
  }
366

367
  $feeds = $result->fetchAll();
368
  return theme('aggregator_page_rss', array('feeds' => $feeds, 'category' => $category));
369 370 371
}

/**
372
 * Prints the RSS page for a feed.
373
 *
374 375 376 377 378
 * @param $variables
 *   An associative array containing:
 *   - feeds: An array of the feeds to theme.
 *   - category: A common category, if any, for all the feeds.
 *
379 380
 * @return void
 *
381 382
 * @ingroup themeable
 */
383 384 385 386
function theme_aggregator_page_rss($variables) {
  $feeds = $variables['feeds'];
  $category = $variables['category'];

387
  drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
388 389

  $items = '';
390
  $feed_length = variable_get('feed_item_length', 'fulltext');
391 392
  foreach ($feeds as $feed) {
    switch ($feed_length) {
393
      case 'teaser':
394 395 396
        $summary = text_summary($feed->description, NULL, variable_get('aggregator_teaser_length', 600));
        if ($summary != $feed->description) {
          $summary .= '<p><a href="' . check_url($feed->link) . '">' . t('read more') . "</a></p>\n";
397
        }
398
        $feed->description = $summary;
399 400
        break;
      case 'title':
401
        $feed->description = '';
402 403
        break;
    }
404
    $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
405 406
  }

407
  $site_name = variable_get('site_name', 'Drupal');
408
  $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE));
409
  $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));
410

411
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
412
  $output .= "<rss version=\"2.0\">\n";
413
  $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
414 415 416 417 418 419 420
  $output .= "</rss>\n";

  print $output;
}

/**
 * Menu callback; generates an OPML representation of all feeds.
421 422 423 424 425
 *
 * @param $cid
 *   If set, feeds are exported only from a category with this ID. Otherwise, all feeds are exported.
 * @return
 *   The output XML.
426 427 428
 */
function aggregator_page_opml($cid = NULL) {
  if ($cid) {
429
    $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));
430 431 432 433
  }
  else {
    $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
  }
434

435
  $feeds = $result->fetchAll();
436
  return theme('aggregator_page_opml', array('feeds' => $feeds));
437 438 439
}

/**
440
 * Prints the OPML page for a feed.
441
 *
442 443 444 445
 * @param $variables
 *   An associative array containing:
 *   - feeds: An array of the feeds to theme.
 *
446 447
 * @return void
 *
448 449
 * @ingroup themeable
 */
450 451 452
function theme_aggregator_page_opml($variables) {
  $feeds = $variables['feeds'];

453
  drupal_add_http_header('Content-Type', 'text/xml; charset=utf-8');
454

455
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
456 457
  $output .= "<opml version=\"1.1\">\n";
  $output .= "<head>\n";
458
  $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) . "</title>\n";
459
  $output .= '<dateModified>' . gmdate(DATE_RFC2822, REQUEST_TIME) . "</dateModified>\n";
460 461
  $output .= "</head>\n";
  $output .= "<body>\n";
462
  foreach ($feeds as $feed) {
463
    $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="' . check_url($feed->url) . "\" />\n";
464 465 466 467 468 469 470 471
  }
  $output .= "</body>\n";
  $output .= "</opml>\n";

  print $output;
}

/**
472
 * Process variables for aggregator-summary-items.tpl.php.
473
 *
474 475 476 477
 * @see aggregator-summary-item.tpl.php
 */
function template_preprocess_aggregator_summary_items(&$variables) {
  $variables['title'] = check_plain($variables['source']->title);
478
  $variables['summary_list'] = theme('item_list', array('items' => $variables['summary_items']));
479 480 481 482 483 484 485
  $variables['source_url'] = $variables['source']->url;
}

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

  $variables['feed_url'] = check_url($item->link);
  $variables['feed_title'] = check_plain($item->title);
492
  $variables['feed_age'] = t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->timestamp)));
493 494 495

  $variables['source_url'] = '';
  $variables['source_title'] = '';
496
  if (!empty($item->feed_link)) {
497 498
    $variables['source_url'] = check_url($item->feed_link);
    $variables['source_title'] = check_plain($item->feed_title);
499 500 501 502
  }
}

/**
503
 * Process variables for aggregator-feed-source.tpl.php.
504
 *
505
 * @see aggregator-feed-source.tpl.php
506
 */
507 508
function template_preprocess_aggregator_feed_source(&$variables) {
  $feed = $variables['feed'];
509

510
  $variables['source_icon'] = theme('feed_icon', array('url' => $feed->url, 'title' => t('!title feed', array('!title' => $feed->title))));
511 512 513
  $variables['source_image'] = $feed->image;
  $variables['source_description'] = aggregator_filter_xss($feed->description);
  $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
514 515

  if ($feed->checked) {
516
    $variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked)));
517 518
  }
  else {
519
    $variables['last_checked'] = t('never');
520 521 522
  }

  if (user_access('administer news feeds')) {
523
    $variables['last_checked'] = l($variables['last_checked'], 'admin/config/services/aggregator');
524 525
  }
}