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

<?php
   $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_model, ';
          break;
        case 'PRODUCT_LIST_NAME':
          $select_column_list .= 'pd.products_name, ';
          break;
          case 'PRODUCT_LIST_DESCRIPTION':
 	        $select_column_list .= 'pd.products_description, ';
          break;
        case 'PRODUCT_LIST_QUANTITY':
          $select_column_list .= 'p.products_quantity, ';
          break;
        case 'PRODUCT_LIST_MAXORDER':
          $select_column_list .= 'p.products_maxorder, ';
          break;
        case 'PRODUCT_LIST_IMAGE':
          $select_column_list .= 'p.products_image, ';
          break;
        case 'PRODUCT_LIST_WEIGHT':
          $select_column_list .= '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, ';
          break;
      }
    }
    
   $listing_sql = "select distinct " . $select_column_list . " p.products_id, p.products_quantity, p.products_price, p.products_tax_class_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, p.products_date_added, m.manufacturers_name ".$podatek." 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 and pd.language_id = '" . $languages_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 left join " .TABLE_PRODUCTS_TO_CATEGORIES. " p2c on p2c.products_id = p.products_id left join " .TABLE_CATEGORIES. " c on c.categories_id = p2c.categories_id ".$from_str." where c.categories_status='1' and p.products_status = '1' and f.status = '1' ".$filtrs;   
?>

