<?php
/**
 * 插件：附件管理
 * By IT平民
**/

class Admin extends PlugController
{
	public $num=0;
	public function __construct()
	{
		parent::__construct();
		if(!C('plug_attachment'))
		{
			die('插件未安装');
		}
		$this->check_admin();
	}

	public function Index()
	{
		$type=getint(F('get.type'),0);
		$gid=getint(F('get.gid'),0);
		$where="1=1";
		$where_query='';
		$order="id desc";
		if(in_array($type,[1,2,3]))
		{
			$where.=" and file_type=$type";
			$where_query=" and file_type=$type";
		}
		if($gid>0)
		{
			$where.=" and gid=$gid";
		}
		$total=$this->db->count("select count(1) from sd_attachment where gid=0 $where_query");
		$this->assign('type',$type);
		$this->assign('gid',$gid);
		$this->assign('where',$where);
		$this->assign('where_query',$where_query);
		$this->assign('order',$order);
		$this->assign('total',$total);
		$this->display("index.php");
	}

	public function del()
	{
		$id=getint(F('get.id'),0);
		$rs=$this->db->row("select file_url,file_local from sd_attachment where id=$id limit 1");
		if($rs)
		{
			$local=$rs['file_local'];
			$file=$rs['file_url'];
			if(!APP_DEMO) $this->db->del('sd_attachment',"id=$id");
			if($local==1)
			{
				if(!APP_DEMO) @unlink(SYS_PATH.$file);
			}
			else
			{
				if(!APP_DEMO) sdcms_api::delfile($file,$local);
			}
		}
		$this->success('删除成功');
		$this->add_log($this->msg);
	}

	public function clear()
	{
		self::get_files('upfile');
		$this->success('共清理'.$this->num.'个缩略图');
		$this->add_log($this->msg);
	}

	public function btach()
	{
		if(IS_POST)
		{
			$id=F('id');
			$arr=explode(',',$id);
			foreach($arr as $key=>$val)
			{
				$val=getint($val,0);
				$rs=$this->db->row("select file_url,file_local from sd_attachment where id=$val limit 1");
				if($rs)
				{
					$local=$rs['file_local'];
					$file=$rs['file_url'];
					if(!APP_DEMO) $this->db->del('sd_attachment',"id=$val");
					if($local==1)
					{
						if(!APP_DEMO) @unlink(SYS_PATH.$file);
					}
					else
					{
						if(!APP_DEMO) sdcms_api::delfile($file,$local);
					}
				}
			}
		}
		$this->success('删除成功');
		$this->add_log($this->msg);
	}

	public function move()
	{
		if(IS_POST)
		{
			$id=F('id');
			$gid=getint(F('gid'),0);
			if($gid>=0)
			{
				$d['gid']=$gid;
				if(!APP_DEMO) $this->db->update("sd_attachment",'id in('.$id.')',$d);
				$this->success('操作成功');
			}
			$this->add_log($this->msg);
		}
		else
		{
			$this->display("move.php");
		}
	}

	public function switchs()
	{
		$id=getint(F('get.id'),0);
		$islock=F('islock');
		$rs=$this->db->row("select aid from sd_attachment_group where aid=".$id." limit 1");
		if($rs)
		{
			if(!APP_DEMO) $this->db->update('sd_attachment_group','aid='.$id.'',['islock'=>$islock]);
		}
		$this->success('修改成功');
		$this->add_log($this->msg);	
	}


	public function lists()
	{
		if(IS_POST)
		{
			$mid=F('mid');
			$ordnum=F('ordnum');
			foreach($mid as $key=>$val)
			{
				if(!APP_DEMO) $this->db->update('sd_attachment_group','aid='.getint($val,0).'',['ordnum'=>getint($ordnum[$key],0)]);
			}
			$this->success('保存成功');
			$this->add_log($this->msg);
		}
		else
		{
			$this->display("group.php");
		}
		
	}

	public function addgroup()
	{
		if(IS_POST)
		{
			$data=[[F('name'),'null','分组名称不能为空']];
			$v=new sdcms_verify($data);
			if($v->result())
			{
				$rs=$this->db->row("select * from sd_attachment_group where gname='".F('name')."' limit 1");
				if($rs)
				{
					$this->error('分组名称已存在');
				}
				else
				{
					$d['gname']=F('name');
					$d['ordnum']=0;
					$d['islock']=1;
					if(!APP_DEMO) $this->db->add('sd_attachment_group',$d);
					$this->success('添加成功');
				}
			}
			else
			{
				$this->error($v->msg);
			}
			$this->add_log($this->msg);
		}
	}

	public function editgroup()
	{
		$id=getint(F('get.id'),0);
		if(IS_POST)
		{
			$data=[[F('t0'),'null','分组名称不能为空']];
			$v=new sdcms_verify($data);
			if($v->result())
			{
				$d['gname']=F('t0');
				$d['ordnum']=getint(F('t1'),0);
				$d['islock']=getint(F('t2'),0);
				if(!APP_DEMO) $this->db->update('sd_attachment_group','aid='.$id.'',$d);
				$this->success('保存成功');
			}
			else
			{
				$this->error($v->msg);
			}
			$this->add_log($this->msg);
		}
		else
		{
			$rs=$this->db->row("select * from sd_attachment_group where aid=".$id." limit 1");
			if($rs)
			{
				foreach($rs as $key=>$val)
				{
					$this->assign($key,$val);
				}
				$this->display("edit.php");
			}
		}
	}

	public function delgroup()
	{
		$id=getint(F('get.id'),0);
		if(!APP_DEMO) $this->db->del('sd_attachment_group','aid='.$id.'');
		if(!APP_DEMO) $this->db->update('sd_attachment','gid='.$id.'',['gid'=>0]);
		$this->success('删除成功');
		$this->add_log($this->msg);
	}

	public function get_files($path)
	{
	  foreach(scandir($path) as $afile)
	  {
	    if($afile=='.'||$afile=='..') continue;
	    if(is_dir($path.'/'.$afile))
	    {
	      self::get_files($path.'/'.$afile);
	    }
	    else
	    {
	      if(substr($afile,0,6)=='thumb_')
	      {
	        if(!APP_DEMO) @unlink($path.'/'.$afile);
	        $this->num=$this->num+1;
	      }
	    }
	  }
	}

}