<?php

/*
  PHP version 5
  Copyright (c) 2002-2018 ECISP.CN、EarcLink.COM
  警告：这不是一个免费的软件，请在许可范围内使用，请尊重知识产权，侵权必究，举报有奖
  作者：黄祥云 E-mail:6326420@qq.com  QQ:6326420 TEL:18665655030
  ESPCMS官网介绍：http://www.ecisp.cn	企业建站：http://www.earclink.cn
 */

use modul_application\ESPCMS_NevigateDB;
use inc_application\link\PublicLink;

class lib_nav {

	public static function call_nav($parameter, $filename = 'nav', $outHTML = null) {
		global $espcms_db_cache, $espcms_command;
		$espcms_lib_template = espcms_web_template();
		$espcms_lib_template->libfile = true;
		$para = espcms_array_getvalue($parameter);
		$lng = espcms_lancode();
		$db_table = ESPCMS_DB_PREFIX . 'nevigate';
		$para['hover_id'] = espcms_ismatches($para['hover_id']) ? $para['hover_id'] : 0;
		$para['doc_id'] = espcms_ismatches($para['doc_id']) ? $para['doc_id'] : 0;
		$para['ishome'] = espcms_ismatches($para['ishome']) ? $para['ishome'] : 1;
		$para['ismenu'] = espcms_ismatches($para['ismenu']) ? $para['ismenu'] : 0;
		if (!espcms_ismatches($para['iswap'])) {
			$para['iswap'] = ESPCMS_WEB_TYPE == 'WAP' && defined('ESPCMS_WEB_TYPE') ? 1 : 0;
		}
		$para['iswap'] = espcms_ismatches($para['iswap']) ? $para['iswap'] : 0;
		$para['isacs'] = espcms_ismatches($para['isacs']) ? $para['isacs'] : 0;
		$para['child'] = $para['child'] && espcms_ismatches($para['child']) ? $para['child'] : 1;
		$cache_key = ESPCMS_CACHE_DBLIST_NAME . $db_table . '_' . $espcms_lib_template->lng . '_' . $espcms_command['IS_HTML'] . '_' . $espcms_command['IS_REWRITE'] . '_' . $para['ismenu'] . '_' . $para['iswap'] . '_' . $para['isacs'] . '_' . $para['child'] . '_' . $para['ishome'];
		$nevigate_array = $espcms_db_cache->cacheCheck($cache_key, false);
		if (!$nevigate_array) {
			$nev_key = 0;
			if ($para['ishome']) {
				$home_array['nevigate_name'] = espcms_web_lan_pack($lng, 'home_title');
				$home_array['title'] = espcms_web_lan_pack($lng, 'home_title');
				$home_array['path'] = 'index';
				$home_array['hover_id'] = 0;
				$home_array['link'] = PublicLink::get_link($lng, 'home');
				$nevigate_array[$nev_key] = $home_array;
			}
			$talbe_field_array = espcms_field_out($db_table);
			$db_where = " AND lng='$lng' AND nevigate_uid=0";
			if (espcms_ismatches($para['ismenu']) && $para['ismenu']) {
				$db_where .= " AND nevigate_ismenu=" . $para['ismenu'];
			}
			if (espcms_ismatches($para['iswap']) && $para['iswap']) {
				$db_where .= " AND nevigate_iswap=" . $para['iswap'];
			}
			if (espcms_ismatches($para['isacs']) && $para['isacs']) {
				$db_where .= " AND nevigate_isaccessory=" . $para['isacs'];
			}
			$menu_array = espcms_db_list_array($db_table, $talbe_field_array, $db_where, 'nevigate_pid,nevigate_id DESC');
			if (is_array($menu_array)) {
				foreach ($menu_array as $key => $value) {
					$nevigate_array_link = ESPCMS_NevigateDB::get_nevigate_link_creat($value);
					if (!is_array($nevigate_array_link)) {
						continue;
					}
					$nev_key = $key == 0 && !$para['ishome'] ? 0 : $nev_key + 1;
					$nevigate_array[$nev_key] = $nevigate_array_link;
					if ($para['child'] > 1) {
						$nevigate_two_array = ESPCMS_NevigateDB::get_nevigate_last_list($value['lng'], $value['nevigate_id'], 0, false);
						if (is_array($nevigate_two_array) && count($nevigate_two_array) > 0) {
							foreach ($nevigate_two_array as $two_key => $last_read) {
								$nevigate_two_array[$two_key] = ESPCMS_NevigateDB::get_nevigate_link_creat($last_read);
								if ($para['child'] > 2) {
									$nevigate_three_array = ESPCMS_NevigateDB::get_nevigate_last_list($value['lng'], $last_read['nevigate_id'], 0, false);
									if (is_array($nevigate_three_array) && count($nevigate_three_array) > 0) {
										foreach ($nevigate_three_array as $three_key => $three_read) {
											$nevigate_three_array[$three_key] = ESPCMS_NevigateDB::get_nevigate_link_creat($three_read);
										}
										$nevigate_two_array[$two_key]['childArray'] = $nevigate_three_array;
									}
								}
							}
							$nevigate_array[$nev_key]['childArray'] = $nevigate_two_array;
						}
					}
				}
			}
			$espcms_db_cache->cacheSave($cache_key, $nevigate_array);
		}
		if ($para['hover_id'] && $para['path'] == 'type' && !$para['doc_id']) {
			$hover_id = ESPCMS_NevigateDB::get_menulink_hover($para['hover_id']);
		} elseif ($para['hover_id'] && $para['path'] == 'article' && $para['doc_id']) {
			$nev_doc = ESPCMS_NevigateDB::get_nevigate_link_view($para['doc_id'], 'article');
			$hover_id = $nev_doc ? $para['doc_id'] : ESPCMS_NevigateDB::get_menulink_hover($para['hover_id']);
			$para['path'] = $nev_doc ? 'article' : 'type';
		} else {
			$hover_id = espcms_ismatches($para['current']) ? $para['current'] : 0;
		}
		if (is_array($nevigate_array) && count($nevigate_array) > 0) {
			foreach ($nevigate_array as $hover_key => $hover_read) {
				if ($para['path'] == 'index' && $hover_read['path'] == 'index') {
					$nevigate_array[$hover_key]['hover'] = 1;
				} else {
					$nevigate_array[$hover_key]['hover'] = $hover_id == $hover_read['nevigate_linkid'] && $hover_read['nevigate_modul'] == $para['path'] ? 1 : 0;
				}
			}
		}
		$espcms_lib_template->into('para', $para);
		$espcms_lib_template->into('array', $nevigate_array);
		if (!empty($outHTML)) {
			$output = $espcms_lib_template->fetch(null, null, $outHTML);
		} else {
			$output = $espcms_lib_template->fetch('lib/' . $filename);
		}
		return $output;
	}

}
