<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: cache_portalcategory.php 24948 2011-10-18 03:21:06Z zhengqingpeng $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

function build_cache_portalcategory() {
	global $_G;

	$data = C::t('portal_category')->range();
	foreach($data as $key => $value) {
		$upid = $value['upid'];
		$data[$key]['level'] = 0;
		if($upid && isset($data[$upid])) {
			$data[$upid]['children'][] = $key;
			while($upid && isset($data[$upid])) {
				$data[$key]['level'] += 1;
				$upid = $data[$upid]['upid'];
			}
		}
	}
	$domain = $_G['setting']['domain'];
	$channelrootdomain = !empty($domain['root']) && !empty($domain['root']['channel']) ? $domain['root']['channel'] : '';
	$portaldomain = '';
	if(!empty($domain['app']['portal'])) {
		$portaldomain = 'http://'.$domain['app']['portal'].$_G['siteroot'];
	} elseif(!empty($domain['app']['default'])) {
		$portaldomain = 'http://'.$domain['app']['default'].$_G['siteroot'];
	} else {
		$portaldomain = $_G['siteurl'];
	}
	foreach($data as $key => $value){
		$url = $topid = '';
		$foldername = $value['foldername'];
		if($value['level']) {
			$topid = $key;
			$foldername = '';
			while($data[$topid]['upid']) {
				if($data[$topid]['foldername'] && $data[$key]['foldername']) {
					$foldername = $data[$topid]['foldername'].'/'.$foldername;
				}
				$topid = $data[$topid]['upid'];
			}
			if($foldername) $foldername = $data[$topid]['foldername'].'/'.$foldername;
		} else {
			$topid = $key;
		}
		$data[$key]['topid'] = $topid;

		if($channelrootdomain && $data[$topid]['domain']){
			$url = 'http://'.$data[$topid]['domain'].'.'.$channelrootdomain.'/';
			if($foldername) {

				if(!empty($value['upid'])) {
					$url .= $foldername;
				}
			} else {
				$url = $portaldomain.'portal.php?mod=list&catid='.$key;
			}
		} elseif ($foldername) {
			$url = $portaldomain.$foldername;
			if(substr($url, -1, 1) != '/') $url.= '/';
		} else {
			$url = $portaldomain.'portal.php?mod=list&catid='.$key;
		}
		$data[$key]['caturl'] = $url;

		if($data[$key]['shownav']) {
			$rs = C::t('common_nav')->update_by_type_identifier(4, $key, array('url' => addslashes($url), 'name' =>$value['catname']));
		}
	}

	savecache('portalcategory', $data);

	if(!function_exists('get_cachedata_mainnav')) {
		include_once libfile('cache/setting','function');
	}
	$data = $_G['setting'];
	list($data['navs'], $data['subnavs'], $data['menunavs'], $data['navmns'], $data['navmn'], $data['navdms']) = get_cachedata_mainnav();
	savecache('setting', $data);
}

?>