<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
class FlinkAction 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_8_1');
		$data = M('Flink');
		$list = $data->order('state,taxis')->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function add(){
		$this->checkUser('1_8_2');
		$this->display();
	}
	public function check(){
		if(empty($_POST['name']) || empty($_POST['url'])) $this->error('不能一无所有的，至少名字和网址嘛!');	
		$this->checkUser('1_8_2');
		unset($_POST['__hash__']);
		$data = M('Flink');
		$_POST['state']= '1';
		if($_FILES['pic']['error']!='4'){
			import("@.ORG.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize =10485760;
			$upload->allowExts = explode(',','jpg,gif,png,jpeg');
			$upload->savePath ='Public/flink/'; 
			$upload->thumb=true;
			$upload->imageClassPath = '@.ORG.Image';
			$upload->thumbPrefix = 's_';
			$upload->thumbMaxWidth  = C('flinkwidth');
			$upload->thumbMaxHeight = C('flinkheight');
			$upload->saveRule = uniqid;
			$upload->thumbRemoveOrigin = true;
			if(!$upload->upload()){
				$this->error($upload->getErrorMsg());
			}else{
				$uploadList = $upload->getUploadFileInfo();
				$_POST['pic'] =$uploadList[0]['savename'];
			}
		}
		if(!$data->add($_POST)) $this->error('添加友情链接失败!请联系网络管理员!');
		$this->assign('jumpUrl','__URL__/index/');
		$this->success('添加友情链接成功! ^_^');
		
	}
	public function edit(){
		$this->checkUser('1_8_2');
		$id = (int)$_GET['id'];
		$data = M('Flink');
		$list = $data->where("id = $id")->find();
		$this->assign('list',$list);
		$this->display();
	}
	public function amend(){
		if(empty($_POST['name']) || empty($_POST['url'])) $this->error('不能一无所有的，至少名字和网址嘛!');	
		$this->checkUser('1_8_2');
		unset($_POST['__hash__']);
		$data = M('Flink');
		$_POST['state']= '1';
		if($_FILES['pic']['error']!='4'){
			$id = (int)$_POST['id'];
			$data = M('Flink');
			$pic = $data->field('pic')->where("id = $id")->find();
			@unlink($_SERVER['DOCUMENT_ROOT'].'/Public/flink/s_'.$pic['pic']);
			import("@.ORG.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize =10485760;
			$upload->allowExts = explode(',','jpg,gif,png,jpeg');
			$upload->savePath ='Public/flink/'; 
			$upload->thumb=true;
			$upload->imageClassPath = '@.ORG.Image';
			$upload->thumbPrefix = 's_';
			$upload->thumbMaxWidth  = C('flinkwidth');
			$upload->thumbMaxHeight = C('flinkheight');
			$upload->saveRule = uniqid;
			$upload->thumbRemoveOrigin = true;
			if(!$upload->upload()){
				$this->error($upload->getErrorMsg());
			}else{
				$uploadList = $upload->getUploadFileInfo();
				$_POST['pic'] =$uploadList[0]['savename'];
			}
		}
		if(!$data->save($_POST)) $this->error('修改友情链接失败!请联系网络管理员!');
		$this->assign('jumpUrl','__URL__/index/');
		$this->success('修改友情链接成功! ^_^');
	}
	public function del(){
		$this->checkUser('1_8_2');
		$id = (int)$_GET['id'];
		$data = M('Flink');
		$pic = $data->field('pic')->where("id = $id")->find();
		@unlink($_SERVER['DOCUMENT_ROOT'].'/Public/flink/s_'.$pic['pic']);
		if(!$data->where("id = $id")->delete()) $this->error('删除友情链接失败!请联系网络管理员!');
		$this->success('删除友情链接成功!');
	}
	public function stop(){
		$this->checkUser('1_8_2');
		$list['id'] = (int)$_GET['id'];
		$list['state'] = (int)$_GET['state'];
		$data = M('Flink');
		if(!$data->save($list)) $this->error('操作失败!请重试');
		$this->success('操作成功!');
	}
}
?>	