<?php
namespace admin\Controller;
use Think\Controller;
class CommonController extends Controller {
	public function _initialize(){
		$user=D('User');
		if (!$user->is_admin_login())
		{
			exit('<script type="text/javascript">top.location="'.RU('/login/login').'";</script>');	
		}
	}
	//获取后台菜单
	public function get_admin_menu($pid='0'){
		$menu=uu_get_cache('admin_menu');
		$arr=array();
		foreach ($menu as $key =>$v){ 
			if ($v['m_pid']==$pid){
			$arr[]=$v;
			} 
    	}
		return $arr;
	}
	//跨入口打开HOME详细页
	public function view(){
		
		$url_model=C('URL_MODEL');
		if ($url_model<>'0' && $url_model<>'1')
		{
		header("Content-type: text/html; charset=utf-8");
		exit("当前URL_MODEL设置为{$url_model}，不支持此跳转！");
		}
		$url=uu_get_url();
		$url=str_replace("admin.php","index.php",$url);
		header("Location: {$url}"); 
	}
/**
 * 删除目录
 */
public function rmdirs($dir,$rm_self=false)  
{  
    $d = dir($dir);  
    while (false !== ($child = $d->read())){  
        if($child != '.' && $child != '..'){  
            if(is_dir($dir.'/'.$child)){
                $this->rmdirs($dir.'/'.$child,true);  
            } 
            else{
                unlink($dir.'/'.$child);  
            } 
        }  
    }  
    $d->close();  
    $rm_self && rmdir($dir);  
}
/**
 * 获取分类层级
 */
public function get_cat_level($type='1')
{
	$map['c_type']=intval($type);
	$map['c_pid'] =0;
	$list = M("Category")->where($map)->select();
	if (empty($list))
	{
		return '0';
	}
	foreach($list as $val)
	{
		$cmap['c_pid'] =intval($val['c_id']);
		$info=M("Category")->where($cmap)->select();
		if (!empty($info))
		{
			return '2';
		}
	}
	return '1';
}
/**
 * 生成分类JS
 */
public function writeJS()
{
	$data = M("Category");
	$map['c_type']=1;
	$map['c_pid'] =0;
	$arrayLevel=1;
	$list = $data->where($map)->order('c_sort desc, c_id asc')->select();
	foreach($list as  $key => $value){
		$parentarr[]="\"".$value['c_id'].",".$value['c_title']."\"";
	}	
 	$content = "//JavaScript Document 生成时间：".date("Y-m-d  H:i:s")."\n\n";
	$content .= "var uu_cat_question_p=new Array(".implode(',',$parentarr).");\n";	
	$content .= "var uu_cat_question  =new Array();\n";
	unset($map,$parentarr);
	foreach($list as $val){
		$map['c_pid'] =$val['c_id'];
		$list1 = $data->where($map)->order('c_sort desc, c_id asc')->select();
		if (!empty($list1))
		{	
			foreach($list1 as $val1)
			{
			$sarr[]=$val1['c_id'].",".$val1['c_title'];
			}
			$content .= "uu_cat_question[".$val['c_id']."]=\"".implode('|',$sarr)."\"; \n";
			$arrayLevel=2;
			unset($sarr);
		}
	}
	$content .= "var uu_cat_question_level  ={$arrayLevel};\n";
	///标签分类
	unset($map);
	$map['c_type']=2;
	$map['c_pid'] =0;
	$arrayLevel=1;
	$list = $data->where($map)->order('c_sort desc, c_id asc')->select();
	foreach($list as  $key => $value){
		$parentarr[]="\"".$value['c_id'].",".$value['c_title']."\"";
	}
	$content .= "var uu_cat_tag_p=new Array(".implode(',',$parentarr).");\n";	
	$content .= "var uu_cat_tag  =new Array();\n";
	unset($map,$parentarr);
	foreach($list as $val){
		$map['c_pid'] =$val['c_id'];
		$list1 = $data->where($map)->order('c_sort desc, c_id asc')->select();
		if (!empty($list1))
		{	
			foreach($list1 as $val1)
			{
			$sarr[]=$val1['c_id'].",".$val1['c_title'];
			}
			$content .= "uu_cat_tag[".$val['c_id']."]=\"".implode('|',$sarr)."\"; \n";	
			$arrayLevel=2;
			unset($sarr);
		}
	}
	$content .= "var uu_cat_tag_level  ={$arrayLevel};\n";
	////写入文件
	$path="data/cache_cat.js";
	if (file_put_contents($path,$content)){
 		return true;
	}else{
		$this->error($path.'生成JS失败！');
	}
}


}
?>