<?php
/**
 * 表示层左侧导航类
 *
 * @author Wzb
 *        
 */
class USL_LeftNav {
	/**
	 * 一个固定的左侧导航
	 *
	 * @param string $title 标题
	 * @param string $url 链接地址
	 * @return array
	 */
	public static function _OneNav($title, $url) {
		$leftNav = array ();
		$leftNav ['theName'] = $title;
		$temp = array ();
		$temp ['title'] = $title;
		$temp ['url'] = $url;
		$temp ['isfocus'] = 1;
		$leftNav ['list'] = array ($temp );
		return $leftNav;
	}
	
	/**
	 * 单页左侧导航
	 *
	 * @param string $title 单页标题
	 * @param string $tag 标签
	 * @return array
	 */
	public static function _Intro($title, $tag) {
		$leftNav = array ();
		$leftNav ['theName'] = $title;
		$temp = array ();
		$temp ['title'] = $title;
		$temp ['url'] = "Intro.php?tag=$tag";
		$temp ['isfocus'] = 1;
		$leftNav ['list'] = array ($temp );
		return $leftNav;
	}
	
	/**
	 * 系列单页左侧导航
	 *
	 * @param int $sortId 类别ID
	 * @param string $sortName 类别名称
	 * @param string $tag 标签
	 * @param int $currentId 当前显示的新闻ID
	 * @return array
	 */
	public static function _IntroNews($sortId, $sortName, $tag, $currentId) {
		$leftNav = array ();
		$leftNav ['theName'] = $sortName;
		$leftNav ['list'] = array ();
		$datalist = BLL_News::getList ( $sortId, false, 'id,title' );
		foreach ( $datalist as $row ) {
			$temp = array ();
			$temp ['title'] = $row ['title'];
			$temp ['url'] = "IntroNews.php?tag=$tag&theId=$row[id]";
			$temp ['isfocus'] = 0;
			if ($currentId == $row ['id']) {
				$temp ['isfocus'] = 1;
			}
			$leftNav ['list'] [] = $temp;
		}
		return $leftNav;
	}
	
	/**
	 * 新闻类别左侧导航
	 *
	 * @param int $sortId 类别ID
	 * @param string $sortName 类别名称
	 * @param string $tag 标签
	 * @param int $currentId 当前ID
	 */
	public static function _NewsSortList($rootSortId, $sortName, $tag, $currentId = 0) {
		$allList = BLL_NewsSort::getList ( $rootSortId, 'id,pid,sortName,serialNum' );
		$leftNav = array ();
		$leftNav ['theName'] = $sortName;
		if (count ( $allList ) > 0) {
			$leftNav ['list'] = self::getLeftNavList ( $rootSortId, $allList, $tag, $currentId );
		} else {
			$temp = array ();
			$temp ['title'] = $sortName;
			$temp ['url'] = "News_List.php?tag=$tag&theId=$rootSortId";
			$temp ['isfocus'] = 1;
			$leftNav ['list'] = array ($temp );
		}
		return $leftNav;
	}
	
	/**
	 * 递归获取左侧新闻类别列表
	 *
	 * @param int $rootSortId 根类别
	 * @param array $allList 所有的类别
	 * @param string $tag 标签
	 * @param int $currentId 当前ID
	 * @param array $tempArray 前一步的数据
	 * @param int $preId 上一步的ID
	 * @return array
	 */
	private static function getLeftNavList($rootSortId, $allList, $tag, $currentId, $tempArray = array(), $preId = 0) {
		$theList = array ();
		$tempList = array_merge ( array_filter ( $allList, create_function ( "\$a", "return intval(\$a['pid'])===$currentId;" ) ) );
		foreach ( $tempList as $row ) {
			$temp = array ();
			$temp ['id'] = $row ['id'];
			$temp ['pid'] = $row ['pid'];
			$temp ['title'] = $row ['sortName'];
			$temp ['url'] = "News_List.php?tag=$tag&theId=$row[id]";
			if (intval ( $preId ) === intval ( $row ['id'] )) {
				$temp ['isfocus'] = 1;
			}
			if (count ( $tempArray ) > 0 && intval ( $tempArray [0] ['pid'] ) == intval ( $row ['id'] )) {
				$temp ['list'] = $tempArray;
			}
			$theList [] = $temp;
		}
		$tempPid = array_merge ( array_filter ( $allList, create_function ( '$a', 'return intval($a["id"])===' . $currentId . ';' ) ) );
		if (intval ( $currentId ) !== intval ( $rootSortId ) && $currentId > 0 && count ( $tempPid ) > 0) {
			return self::getLeftNavList ( $rootSortId, $allList, $tag, $tempPid [0] ['pid'], $theList, $currentId );
		} else {
			return $theList;
		}
	}
}
?>