<?php
/**
* @name 		Amplify News Ticker Module
* @author 		Amplify
* @package		Joomla
* @copyright	Copyright (C) 2008 Amplify
* @license		GNU/GPL
* 
* The Amplify News Ticker provides a mootools fading ticker of your latest news items to your Joomla! site.
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

/**
 * Amplify News Ticker module class
 */
class modAmpNewsTicker
{
	/**
	 * Initialise the Amplify news ticker module
	 */
	function initalise()
	{
		global $mainframe;
		
		JHTML::_('behavior.mootools');
		
		$goDoc =& JFactory::getDocument();
		$goDoc->addScript( JURI::root().'modules/mod_ampnewsticker/ampnewsticker/moostick.js' );
		$goDoc->addStyleSheet( JURI::root().'modules/mod_ampnewsticker/tmpl/stylesheet.css' );
		
		// if the ContentHelperRoute class is not loaded, include the class file now
		if (!class_exists('ContentHelperRoute')) {
			include_once ( JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php' );
		}
	} // end function initalise
	

	/**
	 * Get the news items to display
	 * 
	 * @param object $params Reference to the module parameters
	 * @return array The news items
	 */
	function getList( &$params )
	{
		// get the params
		$items 			 = (int) $params->get( 'items', 5 );
		$secids 	 	 = trim( $params->get( 'secids', '' ) );
		$catids 		 = trim( $params->get( 'catids', '' ) );
		$default_message = trim( $params->get( 'defaultmessage', '' ) );

		$db =& JFactory::getDBO();
		$query = "SELECT id, catid, sectionid, title, publish_down FROM #__content WHERE catid IN ($catids) AND sectionid IN ($secids) AND state = 1 AND (publish_down > NOW() || publish_down = '0000-00-00 00:00:00') ORDER BY created DESC";
		$db->setQuery( $query, 0, $items );
		$rows = $db->loadObjectList();
		
		$list = array();
		
		if ( empty( $rows ) ) {
			$list[] = $default_message;
		}
		else {
			foreach ( $rows as $row )
			{
				$list[] = '<a href="'.JRoute::_( ContentHelperRoute::getArticleRoute( $row->id, $row->catid, $row->sectionid ) ).'">'.htmlspecialchars( $row->title ).'</a>'; 
			}
		}
		return $list;
	} // end function getList
} // end class modAmpNewsTicker
?>