通用Control类 - 基于Thinkphp写的通用资讯管理类

用过各种cms系统的楼主,改了很多,最终还是选择自己做一个独特的cms 因为女朋友叫慧慧 因此起名 huihuicms (程序员有女朋友不容易啊哈哈)

通用control

<?php

class CommonAction extends Action {
	public $pk = 'id'; //主键 为了update查询

    public function index(){
 		$model = $this->_model();
 		$map = $this->_filter($model->create());
		$this->_list($map);

		
		//$this->display();
    }

	public function _list($map=null){

		$model = $this->_model(); 
		import('ORG.Util.Page');
		$count  = $model->where($map)->count();
		$Page   = new Page($count);
		$show   = $Page->show();
		$list = $model->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}

	public function add(){
		$this->display();
	}

	public function edit($id){
		
		if($id==0){
			$this->error("请选择一项再进行编辑");
			die;
		}
		$model = $this->_model();
		$one = $model->where("{$this->pk} = {$id}")->find(); //每个Action里面的主键自己设置
		$this->assign("one",$one);
		$this->display();
	}
	public function insert(){
		$this->_before_insert();

		$model = $this->_model();

		if($model->create()){

			$model->on_time = time();
			$result = $model->add();
			$backurl = isset($_REQUEST['backurl']) ? $_REQUEST['backurl']:__URL__;

			if($result){
				$this->success("添加成功",$backurl);
			}else{
				$this->error("添加失败");
			}
		}else{
			 $this->error($Form->getError());
		}
		
		$this->_after_insert();
	}

	public function _before_insert(){

	}
	public function _after_insert(){

	}

	public function update(){

		$this->_before_update();
		
		$model = $this->_model(); 
		
		if($model->create()){
			//$model->on_time = time();
			$id = $_REQUEST['id'];
			$backurl = isset($_REQUEST['backurl']) ? $_REQUEST['backurl']:__URL__;
			$result = $model->where("{$this->pk} = {$id}")->save(); //每个Action里面的主键$pk自己设置
			if($result){
				$this->success("添加成功",$backurl);
			}else{
				$this->error("添加失败");
			}
		}else{
			 $this->error($Form->getError());
		}
		
		$this->_after_update();
	}

	public function _before_update(){

	}
	public function _after_update(){

	}
	
	public function delete(){
		$this->_before_delete();
		$model = $this->_model(); 
		$id=$_REQUEST['id'];
		$backurl = isset($_REQUEST['backurl']) ? $_REQUEST['backurl']:__URL__;
		$result = $model->where("{$this->pk} = {$id}")->delete(); //每个Action里面的主键$pk自己设置
		if($result){
			$this->success("删除成功",$backurl);
		}else{
			$this->error("删除失败",$backurl);
		}
	}
	
	public function _before_delete(){

	}
	public function _after_delete(){

	}
	public function _model($model_name=MODULE_NAME){
		$model = D($model_name);
		if(!empty($model)){
			return D($model_name);
		}
		return M($model_name);
	}

	public function _filter($arr){
		foreach($arr as $k=>$v){
			if($v=='' || $v==null){
				unset($arr[$k]);
			}
		}
		
		return $arr;
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值