<?php
/**
* @package Mambo Open Source
* @copyright (C) 2005 - 2006 Mambo Foundation Inc.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*
* Mambo was originally developed by Miro (www.miro.com.au) in 2000. Miro assigned the copyright in Mambo to The Mambo Foundation in 2005 to ensure
* that Mambo remained free Open Source software owned and managed by the community.
* Mambo is Free Software
*/ 

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

class modules_html {

	function module( &$module, &$params, $Itemid, $style=0 ) {
		global $mosConfig_live_site, $mosConfig_sitename, $mosConfig_lang;
		$mosConfig_absolute_path = mamboCore::get('mosConfig_absolute_path');
		// custom module params
		$rssurl 			= $params->get( 'rssurl' );
		$rssitems 			= $params->get( 'rssitems', 5 );
		$rssdesc 			= $params->get( 'rssdesc', 1 );
		$rssimage 			= $params->get( 'rssimage', 1 );
		$rssitemdesc		= $params->get( 'rssitemdesc', 1 );
		$moduleclass_sfx 	= $params->get( 'moduleclass_sfx' );
		$words 				= $params->def( 'word_count', 0 );

		if ($style == -1 && !$rssurl) {
			echo $module->content;
			return;
		} else {
			?>
			<table cellpadding="0" cellspacing="0" class="moduletable<?php echo $moduleclass_sfx; ?>">
			<?php
			if ( $module->showtitle != 0 ) {
				?>
				<tr>
					<th valign="top">
					<?php echo $module->title; ?>
					</th>
				</tr>
				<?php
			}

			if ( $module->content ) {
				?>
				<tr>
					<td>
					<?php echo $module->content; ?>
					</td>
				</tr>
				<?php
			}
		}
		// feed output
		if ( $rssurl ) {
			if (!defined('MAGPIE_CACHE_DIR')) define ('MAGPIE_CACHE_DIR', mamboCore::get('mosConfig_absolute_path').'/includes/magpie_cache');
			require_once (mamboCore::get('mosConfig_absolute_path').'/includes/magpierss/rss_fetch.php');
			$rss = fetch_rss($rssurl);
			if (isset($rss->image['title'])) $iTitle = $rss->image['title'];
			if (isset($rss->image['url'])) $iUrl = $rss->image['url'];
			// feed title
			?>
			<tr>
				<td>
				<strong>
				<a href="<?php echo $rss->channel['link']; ?>" target="_blank">
				<?php echo $rss->channel['title']; ?>
				</a>
				</strong>
				</td>
			</tr>
			<?php
			// feed description
			if ( $rssdesc ) {
				?>
				<tr>
					<td>
					<?php echo $rss->channel['description']; ?>
					</td>
				</tr>
				<?php
			}
			// feed image
			if ( $rssimage AND isset($iUrl) ) {
				?>
				<tr>
					<td align="center">
					<image src="<?php echo $iUrl; ?>" alt="<?php echo $iTitle; ?>"/>
					</td>
				</tr>
				<?php
			}
			$itemnumber = 1;
			?>
			<tr>
				<td>
				<ul class="newsfeed<?php echo $moduleclass_sfx; ?>">
			<?php
			foreach ($rss->items as $item) {
				if ($itemnumber > $rssitems) break;
				$itemnumber++;
				// item title
				?>
				<li class="newsfeed<?php echo $moduleclass_sfx; ?>">
				<strong>
				<a href="<?php echo $item['link']; ?>" target="_blank">
				<?php echo $item['title']; ?>
				</a>
				</strong>
				<?php
				// item description
				if ( $rssitemdesc ) {
					// item description
					$text = html_entity_decode( $item['description'] );
						// word limit check
					if ( $words ) {
						$texts = explode( ' ', $text );
						$count = count( $texts );
						if ( $count > $words ) {
							$text = '';
							for( $i=0; $i < $words; $i++ ) {
								$text .= ' '. $texts[$i];
							}
							$text .= '...';
						}
					}
					?>
					<div>
					<?php echo $text; ?>
					</div>
					<?php
				}
				?>
				</li>
				<?php
			}
			?>
			</ul>
			</td>
			</tr>
			<?php
		}
		?>
		</table>
		<?php
	}

	/**
	* @param object
	* @param object
	* @param int The menu item ID
	* @param int -1=show without wrapper and title, -2=x-mambo style
	*/
	function module2( &$module, &$params, $Itemid, $style=0, $count=0 ) {
		global $mosConfig_live_site, $mosConfig_sitename, $mosConfig_lang;
		global $mainframe, $database, $my;
		$mosConfig_absolute_path = mamboCore::get('mosConfig_absolute_path');
		$moduleclass_sfx 		= $params->get( 'moduleclass_sfx' );
		// check for custom language file
/*
		$path = $mosConfig_absolute_path . '/modules/' . $module->module . mamboCore::get('mosConfig_lang') . '.php';
		if (file_exists( $path )) {
			include( $path );
		} else {
			$path = $mosConfig_absolute_path .'/modules/'. $module->module .'.eng.php';
			if (file_exists( $path )) {
				include( $path );
			}
		}
*/
		$number = '';
		if ($count > 0) {
			$number = '<span>' . $count . '</span> ';
		}
		if ($style == -3) {
			// allows for rounded corners
			echo "\n<div class=\"module$moduleclass_sfx\"><div><div><div>";
			if ($module->showtitle != 0) {
				echo "<h3>$module->title</h3>";
			}
 			echo "\n";

			include( $mosConfig_absolute_path .'/modules/'. $module->module .'.php' );
			if (isset( $content)) {
				echo $content;
			}

			echo "\n";
			echo "\n</div></div></div></div>\n";
		} else if ($style == -2) {
			// x-mambo (divs and font headder tags)
			?>
			<div class="moduletable<?php echo $moduleclass_sfx; ?>">
			<?php
			if ($module->showtitle != 0) {
				//echo $number;
				?>
				<h3>
				<?php echo $module->title; ?>
				</h3>
				<?php
			}
			include( $mosConfig_absolute_path .'/modules/'. $module->module .'.php' );

			if (isset( $content)) {
				echo $content;
			}
			?>
			</div>
			<?php
		} else if ($style == -1) {
			// show a naked module - no wrapper and no title
			include( $mosConfig_absolute_path .'/modules/'. $module->module .'.php' );

			if (isset( $content)) {
				echo $content;
			}
		} else {
			?>
			<table cellpadding="0" cellspacing="0" class="moduletable<?php echo $moduleclass_sfx; ?>">
			<?php
			if ( $module->showtitle != 0 ) {
				?>
				<tr>
					<th valign="top">
					<?php //echo $number; ?>
					<?php echo $module->title; ?>
					</th>
				</tr>
				<?php
			}
			?>
			<tr>
				<td>
				<?php
				include( $mosConfig_absolute_path . '/modules/' . $module->module . '.php' );
				if (isset( $content)) {
					echo $content;
				}
				?>
				</td>
			</tr>
			</table>
			<?php
		}
	}
}
?>
