<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
class FlashAction extends Action {
	public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
	public function index(){
		$this->checkUser('1_3_1');
		$data = M('Flash');
		$list = $data->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function add(){
		$this->checkUser('1_3_2');
		$this->display();
	}
	public function check(){
		$this->checkUser('1_3_2');
		if($_FILES['pic']['error']=='4' || empty($_POST['title'])) $this->error('这不是强迫你，图片和标题必须需要！');
		$this->_upload();
	}
	public function _upload(){
		$this->checkUser('1_3_2');
		import("@.ORG.UploadFile");
		$upload = new UploadFile();
		$upload->maxSize =10485760;
		$upload->allowExts = explode(',','jpg,gif,png,jpeg');
		$upload->savePath ='Public/flash/'; 
		$upload->thumb=true;
		$upload->imageClassPath = '@.ORG.Image';
		$upload->thumbPrefix = 'm_';
		$upload->thumbMaxWidth = C('flashwidth');
		$upload->thumbMaxHeight = C('flashheight');
		$upload->saveRule = uniqid;
		$upload->thumbRemoveOrigin = true;
		if(!$upload->upload()){
			$this->error($upload->getErrorMsg());
		}else{
			$uploadList = $upload->getUploadFileInfo();
			$_POST['pic'] =$uploadList[0]['savename'];
		}
		$data = M('Flash');
		$list['url']    = $_POST['url'];
		$list['title']  = $_POST['title'];
		$list['pic']    = $_POST['pic'];
		$list['taxis']  = (int)$_POST['taxis'];
		$list['content'] = stripslashes($_POST['content']);
		$list['state']  = '1';
		if(!$data->add($list)) $this->error('我不是故意的，添加失败！');
		$this->assign('jumpUrl','__URL__/');
		$this->success('添加成功!');
	}
	public function edit(){
		$this->checkUser('1_3_2');
		$id = (int)$_GET['id'];
		$data = M('Flash');
		$list = $data->where("id = $id")->find();
		$this->assign('list',$list);
		$this->display();
	}
	public function amend(){
		$this->checkUser('1_3_2');
		if(empty($_POST['title'])) $this->error('啥都可以带走,标题得留下！');
		if($_FILES['pic']['error']=='4'){
			$this->_edit();
		}else{
			$id = (int)$_POST['id'];
			$data = M('Flash');
			$pic = $data->where("id = $id")->find();
			@unlink($_SERVER['DOCUMENT_ROOT'].'/Public/flash/m_'.$pic['pic']);
			$this->_amend();
			$this->_edit();
		}
	}
	public function _amend(){
		import("@.ORG.UploadFile");
		$upload = new UploadFile();
		$upload->maxSize =10485760;
		$upload->allowExts = explode(',','jpg,gif,png,jpeg');
		$upload->savePath ='Public/flash/'; 
		$upload->thumb=true;
		$upload->imageClassPath = '@.ORG.Image';
		$upload->thumbPrefix = 'm_';
		$upload->thumbMaxWidth = C('flashwidth');
		$upload->thumbMaxHeight = C('flashheight');
		$upload->saveRule = uniqid;
		$upload->thumbRemoveOrigin = true;
		if(!$upload->upload()){
			$this->error($upload->getErrorMsg());
		}else{
			$uploadList = $upload->getUploadFileInfo();
			$_POST['pic'] =$uploadList[0]['savename'];
		}
	}
	public function _edit(){
		$this->checkUser('1_3_2');
		$data = M('Flash');
		$list['id']     = $_POST['id'];
		$list['url']    = $_POST['url'];
		$list['title']  = $_POST['title'];
		if(!empty($_POST['pic'])) $list['pic'] = $_POST['pic'];
		$list['taxis']  = (int)$_POST['taxis'];
		$list['content'] = stripslashes($_POST['content']);
		if(!$data->save($list)) $this->error('我不是故意的，修改失败！');
		$this->assign('jumpUrl','__URL__/');
		$this->success('修改成功!');
	}
	public function del(){
		$this->checkUser('1_3_2');
		$id = (int)$_GET['id'];
		$data = M('Flash');
		$pic = $data->where("id = $id")->find();
		@unlink($_SERVER['DOCUMENT_ROOT'].'/Public/flash/m_'.$pic['pic']);
		if(!$data->where("id = $id")->delete()) $this->error('删除失败!请联系网络管理员!');
		$this->success('删除成功!');
	}
	public function stop(){
		$this->checkUser('1_3_2');
		$list['id'] = (int)$_GET['id'];
		$list['state'] = (int)$_GET['state'];
		$data = M('Flash');
		if(!$data->save($list)) $this->error('操作失败!请重试');
		$this->success('操作成功!');
	}
}
?>