 <?php
	$FOOTER_SHOPPING=true;
	$pview=isset($HTTP_GET_VARS["pview"])?(int)$HTTP_GET_VARS["pview"]:'';
	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
    $subs_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 . "'");
	$subs_category = tep_db_fetch_array($subs_category_query);

               if ( (ALLOW_CATEGORY_DESCRIPTIONS == 'true') && (tep_not_null($subs_category['subscription_categories_heading_title'])) ) {
                 echo $subs_category['subscription_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_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_subscription_category_id) {
      $image = tep_db_query("select subscription_categories_image from " . TABLE_SUBSCRIPTION_CATEGORIES . " where subscription_categories_id = '" . (int)$current_subscription_category_id . "'");
      $image = tep_db_fetch_array($image);
      $image = $image['subscription_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($subs_category['subscription_categories_description'])) ) { ?>
	  <tr>
            <td align="left" colspan="2" class="category_desc"><?php echo $subs_category['subscription_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%', '10'); ?></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 
			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=130;
				$tdesc_length=300;
				$tmax_rows=MAX_DISPLAY_SEARCH_RESULTS;
				$ttitle_class="title";
				$ttotal_cols=2;
				break;
			case 2:
				$timg_width=SMALL_IMAGE_WIDTH;
				$timg_height=SMALL_IMAGE_HEIGHT;
				$trow_height=130;
				$tdesc_length=300;
				$tmax_rows=(int)MAX_DISPLAY_SEARCH_RESULTS/2;
				$ttitle_class="title2";
				$ttotal_cols="1";
				break;
			default:
				$timg_width=30;
				$timg_height=30;
				$trow_height=50;
				$tdesc_length=60;
				$ttitle_class="title3";
				$ttotal_cols="1";
				$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_image(DIR_WS_ICONS . "arrow.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;
		for($icnt=1;$icnt<=3;$icnt++){
			$temp_image=DIR_WS_ICONS . 'ico_view' .$icnt;
			if ($pview==$icnt){
				$template_details["VALUE_VIEW" . $icnt]=tep_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')) . 'pview=' . $icnt) . '">' . tep_image($temp_image . ".gif") . '</a>';
			}
		}
		// Get the category name and description
		$category_query = tep_db_query("select cd.categories_name, cd.categories_heading_title, cd.categories_description, c.categories_image from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . $current_category_id . "' and cd.categories_id = '" . $current_category_id . "' and cd.language_id = '" . $languages_id . "'");
		$category = tep_db_fetch_array($category_query);
		$template_details["VALUE_CATEGORY_TITLE"]=$category["categories_heading_title"];
		$template_details["VALUE_CATEGORY_INFO"]='(' . $current_category_id . ')';
		
		$listing_split = new splitPageResults($listing_sql, $tmax_rows, 'p.products_id');
		if ($listing_split->number_of_rows > 0){
			$template_details["VALUE_PAGE_NAVIGATION_TEXT"]=$listing_split->display_count(TEXT_DISPLAY_NUMBER_OF_PRODUCTS);
			$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')));
			$product_repeat=array();
			$icnt=0;
			$listing_query=tep_db_query($listing_split->sql_query);
			while ($listing = tep_db_fetch_array($listing_query)) {
				$product_repeat[$icnt]=array('MODEL'=>'','NAME'=>'','MANUFACTURER'=>'','PRICE'=>'','QUANTITY'=>'','WEIGHT'=>'','IMAGE'=>'','BUY_NOW'=>'','DESCRIPTION'=>'');
				for ($col=0, $n=sizeof($column_list); $col<$n; $col++) {
					switch ($column_list[$col]) {
						case 'PRODUCT_LIST_MODEL':
							$product_repeat[$icnt]['MODEL']='<br>' . $listing['products_model'];
							break;
						case 'PRODUCT_LIST_NAME':
							if (isset($HTTP_GET_VARS['manufacturers_id'])) {
							$product_repeat[$icnt]['NAME']='<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';
							} else {
							$product_repeat[$icnt]['NAME']='<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>&nbsp;';
							}
							break;
						case 'PRODUCT_LIST_MANUFACTURER':
							$product_repeat[$icnt]['MANUFACTURER'] = '<br><a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $listing['manufacturers_id']) . '">' . $listing['manufacturers_name'] . '</a>&nbsp;';
							break;
						case 'PRODUCT_LIST_PRICE':
							if (tep_get_products_special_price($listing['products_id'])) {
							$product_repeat[$icnt]['PRICE'] = '<s>' .  $currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id']),'1',true) . '</s>&nbsp;&nbsp;<span class="productSpecialPrice">' . $currencies->display_price(tep_get_products_special_price($listing['products_id']), tep_get_tax_rate($listing['products_tax_class_id'])) . '</span>';
							
							} else {
							$product_repeat[$icnt]['PRICE'] = $currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id']),'1',true);
							}
							break;
						case 'PRODUCT_LIST_QUANTITY':
							$product_repeat[$icnt]['QUANTITY'] = '<br>' . $listing['products_quantity'];
							break;
						case 'PRODUCT_LIST_WEIGHT':
							$product_repeat[$icnt]['WEIGHT'] = '<br>' . tep_get_display_weight($listing['products_weight']);
							break;
						case 'PRODUCT_LIST_IMAGE':
							if (isset($HTTP_GET_VARS['manufacturers_id'])) {
							$product_repeat[$icnt]['IMAGE'] = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . tep_product_custom_image($listing['products_image_1'], $listing['products_title_1'],"title=" . $listing['products_name'],$img_width,$timg_height) . '</a>';
							} else {
							$product_repeat[$icnt]['IMAGE'] = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . tep_product_custom_image($listing['products_image_1'], $listing['products_title_1'], "title=" . $listing['products_name'],$timg_width,$timg_height) . '</a>';
							}
							break;
						case 'PRODUCT_LIST_BUY':
							$product_repeat[$icnt]['BUY_NOW'] = '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . $listing['products_id']) . '">' . tep_template_image_button('button_select.gif', IMAGE_BUTTON_BUY_NOW) . '</a>&nbsp;';
							break;
						case 'PRODUCT_LIST_DESCRIPTION':
							$description=strip_tags($listing['products_description'],'<br>');
							$product_repeat[$icnt]['DESCRIPTION'] = substr($description,0,$tdesc_length);
							break;
					}
				}
				$icnt++;
			}
			$template_details["REPEAT_PRODUCT_LIST"]=$product_repeat;
			unset($product_repeat);
			mysql_free_result($listing_query);
			$template_details["SECTION_PRODUCT"]=1;
			$template_details["SECTION_NO_PRODUCT"]=0;
		} else {
			$template_details["VALUE_PRODUCTS_TOTAL"]='';
			$template_details["VALUE_PAGE_NAVIGATION_TEXT"]='';
			$template_details["SECTION_PAGE_NAVIGATION"]=0;
			$template_details["SECTION_PRODUCT"]=0;
			$template_details["SECTION_NO_PRODUCT"]=1;
		}
		$template_details["VALUE_SUB_PADDING"]=0;
		$replaced_content=$template_content;

		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);
					} 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>';
						$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++;
						}
						$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);
					$replaced_content=str_replace(array("{{" .$key . "_START}}","{{" .$key . "_END}}"),"",$replaced_content);
					break;
			}
		}

		echo $replaced_content;
?>
<?php } ?>

