General CRUD function framework in the quick backend of the IcePHP framework (5) SCrud master class

Source: Internet
Author: User
General CRUD function framework in the quick backend of the IcePHP framework (5) SCrud master class /**
* 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; // primary table name
Private $ config; // table configuration array
Public $ fields; // array of all field objects

Public $ title; // The current business title of the entire CRUD
Public $ pageSize = 20; // list page size
Public $ pageSort = 'id'; // Sort by list
Public $ pageDir = 'desc'; // sort the list
Public $ rowNo = true; // whether the row number is displayed in the list

/**
* List filtering
* @ Var Closure
*/
Public $ gridFilter;

// Do you have 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 for future hyperlink splicing
Private $ controller;
Private $ action;

/**
*
* @ Param unknown $ name of the table master table
* @ Param unknown $ controller name
* @ Param unknown $ 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. 'manager ';

// All operation sets
$ This-> operation = new SCrudOperationSet ($ this );

// Data access model
$ This-> model = table ($ table );
}

/**
* Obtain a field.
*
* @ Param unknown $ name
* @ Return SCrudField
*/
Public function field ($ name ){
If (! Isset ($ this-> fields [$ name]) {
Return null;
}
Return $ this-> fields [$ name];
}

/**
* Obtain the field name of the primary key of the table.
* @ Return 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;
}

/**
* Obtain all fields that can be used as sorting bases
* @ Return array of SCrudField
*/
Public function listSortable (){
Return array_filter ($ this-> fields, function ($ f ){
Return $ f-> isSortable ();
});
}

/**
* Retrieve all searchable fields
* @ Return multitype:
*/
Public function listSearchable (){
Return array_filter ($ this-> fields, function ($ f ){
Return $ f-> isSearchable ();
});
}

/**
* Retrieve all fields in the list
* @ Return multitype:
*/
Public function listGridable (){
Return array_filter ($ this-> fields, function ($ f ){
Return $ f-> isGridable ();
});
}

/**
* Obtain all fields used for viewing
* @ Return multitype:
*/
Public function listViewable (){
Return array_filter ($ this-> fields, function ($ f ){
Return $ f-> isViewable ();
});
}

/**
* Obtain all fields involved in the creation
* @ Return multitype:
*/
Public function listInsertable (){
Return array_filter ($ this-> fields, function ($ f ){
Return $ f-> isInsertable ();
});
}

/**
* Obtain all fields involved in editing.
* @ Return multitype:
*/
Public function listUpdatable (){
Return array_filter ($ this-> fields, function ($ f ){
Return $ f-> isUpdatable ();
});
}

/**
* Obtain all the creation time fields. Generally, there is only one
* @ Return multitype:
*/
Public function listCreated (){
Return array_filter ($ this-> fields, function ($ field ){
Return $ field-> isCreated;
});
}

/**
* Obtain all the modified time fields. Generally, there is only one
* @ Return multitype:
*/
Public function listUpdated (){
Return array_filter ($ this-> fields, function ($ field ){
Return $ field-> isUpdated;
});
}

/**
* Construct a URL
*
* @ Param unknown $ method
* Third system parameter (the name is fixed to m)
* @ Param unknown $ params
* Other parameters
* @ 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
)));
}

/**
* Adds a common multiple-choice operation.
*
* @ Return SCrudOperationMulti
*/
Public function operationMulti ($ method ){
Return $ this-> operation-> add (new SCrudOperationMulti ($ this, $ method ));
}

/**
* Adds a common single row operation.
*
* @ Return SCrudOperationRow
*/
Public function operationRow ($ method ){
Return $ this-> operation-> add (new SCrudOperationRow ($ this, $ method ));
}

/**
* Adds a common full table operation.
*
* @ Return SCrudOperationTable
*/
Public function operationTable ($ method ){
Return $ this-> operation-> add (new SCrudOperationTable ($ this, $ method ));
}

/**
* Requests are processed and implemented by the Operation class.
*
* @ Param SRequest $ req
*/
Public function process (SRequest $ req ){
// Search criteria, list, create, modify, view, and sort fields before processing
Foreach ($ this-> fields as $ field ){
If (! $ Field-> isAbandon ){
$ Field-> process ();
}
}

// Set the operation
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 ));
}

// Transfer operations
$ This-> operation-> process ($ req );
}

/**
* Display CRUD fragments
* @ 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 sub-classes implements a method to record the primary CRUD object
*
* @ Author bluehire
*
*/
Abstract class SCrudSub {
// Main CRUD object
Protected $ crud;

The above is the content of the general CRUD functional framework (5) SCrud main control class in the quick backend of the IcePHP framework. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.