<?php
// $Id: googlenews.module,v 1.1.2.1 2009/03/05 19:58:26 webopius Exp $

/**
 * @file 
 * Provides a Google News sitemap within your site using the url: http://www.yoursite.com/googlenews.xml
 *
 * Webopius Ltd, www.webopius.com, info@webopius.com
 */
 
/**
 * Implementation of hook_help().
 */

function googlenews_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#googlenews":
      $output = '<p>'.  t("Generates Google news sitemap from the /googlenews.xml url") .'</p>';
      break;
	case "admin/settings/googlenews":
	  $output = '<p>'. t("Choose the categories that are generated by the Google news feed") . '</p>';
	  break;
  }
  
  return $output;
} // function googlenews_help

/**
 * Generate the news feed.
 */

function _googlenews_getgooglenews() {
		drupal_set_header('Content-Type: text/xml');

		$content='<?xml version="1.0" encoding="UTF-8"?>';
		$content.='<!-- generator="googlenewsmodule/v1.0" -->';
		$content.='<!-- sitemap-generator-url="http://www.webopius.com" sitemap-generator-version="1.0"  -->';

		$content.='<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"';
		$content.=' xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">';
				
		// Build categories. If 'all' is set, this overrides all other selections.
		
		$cats = variable_get('googlenews_category', array());
		$ctypes='';
		if (!isset($cats['_gnall'])) {		
			foreach ($cats as $key => $value) {
				if ($ctypes!='') {
					$ctypes.=",'".$key."'";
				}
				else
				{
					$ctypes.="'".$key."'";						
				}
			}
		}
		
		if ($ctypes!='') {
			$sql = "select n.nid,created from {node} n where status=1 and type in (".$ctypes.") and from_unixtime(created) >= date_sub(curdate(),interval 3 day) order by created desc";
		}
		else
		{
			$sql = "select n.nid,created from {node} n where status=1 and from_unixtime(created) >= date_sub(curdate(),interval 3 day) order by created desc";
		}

		$res = db_query(db_rewrite_sql($sql));
		
		while ($data = db_fetch_object($res)) {
			$nid = $data->nid;		
			$node_date = date(DATE_W3C,$data->created);
			$node_url = url("node/$nid");
						
			$content .= '<url>';
			$content .= '<loc>http://'.$_SERVER['HTTP_HOST'].$node_url.'</loc>';
			$content .= '<news:news>';
			$content .= '<news:publication_date>'.$node_date.'</news:publication_date>';
			$content .= '</news:news>';
			$content .= '</url>';
		}
	
		$content .= '</urlset>';
			
		print $content;
}

/**
 * Implementation of hook_menu().
 *
 * Map the callbacks defined above to specific URL (googlenews.xml)
 * Also create the admin settings configuration menu to specify the content types
 */
 
function googlenews_menu() {
	$items['googlenews.xml'] = array(
		'title' => 'Google News sitemap feed',
		'page callback' => '_googlenews_getgooglenews',
		'access arguments' => array('access content'),
		'type' => MENU_CALLBACK,
	);
	
	$items['admin/settings/googlenews'] = array(
    'title' => 'Google News sitemap feed',
    'description' => 'Specify category types for the Google News feed module.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('googlenews_admin_settings'),
    'access arguments' => array('administer site configuration'),
  );

	return $items;
} // function googlenews_menu()


/**
 * Implementation of hook_settings().
 * Allows the feed to be generated for one or more content types
 */
 
function googlenews_admin_settings() {
  
	$form = array();
	$cats = array('_gnall' => '<'. t('All content types') .'>');
   
	$sql = "select name, type from {node_type} order by name";
	$result = db_query($sql);
	while ($data = db_fetch_object($result)) {
		$node = node_load($data->nid);
		$cats[$data->type] = $data->name;
   	}
	
	if (count($cats) > 1) {
    	$form['googlenews_category'] = array(
        '#type' => 'select',
	    '#multiple' => TRUE,
    	'#title' => t('Select the category types to use for the news feed'),
        '#default_value' => variable_get('googlenews_category', array()),
	    '#options' => $cats,
    	'#description' => t('Select the category types you would like to Generate the Google news feed for'),
      	);
	}

	return system_settings_form($form); 
}

/**
 * That's all!
 */

