<?php

/*

// "Frontpage Slideshow" Module for Joomla! 1.5.x - Version 1.7.1

// Copyright (c) 2006 - 2008 JoomlaWorks, a Komrade LLC company.

// This code cannot be redistributed without permission from JoomlaWorks - http://www.joomlaworks.gr.

// More info at http://www.joomlaworks.gr and http://www.frontpageslideshow.net

// Developers: Fotis Evangelou - George Chouliaras

// ***Last update: February 27th, 2008***

*/



// no direct access

defined('_JEXEC') or die('Restricted access');



global $mainframe;



require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');



$db =& JFactory::getDBO();

$user =& JFactory::getUser();



// Module Parameters

$moduleclass_sfx = $params->get('moduleclass_sfx','');

$fpss_template = $params->get('fpss_template','Default');

$hide_nav = $params->get('hide_nav',0);

$sidebar_width = intval($params->get('sidebar_width',140));

$striptags = $params->get('striptags',0);

$allowed_tags = $params->get('allowed_tags',"<br><br /><a><b><i><u><span>"); // these tags will NOT be stripped off!

$engine = $params->get('engine','jquery');

$disablelib = $params->get('disablelib',0);

$optimizejs = $params->get('optimizejs',0);

$mtCTRloadingTime = $params->get('mtCTRloadingTime',500);

$mtCTRtransitionText = $params->get('mtCTRtransitionText',1000);

$mtCTRrotateAction = $params->get('mtCTRrotateAction','click');

$mtCTRtext_effect = $params->get('mtCTRtext_effect',0);

if ($mtCTRtext_effect) {$mtCTRtext_effect == 'true';} else {$mtCTRtext_effect == 'false';}

$random = $params->get('random',0);

$limitslides = $params->get('limitslides');

$chars = $params->get('chars');

$words = $params->get('words');

$delay = $params->get('delay',6000);

$speed = $params->get('speed',1000);

$width = $params->get('width',480);

$height = $params->get('height',280);

$seperator = $params->get('seperator','>>');



// Group Navigation

if ($fpss_template=='JJ-Obs' || $fpss_template=='JJ-Rasper') { $groupnav = 1; } else { $groupnav = 0; }



// Compatibility check

$fpss = "com_fpslideshow";



$nullDate	= $db->getNullDate();

jimport('joomla.utilities.date');

$date = new JDate();

$now = $date->toMySQL();



$contentConfig 	= &JComponentHelper::getParams( 'com_content' );

$access			= !$contentConfig->get('shownoauth');

$aid			= $user->get('aid', 0);



$qaccess = $access ? "\n AND a.access <= ".(int) $aid." AND cc.access <= ".(int) $aid." AND s.access <= ".(int) $aid." " : '';



// CHECK IF COMPONENT EXIST

$db->setQuery("SELECT `id` FROM `#__components`"

.	"\nWHERE `option`='com_fpslideshow'"

.	"\nLIMIT 1");

$exist = $db->loadResult();



