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

if (isset($HTTP_GET_VARS['manufacturers_id']))
 $db_query = tep_db_query("select manufacturers_htc_title_tag as htc_title, manufacturers_htc_description as htc_description from " . TABLE_MANUFACTURERS_INFO . " where languages_id = '" . (int)$languages_id . "' and manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "'");
else
 $db_query = tep_db_query("select categories_name, categories_htc_title_tag as htc_title, categories_htc_description as htc_description from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$current_category_id . "' and language_id = '" . (int)$languages_id . "'");

 $htc = tep_db_fetch_array($db_query);
 ?>

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

 <!-- Naglowek BOF -->
 <?php
 if (ALLOW_HEADER_TAGS_CONTROLLER=='true') {
 ?>
 <tr>
  <td>
   <?php
   $info_box_contents = array();
   if (tep_not_null($htc['htc_title'])) {
      $info_box_contents[] = array('text' => sprintf($htc['htc_title']));   // jezeli wypelnione jest pole tytul kategori w HTC
   } else {
     if (isset($HTTP_GET_VARS['manufacturers_id'])) {
       $info_box_contents[] = array('text' => sprintf(HEADING_TITLE));    // jezeli nie jest wypelnione pole tytul kategori w HTC
     } else {
       $info_box_contents[] = array('text' => sprintf($htc['categories_name']));
     }
   }
   new contentBoxHeading($info_box_contents);
   ?>
  </td>
 </tr>

 <?php
 } else {
 ?>

 <tr>
  <td>
   <?php
   $info_box_contents = array();
//   $info_box_contents[] = array('text' => sprintf(HEADING_TITLE));
   if (isset($HTTP_GET_VARS['manufacturers_id'])) {
     $info_box_contents[] = array('text' => sprintf(HEADING_TITLE));    // jezeli nie jest wypelnione pole tytul kategori w HTC
   } else {
     $info_box_contents[] = array('text' => sprintf($htc['categories_name']));
   }
   new contentBoxHeading($info_box_contents);
   ?>
  </td>
 </tr>
 <?php
 }
 ?>
 <tr>
  <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?></td>
 </tr>

 <?php if ((int)strlen($htc['htc_description']) > 10) { ?>
 <tr>
  <td>
   <table border="0" width="100%" cellspacing="1" cellpadding="2" class="infoBox">
    <tr class="infoBoxContents">
     <td>
      <table border="0" width="100%" cellspacing="0" cellpadding="3">
       <tr>
        <?php
        $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'];
        if ($image['categories_image'] != '') {
            $szerokosc = SMALL_IMAGE_WIDTH + 10;
            $wysokosc = SMALL_IMAGE_HEIGHT + 10;
            echo '<td align="center" valign="top" width="'.$szerokosc.'">';
			      if ($image['categories_image'] != '') {
            echo tep_image(DIR_WS_IMAGES.$image, $htc['categories_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT,'','','','','',true);
			}
            echo '</td>';
        }
        ?>
        <td class="main" valign="top" align="left"><?php echo $htc['htc_description']; ?></td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
  </td>
 </tr>
 <?php
 }

 if (CZY_POLECANE_PRODUKTY_LISTING != '0') {
   $modules_query = tep_db_query("select configuration_id, configuration_description, configuration_title, configuration_value, location, max_display, info_id, header from " . TABLE_MODULES_CONFIGURATION . "  where configuration_title = '".substr(FILENAME_FEATURED,0,-4)."'");
   while ($modul = tep_db_fetch_array($modules_query)) {
     if ($modul['configuration_value'] == 'yes') {
       $ilosc_maksymalna = (int)$modul['max_display'];
     }
   }
   ?>
   <tr>
     <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '3'); ?></td>
   </tr>
   <tr>
     <td><?php include($modules_folder . FILENAME_FEATURED_LISTING); ?></td>
   </tr>
   <tr>
    <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
   </tr>
   <?php
 }
 ?>
 <tr>
  <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?></td>
 </tr>

 <tr>
  <td>
   <?php
   if (isset($HTTP_GET_VARS['manufacturers_id'])) {
      echo tep_draw_form('filter', FILENAME_DEFAULT.'?manufacturers_id='.(int)$HTTP_GET_VARS['manufacturers_id'], 'post');
     } else {
      echo tep_draw_form('filter', FILENAME_DEFAULT.'?cPath='.(int)$current_category_id, 'post');
   }
   ?>
   <table border="0" width="100%" cellspacing="0" cellpadding="2">
	<?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 '<tr><td align="left" class="main" width="40%">'. TEXT_SHOW . '</td><td align="left" width="60%">';
       echo '<select name="filter_id" style="width:90%" onChange="document.location=this.value">';
       $link = str_replace('&amp;','&',tep_get_all_get_params(array('filter_id')));
	   if (isset($HTTP_GET_VARS['manufacturers_id'])) {
         echo '<option value="index.php?'.$link.'">'.TEXT_ALL_CATEGORIES.'</option>';
	   } else {
         echo '<option value="index.php?'.$link.'">'.TEXT_ALL_MANUFACTURERS.'</option>';
	   }

       if (isset($HTTP_GET_VARS['manufacturers_id'])) {
            // funkcja przypisuje do zmiennej id glownej kategorii dla id podkategorii
            function oscgold_kats($categories_array = '', $parent_id = '0', $indent = '', $podkat = true, $sciezka = '') {
                global $kat_array;
                if (!is_array($categories_array)) $categories_array = array();
                $tab_tmp = array();
                foreach ($kat_array as $tabs) {
                    if ($tabs['kat_parent_id'] == $parent_id) { $tab_tmp[] = array('id' => $tabs['kat_id'], 'text' => $tabs['kat_name']);}
                }
                foreach ($tab_tmp as $tabs) {
                    $categories_array[] = array('id' => $tabs['id'],'text' => $indent . $tabs['text'],'path' => (($sciezka!='') ? $sciezka . '_' . $tabs['id'] : $tabs['id']));
                    if ($podkat == true) {
                        if ($tabs['id'] != $parent_id) {
                            $categories_array = oscgold_kats($categories_array, $tabs['id'], '&nbsp;&nbsp;'.$indent, $podkat, (($sciezka!='') ? $sciezka . '_' . $tabs['id'] : $tabs['id']));
                        }
                    }
                }
                return $categories_array;
            }
            $tablica_kategorii = oscgold_kats();
            function parent_oscgold_kat($categories_id) {
                global $tablica_kategorii;
                foreach ($tablica_kategorii as $kats) { $path = explode('_',$kats['path']); if ($path[sizeof($path)-1] == $categories_id) { return $path[0]; } }
            }
            $glowne_kategorie = array();
            $podkategorie = array();
    	    while ($filterlist = tep_db_fetch_array($filterlist_query)) {
                // podaje id glownej kategorii do ktorej nalezy kategoria - zmienna $caterig_id
                $caterig_id = parent_oscgold_kat($filterlist['id']);
                foreach($tablica_kategorii as $wynik) {
                    if ($caterig_id == $wynik['id']) { $glowne_kategorie[$wynik['id']] = $wynik['text']; }
                    if ($filterlist['id'] == $wynik['id']) { $podkategorie[] = $wynik['id']; }
                }
    	    }
            asort($glowne_kategorie);
            $glowne_kategorie = array_unique($glowne_kategorie);
            foreach($glowne_kategorie as $klucz => $wartosc) {
               // tworzy tablice z podkategoriami dla glownej kategorii
               $tablica_tymczasowa = oscgold_kats('',$klucz);
               $ciag = '';
               foreach ($tablica_tymczasowa as $podkat) { if (in_array($podkat['id'],$podkategorie)) { $ciag .= $podkat['id'].','; } }
               // sprawdza w tablicy jak sie nazywa kategoria
               foreach ($tablica_kategorii as $nazwa_kat) {
                   if ($nazwa_kat['id'] == $klucz) {
                        $ciag_podkat = $klucz.','.$ciag; $ciag_podkat = substr($ciag_podkat,0,strlen($ciag_podkat)-1);
                    	if (isset($_GET['filter_id']) && $_GET['filter_id'] == $ciag_podkat) { $check = 'selected'; } else { $check = ''; }
                        echo '<option value="index.php?'.$link.'filter_id='.$ciag_podkat.'" '.$check.'>'.$nazwa_kat['text'].'</option>';
                   }
               }
               foreach ($tablica_tymczasowa as $rts) {
                  // jezeli podkategoria byla w wyniku zapytania to ja wyswietli
                  foreach ($podkategorie as $nazwa_kat) {
                     if ($nazwa_kat == $rts['id']) {
                       // tworzy tablice z podkategoriami dla glownej kategorii
                        $tablica_tymczasowa = oscgold_kats('',$nazwa_kat); $ciag = '';
                        foreach ($tablica_tymczasowa as $podkat) {
                           if (in_array($podkat['id'],$podkategorie)) { $ciag .= $podkat['id'].','; }
                        }
                        $ciag_podkat = $nazwa_kat.','.$ciag; $ciag_podkat = substr($ciag_podkat,0,strlen($ciag_podkat)-1);
                        //
                    	if (isset($_GET['filter_id']) && $_GET['filter_id'] == $ciag_podkat) { $check = 'selected'; } else { $check = ''; }
                        echo '<option value="index.php?'.$link.'filter_id='.$ciag_podkat.'" '.$check.'>&nbsp;&nbsp;'.$rts['text'].'</option>';
                     }
                  }
                }
            }
    	 } else {
           while ($filterlist = tep_db_fetch_array($filterlist_query)) {
            if (isset($_GET['filter_id']) && $_GET['filter_id'] == $filterlist['id']) { $check = 'selected'; } else { $check = ''; }
            echo '<option value="index.php?'.$link.'filter_id='.$filterlist['id'].'" '.$check.'>'.$filterlist['name'].'</option>';
           }
       }
       echo '</select><br>';
	   echo tep_hide_session_id() . '</td></tr>' . "\n";
	  }
	}
    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="index.php?'.$link.'sort='.($i+1).'a" '.$check_wy.'>'.$nazwa.' ('.TEXT_ASCENDINGLY.') </option>';
            echo '<option value="index.php?'.$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="index.php?'.$link.'dostepny=nie" '.$check_wy.'>'.TEXT_SHOW_ALL_PROD.'</option>';
    	echo '<option value="index.php?'.$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="index.php?'.$link.'promocja=nie" '.$check_wy.'>'.TEXT_SHOW_ALL_PROD.'</option>';
        	echo '<option value="index.php?'.$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="index.php?'.$link.'cena='.$zakres.'" '.$check.'>'.($x*$przedzial).' - '.(($x+1)*$przedzial).'</option>';
                }
            }
          } else {
            $przedzial_cenowy = explode('|',$_GET['cena']);
            if (sizeof($przedzial_cenowy) == 2) {
                $tekst .= '<option value="index.php?'.$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="index.php?'.$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="index.php?<?php echo $link; ?>dis=1" <?php echo (($sposob_wyswietlania=='true') ? 'selected' : ''); ?>><?php echo TEXT_SHOW_DISPLAY_LIST; ?></option>
                <option value="index.php?<?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="index.php?<?php echo $link; ?>str=5" <?php echo (($wyswietlen==5) ? 'selected' : ''); ?>>5</option>
                <option value="index.php?<?php echo $link; ?>str=10" <?php echo (($wyswietlen==10) ? 'selected' : ''); ?>>10</option>
                <option value="index.php?<?php echo $link; ?>str=20" <?php echo (($wyswietlen==20) ? 'selected' : ''); ?>>20</option>
                <option value="index.php?<?php echo $link; ?>str=30" <?php echo (($wyswietlen==30) ? 'selected' : ''); ?>>30</option>
                <option value="index.php?<?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 . FILENAME_PRODUCT_LISTING); ?></td>
 </tr>
</table>