<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/
  $boxHeading = BOX_HEADING_CATEGORIES;
  $corner_left = 'rounded';
  $corner_right = 'square';
  $box_base_name = 'categories';
  $boxContent_attributes = ' align="center"';
  $box_id = $box_base_name . 'Box';

  function tep_show_category($counter) {
    global $tree, $categories_string, $id, $cPath_array, $cat_name, $parent_child;

	$ile_pozycji = explode('_',$tree[$counter]['path']);
	$ilosc_poz = count($ile_pozycji);

    if ($ilosc_poz == 1) {
        $categories_string .= '<div class="box_list">';
        $cPath_new = 'cPath=' . $counter;
      } else {
        $categories_string .= '<div class="boxLink" style="text-align:left;padding-left:'.(($ilosc_poz*5)+5).'px">';
        $cPath_new = 'cPath=' . $tree[$counter]['path'];
    }

    $categories_string .= '<a class="boxLink" href="';

    $categories_string .= tep_href_link(FILENAME_DEFAULT, $cPath_new);
    $categories_string .= '">';

    if (isset($cPath_array) && in_array($counter, $cPath_array)) {
      $categories_string .= '<b>';
    }

    $categories_string .= (($ilosc_poz > 1) ? '&raquo;&nbsp;' : '').$tree[$counter]['name'];

    if (isset($cPath_array) && in_array($counter, $cPath_array)) {
      $categories_string .= '</b>';
    }

    $categories_string .= '</a>';

    if (SHOW_COUNTS == 'true') {
      $products_in_category = tep_count_products_in_category($counter);
      if ($products_in_category > 0) {
        $categories_string .= '<span class="smalltext">&nbsp;(' . $products_in_category . ')</span>';
      }
    }

    $categories_string .= '</div>';

	if ($tree[$counter]['next_id'] != false) {
          if (file_exists(DIR_WS_TEMPLATES . 'images/infobox/category_line.gif')) {
            $categories_string .= '<div class="box_line">' . tep_image(DIR_WS_TEMPLATES . 'images/infobox/category_line.gif') . '</div>';
          }
          tep_show_category($tree[$counter]['next_id']);
  	}
  }

  $categories_string = '';
  $tree = array();

  $categories_query = tep_db_query("select c.categories_id, cd.categories_name, 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 ."' and c.categories_status = '1' order by sort_order, cd.categories_name");

  while ($categories = tep_db_fetch_array($categories_query))  {
	$list_of_categories_ids[] = (int)$categories['categories_id'];
    $tree[$categories['categories_id']] = array(
                                        'name' => $categories['categories_name'],
                                        'parent' => $categories['parent_id'],
                                        'level' => 0,
                                        'path' => $categories['categories_id'],
                                        'next_id' => false
                                       );

    if (isset($prev_id)) {
      $tree[$prev_id]['next_id'] = $categories['categories_id'];
    }

    $prev_id = $categories['categories_id'];

    if (!isset($first_element)) {
      $first_element = $categories['categories_id'];
    }
  }

  //------------------------
  if (tep_not_null($cPath)) {
    $new_path = '';
    $id = split('_', $cPath);
    reset($cPath_array);
    while (list($key, $value) = each($cPath_array)) {
      unset($prev_id);
      unset($first_id);
      $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$value . "' and c.categories_id = cd.categories_id and cd.language_id='" . (int)$languages_id ."' and c.categories_status = '1' order by sort_order, cd.categories_name");
      $category_check = tep_db_num_rows($categories_query);
      if ($category_check > 0) {
        $new_path .= $value;
        while ($row = tep_db_fetch_array($categories_query)) {
 		  $list_of_categories_ids[] = (int)$row['categories_id'];
          $tree[$row['categories_id']] = array(
                                              'name' => $row['categories_name'],
                                              'parent' => $row['parent_id'],
                                              'level' => $key+1,
                                              'path' => $new_path . '_' . $row['categories_id'],
                                              'next_id' => false
                                             );

          if (isset($prev_id)) {
            $tree[$prev_id]['next_id'] = $row['categories_id'];
          }

          $prev_id = $row['categories_id'];

          if (!isset($first_id)) {
            $first_id = $row['categories_id'];
          }

          $last_id = $row['categories_id'];
        }
        $tree[$last_id]['next_id'] = $tree[$value]['next_id'];
        $tree[$value]['next_id'] = $first_id;
        $new_path .= '_';
      } else {
        break;
      }
    }
  }
  if (sizeof($list_of_categories_ids) > 0 ) {
  $select_list_of_cat_ids = implode(",", $list_of_categories_ids);
  $parent_child_query = tep_db_query("select categories_id, parent_id from " . TABLE_CATEGORIES . " where parent_id in (" . $select_list_of_cat_ids . ") ");
  while ($_parent_child = tep_db_fetch_array($parent_child_query)) {
	 $parent_child[] = $_parent_child;
  }
 }
  tep_show_category($first_element);

  $boxContent = '';
  $boxContent = '<div class="box_kont">';
  $boxContent .= $categories_string;
  $boxContent .= '</div>';

  if(file_exists(DIR_WS_BOX_TEMPLATES . $box_base_name . '.tpl.php')) {
      require(DIR_WS_BOX_TEMPLATES . $box_base_name . '.tpl.php');
  }
  else {
      require(DIR_WS_BOX_TEMPLATES . TEMPLATENAME_BOX);
  }
?>
<!-- categories_eof //-->