Projet

Général

Profil

Paste
Télécharger (19,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / aggregator / aggregator.pages.inc @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * User page callbacks for the Aggregator module.
6
 */
7

    
8
/**
9
 * Page callback: Displays the most recent items gathered from any feed.
10
 *
11
 * @return
12
 *   The rendered list of items for the feed.
13
 */
14
function aggregator_page_last() {
15
  drupal_add_feed('aggregator/rss', variable_get('site_name', 'Drupal') . ' ' . t('aggregator'));
16

    
17
  $items = aggregator_feed_items_load('sum');
18

    
19
  return _aggregator_page_list($items, arg(1));
20
}
21

    
22
/**
23
 * Page callback: Displays all the items captured from the particular feed.
24
 *
25
 * @param $feed
26
 *   The feed for which to display all items.
27
 *
28
 * @return
29
 *   The rendered list of items for a feed.
30
 *
31
 * @see aggregator_menu()
32
 */
33
function aggregator_page_source($feed) {
34
  drupal_set_title($feed->title);
35
  $feed_source = theme('aggregator_feed_source', array('feed' => $feed));
36

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

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

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

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

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

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

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

    
93
/**
94
 * Loads and optionally filters feed items.
95
 *
96
 * @param $type
97
 *   The type of filter for the items. Possible values are:
98
 *   - sum: No filtering.
99
 *   - source: Filter the feed items, limiting the result to items from a
100
 *     single source.
101
 *   - category: Filter the feed items by category.
102
 * @param $data
103
 *   Feed or category data used for filtering. The type and value of $data
104
 *   depends on $type:
105
 *   - source: $data is an object with $data->fid identifying the feed used to
106
 *     as filter.
107
 *   - category: $data is an array with $data['cid'] being the category id to
108
 *     filter on.
109
 *   The $data parameter is not used when $type is 'sum'.
110
 *
111
 * @return
112
 *   An array of the feed items.
113
 */
114
function aggregator_feed_items_load($type, $data = NULL) {
115
  $items = array();
116
  switch ($type) {
117
    case 'sum':
118
      $query = db_select('aggregator_item', 'i');
119
      $query->join('aggregator_feed', 'f', 'i.fid = f.fid');
120
      $query->fields('i');
121
      $query->addField('f', 'title', 'ftitle');
122
      $query->addField('f', 'link', 'flink');
123
      break;
124
    case 'source':
125
      $query = db_select('aggregator_item', 'i');
126
      $query
127
        ->fields('i')
128
        ->condition('i.fid', $data->fid);
129
      break;
130
    case 'category':
131
      $query = db_select('aggregator_category_item', 'c');
132
      $query->leftJoin('aggregator_item', 'i', 'c.iid = i.iid');
133
      $query->leftJoin('aggregator_feed', 'f', 'i.fid = f.fid');
134
      $query
135
        ->fields('i')
136
        ->condition('cid', $data['cid']);
137
      $query->addField('f', 'title', 'ftitle');
138
      $query->addField('f', 'link', 'flink');
139
      break;
140
  }
141

    
142
  $result = $query
143
    ->extend('PagerDefault')
144
    ->limit(20)
145
    ->orderBy('i.timestamp', 'DESC')
146
    ->orderBy('i.iid', 'DESC')
147
    ->execute();
148

    
149
  foreach ($result as $item) {
150
    $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();
151
    $items[] = $item;
152
  }
153

    
154
  return $items;
155
}
156

    
157
/**
158
 * Prints an aggregator page listing a number of feed items.
159
 *
160
 * Various menu callbacks use this function to print their feeds.
161
 *
162
 * @param $items
163
 *   The items to be listed.
164
 * @param $op
165
 *   Which form should be added to the items. Only 'categorize' is now
166
 *   recognized.
167
 * @param $feed_source
168
 *   The feed source URL.
169
 *
170
 * @return
171
 *   The rendered list of items for the feed.
172
 */
173
function _aggregator_page_list($items, $op, $feed_source = '') {
174
  if (user_access('administer news feeds') && ($op == 'categorize')) {
175
    // Get form data.
176
    $output = aggregator_categorize_items($items, $feed_source);
177
  }
178
  else {
179
    // Assemble themed output.
180
    $output = $feed_source;
181
    foreach ($items as $item) {
182
      $output .= theme('aggregator_item', array('item' => $item));
183
    }
184
    $output = theme('aggregator_wrapper', array('content' => $output));
185
  }
186

    
187
  return $output;
188
}
189

    
190
/**
191
 * Form constructor to build the page list form.
192
 *
193
 * @param $items
194
 *   An array of the feed items.
195
 * @param $feed_source
196
 *   (optional) The feed source URL. Defaults to an empty string.
197
 *
198
 * @return array
199
 *   An array of FAPI elements.
200
 *
201
 * @see aggregator_categorize_items_submit()
202
 * @see theme_aggregator_categorize_items()
203
 * @ingroup forms
204
 */
205
function aggregator_categorize_items($items, $feed_source = '') {
206
  $form['#submit'][] = 'aggregator_categorize_items_submit';
207
  $form['#theme'] = 'aggregator_categorize_items';
208
  $form['feed_source'] = array(
209
    '#value' => $feed_source,
210
  );
211
  $categories = array();
212
  $done = FALSE;
213
  $form['items'] = array();
214
  $form['categories'] = array(
215
    '#tree' => TRUE,
216
  );
217
  foreach ($items as $item) {
218
    $form['items'][$item->iid] = array('#markup' => theme('aggregator_item', array('item' => $item)));
219
    $form['categories'][$item->iid] = array();
220
    $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));
221
    $selected = array();
222
    foreach ($categories_result as $category) {
223
      if (!$done) {
224
        $categories[$category->cid] = check_plain($category->title);
225
      }
226
      if ($category->iid) {
227
        $selected[] = $category->cid;
228
      }
229
    }
230
    $done = TRUE;
231
    $form['categories'][$item->iid] = array(
232
      '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
233
      '#default_value' => $selected,
234
      '#options' => $categories,
235
      '#size' => 10,
236
      '#multiple' => TRUE
237
    );
238
  }
239
  $form['actions'] = array('#type' => 'actions');
240
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
241

    
242
  return $form;
243
}
244

    
245
/**
246
 * Form submission handler for aggregator_categorize_items().
247
 */
