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

$boxHeading = BOX_HEADING_NEWSDESK_CATEGORIES;
$corner_left = 'rounded';
$corner_right = 'rounded';
$box_base_name = 'newsdesk';
$boxContent_attributes = ' align="center"';
$box_id = $box_base_name . 'Box';

$do_we_have_categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_NEWSDESK_CATEGORIES . " c, ". TABLE_NEWSDESK_CATEGORIES_DESCRIPTION . " cd where c.catagory_status = '1' and c.parent_id = '" . $value . "' and c.categories_id = cd.categories_id and cd.language_id='" . $languages_id ."' order by sort_order, cd.categories_name");
$newsdesk_check = tep_db_num_rows($do_we_have_categories_query);

if ($newsdesk_check > 0) {
  function newsedsk_box_count_products_in_category($category_id, $include_inactive = false) {
	$products_newsdesk_count = 0;
	if ($include_inactive) {
		$products_newsdesk_newsdesk_query = tep_db_query("select count(*) as total from " . TABLE_NEWSDESK . " p, " . TABLE_NEWSDESK_TO_CATEGORIES . "
		p2c where p.newsdesk_id = p2c.newsdesk_id and p2c.categories_id = '" . $category_id . "'");
	} else {
		$products_newsdesk_newsdesk_query = tep_db_query("select count(*) as total from " . TABLE_NEWSDESK . " p, " . TABLE_NEWSDESK_TO_CATEGORIES .
		" p2c where p.newsdesk_id = p2c.newsdesk_id and p.newsdesk_status = '1' and p2c.categories_id = '" . $category_id . "'");
	}
	$products_newsdesk = tep_db_fetch_array($products_newsdesk_newsdesk_query);
	$products_newsdesk_count += $products_newsdesk['total'];

	if (USE_RECURSIVE_COUNT == 'true') {
		$child_categories_query = tep_db_query("select categories_id from " . TABLE_NEWSDESK_CATEGORIES . " where parent_id = '" . $category_id . "'");
		if (tep_db_num_rows($child_categories_query)) {
			while ($child_categories = tep_db_fetch_array($child_categories_query)) {
				$products_newsdesk_count += newsedsk_box_count_products_in_category($child_categories['categories_id'], $include_inactive);
			}
		}
	}

  return $products_newsdesk_count;
  }

  // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  function newsedsk_show_category($counter) {
  // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  global $foo_newdesk, $boxContent, $id;

  for ($a=0; $a<$foo_newdesk[$counter]['level']; $a++) {
	$boxContent .= "&nbsp;&nbsp;";
  }

  $boxContent .= '<div class="box_list">';
  $boxContent .= '<a class="boxLink" href="';

  if ($foo_newdesk[$counter]['parent'] == 0) {
	$newsPath_new = 'newsPath=' . $counter;
  } else {
	$newsPath_new = 'newsPath=' . $foo_newdesk[$counter]['path'];
  }

  $boxContent .= tep_href_link(FILENAME_NEWSDESK_INDEX, $newsPath_new);
  $boxContent .= '">';

  if ( ($id) && (in_array($counter, $id)) ) {
	$boxContent .= '<b>';
  }

  $boxContent .= $foo_newdesk[$counter]['name'];

  if ( ($id) && (in_array($counter, $id)) ) {
	$boxContent .= '</b>';
  }

  $boxContent .= '</a>';

  if (SHOW_COUNTS == 'true') {
	$products_newsdesk_in_category = newsedsk_box_count_products_in_category($counter);
	if ($products_newsdesk_in_category > 0) {
		$boxContent .= '&nbsp;(' . $products_newsdesk_in_category . ')';
	}
  }

  $boxContent .= '</div>';
  if (file_exists(DIR_WS_TEMPLATES . 'images/infobox/category_line.gif')) {
    $boxContent .= '<div class="box_line">' . tep_image(DIR_WS_TEMPLATES . 'images/infobox/category_line.gif') . '</div>';
  } 

  if ($foo_newdesk[$counter]['next_id']) {
	newsedsk_show_category($foo_newdesk[$counter]['next_id']);
  }

  }
  
  $boxContent = '';
  $categories_newsdesk_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from ". TABLE_NEWSDESK_CATEGORIES . " c, ". TABLE_NEWSDESK_CATEGORIES_DESCRIPTION . " cd where c.catagory_status = '1' and c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id='" . $languages_id ."' order by sort_order, cd.categories_name");
  while ($categories_newsdesk = tep_db_fetch_array($categories_newsdesk_query))  {
	$foo_newdesk[$categories_newsdesk['categories_id']] = array(
		'name' => $categories_newsdesk['categories_name'],
		'parent' => $categories_newsdesk['parent_id'],
		'level' => 0,
		'path' => $categories_newsdesk['categories_id'],
		'next_id' => false
	);

	if (isset($prev_id)) {
		$foo_newdesk[$prev_id]['next_id'] = $categories_newsdesk['categories_id'];
	}

	$prev_id = $categories_newsdesk['categories_id'];

	if (!isset($counter)) {
		$counter = $categories_newsdesk['categories_id'];
	}
  }

  newsedsk_show_category($counter);

  $boxContent = '<div class="box_kont">'.$boxContent;
  $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);
  }
}
?>
