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