if($exist) {



	if (file_exists(JPATH_SITE.'/components/'.$fpss.'/fpslideshow.class.php')) {

		include_once (JPATH_SITE.'/components/'.$fpss.'/fpslideshow.class.php');

	}

	if(!isset($MOD_FPSS_CONFIG)) {

		$MOD_FPSS_CONFIG =& $fpss_config;

	}



	// FIND WHAT CONTENT ITEMS WE CAN SEE

	$query = "SELECT a.id, a.introtext, a.title, s.title as stitle, cc.title as ctitle, "

	. "\n CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\":\", a.id, a.alias) ELSE a.id END as slug,"

	. "\n CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(\":\", cc.id, cc.alias) ELSE cc.id END as catslug"

	. "\n FROM #__content AS a"

	. "\n INNER JOIN #__categories AS cc ON cc.id = a.catid"

	. "\n INNER JOIN #__sections AS s ON s.id = a.sectionid"

	. "\n WHERE ( a.state = 1 AND a.sectionid > 0 )"

	. "\n AND ( a.publish_up = '$nullDate' OR a.publish_up <= '$now' )"

	. "\n AND ( a.publish_down = '$nullDate' OR a.publish_down >= '$now' )"

	. $qaccess

	. "\n AND s.published = 1"

	. "\n AND cc.published = 1"

	. "\n ORDER BY a.created DESC"

	;



	$db->setQuery( $query );

	$contents_tmp = $db->loadObjectList();

	$okItems_tmp = array();

	$idArray = array();

	$catidArray = array();

	$secidArray = array();

	$titleTextArray = array();

	$introTextArray = array();

	$sectionTitleArray = array();

	$categoryTitleArray = array();

	foreach ($contents_tmp as $content_tmp) {

		$okItems_tmp[] = $content_tmp->id;

		$idArray[$content_tmp->id] = $content_tmp->slug;

		$catidArray[$content_tmp->id] = $content_tmp->catslug;

		$secidArray[$content_tmp->id] = $content_tmp->sectionid;

		$titleTextArray[$content_tmp->id] = $content_tmp->title;

		$introTextArray[$content_tmp->id] = $content_tmp->introtext;

		$sectionTitleArray[$content_tmp->id] = $content_tmp->stitle;

		$categoryTitleArray[$content_tmp->id] = $content_tmp->ctitle;

	}

	$okItems = implode(",", $okItems_tmp);



	// ORDERING

	$orderBy = ($random==1)?"\n ORDER BY RAND()":"\n ORDER BY ordering ";



	// FIND WHAT SLIDES WE CAN SEE

	$query = "SELECT *"

	. "\n FROM "._FPSS_TABLE_SLIDES

	. "\n WHERE state = 1"

	. "\n AND registers <= ".intval($gid)

	. "\n AND ( publish_up = '$nullDate' OR publish_up <= '$now' )"

	. "\n AND ( publish_down = '$nullDate' OR publish_down >= '$now' )"

	//. "\n AND menu IN ( $okItems )"

	. $orderBy

	;

	$db->setQuery( $query );

	$images = $db->loadObjectList();



	if ($db->getErrorMsg()) {

		JError::raiseError(500, $db->getErrorMsg() );

	}



	/* --------------------- SLIDE CONTENT --------------------- */



	$html = ''; //init

	// Start loop

	$div_id = 1;

	if($limitslides) {$i = 0;}

	foreach ($images as $image) {

		if($limitslides) { if($i>=$limitslides) continue; }



		// CHECK WHAT CONTENT ITEMS WE CAN SEE

		if($image->menu!=0) {

			if(!in_array($image->menu,$okItems_tmp)) {continue;}

		}



		// Fetch Display Options (set in slide edit page)

		$show_title = intval($image->showtitle);

		$show_section_title = $show_category_title = intval($image->showseccat);

		$show_introtext = intval($image->showcustomtext);

		$show_plaintext = intval($image->showplaintext);

		$show_readmore = intval($image->showreadmore);

		$target = (intval($image->target)>0)?' target="_blank"':'';



		// Hide .fpss-introtext completely if the content is hidden as well

		if (!$show_title && !$show_section_title && !$show_category_title && !$show_introtext && !$show_plaintext && !$show_readmore) {

			$hidecontent = ' style="display:none;"';

		} else {

			$hidecontent = '';

		}



		if($image->menu!=0) {

			// Get link

			$link = JRoute::_(ContentHelperRoute::getArticleRoute($idArray[$image->menu], $catidArray[$image->menu], $secidArray[$image->menu]));

			// GET TITLE OF CONTENT

			$slidetitle = $titleTextArray[$image->menu];

			// GET TITLE OF CATEGORY

			$titleCategory = $categoryTitleArray[$image->menu];

			// GET TITLE OF SECTION

			$titleSection = $sectionTitleArray[$image->menu];

			// GET INTROTEXT AND REMOVE ANY MAMBOT TAGS OR IMAGES

			if($image->ctext) {

				$introtext = preg_replace("/<img.+?>/", "", $image->ctext);

			} else {

				$introtext = preg_replace('/{([a-zA-Z0-9\-_]*)\s*(.*?)}/i','', $introTextArray[$image->menu]); // add outside the if?

				$introtext = preg_replace("/<img.+?>/", "", $introtext);

			}

		} elseif ($image->menu2!=0) {

			// Get link

			$db->setQuery("SELECT link, name FROM #__menu"

			. "\nWHERE id='$image->menu2'"

			. "\nLIMIT 1");

			$menu2array = $db->loadRow();

			$link = JRoute::_( $menu2array[0]."&Itemid=".$image->menu2 );

			$menu2name = $menu2array[1];

			// GET TITLE OF CONTENT

			$slidetitle = $menu2name;

			// GET INTROTEXT AND REMOVE ANY IMAGES

			$introtext = preg_replace("/<img.+?>/", "", $image->ctext);

		} elseif ($image->clink!="") {

			// Get link

			$link = $image->clink;

			// GET TITLE OF CONTENT

			$slidetitle = $image->name;

			// GET INTROTEXT AND REMOVE ANY IMAGES

			$introtext = preg_replace("/<img.+?>/", "", $image->ctext);

		}



		// HTML cleanup

		if ($striptags) {$introtext = strip_tags($introtext, $allowed_tags);}



		// if character limitation is defined

		if ($chars) {

			if(function_exists("mb_string")) {

				$introtext = mb_substr($introtext, 0, $chars).'...';

			} else {

				$introtext = substr($introtext, 0, $chars).'...';

			}

		}



		// if word limitation is defined (v2)

		if (!function_exists('word_limiter')) {

			function word_limiter($str, $limit = 100, $end_char = '&#8230;') {

				if (trim($str) == '')

				return $str;

				preg_match('/\s*(?:\S*\s*){'. (int) $limit .'}/', $str, $matches);

				if (strlen($matches[0]) == strlen($str))

				$end_char = '';

				return rtrim($matches[0]).$end_char;

			}

		}

		if ($words) {

			$introtext = word_limiter($introtext,$words);

		}



		$thecontent = ''; // init

		// Title

		if ($show_title) {

			$thecontent .= "<h1><a".$target." href=\"".$link."\">".$slidetitle."</a></h1>\n";

		}



		// Section/category if applicable

		if($image->menu!=0) {

			if ($show_section_title || $show_category_title) {

				$thecontent .= "<h2>";

				if ($show_section_title) {

					$thecontent .= $titleSection;

					if ($show_category_title) {$thecontent .= ' '.$seperator.' '.$titleCategory;}

				} else {

					if ($show_category_title) {$thecontent .= $titleCategory;}

				}

				$thecontent .= "</h2>\n";

			}

		}



		// Tagline text

		if ($show_plaintext) {

			$thecontent .= "<h3>".strip_tags($image->plaintext)."</h3>\n";

		}

		// Slide text

		if ($show_introtext) {

			$thecontent .= "<p>".$introtext."</p>\n";

		}



		// Slide 'read more' link

		if ($show_readmore) {

			$thecontent .= "<a".$target." href=\"".$link."\" class=\"readon\">".JText::_('MORE')."</a>\n";

		}



		// Output

		$html .= '

					<div class="slide">

						<div class="slide-inner">

							<a'.$target.' class="fpss_img" href="'.$link.'">

								<span>

									<span style="background:url('.$image->path.') no-repeat;">

										<span>

											<img src="'.$image->path.'" alt="'.JText::_('FPSS_MOD_IMGALT').'" />

										</span>

									</span>

								</span>

							</a>

							<div class="fpss-introtext"'.$hidecontent.'>

								<div class="slidetext">'.$thecontent.'</div>

							</div>

						</div>

					</div>

				';



		if($limitslides) {$i++;}

		$div_id++;



	} // end foreach



	/* --------------------- NAVIGATION --------------------- */

	$navhtml = '';

	$div_id = 1;

	if($limitslides) {$j = 0;}

	foreach ($images as $key => $image) {

		if($limitslides) {if($j>=$limitslides) continue;}



		if($image->menu!=0) {

			// Get link

			$link = JRoute::_(ContentHelperRoute::getArticleRoute($idArray[$image->menu], $catidArray[$image->menu], $secidArray[$image->menu]));

			$slidetitle = $titleTextArray[$image->menu]; // GET TITLE OF CONTENT

		} elseif ($image->menu2!=0) {

			// Get link

			$db->setQuery("SELECT link, name FROM #__menu"

			. "\nWHERE id='$image->menu2'"

			. "\nLIMIT 1");

			$menu2array = $db->loadRow();

			$link = JRoute::_( $menu2array[0]."&Itemid=".$image->menu2 );

			$menu2name = $menu2array[1];

			$slidetitle = $menu2name; // GET TITLE OF CONTENT

		} elseif ($image->clink!="") {

			$link = $image->clink; // Get link

			$slidetitle = $image->name; // GET TITLE OF CONTENT

		}



		$tagline = strip_tags($image->plaintext);



		$key = $key + 1;

		if ($key < 10) { $key = "0".$key; }

		$navhtml .= '

					<li>

						<a class="button off navi" href="javascript:void(0);" title="'.JText::_('FPSS_MOD_CLICKNAV').'"';

						if ($mtCTRrotateAction=='mouseover') {$navhtml .= ' onclick="parent.location=\''.$link.'\';return false;"';}

						$navhtml .= '>

							<span class="navbar-img"><img src="'.$image->path.'" alt="'.JText::_('FPSS_MOD_CLICKNAV').'" /></span>

							<span class="navbar-key">'.$key.'</span>

							<span class="navbar-title">'.$slidetitle.'</span>

							<span class="navbar-tagline">'.$tagline.'</span>

							<span class="navbar-clr"></span>

						</a>

					</li>

					';

		if($limitslides) {$j++;}

		$div_id++;



	} // end foreach

	

	?>

	

	<!-- JoomlaWorks "Frontpage Slideshow" v1.7 starts here -->

	<script language="javascript" type="text/javascript">

	<!--

	var embedFPSSCSS = '<' + 'style type="text/css" media="all">'

	+ '@import "modules/mod_fpslideshow/mod_fpslideshow/templates/<?php echo $fpss_template; ?>/template_css.php?w=<?php echo $width; ?>&h=<?php echo $height; ?>&sw=<?php echo $sidebar_width;?>";'

	<?php if ($hide_nav) {$sidebar_width = 0; echo "+ '#navi-outer {display:none;}'"; } ?>

	+ '</' + 'style>';

	document.write(embedFPSSCSS);

	-->

	</script>

	<?php if (!$hide_nav) { ?>

	<!--[if lte IE 7]>

	<style type="text/css" media="all">

	@import "modules/mod_fpslideshow/mod_fpslideshow/templates/<?php echo $fpss_template; ?>/template_css_ie.css";

	</style>

	<![endif]-->

	<?php } ?>

	<?php if($optimizejs) {?>

	<script language="javascript" type="text/javascript" src="modules/mod_fpslideshow/mod_fpslideshow/engines/<?php echo $engine; ?>-fpss.php"></script>

	<?php } else { ?>

	<?php if(!$disablelib) {?>

	<script language="javascript" type="text/javascript" src="modules/mod_fpslideshow/mod_fpslideshow/engines/<?php echo $engine; ?>-comp.js"></script>

	<?php } ?>

	<script language="javascript" type="text/javascript" src="modules/mod_fpslideshow/mod_fpslideshow/engines/<?php echo $engine; ?>-fpss-comp.js"></script>

	<?php } ?>

	<script language="javascript" type="text/javascript">

	<?php if ($engine=='jquery') { ?>

	var speed_delay = <?php echo $delay; ?>;

	var slide_speed = <?php echo $speed; ?>;

	var CTRrotateAction = '<?php echo $mtCTRrotateAction; ?>';

	<?php } ?>

	<?php if ($engine=='mootools') { ?>

	var CTRloadingTime = <?php echo $mtCTRloadingTime; ?>;

	var CTRslideInterval = <?php echo $delay; ?>;

	var CTRtransitionDuration = <?php echo $speed; ?>;

	var CTRtransitionText = <?php echo $mtCTRtransitionText; ?>;

	var CTRrotateAction = '<?php echo $mtCTRrotateAction; ?>';

	var CTRtext_effect = <?php echo $mtCTRtext_effect; ?>;

	<?php } ?>

	</script>

	

	<div id="fpss-outer-container"<?php if ($moduleclass_sfx) {echo ' class="'.$moduleclass_sfx.'"';} ?>>

	    <div id="fpss-container">

	        <div id="fpss-slider">

	            <div id="slide-loading"></div>

	            <div id="slide-wrapper">

	                <div id="slide-outer"><?php echo $html; ?></div>

	            </div>

	        </div>		

	        <div id="navi-outer">

	            <div id="pseudobox"></div>

	            <div class="ul_container">

	                <ul>

	                <?php if ($groupnav) { ?>

		                <?php echo $navhtml; ?>

		                <li class="noimages"><a id="fpss-container_next" href="javascript:void(0);" onclick="showNext();clearSlide();" title="<?php echo JText::_('FPSS_MOD_NEXT'); ?>"></a></li>

		                <li class="noimages"><a id="fpss-container_playButton" href="javascript:void(0);" onclick="playButtonClicked();return false;" title="<?php echo JText::_('FPSS_MOD_PLAYPAUSE'); ?>"><?php echo JText::_('FPSS_MOD_PAUSE'); ?></a>

		                <li class="noimages"><a id="fpss-container_prev" href="javascript:void(0);" onclick="showPrev();clearSlide();" title="<?php echo JText::_('FPSS_MOD_PREV'); ?>"></a></li>

		                <li class="clr"></li>

		            <?php } else { ?>

		                <li class="noimages"><a id="fpss-container_prev" href="javascript:void(0);" onclick="showPrev();clearSlide();" title="<?php echo JText::_('FPSS_MOD_PREV'); ?>">&laquo;</a></li>

		                <?php echo $navhtml; ?>

		                <li class="noimages"><a id="fpss-container_next" href="javascript:void(0);" onclick="showNext();clearSlide();" title="<?php echo JText::_('FPSS_MOD_NEXT'); ?>">&raquo;</a></li>

		                <li class="noimages"><a id="fpss-container_playButton" href="javascript:void(0);" onclick="playButtonClicked();return false;" title="<?php echo JText::_('FPSS_MOD_PLAYPAUSE'); ?>"><?php echo JText::_('FPSS_MOD_PAUSE'); ?></a></li>

		            <?php } ?>

		            </ul>

            	</div>

        		</div> 

    		<div class="fpss-clr"></div>

    	</div>

		<div class="fpss-clr"></div> 

	</div>

	<!-- JoomlaWorks "Frontpage Slideshow" v1.7 ends here -->

	<?php

} else echo JText::_('FPSS_MOD_ALERT');  ?>