<?php
/*
	Freeway eCommerce
	http://www.openfreeway.org
	Copyright (c) 2007 ZacWare

	Released under the GNU General Public License 
*/	
// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
	$MAINPAGE_COLS=(isset($MAINPAGE_COLS)?$MAINPAGE_COLS:1);
	$main_page_cols_width=(100/$MAINPAGE_COLS) . "%";
?>

	
<?php
//echo "HELLO WORLD";
	//NEW redirect 
	if(!defined('REDIRECT_URL'))define('REDIRECT_URL', 'index.php');
	
	if( !empty( REDIRECT_URL ) && REDIRECT_URL === true ){ 

	
	
	if(REDIRECT_URL !='')
	{

		tep_redirect(tep_href_link(REDIRECT_URL, '', 'SSL'));
	}
	
	
	}

?>
<!-- Start Modules Display -->
<?php 
include(DIR_WS_MODULES . "mainpage.php");
$loaded_modules=array('mainpage.php'=>1);
?>

<?php 
if (SHOW_MAIN_FEATURED_PRODUCTS=='true')
{
$featured_products="featured_products.php";
}
if (SHOW_MAIN_FEATURED_CATEGORIES=='true')
{
$featured_categories="featured_categories.php";
}

define('INCLUDE_MODULE_TWO','featured_categories.php');
define('INCLUDE_MODULE_THREE','featured_products.php');
define('INCLUDE_MODULE_FOUR','information.php');
define('INCLUDE_MODULE_FIVE','');
define('INCLUDE_MODULE_SIX','');

	$include_modules=array('TWO'=>$featured_categories,'THREE'=>$featured_products,'FOUR'=>'information.php','FIVE'=>'','SIX'=>'');
	$count=array('TWO','THREE','FOUR','FIVE','SIX');
	$loaded_modules=array();
	for($icnt=0,$n=sizeof($count);$icnt<$n;$icnt++)
	{
		$module=trim(constant('INCLUDE_MODULE_' . $count[$icnt]));
		if($module !='' && file_exists(DIR_FS_CATALOG . DIR_WS_TEMPLATES . TEMPLATE_NAME . "/content/" . $module))

		$loaded_modules[]=$module;
	}
	if(sizeof($loaded_modules) <4)
	{
	//FOREACH
	while(list(,$module) = each($include_modules))
	{
		if(!in_array($module,$loaded_modules) && $module!='')
		{
			$loaded_modules[]=$module;
		}	
	}
	}
	$icnt=0;	
	reset($loaded_modules);

	//reset($include_modules);
	//while(list($key,$module)=each($include_modules)){
	for($jcnt=0,$n=sizeof($loaded_modules);$jcnt<$n;$jcnt++)
	{
		$module_filename=$loaded_modules[$jcnt];
		//$module_filename=$module;
		//$module=trim(constant('INCLUDE_MODULE_' . $key));
		
		//if (tep_not_null($module) && $module!='')  $module_filename=$module;
		//if ($module_filename=='') continue;

		//if (isset($loaded_modules[$module_filename])) continue;
		$content=tep_load_template_content($module_filename);
		//$loaded_modules[$module_filename]=1;
		if (file_exists(DIR_WS_LANGUAGES . $FSESSION->language . "/" . $module_filename))
		{
			require DIR_WS_LANGUAGES . $FSESSION->language . "/" . $module_filename;
		}

		if ($content!='')
		{
			// $row_class="rowOdd";
			// if ($module_filename=='information.php') $row_class="rowEven";
			// $replace_array=array("VALUE_COL_CLASS"=>$row_class);
			// $replace_array['VALUE_TITLE_ICON']=tep_template_image('arrow2.gif','','align=absmiddle') . '&nbsp;';
			// $replace_array['VALUE_SEEALL_ICON']=tep_template_image('arrow3.gif','','align=absmiddle') . '&nbsp;';
			// $replace_array['VALUE_INFO_IMAGE']=tep_template_image('arrow3.gif','','align=absmiddle') . '&nbsp;';
			// $replace_array['VALUE_FAG_IMAGE']=tep_template_image('arrow3.gif','','align=absmiddle') . '&nbsp;';
		
			switch($module_filename)
			{
				case "featured_products.php":
				case 'featured_categories.php':

					$customer_exists = '';
					if($FSESSION->is_registered('customer_id'))
					$customer_exists = "OR(p.restrict_to_customers like '%," . $FSESSION->customer_id . ",%' OR p.restrict_to_groups like '%," . $customers_groups_id . ",%')";
					if ($module_filename=='featured_products.php')
					{
						$items_sql = "select p.products_id, pd.products_name, pd.products_description, p.products_image_1,p.products_title_1,p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, p.products_date_added, m.manufacturers_name,p.products_status from " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id left join " . TABLE_PRODUCTS_DESCRIPTION . " pd on p.products_id = pd.products_id left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id left join " . TABLE_FEATURED . " f on p.products_id = f.products_id and pd.language_id = '" . (int)$FSESSION->languages_id . "' where p.products_status = '1' and f.status = '1' " . 
							" and (" . 
									"(p.restrict_to_customers = '' OR ISNULL(p.restrict_to_customers))" . 
									"and(p.restrict_to_groups = '' OR ISNULL(p.restrict_to_groups))" . $customer_exists . 
							") order by rand($mtm) limit 4";
						$items_query = tep_db_query($items_sql);
						$replace_array['VALUE_TITLE']=HEADING_FEATURED_PRODUCTS;
						
						
					if ($module_filename=='featured_categories.php')
					{
						$items_sql = "select c.categories_status,c.categories_id, cd.categories_name,cd.categories_heading_title, c.categories_image, c.parent_id, cd.categories_description, cd.concert_venue,cd.concert_date,cd.concert_time from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_status > '0' and c.parent_id = '" . (int)$current_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$FSESSION->languages_id . "' order by sort_order, cd.categories_id " . 
							" and (" . 
									"(p.restrict_to_customers = '' OR ISNULL(p.restrict_to_customers))" . 
									"and(p.restrict_to_groups = '' OR ISNULL(p.restrict_to_groups))" . $customer_exists . 
							") order by rand($mtm) limit 4";
						$items_query = tep_db_query($items_sql);
						$replace_array['VALUE_TITLE']=HEADING_FEATURED_PRODUCTS;
					}
					
					
					} else 
					{
						$items_sql = "select p.products_id, pd.products_description, pd.products_name, p.products_image_1, p.products_title_1, p.products_price, p.products_tax_class_id, p.products_date_added,  p.products_date_available as date_expected,p.products_status from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where products_date_available >'" . getServerDate() . "' and p.products_id = pd.products_id  and pd.language_id = '" . (int)$FSESSION->languages_id . "' " . 
							" and (" . 
									"(p.restrict_to_customers = '' OR ISNULL(p.restrict_to_customers))" . 
									"and(p.restrict_to_groups = '' OR ISNULL(p.restrict_to_groups))" . $customer_exists . 
							") order by rand() limit 4 ";
						$items_query = tep_db_query($items_sql);
						$replace_array['VALUE_TITLE']=HEADING_NEW_PRODUCTS;	
					}
					if (tep_db_num_rows($items_query)<=0) continue 2;
	
					$replace_array['VALUE_SEE_ALL']='<a href="'.tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT,'inc_product=1&inc_subcat_product=1&sort=1&page=1').'">'.HEADING_SEE_ALL.'</a>';
					
					$temp_items=array();
					while ($items_result = tep_db_fetch_array($items_query)) 
					{
						if ($items_result['specials_new_products_price']) 
						{
						$products_price = '<s>' .  $currencies->display_price($items_result['products_price'], tep_get_tax_rate($items_result['products_tax_class_id']),1,true) . '</s>&nbsp;&nbsp;<span class="productSpecialPrice">' . $currencies->display_price($items_result['specials_new_products_price'], tep_get_tax_rate($items_result['products_tax_class_id'])) . '</span>';
						} else 
						{
						$products_price = $currencies->display_price($items_result['products_price'], tep_get_tax_rate($items_result['products_tax_class_id']),1,true);
						}
						$pstatus=$items_result['products_status'];
						
						$description=substr(strip_tags($items_result['products_description']),0,150);
						$temp_items[] = array(	'ID' => $items_result['products_id'],
											'NAME' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $items_result['products_id'], 'NONSSL') . '">' . $items_result['products_name'] . '</a>',
											'IMAGE' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $items_result['products_id'], 'NONSSL') . '">' . tep_product_small_image($items_result['products_image_1'],$items_result['products_title_1']) . '</a>',
											'PRICE' => $products_price,
											'DESCRIPTION' => $description
										);
					}
					$replace_array['VALUE_ITEM_IMAGE_WIDTH']=SMALL_IMAGE_WIDTH;
					$replace_array['REPEAT_ITEMS']=$temp_items;
					unset($temp_items);
					break;
					
					
				//case 'information.php':
					// $info_query = tep_db_query("select md.page_name, md.page_id from " . TABLE_MAINPAGE . " m, " . TABLE_MAINPAGE_DESCRIPTIONS . " md where m.parent_id = '0' and m.page_id=md.page_id and m.page_status=1 and md.language_id='" . (int)$FSESSION->languages_id ."' order by sort_order, md.page_name limit 4");
					// if (tep_db_num_rows($info_query)<=0) continue 2;
					// $replace_array['VALUE_TITLE']=HEADING_INFORMATION;
					// while($info_result=tep_db_fetch_array($info_query))
					// {
						// $temp_items[]=array('NAME'=>'<a href="' . tep_href_link(FILENAME_DEFAULT,'stcPath=' . $info_result["page_id"]) . '">' . $info_result['page_name'] . '</a>');
					// }
					// $temp_items[]=array('NAME'=>'<a href="' . tep_href_link(FILENAME_CONTACT_US) . '">Contact us</a>');
					// $replace_array['REPEAT_ITEMS']=$temp_items;
					// unset($temp_items);
					// break;
			}
			replace_content($content,$replace_array);
		} 
		if ($icnt%$MAINPAGE_COLS==0)
		{
			if ($icnt>1) '';
		}
		if ($content!='')
		{
		echo '<!--mod--><div>' . $content . '</div>';
		} 
		else 
		{ 
			include(DIR_WS_MODULES . $module_filename);
		}
		$icnt++;
	}
?>