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

  require('includes/application_top.php');

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

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_PRODUCTS_NEW));

  $content = CONTENT_PRODUCTS_NEW;
  
  // filtr
  $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, //EZier new field mods by Noel
                      'PRODUCT_LIST_PRICE' => PRODUCT_LIST_PRICE,
				      'PRODUCT_LIST_SAVE' => PRODUCT_LIST_SAVE, //EZier new field mod by Noel
                      '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($column, $value) = each($define_list)) {
    if ($value) $column_list[] = $column;
  }
    
  $filtrs = '';
  // dodano sprawdzenie dostepnosci - Julek
  if (isset($_GET['dostepny']) && tep_not_null($_GET['dostepny'])) {
     if ($_GET['dostepny'] == 'tak') {
		$filtrs .= ' and p.products_quantity > 0';
	 }
  }  
  // dodano sprawdzenie czy promocja - Julek
  if (isset($_GET['promocja']) && tep_not_null($_GET['promocja'])) {
     if ($_GET['promocja'] == 'tak') {
		$filtrs .= " and s.specials_new_products_price > 0 and s.status = '1'";
	 }
  }
  // dodano zakresy cenowe
  // 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';
   } else {
    $from_str = "";
    $podatek = "";    
  }  
  //  
  if (isset($_GET['cena']) && tep_not_null($_GET['cena'])) {
      $tablica_cena = explode("|",$_GET['cena']);
      if (sizeof($tablica_cena) == 2) {
          if (DISPLAY_PRICE_WITH_TAX == 'true') {
            $filtrs .= " 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] . ")";
            $filtrs .= " 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 {
            $filtrs .= " and (IF(s.status, s.specials_new_products_price, p.products_price) >= " . (double)$tablica_cena[0] . ")";
            $filtrs .= " and (IF(s.status, s.specials_new_products_price, p.products_price) <= " . (double)$tablica_cena[1] . ")";
          }
      }
  }  
  if ( (!isset($_GET['sort'])) || (!ereg('^[1-8][ad]$', $_GET['sort'])) || (substr($_GET['sort'], 0, 1) > sizeof($column_list)) ) {
    //for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
    //  if ($column_list[$i] == 'PRODUCT_LIST_NAME') {
    //    $_GET['sort'] = $i+1 . 'a';
    //    $filtrs .= " order by p.products_date_added DESC, p.sort_order, pd.products_name";
    //    break;
    //  }
    //}
    // domyslne sortowanie wg numeru sortowanie i nazwy
    $_GET['sort'] = '';
    $listing_sql .= " order by p.sort_order, pd.products_name";    
  } else {
    $sort_col = substr($_GET['sort'], 0 , 1);
    $sort_order = substr($_GET['sort'], 1);
    switch ($column_list[$sort_col-1]) {
      case 'PRODUCT_LIST_MODEL':
        $filtrs .= " order by p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", p.products_date_added DESC, pd.products_name";
        break;
      case 'PRODUCT_LIST_NAME':
        $filtrs .= " order by pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
        break;
      case 'PRODUCT_LIST_DESCRIPTION':
	     $filtrs .= " order by pd.products_description ". ($sort_order == 'd' ? "desc" : "");
        break;
      case 'PRODUCT_LIST_MANUFACTURER':
        $filtrs .= " order by m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", p.products_date_added DESC, pd.products_name";
        break;
      case 'PRODUCT_LIST_QUANTITY':
        $filtrs .= " order by p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", p.products_date_added DESC, pd.products_name";
        break;
      case 'PRODUCT_LIST_MAXORDER':
        $filtrs .= " order by p.products_maxorder " . ($sort_order == 'd' ? 'desc' : '') . ", p.products_date_added DESC, pd.products_name";
        break;
	    case 'PRODUCT_LIST_IMAGE':
        $filtrs .= " order by pd.products_name";
        break;
      case 'PRODUCT_LIST_WEIGHT':
        $filtrs .= " order by p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", p.products_date_added DESC, pd.products_name";
        break;
      case 'PRODUCT_LIST_RETAIL_PRICE':
        $filtrs .= " order by p.products_retail_price " . ($sort_order == 'd' ? 'desc' : '') . ", p.products_date_added DESC, pd.products_name";
        break;
      case 'PRODUCT_LIST_PRICE':
        $filtrs .= " order by p.products_price " . ($sort_order == 'd' ? 'desc' : '') . ", p.products_date_added DESC, pd.products_name";
        break;
      }
  }  
  //

  include (bts_select('main', $content)); // BTSv1.5

  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
