<?php
/*
  $Id: categories.php,v 1.3 2007/04/16 05:40:39 ryu Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
  
  modified for Koshoten.net2
  http://www.koshoten.net/
*/
?>
<!-- categories -->
<?php
  if (KN_CFG_CATEGORIES_DISPLAY == 'true') {
    if ( (!isset($display_category_id)) || ($display_category_id == '0') ) {
      $display_categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
    } else {
      if (is_array($display_category_id)) {
        for($i=0, $n=sizeof($display_category_id); $i<$n; $i++) {
          $display_categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$display_category_id[$i] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
          if (tep_db_num_rows($display_categories_query) < 1) {
            // do nothing, go through the loop
          } else {
            break; // we've found the deepest category the customer is in
          }
        }
      } else {
        $display_categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$display_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
      }
    }

    $num_categories = tep_db_num_rows($display_categories_query);
    if (0 < $num_categories) {
      echo '<table border="0" class="categories">' . "\n";

      $col = 0;
      $width = (int)(100 / KN_CFG_MAX_DISPLAY_CATEGORIES_PER_ROW) . '%';

      while ($display_categories = tep_db_fetch_array($display_categories_query)) {
        if ($col == 0) {
          echo '<tr>';
        }
        $cPath_new = tep_get_path($display_categories['categories_id']);
        echo '<td width="' . $width . '">' . "\n";
        switch (KN_CFG_CATEGORIES_DISPLAY_TYPE) {
          case 'type2':
            if (SHOW_COUNTS == 'true') {
              $products_in_display_category = '(' . tep_count_products_in_category($display_categories['categories_id']) . ')';
            } else {
              $products_in_display_category = '';
            }
            echo '<p><a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '"><span class="cName">' . $display_categories['categories_name'] . '</span>' . $products_in_display_category . "</a></p>\n";
            break;
          case 'type1':
          default:
            echo '<p><a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '"><span class="cName">' . $display_categories['categories_name'] . "</span></a></p>\n";
            $child_categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$display_categories['categories_id'] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
            if (tep_db_num_rows($child_categories_query) > 0) {
            	echo "<ul>\n";
        	     while ($child_categories = tep_db_fetch_array($child_categories_query)) {
                $cPath_new = tep_get_path($child_categories['categories_id']);
        	       echo '<li><a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . $child_categories['categories_name'] . "</a>\n";
        	     }
            }
        }
        echo "</td>\n";
        $col ++;
        if ($col == KN_CFG_MAX_DISPLAY_CATEGORIES_PER_ROW) {
          echo "</tr>\n";
          $col = 0;
        }
      }
      if ($col>0) {
        for ($i=$col; $i < KN_CFG_MAX_DISPLAY_CATEGORIES_PER_ROW; $i++) {
          echo '<td width="' . $width . '">&nbsp;</td>' . "\n";
        }
        echo "</tr>\n";
      }
      echo "</table>\n";
    }
  }
?>
<!-- /categories -->
