/[drupal]/drupal/modules/aggregator/aggregator.pages.inc
ViewVC logotype

Contents of /drupal/modules/aggregator/aggregator.pages.inc

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


Revision 1.37 - (show annotations) (download) (as text)
Fri Oct 9 00:59:55 2009 UTC (7 weeks ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.36: +42 -30 lines
File MIME type: text/x-php
- Patch #572618 by effulgentsia, pwolanin, sun: all theme functions should take a single argument. Code clean-up and performance improvement. Woot.
1 <?php
2 // $Id: aggregator.pages.inc,v 1.36 2009/09/30 18:36:02 dries Exp $
3
4 /**
5 * @file
6 * User page callbacks for the aggregator module.
7 */
8
9 /**
10 * Menu callback; displays the most recent items gathered from any feed.
11 *
12 * @return
13 * 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 * If there are two arguments then this function is the categorize form.
27 *
28 * @param $arg1
29 * If there are two arguments then $arg1 is $form_state. Otherwise, $arg1 is $feed.
30 * @param $arg2
31 * If there are two arguments then $arg2 is feed.
32 * @return
33 * The item's HTML.
34 */
35 function aggregator_page_source($arg1, $arg2 = NULL) {
36 // If there are two arguments then this function is the categorize form, and
37 // $arg1 is $form_state and $arg2 is $feed. Otherwise, $arg1 is $feed.
38 $feed = is_object($arg2) ? $arg2 : $arg1;
39 drupal_set_title($feed->title);
40 $feed_source = theme('aggregator_feed_source', array('feed' => $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('source', $feed);
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 */
61 function 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('category', $category);
71
72 return _aggregator_page_list($items, arg(3));
73 }
74
75 /**
76 * Load feed items
77 *
78 * @param $type
79 * The filter for the items. Possible values: 'sum', 'source', 'category'
80 * @param $data
81 * Feed or category data for filtering
82 * @return
83 * An array of the feed items.
84 */
85 function aggregator_feed_items_load($type, $data = NULL) {
86 $items = array();
87 $range_limit = 20;
88 switch ($type) {
89 case 'sum':
90 $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);
91 break;
92 case 'source':
93 $result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC', 0, $range_limit, array(':fid' => $data->fid));
94 break;
95 case 'category':
96 $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']));
97 break;
98 }
99
100 foreach ($result as $item) {
101 $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();
102 $items[] = $item;
103 }
104
105 return $items;
106 }
107
108 /**
109 * Prints an aggregator page listing a number of feed items.
110 *
111 * Various menu callbacks use this function to print their feeds.
112 *
113 * @param $items
114 * The items to be listed.
115 * @param $op
116 * Which form should be added to the items. Only 'categorize' is now recognized.
117 * @param $feed_source
118 * The feed source URL.
119 * @return
120 * The items HTML.
121 */
122 function _aggregator_page_list($items, $op, $feed_source = '') {
123 if (user_access('administer news feeds') && ($op == 'categorize')) {
124 // Get form data.
125 $output = aggregator_categorize_items($items, $feed_source);
126 }
127 else {
128 // Assemble themed output.
129 $output = $feed_source;
130 foreach ($items as $item) {
131 $output .= theme('aggregator_item', array('item' => $item));
132 }
133 $output = theme('aggregator_wrapper', array('content' => $output));
134 }
135
136 return $output;
137 }
138
139 /**
140 * Form builder; build the page list form.
141 *
142 * @param $items
143 * An array of the feed items.
144 * @param $feed_source
145 * The feed source URL.
146 * @return
147 * The form structure.
148 * @ingroup forms
149 * @see aggregator_categorize_items_validate()
150 * @see aggregator_categorize_items_submit()
151 */
152 function aggregator_categorize_items($items, $feed_source = '') {
153 $form['#submit'][] = 'aggregator_categorize_items_submit';
154 $form['#validate'][] = 'aggregator_categorize_items_validate';
155 $form['#theme'] = 'aggregator_categorize_items';
156 $form['feed_source'] = array(
157 '#value' => $feed_source,
158 );
159 $categories = array();
160 $done = FALSE;
161 $form['items'] = array();
162 $form['categories'] = array(
163 '#tree' => TRUE,
164 );
165 foreach ($items as $item) {
166 $form['items'][$item->iid] = array('#markup' => theme('aggregator_item', array('item' => $item)));
167 $form['categories'][$item->iid] = array();
168 $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));
169 $selected = array();
170 foreach ($categories_result as $category) {
171 if (!$done) {
172 $categories[$category->cid] = check_plain($category->title);
173 }
174 if ($category->iid) {
175 $selected[] = $category->cid;
176 }
177 }
178 $done = TRUE;
179 $form['categories'][$item->iid] = array(
180 '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
181 '#default_value' => $selected,
182 '#options' => $categories,
183 '#size' => 10,
184 '#multiple' => TRUE
185 );
186 }
187 $form['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
188
189 return $form;
190 }
191
192 /**
193 * Validate aggregator_categorize_items() form submissions.
194 */
195 function aggregator_categorize_items_validate($form, &$form_state) {
196 if (!user_access('administer news feeds')) {
197 form_error($form, t('You are not allowed to categorize this feed item.'));
198 }
199 }
200
201 /**
202 * Process aggregator_categorize_items() form submissions.
203 */
204 function aggregator_categorize_items_submit($form, &$form_state) {
205 if (!empty($form_state['values']['categories'])) {
206 foreach ($form_state['values']['categories'] as $iid => $selection) {
207 db_delete('aggregator_category_item')
208 ->condition('iid', $iid)
209 ->execute();
210 $insert = db_insert('aggregator_category_item')->fields(array('iid', 'cid'));
211 $has_values = FALSE;
212 foreach ($selection as $cid) {
213 if ($cid && $iid) {
214 $has_values = TRUE;
215 $insert->values(array(
216 'iid' => $iid,
217 'cid' => $cid,
218 ));
219 }
220 }
221 if ($has_values) {
222 $insert->execute();
223 }
224 }
225 }
226 drupal_set_message(t('The categories have been saved.'));
227 }
228
229 /**
230 * Theme the page list form for assigning categories.
231 *
232 * @param $variables
233 * An associative array containing:
234 * - form: An associative array containing the structure of the form.
235 *
236 * @return
237 * The output HTML.
238 * @ingroup themeable
239 */
240 function theme_aggregator_categorize_items($variables) {
241 $form = $variables['form'];
242
243 $output = drupal_render($form['feed_source']);
244 $rows = array();
245 if (!empty($form['items'])) {
246 foreach (element_children($form['items']) as $key) {
247 $rows[] = array(
248 drupal_render($form['items'][$key]),
249 array('data' => drupal_render($form['categories'][$key]), 'class' => array('categorize-item')),
250 );
251 }
252 }
253 $output .= theme('table', array('header' => array('', t('Categorize')), 'rows' => $rows));
254 $output .= drupal_render($form['submit']);
255 $output .= drupal_render_children($form);
256
257 return theme('aggregator_wrapper', array('content' => $output));
258 }
259
260 /**
261 * Process variables for aggregator-wrapper.tpl.php.
262 *
263 * @see aggregator-wrapper.tpl.php
264 */
265 function template_preprocess_aggregator_wrapper(&$variables) {
266 $variables['pager'] = theme('pager', array('tags' => NULL));
267 }
268
269 /**
270 * Process variables for aggregator-item.tpl.php.
271 *
272 * @see aggregator-item.tpl.php
273 */
274 function template_preprocess_aggregator_item(&$variables) {
275 $item = $variables['item'];
276
277 $variables['feed_url'] = check_url($item->link);
278 $variables['feed_title'] = check_plain($item->title);
279 $variables['content'] = aggregator_filter_xss($item->description);
280
281 $variables['source_url'] = '';
282 $variables['source_title'] = '';
283 if (isset($item->ftitle) && isset($item->fid)) {
284 $variables['source_url'] = url("aggregator/sources/$item->fid");
285 $variables['source_title'] = check_plain($item->ftitle);
286 }
287 if (date('Ymd', $item->timestamp) == date('Ymd')) {
288 $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->timestamp)));
289 }
290 else {
291 $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
292 }
293
294 $variables['categories'] = array();
295 foreach ($item->categories as $category) {
296 $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/' . $category->cid);
297 }
298 }
299
300 /**
301 * Menu callback; displays all the feeds used by the aggregator.
302 */
303 function aggregator_page_sources() {
304 $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
306 $output = '';
307 foreach ($result as $feed) {
308 // Most recent items:
309 $summary_items = array();
310 if (variable_get('aggregator_summary_items', 3)) {
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', 0, variable_get('aggregator_summary_items', 3), array(':fid' => $feed->fid));
312 foreach ($items as $item) {
313 $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
314 }
315 }
316 $feed->url = url('aggregator/sources/' . $feed->fid);
317 $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $feed));
318 }
319 $output .= theme('feed_icon', array('url' => url('aggregator/opml'), 'title' => t('OPML feed')));
320
321 return theme('aggregator_wrapper', array('content' => $output));
322 }
323
324 /**
325 * Menu callback; displays all the categories used by the aggregator.
326 */
327 function aggregator_page_categories() {
328 $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
330 $output = '';
331 foreach ($result as $category) {
332 if (variable_get('aggregator_summary_items', 3)) {
333 $summary_items = array();
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', 0, variable_get('aggregator_summary_items', 3), array(':cid' => $category->cid));
335 foreach ($items as $item) {
336 $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
337 }
338 }
339 $category->url = url('aggregator/categories/' . $category->cid);
340 $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $category));
341 }
342
343 return theme('aggregator_wrapper', array('content' => $output));
344 }
345
346 /**
347 * Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
348 */
349 function aggregator_page_rss() {
350 $result = NULL;
351 // arg(2) is the passed cid, only select for that category.
352 if (arg(2)) {
353 $category = db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = :cid', array(':cid' => arg(2)))->fetchObject();
354 $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));
355 }
356 // Or, get the default aggregator items.
357 else {
358 $category = NULL;
359 $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));
360 }
361
362 $feeds = $result->fetchAll();
363 return theme('aggregator_page_rss', array('feeds' => $feeds, 'category' => $category));
364 }
365
366 /**
367 * Theme the RSS output.
368 *
369 * @param $variables
370 * An associative array containing:
371 * - feeds: An array of the feeds to theme.
372 * - category: A common category, if any, for all the feeds.
373 *
374 * @ingroup themeable
375 */
376 function theme_aggregator_page_rss($variables) {
377 $feeds = $variables['feeds'];
378 $category = $variables['category'];
379
380 drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
381
382 $items = '';
383 $feed_length = variable_get('feed_item_length', 'fulltext');
384 foreach ($feeds as $feed) {
385 switch ($feed_length) {
386 case 'teaser':
387 $summary = text_summary($feed->description, NULL, variable_get('aggregator_teaser_length', 600));
388 if ($summary != $feed->description) {
389 $summary .= '<p><a href="' . check_url($feed->link) . '">' . t('read more') . "</a></p>\n";
390 }
391 $feed->description = $summary;
392 break;
393 case 'title':
394 $feed->description = '';
395 break;
396 }
397 $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
398 }
399
400 $site_name = variable_get('site_name', 'Drupal');
401 $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE));
402 $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));
403
404 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
405 $output .= "<rss version=\"2.0\">\n";
406 $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
407 $output .= "</rss>\n";
408
409 print $output;
410 }
411
412 /**
413 * Menu callback; generates an OPML representation of all feeds.
414 *
415 * @param $cid
416 * If set, feeds are exported only from a category with this ID. Otherwise, all feeds are exported.
417 * @return
418 * The output XML.
419 */
420 function aggregator_page_opml($cid = NULL) {
421 if ($cid) {
422 $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));
423 }
424 else {
425 $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
426 }
427
428 $feeds = $result->fetchAll();
429 return theme('aggregator_page_opml', array('feeds' => $feeds));
430 }
431
432 /**
433 * Theme the OPML feed output.
434 *
435 * @param $variables
436 * An associative array containing:
437 * - feeds: An array of the feeds to theme.
438 *
439 * @ingroup themeable
440 */
441 function theme_aggregator_page_opml($variables) {
442 $feeds = $variables['feeds'];
443
444 drupal_add_http_header('Content-Type', 'text/xml; charset=utf-8');
445
446 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
447 $output .= "<opml version=\"1.1\">\n";
448 $output .= "<head>\n";
449 $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) . "</title>\n";
450 $output .= '<dateModified>' . gmdate(DATE_RFC2822, REQUEST_TIME) . "</dateModified>\n";
451 $output .= "</head>\n";
452 $output .= "<body>\n";
453 foreach ($feeds as $feed) {
454 $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="' . check_url($feed->url) . "\" />\n";
455 }
456 $output .= "</body>\n";
457 $output .= "</opml>\n";
458
459 print $output;
460 }
461
462 /**
463 * Process variables for aggregator-summary-items.tpl.php.
464 *
465 * @see aggregator-summary-item.tpl.php
466 */
467 function template_preprocess_aggregator_summary_items(&$variables) {
468 $variables['title'] = check_plain($variables['source']->title);
469 $variables['summary_list'] = theme('item_list', array('items' => $variables['summary_items']));
470 $variables['source_url'] = $variables['source']->url;
471 }
472
473 /**
474 * Process variables for aggregator-summary-item.tpl.php.
475 *
476 * @see aggregator-summary-item.tpl.php
477 */
478 function template_preprocess_aggregator_summary_item(&$variables) {
479 $item = $variables['item'];
480
481 $variables['feed_url'] = check_url($item->link);
482 $variables['feed_title'] = check_plain($item->title);
483 $variables['feed_age'] = t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->timestamp)));
484
485 $variables['source_url'] = '';
486 $variables['source_title'] = '';
487 if (!empty($item->feed_link)) {
488 $variables['source_url'] = check_url($item->feed_link);
489 $variables['source_title'] = check_plain($item->feed_title);
490 }
491 }
492
493 /**
494 * Process variables for aggregator-feed-source.tpl.php.
495 *
496 * @see aggregator-feed-source.tpl.php
497 */
498 function template_preprocess_aggregator_feed_source(&$variables) {
499 $feed = $variables['feed'];
500
501 $variables['source_icon'] = theme('feed_icon', array('url' => $feed->url, 'title' => t('!title feed', array('!title' => $feed->title))));
502 $variables['source_image'] = $feed->image;
503 $variables['source_description'] = aggregator_filter_xss($feed->description);
504 $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
505
506 if ($feed->checked) {
507 $variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked)));
508 }
509 else {
510 $variables['last_checked'] = t('never');
511 }
512
513 if (user_access('administer news feeds')) {
514 $variables['last_checked'] = l($variables['last_checked'], 'admin/config/services/aggregator');
515 }
516 }

  ViewVC Help
Powered by ViewVC 1.1.2