<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/

$PHP_SELF = !strlen(basename($PHP_SELF)) ? 'index.php' : $PHP_SELF;

require('includes/application_top.php');

$category_depth = (isset($_GET['manufacturers_id']) ? 'products' : 'top');
if (isset($cPath) && tep_not_null($cPath)) {
  $categories_products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'");
  $cateqories_products = tep_db_fetch_array($categories_products_query);
  if ($cateqories_products['total'] > 0) {
    $category_depth = 'products';
  }
  $category_parent_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$current_category_id . "'");
  $category_parent = tep_db_fetch_array($category_parent_query);
  if ($category_parent['total'] > 0) {
    if ($category_depth == 'top') {
      $category_depth = 'nested'; 
    } else {
      $category_depth = 'both'; 
    }
  } else {
      $category_depth = 'products'; 
  }
}

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_DEFAULT);

// Header Tag Controller BOF
if (($category_depth == 'nested') || ($category_depth == 'both')) { 
  $category_query = tep_db_query("select cd.categories_name, c.categories_image, cd.categories_htc_title_tag, cd.categories_htc_description from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . (int)$current_category_id . "' and cd.categories_id = '" . (int)$current_category_id . "' and cd.language_id = '" . (int)$languages_id . "'");
  // Header Tag Controller EOF
  $category = tep_db_fetch_array($category_query);
}

if ($category_depth == 'nested') {
  $content = CONTENT_INDEX_NESTED;
} else if ($category_depth == 'both') {
  $content = CONTENT_INDEX_BOTH;
} else if ($category_depth == 'products') {
  $content = CONTENT_INDEX_PRODUCTS;
}

if (($category_depth == 'products') || ($category_depth == 'both') || isset($_GET['manufacturers_id'])) {
  // create column list
  $define_list = array('PRODUCT_LIST_MODEL' => PRODUCT_LIST_MODEL,
                         'PRODUCT_LIST_NAME' => PRODUCT_LIST_NAME,
                         'PRODUCT_LIST_DESCRIPTION' => PRODUCT_LIST_DESCRIPTION,
                         'PRODUCT_LIST_MANUFACTURER' => PRODUCT_LIST_MANUFACTURER,
						 'PRODUCT_LIST_RETAIL_PRICE' => PRODUCT_LIST_RETAIL_PRICE, 
                         'PRODUCT_LIST_PRICE' => PRODUCT_LIST_PRICE,
						 'PRODUCT_LIST_SAVE' => PRODUCT_LIST_SAVE, 
                         'PRODUCT_LIST_QUANTITY' => PRODUCT_LIST_QUANTITY,
                         'PRODUCT_LIST_MAXORDER' => PRODUCT_LIST_MAXORDER,
                         'PRODUCT_LIST_WEIGHT' => PRODUCT_LIST_WEIGHT,
                         'PRODUCT_LIST_IMAGE' => PRODUCT_LIST_IMAGE,
                         'PRODUCT_LIST_PRODUCTS_AVAILABILITY' => PRODUCT_LIST_PRODUCTS_AVAILABILITY,
						 'PRODUCT_LIST_PDF' => PRODUCT_LIST_PDF,
                         'PRODUCT_LIST_BUY_NOW' => PRODUCT_LIST_BUY_NOW);

  asort($define_list);
  $column_list = array();
  reset($define_list);
  while (list($key, $value) = each($define_list)) {
    if ($value > 0) $column_list[] = $key;
  }

  $select_column_list = '';

  for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
    switch ($column_list[$i]) {
      case 'PRODUCT_LIST_MODEL':
        $select_column_list .= 'p.products_quantity, p.products_model, ';
        break;
      case 'PRODUCT_LIST_NAME':
        $select_column_list .= 'p.products_quantity, pd.products_name, ';
        break;
      case 'PRODUCT_LIST_DESCRIPTION':
 	      $select_column_list .= 'pd.products_description, ';
        break;
      case 'PRODUCT_LIST_MANUFACTURER':
        $select_column_list .= 'p.products_quantity, ';
        break;
      case 'PRODUCT_LIST_QUANTITY':
        $select_column_list .= 'p.products_quantity, p.products_quantity, ';
        break;
      case 'PRODUCT_LIST_MAXORDER':
        $select_column_list .= 'p.products_maxorder, ';
        break;
      case 'PRODUCT_LIST_IMAGE':
        $select_column_list .= 'p.products_quantity, p.products_image, ';
        break;
      case 'PRODUCT_LIST_WEIGHT':
        $select_column_list .= 'p.products_quantity, p.products_weight, ';
        break;
      case 'PRODUCT_LIST_PDF':
        $select_column_list .= 'p.products_image_pop, ';
        break;			
      case 'PRODUCT_LIST_PRODUCTS_AVAILABILITY':
        $select_column_list .= 'p.products_availability_id, p.products_weight, ';
        break;
    }
  }

  // fragment odpowiedzialny za podatek
  if ( (DISPLAY_PRICE_WITH_TAX == 'true') && isset($HTTP_GET_VARS['cena']) && tep_not_null($HTTP_GET_VARS['cena']) ) {
    if (!tep_session_is_registered('customer_country_id')) {
      $customer_country_id = STORE_COUNTRY;
      $customer_zone_id = STORE_ZONE;
    }      
    $from_str = " left join " . TABLE_TAX_RATES . " tr on p.products_tax_class_id = tr.tax_class_id left join " . TABLE_ZONES_TO_GEO_ZONES . " gz on tr.tax_zone_id = gz.geo_zone_id and (gz.zone_country_id is null or gz.zone_country_id = '0' or gz.zone_country_id = '" . (int)$customer_country_id . "') and (gz.zone_id is null or gz.zone_id = '0' or gz.zone_id = '" . (int)$customer_zone_id . "')";
    $podatek = ', tr.tax_rate as tax_rate';
  }  
  //
  
  if (isset($HTTP_GET_VARS['manufacturers_id'])) {
    if (isset($HTTP_GET_VARS['filter_id']) && tep_not_null($HTTP_GET_VARS['filter_id'])) {
      $listing_sql = "select distinct " . $select_column_list . " m.manufacturers_name, p.products_id, p.sort_order, p.manufacturers_id, p.products_retail_price, p.products_price, p.products_tax_class_id, products_jm_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price ".$podatek." from (((" . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c) left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id) ".$from_str." left join " . TABLE_CATEGORIES . " c on p2c.categories_id = c.categories_id) where c.categories_status = '1' and p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id in (" . $HTTP_GET_VARS['filter_id'] . ")";
    } else {
      $listing_sql = "select distinct " . $select_column_list . " m.manufacturers_name, p.products_id, p.sort_order, p.manufacturers_id, p.products_retail_price, p.products_price, p.products_tax_class_id, products_jm_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price ".$podatek." from ((((" . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m) left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id) left join " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c on p.products_id = p2c.products_id) ".$from_str." left join " . TABLE_CATEGORIES . " c on p2c.categories_id = c.categories_id) where c.categories_status = '1' and p.products_status = '1' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "'";
	  }
  } else {
    if (isset($HTTP_GET_VARS['filter_id']) && tep_not_null($HTTP_GET_VARS['filter_id'])) {
      $listing_sql = "select distinct " . $select_column_list . " m.manufacturers_name, p.products_id, p.sort_order, p.manufacturers_id, p.products_retail_price, p.products_price, p.products_tax_class_id, products_jm_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price ".$podatek." from (((" . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c) left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id) ".$from_str." left join " . TABLE_CATEGORIES . " c on c.categories_id = p2c.categories_id) where c.categories_status = '1' and p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$HTTP_GET_VARS['filter_id'] . "' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";
    } else {
      $listing_sql = "select distinct " . $select_column_list . " m.manufacturers_name, p.products_id, p.sort_order, p.manufacturers_id, p.products_retail_price, p.products_price, p.products_tax_class_id, products_jm_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price ".$podatek." from ((((" . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p) left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c) left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id) ".$from_str." left join " . TABLE_CATEGORIES . " c on c.categories_id = p2c.categories_id) where c.categories_status = '1' and p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";
	  }
  }

  // dodano sprawdzenie dostepnosci - Julek
  if (isset($HTTP_GET_VARS['dostepny']) && tep_not_null($HTTP_GET_VARS['dostepny'])) {
     if ($HTTP_GET_VARS['dostepny'] == 'tak') {
		$listing_sql .= ' and p.products_quantity > 0';
	 }
  }
  // dodano sprawdzenie czy promocja - Julek
  if (isset($HTTP_GET_VARS['promocja']) && tep_not_null($HTTP_GET_VARS['promocja'])) {
     if ($HTTP_GET_VARS['promocja'] == 'tak') {
		$listing_sql .= " and s.specials_new_products_price > 0 and s.status = '1'";
	 }
  }
  //
  
  // NOWE - ZAKRESY CENOWE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
  if (isset($HTTP_GET_VARS['cena']) && tep_not_null($HTTP_GET_VARS['cena'])) {
      $tablica_cena = explode("|",$HTTP_GET_VARS['cena']);
      if (sizeof($tablica_cena) == 2) {
          if (DISPLAY_PRICE_WITH_TAX == 'true') {
            $listing_sql .= " and (IF(s.status, s.specials_new_products_price, p.products_price) * if(gz.geo_zone_id is null, 1, 1 + (tr.tax_rate / 100) ) >= " . (double)$tablica_cena[0] . ")";
            $listing_sql .= " and (IF(s.status, s.specials_new_products_price, p.products_price) * if(gz.geo_zone_id is null, 1, 1 + (tr.tax_rate / 100) ) <= " . (double)$tablica_cena[1] . ")";
          } else {
            $listing_sql .= " and (IF(s.status, s.specials_new_products_price, p.products_price) >= " . (double)$tablica_cena[0] . ")";
            $listing_sql .= " and (IF(s.status, s.specials_new_products_price, p.products_price) <= " . (double)$tablica_cena[1] . ")";
          }
      }
  }  
  
  if ( (!isset($HTTP_GET_VARS['sort'])) || (!ereg('^[1-8][ad]$', $HTTP_GET_VARS['sort'])) || (substr($HTTP_GET_VARS['sort'], 0, 1) > sizeof($column_list)) ) {
    // domyslne sortowanie
    //for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
    //  if ($column_list[$i] == 'PRODUCT_LIST_NAME') {
    //    $HTTP_GET_VARS['sort'] = $i+1 . 'a';
    //    $listing_sql .= " order by p.sort_order, pd.products_name";
    //    // $HTTP_GET_VARS['sort'] = '';
    //    //  $listing_sql .= " order by final_price";
    //    break;
    //  }
    //}
    // domyslne sortowanie wg numeru sortowanie i nazwy
    $HTTP_GET_VARS['sort'] = '';
    $listing_sql .= " order by p.sort_order, pd.products_name";
  } else {
    $sort_col = substr($HTTP_GET_VARS['sort'], 0 , 1);
    $sort_order = substr($HTTP_GET_VARS['sort'], 1);
    switch ($column_list[$sort_col-1]) {
      case 'PRODUCT_LIST_MODEL':
        $listing_sql .= " order by p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
        break;
      case 'PRODUCT_LIST_NAME':
        $listing_sql .= " order by pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
        break;
      case 'PRODUCT_LIST_DESCRIPTION':
	     $listing_sql .= " order by pd.products_description ". ($sort_order == 'd' ? "desc" : "");
        break;
      case 'PRODUCT_LIST_MANUFACTURER':
        $listing_sql .= " order by m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
        break;
      case 'PRODUCT_LIST_QUANTITY':
        $listing_sql .= " order by p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
        break;
      case 'PRODUCT_LIST_MAXORDER':
        $listing_sql .= " order by p.products_maxorder " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
        break;
	    case 'PRODUCT_LIST_IMAGE':
        $listing_sql .= " order by pd.products_name";
        break;
      case 'PRODUCT_LIST_WEIGHT':
        $listing_sql .= " order by p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
        break;
		  case 'PRODUCT_LIST_RETAIL_PRICE':
		    $listing_sql .= " order by p.products_retail_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
		    break;
      case 'PRODUCT_LIST_PRICE':
        $listing_sql .= " order by final_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
        break;
      }
    }
}

if ($category_depth == 'top') {
  $content = CONTENT_INDEX_DEFAULT;
}

include (bts_select('main', $content));
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>