ArticleAction.class.php 11.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
<?php
class ArticleAction extends CommonAction {
	public $tab='Apply';
	public $catab='Category';
	public $tabView='ApplyView';
	public function __construct() {
		parent::__construct();
	}
	
    public function index(){
		
		$Apply=M($this->tab);
		//获取字段信息
		$where_field['isshow'] = array('eq',1);	//状态为开启的字段
		$where_field['isshow3'] = array('eq',1);//审核列表显示的字段
		$file_info = M('Regfields')->where($where_field)->order('sort')->select();
		if($file_info){	//将字段信息重新整理
			foreach($file_info as $k => $v){
				$field[] = $v['name'];
				$new_regfields[$v['name']] = $v;
			}
		}
		$field_arr=array_merge($field,array('id','uid','username','addtime','status','reviewtime','isshow','depth'));
		$data = $this->_getLists($Apply,$where,$order,$field_arr,12,'Pages3',array('header'=>'<li style="border:none;">共%totalRows%条记录','pages'=>'%nowPage%/%totalPages%页 </li>','prev'=>'<上一页','next'=>'下一页>','first'=>'首页','last'=>'尾页','theme'=>'%header% %pages% %prePage% %linkPage% %nextPage% %end%'));
		$this->assign('list',$data['list']?$data['list']:array());	//列表
		$this->assign('page',$data['page']);
		$this->assign('p',$data['p']);
		$colnum=count($file_info) + 4;	//列数
		$this->assign('empty','<tr><td colspan="'.$colnum.'" style="padding-left:15px;font-size:14px;">暂无相关信息!</td></tr>');
		$list=$Apply->field($field_arr)->select();
		$this->assign('colnum',$colnum);
		$this->assign('field',$field);	//字段(排序)
		$this->assign('regfields',$new_regfields);	//字段信息 键名为name
		$this->display();
	}
/**
 * 查看详情
 * @param $id 内容id
 * @param $page_title 页面标题
 */
	public function look()
	{
		if(I('id')){
			$id = I('id');
		}else{
			$id = 0;
		}
		$model = M('Apply');
		$where['id'] = array('eq',$id);
		//获取字段信息
		$where_field['isshow'] = array('eq',1);	//状态为开启的字段
		$file_info = M('Regfields')->where($where_field)->order('sort')->select();
		if($file_info){	//将字段信息重新整理
			foreach($file_info as $k => $v){
				$field[] = $v['name'];
				$new_regfields[$v['name']] = $v;
			}
		}
		$field[]='status';
		$strfield = implode(',',$field);
		$data = $this->_getRow($model,$where,$strfield);
		//dump($regfields);
		$this->assign('data',$data);
		$this->assign('field',$field);	//字段(排序)
		$this->assign('regfields',$new_regfields);	//字段信息 键名为name
		$this->assign('page_title','资料预览');//页面标题
		$this->display();
	}
	//删除文件
	public function delete(){
		$Apply=D($this->tab);
		if($this->isGet()){
			$map['id']=array('eq',$this->_param('id'));
			//获取字段信息
			$where_field['isshow'] = array('eq',1);	//状态为开启的字段
			$file_info = M('Regfields')->where($where_field)->order('sort')->select();
			$fileds='';
			if($file_info){	//获取有附件的字段信息
				foreach($file_info as $k => $v){
					if($v['upfile']){
						$fileds.=$fileds?','.$v['name']:$v['name'];
					}
				}
			}
			$file_arr=$Apply->where($map)->field($fileds)->find();
			foreach($file_arr as $fik=>$fiv){
				$this->delFile2($fiv);//删除相关附件
			}
			$aa=$Apply->where($map)->delete();//删除文件
			if($aa){
				//同时删除内容
				$this->success('删除成功!',U('index',array('tid'=>$this->_param('tid'))));
			}else{
				$this->error('删除失败!');
			}
		}
	}




