<?php
// $Id: xpress.php
// FILE		::	xpress.php
// AUTHOR	::	toemon
//
// WordPress 2.0+

if( ! defined( 'XOOPS_ROOT_PATH' ) ) exit ;
$mydirname = basename( dirname( dirname( __FILE__ ) ) ) ;

eval( '
function b_sitemap_' . $mydirname . '(){	
	return _sitemap_xpress("'.$mydirname.'");
}
') ;

if(!function_exists('_sitemap_xpress')){
	function _sitemap_xpress($mydirname){
		global $sitemap_configs;
		
		$xpress_sitemap =array();	
		$disp_sub =@$sitemap_configs["show_subcategoris"];		
		get_sitemap_category_list($mydirname,&$xpress_sitemap, 0, 0, $disp_sub,1);		
		return $xpress_sitemap;
	}
}

if(!function_exists('get_sitemap_category_list')){
	function get_sitemap_category_list($mydirname,$xpress_sitemap,$parent = 0, $parent_index ,$disp_sub,$depth) 
	{
		global $xoopsModule, $wp_db_version;

		if (!file_exists(XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/wp-includes/version.php')){
			return '';
		}
		
		include_once (XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/wp-includes/version.php');
		$db_version = $wp_db_version;
		$xoopsDB =& Database::getInstance();
		$myts =& MyTextSanitizer::getInstance();
		
		$prefix = $mydirname;
		if ($prefix == 'wordpress') $prefix ='wp';
		$wp_prefix = $xoopsDB->prefix($prefix);
		
		switch($db_version){
			case ($db_version>= 6000):
				$id_name = "term_taxonomy_id";
				$pid_name = "parent";
				$title_name = "name";
				$term_taxonomy_db = $wp_prefix . '_term_taxonomy';
				$terms_db = $wp_prefix . '_terms';
				$sql  = "SELECT $term_taxonomy_db.$id_name,$terms_db.$title_name ";
				$sql .= "FROM $term_taxonomy_db INNER JOIN $terms_db ON $term_taxonomy_db.term_id = $terms_db.term_id ";
				$sql .= "WHERE $term_taxonomy_db.parent = $parent AND $term_taxonomy_db.taxonomy = 'category'";
				break;
			default:
				$id_name = "cat_ID";
				$pid_name = "category_parent";
				$title_name = "cat_name";
				$cat_db = $wp_prefix . '_categories';
	 			$sql  = "SELECT $id_name , $title_name ";
				$sql .= "FROM  $cat_db ";
				$sql .= "WHERE $pid_name = $parent";				
				break;
		}
						
		$res =  $xoopsDB->query($sql, 0, 0);
	    if ($res === false){
	    	return ;
		} else {
			$index = 0;
			while($row = $xoopsDB->fetchArray($res)){
				if ($depth == 1){
					$xpress_sitemap['parent'][$index]['id'] = $row[$id_name];
					$xpress_sitemap['parent'][$index]['title'] = $myts->makeTboxData4Show( $row[$title_name] ) ;
					$url = "index.php?cat=";
					$xpress_sitemap['parent'][$index]['url'] = $url.$row[$id_name];
					if ($disp_sub){
						get_sitemap_category_list($mydirname,&$xpress_sitemap,$row[$id_name] , $index, $disp_sub,$depth + 1);	
					}
				} else {
					$xpress_sitemap['parent'][$parent_index]['child'][$index]['id'] = $row[$id_name];
					$xpress_sitemap['parent'][$parent_index]['child'][$index]['title'] = $myts->makeTboxData4Show( $row[$title_name] ) ;
					$url = "index.php?cat=";
					$xpress_sitemap['parent'][$parent_index]['child'][$index]['url'] = $url.$row[$id_name];
					$xpress_sitemap['parent'][$parent_index]['child'][$index]['image'] = (($depth > 3) ? 4 : $depth);;
//					get_sitemap_category_list($mydirname,&$xpress_sitemap,$row[$id_name] , $index, $disp_sub,$depth +1);
				}
				$index++;
			}
	    }
	}
}

?>