--- googlenews.module	2010-11-13 00:38:59.000000000 +0000
+++ googlenews.module.new	2010-11-13 00:38:11.000000000 +0000
@@ -17,7 +17,7 @@ if (!defined('DATE_W3C')) {
 }
 
 /**
- * Implementation of hook_menu().
+ * Implementation of hook_menu(). 
  */
 function googlenews_menu() {
   $items['googlenews.xml'] = array(
@@ -45,17 +45,39 @@ function _googlenews_getgooglenews() {
   $content .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"';
   $content .= '  xmlns:n="http://www.google.com/schemas/sitemap-news/0.9">';
 
-  $node_types = variable_get('googlenews_node_types', array_keys(node_get_types()));
-  $args = array_merge($node_types, array(time() - 172800));
-  $sql = "SELECT n.nid, n.created, n.title FROM {node} n WHERE n.type IN (" . db_placeholders($node_types, 'varchar') . ") AND n.status = 1 AND n.created >= %d ORDER BY n.created DESC";
-  $query = db_query_range(db_rewrite_sql($sql), $args, 0, 1000);
-
-  while ($node = db_fetch_object($query)) {
+  $node_types = variable_get('node_options', array_keys(node_get_types()));
+  $taxonomy_terms = variable_get('taxonomy_options', array_keys(taxonomy_form_all()));
+  $feed_name = variable_get('feed_name');
+  $feed_items = variable_get('feed_items');
+  $feed_time = variable_get('feed_time');
+  if (empty($feed_time)) {
+  $feed_time = 172800;
+  }
+  
+  if(empty($taxonomy_terms)) {
+  // If there are no selected terms, geneate the feed from the selected content types.
+   $args = array_merge($node_types, array(time() - $feed_time));  
+   $sql = "SELECT n.nid, n.created, n.title FROM {node} n WHERE n.type IN (" . db_placeholders($node_types, 'varchar') . ") AND n.status = 1 AND n.created >= %d ORDER BY n.created DESC";
+    if (!empty($feed_items)) {
+	 $result = db_query_range(db_rewrite_sql($sql), $args, 0, $feed_items);
+	} else {
+     $result = db_query_range(db_rewrite_sql($sql), $args, 0, 10);
+	}
+  } else {
+  // If there are terms selected, generate the feed from terms and content types.
+   $result = _googlenews_select_nodes($taxonomy_terms, 'or', '', FALSE);
+  }
+  
+  while ($node = db_fetch_object($result)) {
     $content .= '<url>';
     $content .= '<loc>' . url('node/' . $node->nid, array('absolute' => TRUE)) . '</loc>';
     $content .= '<n:news>';
     $content .= '<n:publication>';
+	 if (!empty($feed_name)) {
+	  $content .= '<n:name>' . $feed_name . '</n:name>';
+	 } else {
       $content .= '<n:name>' . variable_get('site_name', 'Drupal') . '</n:name>';
+	 }
       $content .= '<n:language>' . check_plain($language->language) . '</n:language>';
     $content .= '</n:publication>';
     $content .= '<n:title>' . check_plain($node->title) . '</n:title>';
@@ -75,22 +97,115 @@ function _googlenews_getgooglenews() {
  */
 function googlenews_admin_settings() {
   $node_types = node_get_types('names');
-  $form['googlenews_node_types'] = array(
+  $taxonomy_terms = taxonomy_form_all();
+ 
+ 
+ $form['googlenews']['options'] = array(
+  '#type' => 'fieldset',
+  '#title' => t('Configuration'),
+  '#weight' => 5,
+  '#collapsible' => TRUE,
+  '#collapsed' => FALSE,
+ ); 
+ $form['googlenews']['options']['feed_time'] = array(
+  '#type' => 'textfield',
+  '#title' => t('Time span'),
+  '#description' => t('Enter the number of lapsed seconds for new content to be included. The default is 172800 seconds (2 days).'),
+  '#default_value' => variable_get('feed_time'),
+  '#weight' => 6,
+ ); 
+  $form['googlenews']['options']['feed_name'] = array(
+  '#type' => 'textfield',
+  '#title' => t('Feed name'),
+  '#description' => t('Enter the feed name. The site name is used by default.'),
+  '#default_value' => variable_get('feed_name'),
+  '#weight' => 4,
+ ); 
+ $form['googlenews']['options']['feed_items'] = array(
+  '#type' => 'textfield',
+  '#title' => t('Number of items'),
+  '#description' => t('Enter the number of items to include. The default is 10.'),
+  '#default_value' => variable_get('feed_items'),
+  '#weight' => 5,
+ ); 
+ $form['googlenews']['node'] = array(
   '#type' => 'fieldset',
   '#title' => t('Node types'),
+  '#description' => t('Select the content types to include in the <a href="@news-sitemap">news sitemap</a>.', array('@news-sitemap' => url('googlenews.xml'))),
   '#weight' => 5,
   '#collapsible' => TRUE,
   '#collapsed' => FALSE,
-);
-  $form['googlenews_node_types'] = array(
-    '#type' => 'checkboxes',
-    '#title' => t('Select the content types to include in the <a href="@news-sitemap">news sitemap</a>', array('@news-sitemap' => url('googlenews.xml'))),
-    '#default_value' => variable_get('googlenews_node_types', array_keys($node_types)),
-    '#options' => $node_types,
-  );
-  $form['array_filter'] = array(
-    '#type' => 'value',
-    '#value' => TRUE,
-  );
+ );
+ $form['googlenews']['node']['node_options'] = array(
+  '#type' => 'checkboxes',
+  '#title' => t('Node types'),
+  '#default_value' => variable_get('node_options', array_keys($node_types)),
+  '#options' => $node_types,
+ );
+ $form['googlenews']['taxonomy'] = array(
+  '#type' => 'fieldset',
+  '#title' => t('Taxonomy terms'),
+  '#description' => t('Select the taxonomy terms to include in the <a href="@news-sitemap">news sitemap</a>.', array('@news-sitemap' => url('googlenews.xml'))),
+  '#weight' => 5,
+  '#collapsible' => TRUE,
+  '#collapsed' => FALSE,
+ );
+ $form['googlenews']['taxonomy']['taxonomy_options'] = array(
+  '#type' => 'select',
+  '#multiple' => 'true',
+  '#title' => t('Taxonomy terms'),
+  '#default_value' => variable_get('taxonomy_options', array_keys($taxonomy_terms)),
+  '#options' => $taxonomy_terms,
+ );
+ $form['array_filter'] = array(
+  '#type' => 'value',
+  '#value' => TRUE,
+ );
   return system_settings_form($form);
 }
+
+
+
+/**
+ * Retrieve the database queries
+ */
+function _googlenews_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC') {
+ $feed_items = variable_get('feed_items');
+ $feed_time = variable_get('feed_time');
+  if (empty($feed_time)) {
+   $feed_time = 172800;
+  } 
+  if (count($tids) > 0) {
+    // For each term ID, generate an array of descendant term IDs to the right depth.
+    $descendant_tids = array();
+    if ($depth === 'all') {
+      $depth = NULL;
+    }
+    foreach ($tids as $index => $tid) {
+      $term = taxonomy_get_term($tid);
+      $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
+      $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
+    }
+
+	  $node_types = variable_get('node_options', array_keys(node_get_types()));
+	  $tids = call_user_func_array('array_merge', $descendant_tids);
+ 
+	if (!empty($tids) && !empty($node_types)) {
+	// Both a term and a node type are selected, use the term ID(s) and content type(s) for the query.
+	 $args = array_merge($tids, $node_types, array(time() - $feed_time));
+     $sql = "SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, n.type FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN (" . db_placeholders($tids, 'int') . ") AND n.type IN (" . db_placeholders($node_types, 'varchar') . ") AND n.status = 1 AND n.created >= %d ORDER BY n.created DESC";
+    } else {
+	// If just a term is selected, use the term ID(s) for the query.
+	 $args = array_merge($tids, array(time() - $feed_time));
+	 $sql = "SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, n.type FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN (" . db_placeholders($tids, 'int') . ") AND n.status = 1 AND n.created >= %d ORDER BY n.created DESC";
+	}
+    $sql = db_rewrite_sql($sql);
+	if (!empty($feed_items)) {
+     $result = db_query_range($sql, $args, 0, $feed_items);
+	} else {
+	 $result = db_query_range($sql, $args, 0, 10);
+	}
+  }
+
+  return $result;
+}
\ No newline at end of file