	public function edit(){
		$tid=$this->_param('tid');
		if(!$tid){
			$this->error('数据错误!');
		}
		$Article=D($this->tab);
		$Category=D($this->catab);	
		$calist=$Category->getList(0);
		
		if($this->isPost()){
			//热门 推荐
			$flag='';
			foreach($this->_param('flag') as $k=>$v){
				$flag .= ','.$v;
			}
			$_POST['flag']=substr($flag,1);
			
			//多图处理
			if($imgs=$this->_param('img'))
			{
				$_POST['img']=marray2string($imgs);
			}
			//文件 缩略图处理
			if($this->_param('thumb') && $this->_param('thumb') != $this->_param('oldthumb'))
			{
				$this->delFile2($this->_param('oldthumb'));
			}
			if($this->_param('file') && $this->_param('file') != $this->_param('oldfile'))
			{
				$this->delFile2($this->_param('oldfile'));
			}
			
			if($Article->create()){
			
				$lastid=$Article->where('id='.$this->_param('id'))->save();
				
				if($lastid){
					$this->success('文档修改成功',U('Article/index',array('tid'=>$tid)));
				}else{
					$this->error('文档修改失败');
				}
			
			}else{
				$this->error($Article->getError());
			}
		}else{
			//获取分类附件类型信息
			$cate_data=$Category->field('picnum')->find($tid);
			$this->assign('picnum',$cate_data['picnum']);
		
			$data=$Article->where('id='.$this->_param('id'))->find();
			//热门 推荐
			$flag=explode(',',$data['flag']);
			$this->assign('flag',$flag);
			
			// 多图
			if($data['img'])
			{
				$imgs=explode(',',$data['img']);
				$this->assign('img',$imgs);
			}
			
			$this->assign('data',$data);
			$this->assign('cat_list',$calist);
			
			$this->assign('topid',$this->topId($data['tid']));
			
			$this->display();
		}		
	}

	public function add(){
		
		
		
		$tid=$this->_param('tid');
		$tid=$tid>0?$tid:0;
		if(!$tid){
			$this->error('数据错误!');
		}
		
		$Article=D($this->tab);
		$Category=D($this->catab);
		$calist=$Category->getList(0);
		
		//最大排序值
		// $maxp['tid']=array('in',$this->AllCatid($tid,1,'Category'));
		// $maxsort=$Article->where($maxp)->max('sort');
		
		if($this->isPost()){
			//热门 推荐
			$flag='';
			foreach($this->_param('flag') as $k=>$v){
				$flag .= ','.$v;
			}
			$_POST['flag']=substr($flag,1);
			
			//多图处理
			if($imgs=$this->_param('img'))
			{
				$_POST['img']=marray2string($imgs);
			}
			
			if($Article->create()){
			
				$lastid=$Article->add();
				//echo $Article->getLastSql();
				if($lastid){
					$this->success('文档添加成功',U('Article/index',array('tid'=>$this->_param('tid'))));
				}else{
					$this->error('文档添加失败');
				}
			
			}else{
				$this->error($Article->getError());
			}
		}else{
			//获取分类附件类型信息
			$cate_data=$Category->field('picnum')->find($tid);
			$this->assign('picnum',$cate_data['picnum']);
			
			$this->assign('cat_list',$calist);
			// $this->assign('maxsort',$maxsort+1);//	排序默认值
			$this->assign('maxsort',0);
			$this->assign('topid',$this->topId($this->_param('tid')));
			$this->display();
		}
	}
	
	public function form(){
		$Article=D($this->tab);
		if($this->isPost()){
			$map['id']=array('in',$this->_param('check'));
			
			if($this->_param('ShenHe')){
				
				$this->IsAuth(MODULE_NAME.'/ShenHe');
				
				$data['isshow'] = '1';
				$data['updatatime'] = time();
				$aa=$Article->where($map)->save($data);	
				if($aa){
					$this->success('审核成功!',U('index',array('tid'=>$this->_param('tid'),'p'=>$this->_param('p'))));
				}else{
					$this->error('审核失败!');
				}
			}
			if($this->_param('Delete')){
				
				$this->IsAuth(MODULE_NAME.'/Delete');
				
				$aa=$Article->where($map)->delete();
				if($aa){
					$this->success('删除成功!',U('index',array('tid'=>$this->_param('tid'),'p'=>$this->_param('p'))));
				}else{
					$this->error('删除失败!');
				}
			}
			if($this->_param('NewSort')){
				
				$this->IsAuth(MODULE_NAME.'/NewSort');
				
				$n=count($this->_param('sort'));
				$i=0;
				$data['updatatime'] = time();
				foreach($this->_param('sort') as $k=>$v){
					$data['sort']=$v;
					$ab=$Article->where('id='.$k)->save($data);	
					if($ab){
						$i++;
					}
				}
				if($n==$i){
					$this->success('排序成功!',U('index',array('tid'=>$this->_param('tid'),'p'=>$this->_param('p'))));
				}else{
					$this->error('部分排序失败!');
				}
			}
			if($this->_param('Remove')){
				//echo 'Remove';
			}
		
		}
	}
	