248
function aggregator_categorize_items_submit($form, &$form_state) {
249
  if (!empty($form_state['values']['categories'])) {
250
    foreach ($form_state['values']['categories'] as $iid => $selection) {
251
      db_delete('aggregator_category_item')
252
        ->condition('iid', $iid)
253
        ->execute();
254
      $insert = db_insert('aggregator_category_item')->fields(array('iid', 'cid'));
255
      $has_values = FALSE;
256
      foreach ($selection as $cid) {
257
        if ($cid && $iid) {
258
          $has_values = TRUE;
259
          $insert->values(array(
260
            'iid' => $iid,
261
            'cid' => $cid,
262
          ));
263
        }
264
      }
265
      if ($has_values) {
266
        $insert->execute();
267
      }
268
    }
269
  }
270
  drupal_set_message(t('The categories have been saved.'));
271
}
272

    
273
/**
274
 * Returns HTML for the aggregator page list form for assigning categories.
275
 *
276
 * @param $variables
277
 *   An associative array containing:
278
 *   - form: A render element representing the form.
279
 *
280
 * @ingroup themeable
281
 */
282
function theme_aggregator_categorize_items($variables) {
283
  $form = $variables['form'];
284

    
285
  $output = drupal_render($form['feed_source']);
286
  $rows = array();
287
  if (!empty($form['items'])) {
288
    foreach (element_children($form['items']) as $key) {
289
      $rows[] = array(
290
        drupal_render($form['items'][$key]),
291
        array('data' => drupal_render($form['categories'][$key]), 'class' => array('categorize-item')),
292
      );
293
    }
294
  }
295
  $output .= theme('table', array('header' => array('', t('Categorize')), 'rows' => $rows));
296
  $output .= drupal_render($form['submit']);
297
  $output .= drupal_render_children($form);
298

    
299
  return theme('aggregator_wrapper', array('content' => $output));
300
}
301

    
302
/**
303
 * Processes variables for aggregator-wrapper.tpl.php.
304
 *
305
 * @see aggregator-wrapper.tpl.php
306
 */
307
function template_preprocess_aggregator_wrapper(&$variables) {
308
  $variables['pager'] = theme('pager');
309
}
310

    
311
/**
312
 * Processes variables for aggregator-item.tpl.php.
313
 *
314
 * @see aggregator-item.tpl.php
315
 */
