<?php


// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
	$FOOTER_SHOPPING=true;
	$pview=isset($HTTP_GET_VARS["pview"])?(int)$HTTP_GET_VARS["pview"]:'2';
	if (!($pview>=1 && $pview<=3)){
		$pview=isset($HTTP_SESSION_VARS["product_view"])?(int)$HTTP_SESSION_VARS["product_view"]:'3';
	}
	$default_view=true;
	if ($pview>=1 && $pview<=3){
		if (!tep_session_is_registered("product_view")) tep_session_register("product_view");
		tep_set_session_value("product_view",$pview);
		$template_content=tep_load_template_content('subscription_listing.tpl.php');
		if ($template_content!="") $default_view=false;
	}
	if ($default_view){ 
?>

<table border="0" width="100%" cellspacing="0" cellpadding="<?php echo $CELLPADDING_SUB;?>">
<?php
// BOF: Lango Added for template MOD
if (SHOW_HEADING_TITLE_ORIGINAL == 'yes') {
$header_text = '&nbsp;' 
//EOF: Lango Added for template MOD
?>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
                        <td class="pageHeading">
             <?php 
    // Get the category name and description
    $category_query = tep_db_query("select sucd.subscription_categories_name, sucd.subscription_categories_heading_title, sucd.subscription_categories_description, suc.subscription_categories_image from " . TABLE_SUBSCRIPTION_CATEGORIES . " suc, " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sucd where suc.subscription_categories_id = '" . $current_subscription_category_id . "' and sucd.subscription_categories_id = '" . $current_subscription_category_id . "' and sucd.language_id = '" . $languages_id . "'");
	$category = tep_db_fetch_array($category_query);

               if ( (ALLOW_CATEGORY_DESCRIPTIONS == 'true') && (tep_not_null($category['categories_heading_title'])) ) {
                 echo $category['categories_heading_title'];
               } else {
                 echo HEADING_TITLE;
               }
             ?>
            </td>
<?php
// optional Product List Filter
    if (PRODUCT_LIST_FILTER > 0) {
      if (isset($HTTP_GET_VARS['manufacturers_id'])) {
        $filterlist_sql = "select distinct c.categories_id as id, cd.categories_name as name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where p.products_status = '1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and p2c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' and p.manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "' order by cd.categories_name";
      } else {
        $filterlist_sql= "select distinct m.manufacturers_id as id, m.manufacturers_name as name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_MANUFACTURERS . " m where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$current_subscription_category_id . "' order by m.manufacturers_name";
      }
      $filterlist_query = tep_db_query($filterlist_sql);
      if (tep_db_num_rows($filterlist_query) > 1) {
        echo '            <td align="center" class="main">' . tep_draw_form('filter', FILENAME_DEFAULT, 'get') . TEXT_SHOW . '&nbsp;';
        if (isset($HTTP_GET_VARS['manufacturers_id'])) {
          echo tep_draw_hidden_field('manufacturers_id', $HTTP_GET_VARS['manufacturers_id']);
          $options = array(array('id' => '', 'text' => TEXT_ALL_CATEGORIES));
        } else {
          echo tep_draw_hidden_field('cPath', $cPath);
          $options = array(array('id' => '', 'text' => TEXT_ALL_MANUFACTURERS));
        }
        echo tep_draw_hidden_field('sort', $HTTP_GET_VARS['sort']);
        while ($filterlist = tep_db_fetch_array($filterlist_query)) {
          $options[] = array('id' => $filterlist['id'], 'text' => $filterlist['name']);
        }
        echo tep_draw_pull_down_menu('filter_id', $options, (isset($HTTP_GET_VARS['filter_id']) ? $HTTP_GET_VARS['filter_id'] : ''), 'onchange="this.form.submit()"');
        echo '</form></td>' . "\n";
      }
    }

// Get the right image for the top-right
    $image = DIR_WS_IMAGES . 'table_background_list.gif';
    if (isset($HTTP_GET_VARS['manufacturers_id'])) {
      $image = tep_db_query("select manufacturers_image from " . TABLE_MANUFACTURERS . " where manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "'");
      $image = tep_db_fetch_array($image);
      $image = $image['manufacturers_image'];
    } elseif ($current_category_id) {
      $image = tep_db_query("select categories_image from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'");
      $image = tep_db_fetch_array($image);
      $image = $image['categories_image'];
    }
?>
      <td align="right"><?php echo tep_image(DIR_WS_IMAGES . $image, HEADING_TITLE, HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
	  <?php if ( (ALLOW_CATEGORY_DESCRIPTIONS == 'true') && (tep_not_null($category['categories_description'])) ) { ?>
	  <tr>
            <td align="left" colspan="2" class="category_desc"><?php echo $category['categories_description']; ?></td>
	  </tr>
	  <?php } ?>
        </table></td>
      </tr>
<?php
}else{ 
$header_text =  HEADING_TITLE;
}
	if(!isset($hide_row_top)) {
?>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', $CONTENT_TOP_HEIGHT); ?></td>
      </tr>
<?php
	}
// BOF: Lango Added for template MOD
if (MAIN_TABLE_BORDER == 'yes'){

table_image_border_top(false, false, $header_text);
}
// EOF: Lango Added for template MOD
?>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', 10); ?></td>
      </tr>
      <tr>
        <td><?php 
			include(DIR_WS_MODULES . FILENAME_SUBSCRIPTION_LISTING_COL); ?></td>
      </tr>
<?php
// BOF: Lango Added for template MOD
if (MAIN_TABLE_BORDER == 'yes'){
table_image_border_bottom();
}
// EOF: Lango Added for template MOD
?>
    </table>
<?php } else { 
		// set some default values
		switch($pview){
			case 1:
				$timg_width=70;
				$timg_height=70;
				$trow_height=70;
				$tdesc_length=300;
				$tmax_rows=MAX_DISPLAY_SEARCH_RESULTS;
				$ttitle_class="title";
				$tprice_class="priceText";
				$ttotal_cols=2;
				$trow_gap=15;
				break;
			case 2:
				$timg_width=SMALL_IMAGE_WIDTH;
				$timg_height=SMALL_IMAGE_HEIGHT;
				$trow_height=90;
				$tdesc_length=300;
				$tmax_rows=(int)round(MAX_DISPLAY_SEARCH_RESULTS/2);
				$ttitle_class="title2";
				$tprice_class="priceText2";
				$ttotal_cols="1";
				$trow_gap=15;
				break;
			default:
				$timg_width=120;
				$timg_height=120;
				$trow_height=40;
				$tdesc_length=60;
				$ttitle_class="title3";
				$tprice_class="priceText3";
				$ttotal_cols="1";
				$trow_gap=20;
				$tmax_rows=MAX_DISPLAY_SEARCH_RESULTS;
		}
		if($ttotal_cols!='') $tcols_width=sprintf(" width=%s%%",(100/$ttotal_cols));
		
		
		$template_details['VALUE_HEADING_ICON']=tep_template_image('arrow_1.gif','','align="absmiddle"') . '&nbsp;';
		$template_details['VALUE_HEADING_TITLE']=HEADING_TITLE;
		$template_details['VALUE_ARROW']=tep_template_image("arrow_1.gif",'','','align="absmiddle"');
		$template_details['VALUE_VIEWS']=TEXT_PRODUCT_VIEWS;
		$template_details["VALUE_LIST_ROW_HEIGHT"]=$trow_height;
		$template_details["VALUE_LIST_IMAGE_WIDTH"]=$timg_width;
		$template_details["VALUE_TITLE_CLASS"]=$ttitle_class;
		$template_details["VALUE_PRICE_CLASS"]=$tprice_class;
		$template_details["VALUE_NO_SUBSCRIPTION"]='No Subscriptions';
		$template_details["VALUE_ROW_GAP"]=$trow_gap;
		for($icnt=1;$icnt<=3;$icnt++){
			$temp_image='ico_view' .$icnt;
			if ($pview==$icnt){
				$template_details["VALUE_VIEW" . $icnt]=tep_template_image($temp_image . "_select.gif");
			} else {
				$template_details["VALUE_VIEW" . $icnt]='<a href="' . tep_href_link(FILENAME_DEFAULT,tep_get_all_get_params(array('pview','page')) . 'pview=' . $icnt) . '">' . tep_template_image($temp_image . ".gif") . '</a>';
			}
		}
		// Get the category name and description
		$category_query = tep_db_query("select sucd.subscription_categories_name, sucd.subscription_categories_heading_title, sucd.subscription_categories_description, suc.subscription_categories_image from " . TABLE_SUBSCRIPTION_CATEGORIES . " suc, " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sucd where suc.subscription_categories_id = '" . $current_subscription_category_id . "' and sucd.subscription_categories_id = '" . $current_subscription_category_id . "' and sucd.language_id = '" . $languages_id . "'");
		$category = tep_db_fetch_array($category_query);

		$template_details["VALUE_CATEGORY_TITLE"]=($category["subscription_categories_heading_title"]!=''?$category["subscription_categories_heading_title"]:$category["subscription_categories_name"]);
		$template_details["VALUE_CATEGORY_INFO"]='(' . $current_subscription_category_id . ')';
		
		$listing_split = new splitPageResults($listing_sql, $tmax_rows, 'su.subscription_id');
		if ($listing_split->number_of_rows > 0){
			$template_details["VALUE_PAGE_NAVIGATION_TEXT"]=$listing_split->display_count(TEXT_DISPLAY_NUMBER_OF_SUBSCRIPTIONS);
			$template_details["SECTION_PAGE_NAVIGATION"]=1;
			$template_details["VALUE_PAGE_NAVIGATION_LINKS"]=TEXT_RESULT_PAGE . ' ' . $listing_split->display_links(MAX_DISPLAY_PAGE_LINKS, tep_get_all_get_params(array('page', 'info', 'x', 'y')));
			$subscription_repeat=array();
			$icnt=0;
			$listing_query=tep_db_query($listing_split->sql_query);
			while ($listing = tep_db_fetch_array($listing_query)) {

				$subscription_repeat[$icnt]=array('NAME'=>'','PRICE'=>'','IMAGE'=>'');
				$subscription_repeat[$icnt]['NAME']='<a href="' . tep_href_link(FILENAME_SUBSCRIPTIONS_INFO, ($sucPath ? 'sucPath=' . $sucPath . '&' : '') . 'subscription_id=' . $listing['subscription_id']) . '">' . $listing['subscription_name'] . '</a>';
				$subscription_repeat[$icnt]['PRICE'] = $currencies->display_price($listing['subscription_costs'], tep_get_tax_rate($listing['subscription_tax_class_id']));
				if(file_exists(DIR_WS_IMAGES . $listing['subscription_image']) && $listing['subscription_image']!=''){
					$subscription_repeat[$icnt]['IMAGE']='<a href="' . tep_href_link(FILENAME_SUBSCRIPTIONS_INFO, ($sucPath ? 'sucPath=' . $sucPath . '&' : '') . 'subscription_id=' . $listing['subscription_id']) . '">' . tep_image(DIR_WS_IMAGES . $listing['subscription_image'], $listing['subscription_name'], $timg_width) . '</a>';
				}else{
					$subscription_repeat[$icnt]['IMAGE']=tep_image(DIR_WS_IMAGES . 'pixel_trans.gif', $listing['subscription_name'], $timg_width);
				}

				$subscription_description=strip_tags($listing['subscription_description'],'<br>');
				$subscription_repeat[$icnt]['DESCRIPTION'] = substr($subscription_description,0,$tdesc_length);
				$icnt++;
			}
			$template_details["REPEAT_SUBSCRIPTION_LIST"]=$subscription_repeat;
			unset($subscription_repeat);
			mysql_free_result($listing_query);
			$template_details["SECTION_SUBSCRIPTION"]=1;
			$template_details["SECTION_NO_SUBSCRIPTION"]=0;
		} else {
			$template_details["VALUE_PAGE_NAVIGATION_TEXT"]='';
			$template_details["SECTION_PAGE_NAVIGATION"]=0;
			$template_details["SECTION_SUBSCRIPTION"]=0;
			$template_details["SECTION_NO_SUBSCRIPTION"]=1;
		}
		$template_details["VALUE_SUB_PADDING"]=0;
		$replaced_content=$template_content;
		if(tep_session_is_registered("customer_id")){
			$template_sql = "select customers_selected_template from " . TABLE_CUSTOMERS . " where customers_id='" . $HTTP_SESSION_VARS['customer_id'] . "'";
			$template_query = tep_db_query($template_sql);
			$template_result = tep_db_fetch_array($template_query);
			$template_name = $template_result['customers_selected_template'];
		}

		while(list($key,$value)=each($template_details)){
			switch(substr($key,0,strpos($key,'_'))){ 
				case "VALUE":
					$replaced_content=str_replace("{{" . $key . "}}",$value,$replaced_content);
					break;
				case "SECTION":
					if ($value!=1){
						//$replaced_content=preg_replace("/\{\{" . $key ."_START\}\}((.|\n)*)\{\{" . $key . "_END\}\}/","",$replaced_content);
						$start_pos=strpos($replaced_content,"{{" . $key . "_START}}");
						$end_pos=strpos($replaced_content,"{{" . $key . "_END}}");
						$temp_content=substr($replaced_content,0,$start_pos);
						$temp_content.=substr($replaced_content,$end_pos+strlen("{{" . $key ."_END}}"));
						$replaced_content=$temp_content;
						unset($temp_content);
					} else {
						$replaced_content=str_replace(array("{{" .$key . "_START}}","{{" .$key . "_END}}"),"",$replaced_content);
					}
					break;
				case "REPEAT":
					$start_pos=strpos($replaced_content,"{{" . $key . "_START}}")+strlen("{{" . $key ."_START}}");
					$end_pos=strpos($replaced_content,"{{" . $key . "_END}}");
					$repeat_content=substr($replaced_content,$start_pos,$end_pos-$start_pos);
					$merged_total_content='<table cellpadding="0" cellspacing="0" width="100%" border="0">' . "\n";
					$col=0;
					$row=0;
					for ($icnt=0,$n=count($value);$icnt<$n;$icnt++){
						$merged_content='';
						if ($col==0 || $ttotal_cols==1) $merged_content.='<tr>';
						if(strtolower($template_name)=="carshop"){
							$merged_content.='<td class="main_bg" valign="top"' . ($row==0?$tcols_width:''). '>' . $repeat_content;
						}else{
							$merged_content.='<td valign="top"' . ($row==0?$tcols_width:''). '>' . $repeat_content;
						}

						reset($value[$icnt]);
						while(list($itemkey,)=each($value[$icnt])){
							$merged_content=str_replace("{{" . $key . "_" . $itemkey . "}}",$value[$icnt][$itemkey],$merged_content);
						}
						$col++;
						if ($col==$ttotal_cols) {
							$col=0;
							$row++;
						}
						if ($col==0 || $ttotal_cols==1) $merged_content.='</tr><tr><td class="products_bottom" colspan="2"></td></tr><tr height="10"></tr>';
						$merged_total_content.="\n" . $merged_content;
					}
					$merged_total_content.="</table>";
					//$replaced_content=preg_replace("/\{\{" . $key ."_START\}\}((.|\n)*)\{\{" . $key . "_END\}\}/",$merged_total_content,$replaced_content);
					$replaced_content=substr($replaced_content,0,$start_pos) . $merged_total_content . substr($replaced_content,$end_pos); 
					unset($merged_total_content);
					$replaced_content=str_replace(array("{{" .$key . "_START}}","{{" .$key . "_END}}"),"",$replaced_content);
					break;
			}
		}

		echo $replaced_content;
		unset($replaced_content);
	}

 ?>