	public function Shenhe(){

		$Article=D($this->tab);
		$is=$Article->where('id='.$this->_param('id'))->getField('isshow');
		if($is==1){
			$isnew=0;
		}else{
			$isnew=1;
		}
		$aa=$Article->where('id='.$this->_param('id'))->setField('isshow',$isnew);
		if($aa){
			//同时删除内容
			//$this->success('更改成功!',U('index',array('tid'=>$this->_param('tid'),'p'=>$this->_param('p'))));
			echo 1;
		}else{
			//$this->error('更改失败!');
			echo 0;
		}
	}
	
/**
 * 删除文件
**/
	public function delFile()
	{
		$file=$this->_param('file');
		if($file)
		{
			$file2='.'.$file;
			if(file_exists($file2))
			{
				if(unlink($file2))
					echo 'yes';
				else
					echo 'no';
			}
		}
		else
		{
			echo 'no';
		}
	}
	private function delFile2($file)
	{
		if($file)
		{
			$file2=iconv('UTF-8','GB2312','.'.$file);//支持中文名文件
			if(file_exists($file2))
			{
				unlink($file2);
			}
		}
	}
/**
 * 获取图片 多图片尺寸
 */
	public function getchicun()
	{
		$data=M($this->catab)->field('img_size,imgs_size')->where('id='.I('tid'))->find();
		$data['img_size'] = $data['img_size']?$data['img_size']:'未定义';
		$data['imgs_size'] = $data['imgs_size']?$data['imgs_size']:'未定义';
		echo json_encode($data);
	}
/**
 * 获取分类附件类型信息
**/
	public function getPicnum()
	{
		$Category=M("Category");
		$tid=$this->_param('tid');
		$cate_data=$Category->field('picnum')->find($tid);
		echo $cate_data['picnum'];
	}	
/**
 * 图片裁剪
**/
	public function modifyFace()
	{
		//if ($_SERVER['REQUEST_METHOD'] == 'POST'){
			//删除会员以前的头像
			if(file_exists($MemberFace)) {
				unlink($MemberFace);
			}
			$MemberFace = $this->sliceBanner();
			echo $MemberFace;
			//此处根据自己的程序代码自行调整		
			//$table = "member"; //数据表名称
			//$MemberUser = "cuteboy"; //会员名字
			//mysql_query("UPDATE ".$table." SET MemberFace='".$MemberFace."' Where MemberUser = '".$MemberUser."'");
			//echo "<script>alert('头像修改成功!');location.href='index.php';<\/script>");
			
			exit;
		//}
	}
/**
 * 初始化图片
 * @param $url 原始图片路径
**/
	private function getImageHander ($url) {
		$pp_file=dirname(__FILE__);
		$pp_file=realpath($pp_file.'/../../..'.$url);
		$size=@getimagesize($pp_file);
		switch($size['mime']){
			case 'image/jpeg': $im = imagecreatefromjpeg($pp_file);break;
			case 'image/gif' : $im = imagecreatefromgif($pp_file);break;
			case 'image/png' : $im = imagecreatefrompng($pp_file);break;
			default: $im=false;
		}
		return $im;
	}
/**
 * 生成裁剪后的图片
 * @param $picpre
**/
	private function sliceBanner($picpre='pp'){
			$x = (int)$_POST['x'];
			$y = (int)$_POST['y'];
			$w = (int)$_POST['w'];
			$h = (int)$_POST['h'];
			$pic = $_POST['src'];
			
			//剪切后小图片的名字
			$str = explode(".",$pic);//图片的格式
			$type = $str[1]; //图片的格式
			$filename = $picpre."_".date("YmdHis").".". $type; //重新生成图片的名字
			$uploadBanner = $pic;
			$sliceBanner = "Uploads/Thumb/".$filename;//剪切后的图片存放的位置
			
			//创建图片
			$src_pic = $this->getImageHander($uploadBanner);
			$dst_pic = imagecreatetruecolor($w, $h);
			imagecopyresampled($dst_pic,$src_pic,0,0,$x,$y,$w,$h,$w,$h);
			imagejpeg($dst_pic, $sliceBanner);
			imagedestroy($src_pic);
			imagedestroy($dst_pic);
			
			//删除已上传未裁切的图片
			if(file_exists($uploadBanner)) {
				unlink($uploadBanner);
			}
			//返回新图片的位置
			return '/'.$sliceBanner;
		}
}
?>