<table border="0" width="100%" cellspacing="0" cellpadding="0">


 <tr>
  <td>
   <?php
   $info_box_contents = array();
   $info_box_contents[] = array('text' => sprintf(HEADING_TITLE));
   new contentBoxHeading($info_box_contents);
   ?>
  </td>
 </tr>
 <tr>
  <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '1'); ?></td>
 </tr>

 <tr>
  <td>
   <?php 
   echo tep_draw_form('filter', _FEATURED_PRODUCTS, 'post'); 
   ?>
   <table border="0" width="100%" cellspacing="0" cellpadding="2">
	<?php
    if (CZY_DISPLAY_SORT == '1') {
        echo '<tr><td align="left" class="main" width="40%">'.TEXT_SHOW_SORT.'</td><td align="left" width="60%">';
        echo '<select name="sort" style="width:90%" onChange="document.location=this.value">';
        $link = str_replace('&amp;','&',tep_get_all_get_params(array('sort')));
        if ($_GET['sort'] == '') {
            echo '<option value="index.php?'.$link.'" selected>'.TEXT_SHOW_SORT_DOM.' </option>';
        }            
        
        for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
          if ($column_list[$i] != 'PRODUCT_LIST_BUY_NOW' && $column_list[$i] != 'PRODUCT_LIST_IMAGE' && $column_list[$i] != 'PRODUCT_LIST_DESCRIPTION' && $column_list[$i] != 'PRODUCT_LIST_SAVE' && $column_list[$i] != 'PRODUCT_LIST_DESCRIPTION') {
            //
            $check_wl = ''; $check_wy = '';
            if (isset($_GET['sort']) && (int)$_GET['sort'] > 0) {
                if ((int)$_GET['sort'] == ($i+1) && strpos($_GET['sort'],'d') > 0) {
                    $check_wl = 'selected'; $check_wy = '';
                } else if ((int)$_GET['sort'] == ($i+1) && strpos($_GET['sort'],'a') > 0) {
                    $check_wl = ''; $check_wy = 'selected';
                }
            }
            //
            switch ($column_list[$i]) {
              case 'PRODUCT_LIST_MODEL':$nazwa = TABLE_HEADING_MODEL;break;
              case 'PRODUCT_LIST_NAME':$nazwa = TABLE_HEADING_PRODUCTS;break;
              case 'PRODUCT_LIST_MANUFACTURER':$nazwa = TABLE_HEADING_MANUFACTURER;break;
              case 'PRODUCT_LIST_RETAIL_PRICE':$nazwa = TABLE_HEADING_RETAIL_PRICE;break;
              case 'PRODUCT_LIST_PRICE':$nazwa = TABLE_HEADING_PRICE;break;
              case 'PRODUCT_LIST_QUANTITY':$nazwa = TABLE_HEADING_QUANTITY;break;
              case 'PRODUCT_LIST_MAXORDER':$nazwa = TABLE_HEADING_MAXORDER;break;
              case 'PRODUCT_LIST_WEIGHT':$nazwa = TABLE_HEADING_WEIGHT;break;
              case 'PRODUCT_LIST_PRODUCTS_AVAILABILITY':$nazwa = TABLE_HEADING_PRODUCTS_AVAILABILITY;break;
            }
            //
            echo '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'sort='.($i+1).'a" '.$check_wy.'>'.$nazwa.' ('.TEXT_ASCENDINGLY.') </option>';
            echo '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'sort='.($i+1).'d" '.$check_wl.'>'.$nazwa.' ('.TEXT_DESCENDINGLY.') </option>';
          }
        }
        echo '</select></td></tr>';
    }

    if ( CZY_DISPLAY_DOSTEPNE == '1' ) {
    	echo '<tr><td align="left" width="40%" class="main">'.TEXT_SHOW_PROD.'</td><td width="60%" align="left">';
    	echo '<select name="dostepny" style="width:90%" onChange="document.location=this.value">';
        $link = str_replace('&amp;','&',tep_get_all_get_params(array('dostepny')));
    	//
    	if (isset($_GET['dostepny']) && $_GET['dostepny'] == 'tak') {
    		$check_wl = 'selected'; $check_wy = '';
    		} else {
    		$check_wl = ''; $check_wy = 'selected';
    	}
    	//
    	echo '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'dostepny=nie" '.$check_wy.'>'.TEXT_SHOW_ALL_PROD.'</option>';
    	echo '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'dostepny=tak" '.$check_wl.'>'.TEXT_SHOW_ONLY_AVA.'</option>';
    	echo '</select></td></tr>';
    }

    if ( CZY_DISPLAY_PROMOCJE == '1' ) {
        if ( SPECIAL_PRICES_HIDE == 'false' ) {
        	echo '<tr><td align="left" class="main" width="40%">'.TEXT_SHOW_PROM.'</td><td align="left" width="60%">';
        	echo '<select name="promocja" style="width:90%" onChange="document.location=this.value">';
            $link = str_replace('&amp;','&',tep_get_all_get_params(array('promocja')));
        	//
        	if (isset($_GET['promocja']) && $_GET['promocja'] == 'tak') {
        		$check_wl = 'selected'; $check_wy = '';
        		} else {
        		$check_wl = ''; $check_wy = 'selected';
        	}
        	//
        	echo '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'promocja=nie" '.$check_wy.'>'.TEXT_SHOW_ALL_PROD.'</option>';
        	echo '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'promocja=tak" '.$check_wl.'>'.TEXT_SHOW_ONLY_PROM.'</option>';
        	echo '</select></td></tr>';
        }
    }
    
    if ( CZY_DISPLAY_CENA == '1' ) {
        $link = str_replace('&amp;','&',tep_get_all_get_params(array('cena')));
        // zakres cenowy
        $tablica_liczb = array();   // cena brutto
        $tablica_id = array();      // id produktow
        $tablica_liczb_netto = array();   // cena netto
        $filterlist_sql = tep_db_query($listing_sql);
        while ($produkt = tep_db_fetch_array($filterlist_sql)) {
            $tablica_liczb[] = tep_add_tax($produkt['final_price'],tep_get_tax_rate($produkt['products_tax_class_id']));
            $tablica_id[] = $produkt['products_id'];
        }     
        if (!isset($HTTP_GET_VARS['cena'])) {  
            // tworzy przedzialy cenowe
            sort($tablica_liczb);
            $max = (int)$tablica_liczb[sizeof($tablica_liczb)-1];          
            $przedzial = floor(zaokr($max)/20);
            $tekst = '';        
            for ($x=0;$x<20;$x++) {
                // sprawdza czy dla danego przedzialu sa pozycje
                $licznik = 0;
                foreach ($tablica_liczb as $tablica) {
                    if ($tablica >= ($x*$przedzial) && $tablica <= (($x+1)*$przedzial)) { $licznik++; }
                }
                if ($licznik > 0) {
                    $zakres = ($x*$przedzial).'|'.(($x+1)*$przedzial);
                    //
                	if (isset($_GET['cena']) && $zakres == $_GET['cena']) {
                		$check = 'selected'; } else { $check = '';
                	}
                	//             
                    $tekst .= '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'cena='.$zakres.'" '.$check.'>'.($x*$przedzial).' - '.(($x+1)*$przedzial).'</option>';
                }
            } 
          } else {
            $przedzial_cenowy = explode('|',$_GET['cena']);
            if (sizeof($przedzial_cenowy) == 2) {
                $tekst .= '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'cena='.$_GET['cena'].'" selected>'.$przedzial_cenowy[0].' - '.$przedzial_cenowy[1].'</option>';
            }
        }
    	echo '<tr><td align="left" width="40%" class="main"><b>'.TEXT_SHOW_PRZED_CEN.'</b></td><td width="60%" align="left">';
    	echo '<select name="cena" style="width:90%" onChange="document.location=this.value">';        
        echo '<option value="'.FILENAME_FEATURED_PRODUCTS.'?'.$link.'">'.TEXT_SHOW_PRZED_CEN_OPTION.'</option>';
    	echo $tekst;
    	echo '</select></td></tr>'; 
    }
    
    //
    if ( CZY_DISPLAY_SPOSOB == '1' ) {
        if (isset($_GET['dis'])) {
            if ($_GET['dis'] == '1') {
                $sposob_wyswietlania = 'true';
              } else {
                $sposob_wyswietlania = 'false';
            }
          } else {
            $sposob_wyswietlania = PRODUCTS_LISTING_DISPLAY_MODE;
        }
        $link = str_replace('&amp;','&',tep_get_all_get_params(array('dis')));
        ?>    
        <tr>
            <td class="smallText" width="40%"><b><?php echo TEXT_SHOW_DISPLAY; ?></b></td>
            <td class="smallText" align="left" width="60%">
                <select name="display" onChange="document.location=this.value">
                <option value="<?php echo FILENAME_FEATURED_PRODUCTS; ?>?<?php echo $link; ?>dis=1" <?php echo (($sposob_wyswietlania=='true') ? 'selected' : ''); ?>><?php echo TEXT_SHOW_DISPLAY_LIST; ?></option>
                <option value="<?php echo FILENAME_FEATURED_PRODUCTS; ?>?<?php echo $link; ?>dis=0" <?php echo (($sposob_wyswietlania=='false') ? 'selected' : ''); ?>><?php echo TEXT_SHOW_DISPLAY_BOX; ?></option>       
                </select>
            </td>
        </tr>
    <?php } 
    if ( CZY_DISPLAY_WYNIKI == '1' ) {
        if (isset($_GET['str'])) {
         if ((int)$_GET['str'] > 0) {
            if ((int)$_GET['str'] > 50) {
                $wyswietlen = 50;
               } else {
                $wyswietlen = (int)$_GET['str'];
            }
          }
        } else {
          $wyswietlen = MAX_DISPLAY_SEARCH_RESULTS;
        }
        $link = str_replace('&amp;','&',tep_get_all_get_params(array('str')));
        ?>    
        <tr>
            <td class="smallText" colspan="2" align="right"><?php echo TEXT_SHOW_ILE_NA_STR; ?>&nbsp;
                <select name="str" onChange="document.location=this.value">
                <option value="<?php echo FILENAME_FEATURED_PRODUCTS; ?>?<?php echo $link; ?>str=5" <?php echo (($wyswietlen==5) ? 'selected' : ''); ?>>5</option>
                <option value="<?php echo FILENAME_FEATURED_PRODUCTS; ?>?<?php echo $link; ?>str=10" <?php echo (($wyswietlen==10) ? 'selected' : ''); ?>>10</option>
                <option value="<?php echo FILENAME_FEATURED_PRODUCTS; ?>?<?php echo $link; ?>str=20" <?php echo (($wyswietlen==20) ? 'selected' : ''); ?>>20</option>
                <option value="<?php echo FILENAME_FEATURED_PRODUCTS; ?>?<?php echo $link; ?>str=30" <?php echo (($wyswietlen==30) ? 'selected' : ''); ?>>30</option>
                <option value="<?php echo FILENAME_FEATURED_PRODUCTS; ?>?<?php echo $link; ?>str=50" <?php echo (($wyswietlen==50) ? 'selected' : ''); ?>>50</option>        
                </select>
            </td>
        </tr>     
    <?php
    }        
    ?>
    </table>
    </form>
   </td>
  </tr>
  
 <tr>
  <td>
   <?php
   include($modules_folder . 'product_listing.php');
   ?>
  </td>
 </tr>
</table>