316
function template_preprocess_aggregator_item(&$variables) {
317
  $item = $variables['item'];
318

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

    
323
  $variables['source_url'] = '';
324
  $variables['source_title'] = '';
325
  if (isset($item->ftitle) && isset($item->fid)) {
326
    $variables['source_url'] = url("aggregator/sources/$item->fid");
327
    $variables['source_title'] = check_plain($item->ftitle);
328
  }
329
  if (date('Ymd', $item->timestamp) == date('Ymd')) {
330
    $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->timestamp)));
331
  }
332
  else {
333
    $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
334
  }
335

    
336
  $variables['categories'] = array();
337
  foreach ($item->categories as $category) {
338
    $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/' . $category->cid);
339
  }
340
}
341

    
342
/**
343
 * Page callback: Displays all the feeds used by the aggregator.
344
 *
345
 * @return
346
 *   An HTML-formatted string.
347
 *
348
 * @see aggregator_menu()
349
 */
350
function aggregator_page_sources() {
351
  $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');
352

    
353
  $output = '';
354
  foreach ($result as $feed) {
355
    // Most recent items:
356
    $summary_items = array();
357
    if (variable_get('aggregator_summary_items', 3)) {
358
      $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));
359
      foreach ($items as $item) {
360
        $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
361
      }
362
    }
363
    $feed->url = url('aggregator/sources/' . $feed->fid);
364
    $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $feed));
365
  }
366
  $output .= theme('feed_icon', array('url' => 'aggregator/opml', 'title' => t('OPML feed')));
367

    
368
  return theme('aggregator_wrapper', array('content' => $output));
369
}
370

    
371
/**
372
 * Page callback: Displays all the categories used by the Aggregator module.
373
 *
374
 * @return string
375
 *   An HTML formatted string.
376
 *
377
 * @see aggregator_menu()
378
 */
379
function aggregator_page_categories() {
380
  $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');
381

    
382
  $output = '';
383
  foreach ($result as $category) {
384
    if (variable_get('aggregator_summary_items', 3)) {
385
      $summary_items = array();
386
      $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));
387
      foreach ($items as $item) {
388
        $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
389
      }
390
    }
391
    $category->url = url('aggregator/categories/' . $category->cid);
392
    $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $category));
393
  }
394

    
395
  return theme('aggregator_wrapper', array('content' => $output));
396
}
397

    
398
/**
399
 * Page callback: Generates an RSS 0.92 feed of aggregator items or categories.
400
 *
401
 * @return string
402
 *   An HTML formatted string.
403
 */
404
function aggregator_page_rss() {
405
  $result = NULL;
406
  // arg(2) is the passed cid, only select for that category.
407
  if (arg(2)) {
408
    $category = db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = :cid', array(':cid' => arg(2)))->fetchObject();
409
    $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));
410
  }
411
  // Or, get the default aggregator items.
412
  else {
413
    $category = NULL;
414
    $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));
415
  }
416

    
417
  $feeds = $result->fetchAll();
418
  return theme('aggregator_page_rss', array('feeds' => $feeds, 'category' => $category));
419
}
420

    
421
/**
422
 * Prints the RSS page for a feed.
423
 *
424
 * @param $variables
425
 *   An associative array containing:
426
 *   - feeds: An array of the feeds to theme.
427
 *   - category: A common category, if any, for all the feeds.
428
 *
429
 * @return void
430
 *
431
 * @ingroup themeable
432
 */
433
function theme_aggregator_page_rss($variables) {
434
  $feeds = $variables['feeds'];
435
  $category = $variables['category'];
436

    
437
  drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
438

    
439
  $items = '';
440
  $feed_length = variable_get('feed_item_length', 'fulltext');
441
  foreach ($feeds as $feed) {
442
    switch ($feed_length) {
443
      case 'teaser':
444
        $summary = text_summary($feed->description, NULL, variable_get('aggregator_teaser_length', 600));
445
        if ($summary != $feed->description) {
446
          $summary .= '<p><a href="' . check_url($feed->link) . '">' . t('read more') . "</a></p>\n";
447
        }
448
        $feed->description = $summary;
449
        break;
450
      case 'title':
451
        $feed->description = '';
452
        break;
453
    }
454
    $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
455
  }
456

    
457
  $site_name = variable_get('site_name', 'Drupal');
458
  $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE));
