/**
* crud主控类
* @author bluehire
*/
class scrud {
const path_view='crud'; //视图路径名
/**
* 当前列表操作配置
* @var scrudoperation
*/
public $operation;
/**
* 数据访问对象
* @var stable
*/
public $model;
private $table; // 主表名
private $config; //表配置 array
public $fields; //所有字段对象数组
public $title; // 当前整个crud的业务标题
public $pagesize=20; //列表分页尺寸
public $pagesort='id'; //列表排序依据
public $pagedir='desc'; //列表排序方向
public $rowno=true; //列表是否显示行号
/**
* 列表时的过滤
* @var closure
*/
public $gridfilter;
//是否有以下操作
public $operationview=true;
public $operationinsert=true;
public $operationupdate=true;
public $operationdelete=true;
public $operationmultidelete=true;
// 当前请求的控制器与动作名称,用于以后拼接超链接
private $controller;
private $action;
/**
*
* @param unknown $table 主表名
* @param unknown $controller 控制器名称
* @param unknown $action 动作名称
*/
public function __construct($table, $controller, $action) {
$this->table = $table;
$this->controller = $controller;
$this->action = $action;
// 生成各种配置对象
$this->config = config ( 'crud/' . $table );
foreach ( $this->config as $c => $v ) {
if (strpos ( $c, '_' ) === 0) {
$n = substr ( $c, 1 );
$this->$n = $v;
} else {
$this->fields [$c] = new scrudfield ( $this, $v );
}
}
//本功能的标题
$this->title=$this->title?:$table.'管理';
//所有操作集合
$this->operation = new scrudoperationset ( $this );
//数据访问模型
$this->model = table($table);
}
/**
* 取一个字段
*
* @param unknown $name
* @return scrudfield
*/
public function field($name) {
if (! isset ( $this->fields [$name] )) {
return null;
}
return $this->fields [$name];
}
/**
* 取本表主键字段名
* @return scrudfield
*/
public function getprimaryfield() {
// 查看所有字段
foreach ( $this->fields as $field ) {
// 主键字段必须查询
if ($field->primarykey) {
return $field;
}
}
return false;
}
/**
* 获取所有可作为排序依据字段
* @return array of scrudfield
*/
public function listsortable(){
return array_filter ( $this->fields, function ($f) {
return $f->issortable ();
} );
}
/**
* 获取所有可搜索字段
* @return multitype:
*/
public function listsearchable(){
return array_filter ( $this->fields, function ($f) {
return $f->issearchable ();
} );
}
/**
* 获取所有参与列表的字段
* @return multitype:
*/
public function listgridable(){
return array_filter ( $this->fields, function ($f) {
return $f->isgridable ();
} );
}
/**
* 获取所有参与查看的字段
* @return multitype:
*/
public function listviewable(){
return array_filter ( $this->fields, function ($f) {
return $f->isviewable ();
} );
}
/**
* 获取所有参与创建的字段
* @return multitype:
*/
public function listinsertable(){
return array_filter ( $this->fields, function ($f) {
return $f->isinsertable ();
} );
}
/**
* 获取所有参与编辑的字段
* @return multitype:
*/
public function listupdatable(){
return array_filter ( $this->fields, function ($f) {
return $f->isupdatable ();
} );
}
/**
* 获取所有 创建时间 字段,通常只有一个
* @return multitype:
*/
public function listcreated(){
return array_filter ( $this->fields, function ($field) {
return $field->iscreated;
} );
}
/**
* 获取所有修改时间字段,通常只有一个
* @return multitype:
*/
public function listupdated(){
return array_filter ( $this->fields, function ($field) {
return $field->isupdated;
} );
}
/**
* 构造 url
*
* @param unknown $method
* 第三个系统参数(名称固定为m)
* @param unknown $params
* 其它参数
* @return string
*/
public function url($method, $params = array()) {
return stemplate::append ( lurl::ice () . '/', array_merge ( $params, array (
'c' => $this->controller,
'a' => $this->action,
'm' => $method
) ) );
}
/**
* 增加一个普通多选操作
*
* @return scrudoperationmulti
*/
public function operationmulti($method) {
return $this->operation->add ( new scrudoperationmulti ( $this, $method ) );
}
/**
* 增加一个普通单行操作
*
* @return scrudoperationrow
*/
public function operationrow($method) {
return $this->operation->add ( new scrudoperationrow ( $this, $method ) );
}
/**
* 增加一个普通全表操作
*
* @return scrudoperationtable
*/
public function operationtable($method) {
return $this->operation->add ( new scrudoperationtable ( $this, $method ) );
}
/**
* 处理请求,由操作类来实现
*
* @param srequest $req
*/
public function process(srequest $req) {
//处理前,先为搜索条件,列表,创建,修改,查看,排序 初始化字段
foreach($this->fields as $field){
if(!$field->isabandon){
$field->process();
}
}
//设置操作
if($this->operationinsert){
$this->operation->insert(new scrudoperationtable($this,scrudoperation::method_insert));
$this->operation->insert(new scrudoperation($this,scrudoperation::method_doinsert));
}
if($this->operationmultidelete){
$this->operation->insert(new scrudoperationmulti($this,scrudoperation::method_deletemulti));
}
if($this->operationdelete){
$this->operation->insert(new scrudoperationrow($this,scrudoperation::method_delete));
}
if($this->operationupdate){
$this->operation->insert(new scrudoperationrow($this,scrudoperation::method_update));
$this->operation->insert(new scrudoperation($this,scrudoperation::method_doupdate));
}
if($this->operationview){
$this->operation->insert(new scrudoperationrow($this,scrudoperation::method_view));
}
//转交操作类处理
$this->operation->process ( $req );
}
/**
* 显示crud片段
* @param unknown $tpl 模板名
* @param unknown $params
*/
public function display($tpl, $params = array()) {
display ( self::path_view . directory_separator . $tpl, array_merge ( $params, array (
'url_view' => $this->url ( scrudoperation::method_view ),
'url_index' => $this->url ( scrudoperation::method_index ),
'url_search' => $this->url ( scrudoperation::method_search ),
'url_insert' => $this->url ( scrudoperation::method_insert ),
'url_update' => $this->url( scrudoperation::method_update ),
'url_doupdate' => $this->url ( scrudoperation::method_doupdate ),
'url_doinsert' => $this->url ( scrudoperation::method_doinsert ),
'url_delete' =>$this->url ( scrudoperation::method_delete ),
'url_delete_multi'=>$this->url(scrudoperation::method_deletemulti)
) ) );
}
}
/**
* 所有crud子类的基类,实现了一个记录主crud对象的方法
*
* @author bluehire
*
*/
abstract class scrudsub {
// 主crud对象
protected $crud;
以上就是IcePHP框架中的快速后台中的通用CRUD功能框架(五) SCrud 主控类的内容,更多相关内容请关注PHP中文网(www.php.cn)!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号