<?php
// $Id: viewcat.php,v 1.4 2005/10/28 11:56:02 ohwada Exp $

// 2005-09-04 K.OHWADA
// BUG 2932: dont work correctly when register_long_arrays = off

// 2004-01-20 K.OHWADA
// xoops_pagetitle
// weblinks_pagenavi.php

// 2004-12-12 K.OHWADA
// correspond to order of category arrange

// 2004-11-20 K.OHWADA
// recommend and mutual link display at upper rank

// 2004-10-20 K.OHWADA
// enable to modeify the display layout of a category path in the template 
// show category image
// use class pagenavi

// 2004/08/10 K.OHWADA
// use class weblinksCatlink
// enable to install this module two or more. 

//================================================================
// view category and link
// use class weblinksCategory, weblinksLink
// 2004/01/23 K.OHWADA
//================================================================

include "header.php";

include_once $MODULE_ROOT."/class/pagenavi.php";
include_once $MODULE_ROOT."/class/weblinks_pagenavi.php";
$pagenavi = new weblinks_pagenavi();

// BUG 2932: dont work correctly when register_long_arrays = off
$cid = get_get_int('cid');

$tbl_category->getAll();

if ( !( $tbl_category->isCid($cid) ) )
{
	redirect_header("index.php", 2, _WLS_NOMATCH);
	exit();
}

// enable to install this module two or more. 
$xoopsOption['template_main'] = $MODULE_DIRNAME."_viewcat.html";

include XOOPS_ROOT_PATH."/header.php";

assignIndex();

// recommend and mutual link display at upper rank
$pagenavi->get_get_parm();
$orderby  = 'recommend DESC, mutual DESC,';
$orderby .= $pagenavi->get_orderby();

$title     = $tbl_category->getValueByCid($cid, 'title', 'S');
$pagetitle = $MODULE_NAME.' - '.$title;
$xoopsTpl->assign('xoops_pagetitle', $pagetitle);

// --- category list ---
$catpath   = $tbl_category->getParentPath($cid);
$totallink = $tbl_catlink->getLinkNumByCid($cid);

// image
$image = '';
if ($weblinksConfig['cat_img_mode'] == 2)
{
	$imgurl = $tbl_category->getValueByCid($cid, 'imgurl');
	$image  = $tbl_category->makeImageLink( $imgurl );
}

$xoopsTpl->assign('category_id', $cid);
$xoopsTpl->assign('catpath', $catpath);
$xoopsTpl->assign('category_totallink', $totallink);
$xoopsTpl->assign('category_image', $image);

// get child category objects
$child_arr = $tbl_category->getCidArrayByPid($cid);
if ( count($child_arr) > 0 )
{
    $scount = 1;
	foreach($child_arr as $ch_id)
    {
		$title = $tbl_category->getValueByCid($ch_id, 'title', 'S');

		list ($space, $chcount, $infercategories) =
			$tbl_category->makeSubcategorys($ch_id);

		$sublink = $tbl_catlink->getLinkNumByCid($ch_id);

		$xoopsTpl->append('subcategories', array(
			'title'      => $title,
			'id'         => $ch_id,
			'totallinks' => $sublink,
			'count'      => $scount,
			'infercategories' => $infercategories,
			));

		$scount++;
    }
}

// --- link list , if first child exist ---
$numrows = $tbl_catlink->getNumByCid($cid);

if ($numrows>0)
{
	$limit  = $pagenavi->get_show();
	$offset = $pagenavi->get_start($numrows);

	if ($offset == -1)
	{
		echo "system error: numrows = $numrows <br>\n";
		include XOOPS_ROOT_PATH.'/footer.php';
		exit();
	}

	$xoopsTpl->assign('show_links', true);
	assignDisplayLink();

	$lid_arr = $tbl_link->getLidArrayByCid($cid, $orderby, $limit, $offset);

	foreach ($lid_arr as $lid) 
	{
		$record = $tbl_link->getRowByLid($lid);
		$link   = $tbl_link->makeDisplay($record);
		$xoopsTpl->append('links', $link);
	}

//if 2 or more items in result, show the sort menu
	if($numrows > 1)
	{
		$script = $MODULE_URL."/viewcat.php?cid=$cid";
		$navi   = $pagenavi->build_navi($script, $numrows);

		if ($navi == -1)
		{
			echo "system error: script = $script, numrows = $numrows <br>\n";
			include XOOPS_ROOT_PATH.'/footer.php';
			exit();
		}

		$pagenavi->assign_navi($xoopsTpl, $navi);
	}
}

// --- link list , if first child is zero ---
else
{
// get all child cat ids for a given cat id
	$array   = $tbl_category->getAllChildId($cid);
	$orderby = "time_update DESC";
	$maxlink = $xoopsModuleConfig['newlinks'];

	$lid_arr = $tbl_link->getLidArrayByCidArray($array,$orderby,$maxlink,0);

	if ( count($lid_arr)>0 )
    {
    	$xoopsTpl->assign('show_linklist', true);
    	assignDisplayLink();
    }

	foreach ($lid_arr as $lid) 
	{
		$record = $tbl_link->getRowByLid($lid);
		$link   = $tbl_link->makeDisplay($record);
		$xoopsTpl->append('links', $link);
	}

}

include XOOPS_ROOT_PATH.'/footer.php';

exit();

?>
