<?php
include_once(XOOPS_ROOT_PATH . '/class/xoopstree.php');

// 本体
function sitemap_show()
{
	global $xoopsUser, $xoopsConfig;
    $plugin_dir = XOOPS_ROOT_PATH . "/modules/sitemap/plugin/";

	$block = array();

	$block['lang_home'] = _MB_SYSTEM_HOME;
	$block['lang_close'] = _CLOSE;
	$module_handler =& xoops_gethandler('module');
	$criteria = new CriteriaCompo(new Criteria('hasmain', 1));
	$criteria->add(new Criteria('isactive', 1));
	$criteria->add(new Criteria('weight', 0, '>'));
	$modules =& $module_handler->getObjects($criteria, true);
	$moduleperm_handler =& xoops_gethandler('groupperm');
	$groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
	$read_allowed =& $moduleperm_handler->getItemIds('module_read', $groups);
	foreach (array_keys($modules) as $i) {
		if (in_array($i, $read_allowed)) {
			if ($modules[$i]->getVar('dirname') == 'sitemap') {
				continue;
			}
			$block['modules'][$i]['id'] = $i;
			$block['modules'][$i]['name'] = $modules[$i]->getVar('name');
			$block['modules'][$i]['directory'] = $modules[$i]->getVar('dirname');
			$sublinks =& $modules[$i]->subLink();
			if (count($sublinks) > 0) {
				foreach($sublinks as $sublink){
					$block['modules'][$i]['sublinks'][] = array('name' => $sublink['name'], 'url' => XOOPS_URL.'/modules/'.$modules[$i]->getVar('dirname').'/'.$sublink['url']);
				}
			} else {
				$block['modules'][$i]['sublinks'] = array();
			}
            /// こっからプラグイン処理 by Ryuji
            //モジュールのプラグインがあれば、requireして、情報を得る。
            // モジュール側にプラグインが用意されているかチェック
            //  plugin modules/DIRNAME/include/sitemap.plugin.php
            //  lang   modules/DIRNAME/language/LANG/sitemap.php
            $mod = $modules[$i]->getVar("dirname");
            $plugin_flag = false;
            $mod_plugin_file = XOOPS_ROOT_PATH."/modules/".$mod."/include/sitemap.plugin.php";
            if(file_exists($mod_plugin_file)){
                $mod_plugin_lng = XOOPS_ROOT_PATH."/modules/".$mod."/language/".$xoopsConfig['language']."/sitemap.php";
                if(file_exists($mod_plugin_lng)){
                    include_once($mod_plugin_lng);
                }else{
                    $mod_plugin_lng = XOOPS_ROOT_PATH."/modules/".$mod."/language/english/sitemap.php";
                    if(file_exists($mod_plugin_lng)){
                        include_once($mod_plugin_lng);
                    }
                }
                require_once($mod_plugin_file);
                $plugin_flag = true;
            }else{
                // モジュール側になければ、sitemap内で探す。
                $mod_plugin_dir = $plugin_dir . $mod ."/";
                $mod_plugin_file = $mod_plugin_dir . $mod . ".php";
                $mod_plugin_lng = $mod_plugin_dir . $xoopsConfig['language'] . ".lng.php";
                //言語ファイルを読み込む
                if (file_exists($mod_plugin_lng)){
                    include_once($mod_plugin_lng);
                }else{
                    $mod_plugin_lng = $mod_plugin_dir . "english" . ".lng.php";
                    if (file_exists($mod_plugin_lng)){
                        include_once($mod_plugin_lng);
                    }
                }
                //プラグインを読み込む
                if (file_exists($mod_plugin_file)){
                    require_once($mod_plugin_file);
                    $plugin_flag = true;
                }
            }
    
            // プラグインをcall
            if($plugin_flag){
                if (function_exists("b_sitemap_" . $mod)){
                    //カテゴリリストを得る。
                    $_tmp = call_user_func("b_sitemap_" . $mod);
                    //$block['modules'][$i]['path'] = $_tmp["path"];
                    $block['modules'][$i]['parent'] = $_tmp["parent"];
                }
            }
        }
    }
	return $block;
}

// mylinksやnewsなどよくあるパターンのカテゴリリストを得るためのfunction
function sitemap_get_categoires_map($table, $id_name, $pid_name, $title_name, $url, $order = ""){
    global $xoopsModuleConfig;
    $mytree = new XoopsTree($table, $id_name, $pid_name);
    $xoopsDB =& Database::getInstance();
    
    $sitemap = array();
	$myts =& MyTextSanitizer::getInstance();

	$i = 0;
	$sql = 'SELECT ' . $id_name . ', ' . $title_name . ' FROM ' . $table . ' WHERE ' . $pid_name . ' = 0';
	if ($order != '')
	{
		$sql .= ' ORDER BY ' . $order;
	}
	$result = $xoopsDB->query($sql);
	while (list($catid, $name) = $xoopsDB->fetchRow($result))
	{
		// 親の出力
		$sitemap['parent'][$i]['id'] = $catid;
		$sitemap['parent'][$i]['title'] = $name;
		$sitemap['parent'][$i]['url'] = $url.$catid;

		// 子の出力
        if($xoopsModuleConfig["show_subcategoris"]){ // サブカテ表示のときのみ実行 by Ryuji
    		$j = 0;
    		$child_ary = $mytree->getChildTreeArray($catid, $order);
    		foreach ($child_ary as $child)
    		{
    			$count = strlen($child['prefix']) + 1; // MEMO prefixの長さでサブカテの深さを判定してる
    			$sitemap['parent'][$i]['child'][$j]['id'] = $child[$id_name];
    			$sitemap['parent'][$i]['child'][$j]['title'] = $child[$title_name];
    			$sitemap['parent'][$i]['child'][$j]['image'] = (($count > 3) ? 4 : $count);
    			$sitemap['parent'][$i]['child'][$j]['url'] = $url.$child[$id_name];
    
    			$j++;
    		}
        }
        $i++;
	}
    return $sitemap;
}

?>