<?php
/*
　* @$Id
　* @
　* @Ver: 2.0
　* @Create: 2009-11-18 8:10
　* @Modify: 
　* @Author: CPWEB http://www.cpweb.net.cn
　* @Copyright (C) 2008 www.cpweb.net.cn. All Rights Reserved.
　* @License CPWEB is free software and use is subject to license terms
　*/
class GlobalAction extends Action  
{
	public $powered;
	public $channel;
	public $model=MODULE_NAME;
	public $cpweb_model=false;
	public $cpweb_find=array();
	private $_user=array();
	public $head=array();
	public $CPWEB=array();
	function _initialize()
	{
		if(C('STATUS')=='2'){
			die(C('STOPD'));
			exit();
		}
		$this->cpweb_find();
		$xt_set=$this->cpweb_setall('xt_set');
		foreach ($xt_set AS $key ){
			$this->CPWEB[$key['title']]=$key['set_values'];
		}
		if($this->CPWEB['status']=='2'){
			die($this->CPWEB['stopd']);
			exit();
		}
		$attach_set=$this->cpweb_setall('attach_set');
		foreach($attach_set AS $key ){
			$this->CPWEB[$key['title']]=$key['set_values'];
		}
		$seo_set=$this->cpweb_setall('seo_set');
		foreach ($seo_set AS $key ){
			$this->CPWEB[$key['title']]=$key['set_values'];
		}
		$member_set=$this->cpweb_setall('member_set');
		foreach ($member_set AS $key ){
			$this->CPWEB[$key['title']]=$key['set_values'];
		}
		
		//检测前台开启会员功能，并获取会员信息
		
		//加载全站单页介绍
		$PagesView=D("PagesView");
		$pages_list=$PagesView->where($map_p)->findall();
		$this->assign('pages_list',$pages_list);
		//加载导航栏
		$ChannelView = D('ChannelView');
		$map_c['Channel.opens'] = 1;
		$map_c['Channel.navigation'] = 1;
		$channelview = $ChannelView->where($map_c)->order("displayorder")->findAll();
		$this->assign('navigation',$channelview);
	}
	/*
	@运行头部项目
	*/
	public function cpweb_head(){
		if(MODULE_NAME!='Index') $this->head('title',$this->channel['name']);
		if($this->channel['seotitle']) $this->CPWEB['seotitle']=$this->channel['seotitle'];
		if($this->channel['seokeywords']) $this->CPWEB['seokeywords']=$this->channel['seokeywords'];
		if($this->channel['seodescription']) $this->CPWEB['seodescription']=$this->channel['seodescription'];
	}
	/*
	@加载验证码
	*/
	public function verify()
	{
		import("ORG.Util.Image");
		Image::buildImageVerify(4,1,'png',50,22,'cpweb');
	}
	/*
	@添加头部信息
	*/
	public function head($keey,$values,$i=' - '){
		if($this->head[$keey]){
			$this->head[$keey] = $values.$i.$this->head[$keey];
		}else{
			$this->head[$keey] = $values;
		}
	}
	/*
	@获取设置
	*/
	public function cpweb_set($title){
		$maps["title"]=$title;
		$Settings=M('Settings')->find($maps);
		return $Settings['set_values'];
	}
	public function cpweb_setall($type_values){
		$map["Type.type_values"]=$type_values;
		$Settings=D('SettingsView')->where($map)->findall();
		return $Settings;
	}
	/*
	@输出指定网站皮肤，设置皮肤变量
	*/
	public function cpweb_display($name){
		$tpl['SKIN_PATH']=__ROOT__.'/Cpweb_App/Tpl/'.C('DEFAULT_THEME').'/';
		$tpl['SITE_URL']=__ROOT__.'/';
		//加载头部信息
		$this->head['title']=$this->head['title'].' - '.$this->CPWEB['seotitle'];
		$this->head('title',$this->CPWEB['sitename']);
		if(MODULE_NAME=='Cpweb'){
			if($this->CPWEB['indexseotitle']) $this->head('title',$this->CPWEB['indexseotitle']);
		}
		$this->head['keywords']=$this->CPWEB['seokeywords'];
		$this->head['description']=$this->CPWEB['seodescription'];
		
		$this->assign('channel',$this->channel);
		$this->assign('tpl',$tpl);
		$this->assign('CPWEB',$this->CPWEB);
		$this->assign('head',$this->head);
		if($_GET['newtpl']) $name=$name.'new';
		$this->display($name);
	}
	/*
	@加载内容列表
	*/
	public function cpweb_list(){
		if($this->cpweb_find['map']){
			$map=$this->cpweb_find['map'];
		}
		if($this->cpweb_model){
			if($this->cpweb_model=='model'){
				if($this->cpweb_find['view']){
					if($this->channel['id']) $map['Channel.id']=$this->channel['id'];
					if($this->cpweb_find['category']){
						$map['Category.id']=$this->cpweb_find['category'];
					}
				}else{
					if($this->cpweb_find['category']){
						$map['category_id']=$this->cpweb_find['category'];
					}
				}
			}
			if($this->cpweb_find['view']){
				$List=D($this->model.$this->cpweb_find['view']);
			}else{
				$List=M($this->model);
			}
		}else{
			if($this->cpweb_find['view']){
				$List=D($this->model.$this->cpweb_find['view']);
			}else{
				$List=M($this->model);
			}
		}
		if($_REQUEST['key']){
			$map[$_REQUEST['key']['act']]=array('like',$_REQUEST['key']['keyword']."%");
			if($_REQUEST['key']['category_id']){
				$map[$this->model.'.category_id']=$_REQUEST['key']['category_id'];
			}
		}
		$count=$List->where($map)->count();
		if($count){
			if($this->cpweb_find['page']['open']){
				import("ORG.Util.Page");
				$p=new Page($count,$this->cpweb_find['limit']);
				$list=$List->limit($p->firstRow.','.$p->listRows)->order($this->cpweb_find['order'])->where($map)->findAll();
				$page=$p->show();
			}else{
				$list=$List->where($map)->limit($this->cpweb_find['limit'])->order($this->cpweb_find['order'])->findAll();
				$page='共'.$count.'条记录';
				dump($this->cpweb_find);
			}
			$this->cpweb_assign('list',$list);
			if($this->cpweb_find['page']['value']) $this->cpweb_assign('page',$page);
		}
		if($this->cpweb_find['page']['value']) $this->cpweb_assign('count',$count);
	}
	/*
	@加载内容
	*/
	public function cpweb_read(){
		if(!$this->cpweb_find['read']['id']){
			$id=intval($_REQUEST["id"]);
			if(!$id) $this->error('你所访问的内容不存在。');
			if($this->cpweb_find['view']){
				$map[$this->model.'.id']=$id;
			}else{
				$map['id']=$id;
			}
		}
		if($this->cpweb_model){
			if($this->cpweb_model=='model'){
				$map['Channel.id']=$this->channel['id'];
			}
			if($this->cpweb_find['view']){
				$Read=D($this->model.$this->cpweb_find['view']);
			}else{
				$Read=M($this->model);
			}
		}else{
			if($this->cpweb_find['view']){
				$Read=D($this->model.$this->cpweb_find['view']);
			}else{
				$Read=M($this->model);
			}
		}
		$read=$Read->where($map)->find();
		if (!$read) $this->error('你所访问的内容不存在。');
		if($this->cpweb_find['setInc']){
			if($this->cpweb_find['view']){
				$Read->setInc($this->model.'.'.$this->cpweb_find['setInc'],$this->model.'.id='.$id);
			}else{
				$Read->setInc($this->cpweb_find['setInc'],'id='.$id);
			}
		}
		if($this->cpweb_find['front']){
			if($this->cpweb_find['view']){
				$map[$this->model.'.id']=array('lt',$id);
			}else{
				$map['id']=array('lt',$id);
			}
			if($this->cpweb_find['view']){
				$front = $Read->where($map)->order($this->model.'.id desc')->find();
			}else{
				$front = $Read->where($map)->order('id desc')->find();
			}
			$f=!$front?'没有了':'<A href="'.__APP__.'/'.$front['folder'].'/read/id/'.$front['id'].'.html'.'">'.$front['subject'].'</A>';
			$this->cpweb_assign('front',$f);
		}
		if($this->cpweb_find['after']){
			if($this->cpweb_find['view']){
				$map[$this->model.'.id']=array('gt',$id);
			}else{
				$map['id']=array('gt',$id);
			}
			$after = $Read->where($map)->find();
			$a=!$after?'没有了':'<A href="'.__APP__.'/'.$after['folder'].'/read/id/'.$after['id'].'.html'.'">'.$after['subject'].'</A>';
			$this->cpweb_assign('after',$a);
		}
		if($this->model!='Pages') $this->head('title',$read['title']);
		$this->head('title',$read['subject']);
		$this->cpweb_assign('read',$read);
	}
	/*
	@加载分类列表
	*/
	public function cpweb_category_list(){
		$map['Model.address'] = $this->model;
		$map['Channel.id']=$this->channel['id'];
		$Category=D('CategoryView')->order("Category.displayorder desc,Category.id")->where($map)->findall();
		$this->cpweb_assign('category_list',$Category);
	}
	public function cpweb_category_list_a($i){
		$Category=D('CategoryView')->where($map)->findall();
		return $Category;
	}
	/*
	@加载分类
	*/
	public function cpweb_category(){
		$map['Model.address'] = $this->model;
		$map['Channel.id']=$this->channel['id'];
		$map['Category.id']=$this->cpweb_find['category'];
		$Category=D('CategoryView')->order("Category.displayorder desc,Category.id")->where($map)->find();
		$this->head('title',$Category['title']);
		if($Category['seotitle']) $this->CPWEB['seotitle']=$Category['seotitle'];
		if($Category['seokeywords']) $this->CPWEB['seokeywords']=$Category['seokeywords'];
		if($Category['seodescription']) $this->CPWEB['seodescription']=$Category['seodescription'];
		$this->cpweb_assign('category',$Category);
	}
	/*
	@返回结果
	*/
	public function cpweb_assign($value,$name){
		if($this->cpweb_find['assign']['open']){
			return $this->cpweb_find['assign']['value'][$value]=$name;
		}else{
			$this->assign($value,$name);
		}
	}
	/*
	@栏目标签
	*/
	public function cpweb_tag(){
		if($_REQUEST['limit']){
			$this->cpweb_find['limit']=$_REQUEST['limit'];
		}
		$this->cpweb_find['assign']['open']=true;
		$this->cpweb_page=false;
		$this->cpweb_list();
		import("@.ORG.CpwebLi");
		$Cpwebli=new CpwebLi();
		$cpweb_li=$Cpwebli->li_list($this->cpweb_find['assign']['value']['list']);
		echo $cpweb_li;
		//$xm=xml_encode($this->cpweb_find['assign']['value']['list']);
		//echo($xm);
	}
	/*
	@格式化查询条件
	*/
	public function cpweb_find(){
		$this->cpweb_find['page']['open']=true;
		$this->cpweb_find['page']['value']=true;
		$this->cpweb_find['limit']=10;
		$this->cpweb_find['order']='id desc';
		$this->cpweb_find['map']='';
	}
}
?>