<?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{
	private $_user=array();
	public $CPWEB=array();
	public $model=MODULE_NAME;
	public $cpweb_model=false;
	public $cpweb_thumb=false;
	public $cpweb_find=array();
	public $cpweb_ajax=false;
	function _initialize()
	{
		header("Content-Type:text/html; charset=utf-8");
		if($_REQUEST['ajax']){
			$this->cpweb_ajax=true;
		}
		//初始化时获取用户ID和用户组ID
		$this->_user['member_id']=intval(Session::get(C('USER_AUTH_KEY')));
		$this->_user['username']=Session::get('username');
		$this->_user['log']=Session::get('lastlogintime');
		if($this->_user['member_id']==false)
		{
			if($this->cpweb_ajax){
				$this->cpweb_error('请登录后再进行此操作。');
			}else{
				redirect(__APP__.'/Public/login');
			}
		}
		$user=D('Member')->find($this->_user['member_id']);
		$this->_user=array_merge($this->_user,$user);
		//加载设置
		$xt_set=$this->cpweb_setall('xt_set');
		foreach ($xt_set AS $key){
			$this->CPWEB[$key['title']]=$key['set_values'];
		}
		$attach_set=$this->cpweb_setall('attach_set');
		foreach ($attach_set AS $key){
			$this->CPWEB[$key['title']]=$key['set_values'];
		}
		//设置网站路径
		$tpl['SITE_URL']=__ROOT__.'/';
		$this->assign('tpl',$tpl);
		$this->assign('CPWEB',$this->CPWEB);
		$this->assign('USER',$this->_user);
		$this->cpweb_find();
	}
	/*
		@ 公共操作
		@ 删除/锁定/推荐/置顶/审核/移动
		@ $dbclass 指定操作数据库默认为 $this->name		
	*/
	public function _subAction($dbclass="")
	{
		$getid=$_REQUEST['id'];
		if (!$getid) $this->cpweb_error('未选择记录') ;
		$getids=implode(',',$getid);
		//如果GETID为数组则需要判断是否有批量操作权限
		if (is_array($getid)) {
			$security=M('Usergroup')->find('id='.$this->groupId);
				if (!$security) $this->cpweb_error('未获取用户组ID权限，请联系系统管理员');//未获取用户组ID权限，请联系系统管理员
				$allowbat=intval($security['allowbat']);
					if($allowbat!==1)$this->cpweb_error('当前用户没有批量操作权限');//当前用户没有批量操作权限
		}
		$id = is_array($getid)?$getids:$getid;
		$act =$_REQUEST['act'];
		$category =$_REQUEST['category'];
		//过滤操作类型
		if (!$act) $this->cpweb_error('操作类型必须选择');//操作类型必须选择
		$allowact=array('good','ugood','top','utop','ischecked','uischecked','remove','delete');
		if (!in_array($act,$allowact))$this->cpweb_error('未知操作');//未知操作
		if (!$id) $this->cpweb_error('ID丢失');
		$dbname=$dbclass==""?$this->name:$dbclass;
		$Category=D($dbname);
		switch ($act){
			case 'good':$Result=D($dbname)->execute('UPDATE __TABLE__ SET `isgood`=1 WHERE `id` IN ('.$id.')');$say='推荐成功';break;//推荐
			case 'ugood':$Result=D($dbname)->execute('UPDATE __TABLE__ SET `isgood`=0 WHERE `id` IN ('.$id.')');$say='取消推荐成功';break;//取消推荐
			case 'top':$Result=D($dbname)->execute('UPDATE __TABLE__ SET `istop`=1 WHERE `id` IN ('.$id.')');$say='置顶成功';break;//置顶
			case 'utop':$Result=D($dbname)->execute('UPDATE __TABLE__ SET `istop`=0 WHERE `id` IN ('.$id.')');$say='取消置顶成功';break;//取消置顶
			case 'ischecked':$Result=D($dbname)->execute('UPDATE __TABLE__ SET `ischecked`=0 WHERE `id` IN ('.$id.')');$say='审核成功';;break;//审核
			case 'uischecked':$Result=D($dbname)->execute('UPDATE __TABLE__ SET `ischecked`=1 WHERE `id` IN ('.$id.')');$say='取消审核成功';;break;//取消审核
			case 'remove':$Result=D($dbname)->execute('UPDATE __TABLE__ SET `cid`='.$category.' WHERE `id` IN ('.$id.')');$say='移动成功';;break;//移动
			case 'delete':$Result=D($dbname)->execute('DELETE FROM __TABLE__ where `id` IN ('.$id.')');$say='删除成功';;break;//删除
			default:$this->cpweb_error(L('_OPERATION_WRONG_'));break;//未知操作类型
		}		
		if($Result===false){
			$this->cpweb_error(L('_OPERATION_FAIL_'));
		}else{
			$this->assign('jumpUrl',__URL__/$dbname);
	        $this->cpweb_success($say);
		}
	}
	/*
	@新公共上传
	*/
	public function cpweb_upload_new(){
		$path=date('Ym');
		$path=C('ATTACHDIR').'/new/'.$path.'/';
		if (!is_dir($path))	@mk_dir($path);    
		import("@.ORG.UploadFile");
        $upload = new UploadFile();
		$attachsize=C('ATTACHSIZE');
		$attach=C('ATTACH');
		$thumbmaxwidth=C('THUMBMAXWIDTH');
		$upload->maxSize=$attachsize;
		$upload->allowExts=explode(',',strtolower(C('attachext')));
		$upload->savePath=$path;
		$upload->saveRule='uniqid';
		isset($this->cpweb_thumb)?$upload->thumb=$this->cpweb_thumb:$upload->thumb=$attach;
		isset($width)?$upload->thumbMaxWidth=$width:$upload->thumbMaxWidth=C('thumbmaxwidth');
		isset($height)?$upload->thumbMaxHeight=$height:$upload->thumbMaxHeight=C('thumbmaxheight');
		$upload->thumbPrefix ='';
		$upload->thumbSuffix =C('thumbsuffix');
        if(!$upload->upload()){
			$return_upload=array(0,$upload->getErrorMsg(),0);
			return $return_upload;
        }else{
			$cpweb_up=$upload->getUploadFileInfo();
			$cpweb_newup['cpweb_url']=$upload->savePath.$cpweb_up[0]['savename'];
			$cpweb_newup['cpweb_newup']=1;
			if($this->cpweb_thumb) $cpweb_thumb=explode('.',$cpweb_up[0]['savename']);
			if($this->cpweb_thumb) $cpweb_newup['cpweb_thumb_url']=$upload->savePath.$cpweb_thumb[0].C('thumbsuffix').'.'.$cpweb_thumb[1];
			return $cpweb_newup;
    	}
	}
	/*
	@ajax上存附件
	*/
	public function cpweb_ajax_upload_attach(){
		if($this->cpweb_ajax){
			$this->ajaxReturn(0,'链接成功',1);
			$up=$this->cpweb_upload_new();
			if($up['cpweb_newup']==1){
				$this->cpweb_success($up);
			}else{
				$this->cpweb_error($up);
			}
		}
	}
	/*
	@获取网站设置
	*/
	public function cpweb_set($title){
		$maps["title"]=$title;
		$Settings=M('Settings')->where($maps)->find();
		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_list(){
		$Curd=A('Curd');
		$Curd->cpweb_model=$this->cpweb_model;
		$Curd->cpweb_find=$this->cpweb_find;
		$Curd->cpweb_list();
		$this->assign('count',$Curd->cpweb_count);
		if($Curd->cpweb_count){
			$this->assign('list',$Curd->cpweb_list);
			$this->assign('page',$Curd->cpweb_page);
		}
	}
	/*
	@ajax加载内容列表
	*/
	public function cpweb_ajax_list(){
		$name='model';
		$view='View';
		if($name=='model'){
			if($view){
				$List=D($this->model.$view);
			}else{
				$List=M($this->model);
			}
		}elseif($name=='module'){
			if($view){
				$List=D($this->model.$view);
			}else{
				$List=M($this->model);
			}
		}else{
			if($view){
				$List=D($this->model.$view);
			}else{
				$List=M($this->model);
			}
		}
		$listRows=10;
		$map[$_REQUEST["act"]]=array('like',$_REQUEST['keyword']."%");
		$list=$List->order('id desc')->where($map)->findAll();
		$counter=0;
		foreach($list AS $key => $value){
			if($counter>0){
				$options=$options."|";
			}
			$counter++;
			$options=$options.$value["subject"];
		}
		print($options);
	}
	/*
	@加载内容
	*/
	public function cpweb_read(){
		$Curd=A('Curd');
		$Curd->model=$this->model;
		$Curd->cpweb_model=$this->cpweb_model;
		$Curd->cpweb_find=$this->cpweb_find;
		$Curd->cpweb_read();
		$this->assign('read',$Curd->cpweb_read);
	}
	/*
	@加载栏目列表
	*/
	public function cpweb_channel(){
		$map['Model.address'] = $this->model;
		$Channel=D('ChannelView')->order("displayorder desc,id desc")->where($map)->findall();
		$this->assign('Channel',$Channel);
	}
	/*
	@加载分类列表
	*/
	public function category(){
		$this->cpweb_category();
		$this->assign('title','分类管理');
		$this->display('Category:index');
	}
	public function cpweb_category(){
		$map['Model.address'] = $this->model;
		$Category=D('CategoryView')->order("Category.displayorder desc,Category.id desc")->where($map)->findall();
		$this->assign('category',$Category);
	}
	/*
	@添加内容
	*/
	public function adds()
	{
		$this->cpweb_add();
	}
	public function categoryadd()
	{
		$this->cpweb_channel();
		$this->assign('title','添加分类');
		$this->display('Category:add');
	}
	public function categoryadds()
	{
		$Curd=A('Curd');
		$Curd->cpweb_find=$this->cpweb_find;
		$Curd->model='Category';
		$Curd->cpweb_add();
	}
	public function cpweb_add(){
		$Curd=A('Curd');
		$Curd->cpweb_find=$this->cpweb_find;
		$Curd->cpweb_add();
	}
	/*
	@编辑内容
	*/
	public function edits()
	{
		$this->cpweb_edit();
	}
	public function categoryedit()
	{
		$this->cpweb_channel();
		$this->model="Category";
		$this->cpweb_model='module';
		$this->cpweb_read();
		$this->assign('title','编辑分类');
		$this->display('Category:edit');
	}
	public function categoryedits()
	{
		$Curd=A('Curd');
		$Curd->model='Category';
		$Curd->cpweb_edit();
	}
	public function cpweb_edit(){
		$Curd=A('Curd');
		$Curd->cpweb_find=$this->cpweb_find;
		$Curd->cpweb_edit();
	}
	/*
	@删除内容
	*/
	public function delete()
	{
		$this->cpweb_delete();
	}
	public function categorydelete()
	{
		$this->model="Category";
		$this->cpweb_delete();
	}
	public function cpweb_delete(){
		$Curd=A('Curd');
		$Curd->model=$this->model;
		$Curd->cpweb_find['xt']=true;
		$Curd->cpweb_delete();
	}
	public function cpweb_error($name){
		if($this->cpweb_ajax){
			$this->ajaxReturn(0,$name,0);
		}else{
			$this->error($name);
		}
	}
	public function cpweb_success($name){
		if($this->cpweb_ajax){
			$this->ajaxReturn(0,$name,1);
		}else{
			$this->success($name);
		}
	}
	/*
	@格式化查询条件
	*/
	public function cpweb_find(){
		$cpweb_find['attach']['path']=true;
		$cpweb_find['attach']['thumb']=true;
		$this->cpweb_find=$cpweb_find;
	}
	/*
	@删除附件
	*/
	public function cpweb_delete_attach($name=false,$thumb=false){
		if($_REQUEST['ajax_delete_attach']){
			$oldattachment=$_REQUEST['attach'];
		}else{
			if($thumb){
				$this->cpweb_new_delete_attach($name);
			}else{
				$this->cpweb_new_delete_attachthumb($name);
			}
		}
		if($_REQUEST['ajax_delete_attach']){
			$this->ajaxReturn(0,L('CPWEB_SUCCESS_DELETE'),1);
		}
	}
	/*
	@ajax删除附件
	*/
	public function cpweb_ajax_delete_attach(){
		$Curd=A('Curd');
		$Curd->cpweb_model=$this->cpweb_model;
		$Curd->cpweb_find=$this->cpweb_find;
		$Curd->cpweb_read();
		$read=$Curd->cpweb_read;
		if($read['attachment']) $this->cpweb_new_delete_attach($read);
		if($read['attachthumb']) $this->cpweb_new_delete_attachthumb($read);
		$this->ajaxReturn(0,L('CPWEB_SUCCESS_DELETE'),1);
	}
	/*
	@删除原文件
	*/
	public function cpweb_new_delete_attach($name){
		$delete=attach($name,$this->model,C('ATTACHDIR'),false);
		$this->cpweb_new_delete_file($delete);
	}
	/*
	@删除缩略图
	*/
	public function cpweb_new_delete_attachthumb($name){
		$delete=attachthumb($name,$this->model,c('ATTACHDIR'),false);
		$this->cpweb_new_delete_file($delete);
	}
	/*
	@删除文件
	*/
	public function cpweb_new_delete_file($name){
		$delete=@unlink(CPWEB_ROOT.$name);
	}
}
?>