459
  $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));
460

    
461
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
462
  $output .= "<rss version=\"2.0\">\n";
463
  $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
464
  $output .= "</rss>\n";
465

    
466
  print $output;
467
}
468

    
469
/**
470
 * Page callback: Generates an OPML representation of all feeds.
471
 *
472
 * @param $cid
473
 *   (optional) If set, feeds are exported only from a category with this ID.
474
 *   Otherwise, all feeds are exported. Defaults to NULL.
475
 *
476
 * @return
477
 *   An OPML formatted string.
478
 */
479
function aggregator_page_opml($cid = NULL) {
480
  if ($cid) {
481
    $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));
482
  }
483
  else {
484
    $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
485
  }
486

    
487
  $feeds = $result->fetchAll();
488
  return theme('aggregator_page_opml', array('feeds' => $feeds));
489
}
490

    
491
/**
492
 * Prints the OPML page for the feed.
493
 *
494
 * @param $variables
495
 *   An associative array containing:
496
 *   - feeds: An array of the feeds to theme.
497
 *
498
 * @ingroup themeable
499
 */
500
function theme_aggregator_page_opml($variables) {
501
  $feeds = $variables['feeds'];
502

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

    
505
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
506
  $output .= "<opml version=\"1.1\">\n";
507
  $output .= "<head>\n";
508
  $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) . "</title>\n";
509
  $output .= '<dateModified>' . gmdate(DATE_RFC2822, REQUEST_TIME) . "</dateModified>\n";
510
  $output .= "</head>\n";
511
  $output .= "<body>\n";
512
  foreach ($feeds as $feed) {
513
    $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="' . check_url($feed->url) . "\" />\n";
514
  }
515
  $output .= "</body>\n";
516
  $output .= "</opml>\n";
517

    
518
  print $output;
519
}
520

    
521
/**
522
 * Processes variables for aggregator-summary-items.tpl.php.
523
 *
524
 * @see aggregator-summary-items.tpl.php
525
 */
526
function template_preprocess_aggregator_summary_items(&$variables) {
527
  $variables['title'] = check_plain($variables['source']->title);
528
  $variables['summary_list'] = theme('item_list', array('items' => $variables['summary_items']));
529
  $variables['source_url'] = $variables['source']->url;
530
}
531

    
532
/**
533
 * Processes variables for aggregator-summary-item.tpl.php.
534
 *
535
 * @see aggregator-summary-item.tpl.php
536
 */
537
function template_preprocess_aggregator_summary_item(&$variables) {
538
  $item = $variables['item'];
539

    
540
  $variables['feed_url'] = check_url($item->link);
541
  $variables['feed_title'] = check_plain($item->title);
542
  $variables['feed_age'] = t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->timestamp)));
543

    
544
  $variables['source_url'] = '';
545
  $variables['source_title'] = '';
546
  if (!empty($item->feed_link)) {
547
    $variables['source_url'] = check_url($item->feed_link);
548
    $variables['source_title'] = check_plain($item->feed_title);
549
  }
550
}
551

    
552
/**
553
 * Processes variables for aggregator-feed-source.tpl.php.
554
 *
555
 * @see aggregator-feed-source.tpl.php
556
 */
557
function template_preprocess_aggregator_feed_source(&$variables) {
558
  $feed = $variables['feed'];
559

    
560
  $variables['source_icon'] = theme('feed_icon', array('url' => $feed->url, 'title' => t('!title feed', array('!title' => $feed->title))));
561

    
562
  if (!empty($feed->image) && !empty($feed->title) && !empty($feed->link)) {
563
    $variables['source_image'] = l(theme('image', array('path' => $feed->image, 'alt' => $feed->title)), $feed->link, array('html' => TRUE, 'attributes' => array('class' => 'feed-image')));
564
  }
565
  else {
566
    $variables['source_image'] = '';
567
  }
568

    
569
  $variables['source_description'] = aggregator_filter_xss($feed->description);
570
  $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
571

    
572
  if ($feed->checked) {
573
    $variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked)));
574
  }
575
  else {
576
    $variables['last_checked'] = t('never');
577
  }
578

    
579
  if (user_access('administer news feeds')) {
580
    $variables['last_checked'] = l($variables['last_checked'], 'admin/config/services/aggregator');
581
  }
582
}