/**
* CRUD master class
* @author bluehire
*/
class SCrud {
const PATH_VIEW='crud'; //View path name
/**
* Current list operation configuration
* @var SCrudOperation
*/
public $operation;
/**
* Data access object
* @var STable
*/
public $model;
private $table; //Main table name
private $config; //Table configuration array
public $fields; //array of all field objects
public $title; //current business title of the entire CRUD
public $pageSize=20; //list paging size
public $pageSort='id'; //List sorting basis
public $pageDir='desc'; //List sorting direction
public $rowNo=true; //Whether the list displays row numbers
/**
* Filtering when listing
* @var Closure
*/
public $gridFilter;
//Whether there are the following operations
public $operationView=true;
public $operationInsert=true;
public $ operationUpdate=true;
public $operationDelete=true;
public $operationMultiDelete=true;
// The controller and action name of the current request, used for splicing hyperlinks in the future
private $ controller;
private $action;
/**
*
* @param unknown $table main table name
* @param unknown $controller controller name
* @param unknown $action action name
*/
public function __construct($table, $controller, $action) {
$this->table = $table;
$this->controller = $controller;
$this->action = $action;
// Generate various configuration objects
$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 );
}
}
//Title of this function
$this->title=$this->title?: $table.'Management';
//All operation sets
$this->operation = new SCrudOperationSet ($this);
//Data access model
$ this->model = table($table);
}
/**
* Get a field
*
* @param unknown $name
* @return SCrudField
*/
public function field($name) {
if (! isset ( $ this->fields [$name] )) {
return null;
}
return $this->fields [$name];
}
/**
* Get all fields that can be sorted by
* @return array of SCrudField
*/
public function getPrimaryField() {
// View all fields
foreach ( $this->fields as $field ) {
// The primary key field must be queried
if ($field->primaryKey) {
return $field;
}
}
return false;
}
/**
* Get all searchable fields
* @return multitype:
*/
public function listSortable(){
return array_filter ( $this->fields, function ($f) {
return $f->isSortable ();
} );
}
/**
* Get all fields participating in the viewing
* @return multitype:
*/
public function listSearchable(){
return array_filter ( $this->fields, function ($f) {
return $ f->isSearchable ();
} );
}
/**
* Get all fields involved in creation
* @return multitype:
*/
public function listGridable(){
return array_filter ( $this- >fields, function ($f) {
return $f->isGridable ();
} );
}
/**
* Get all fields participating in editing
* @return multitype:
*/
public function listViewable(){
return array_filter ( $this->fields, function ($f) {
return $f->isViewable ();
} );
}
/**
* Get all creation time fields, usually only one
* @return multitype:
*/
public function listInsertable(){
return array_filter ( $this->fields, function ($f) {
return $f->isInsertable ();
} );
}
/**
* Get all modification time fields, usually only one
* @return multitype:
*/
public function listUpdatable(){
return array_filter ( $this->fields, function ($f) {
return $f->isUpdatable ();
} );
}
/**
* Construct URL
*
* @param unknown $method
* The third system parameter (name is fixed to m)
* @param unknown $params
* Other parameters
* @return string
*/
public function listCreated() {
return array_filter ( $this->fields, function ($field) {
return $field->isCreated;
} );
}
/** &*/
public function listUpdated(){
return array_filter ( $this->fields, function ($field) {
return $field->isUpdated;
} );
}
/**&*/
public function url($method, $params = array()) {
return STemplate::append ( LUrl::ice ( ) . '/', array_merge ( $params, array (
'c' => $this->controller,
'a' => $this->action,
'm' => $method
) ) );
}
/**
* Add a common multi-selection operation
*
* @return SCrudOperationMulti
*/
public function operationMulti($method) {
return $this->operation->add ( new SCrudOperationMulti ( $this, $method ) );
}
/**
* Add a common single-row operation
*
* @return SCrudOperationRow
*/
public function operationRow($method) {
return $this->operation->add ( new SCrudOperationRow ( $this, $method ) );
}
/**
* Add a common full table operation
*
* @return SCrudOperationTable
*/
public function operationTable($method) {
return $this->operation->add ( new SCrudOperationTable ( $this, $method ) );
}
/**
* Processing requests, implemented by operation classes
*
* @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 );
}
/**
* Display CRUD fragment
* @param unknown $tpl template name
* @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)
) ) );
}
}
/**
* The base class of all CRUD subclasses, implements a method to record the main CRUD object
*
* @author bluehire
*
*/
abstract class SCrudSub {
// 主CRUD对象
protected $crud;
以上就是IcePHP框架中的快速后台中的通用CRUD功能框架(五) SCrud 主控类的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!