<?php
/**
* 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;
}