General CRUD function framework in the quick backend of the IcePHP framework (6) SCrudField class

Source: Internet
Author: User
General CRUD function framework in the quick backend of the IcePHP framework (6) SCrudField class /**
* CRUD field class
* @ Author bluehire
*
*/
Class SCrudField extends SCrudSub {
// The following attributes are from the database (configuration file, config/crud/*. config. php)
Public $ name; // field name
Private $ scale; // precision
Private $ type; // complete type
Private $ maxLength; // maximum length
Public $ simpleType; // simple type CILNDTXBR
Private $ notNull; // null is not allowed
Public $ primaryKey; // whether the primary key is used
Private $ autoIncrement; // whether to increase by itself
Private $ binary; // whether it is binary
Private $ unsigned; // unsigned
Private $ hasDefault; // whether the default value exists
Public $ defaultValue; // default value
Public $ description; // field remarks

// The default value for re-calculation. it can be modified.
Public $ title; // field title

// The following attributes are Boolean and can be set.
Public $ isPassword; // password field
Public $ isAbandon; // whether the user is abandoned
Public $ inGrid; // list of participants
Public $ inInsert; // whether to participate in the creation
Public $ inUpdate; // whether to participate in modification
Public $ inView; // whether to check
Public $ inSort; // whether to participate in sorting
Public $ isCreated; // whether to create a time field
Public $ isUpdated; // whether to modify the time field

Public $ showType; // The CRUD type of the field Text/Image/Date/Time/String
Public $ updateType; // The CRUD type of the field Text/Image/Date/Time/String
Public $ searchType; // search type LIKE/EQUAL/DATE/TIME/RANGE/DATERANGE/CHECK/RADIO/TREE/LIST

Public $ enum; // This field is enumeration, where the storage value of the field corresponds to the displayed value
Public $ foreignKey; // This field is the foreign key of another table. set the primary table name and primary table field name here.

Public $ regular; // the regular expression used for front-end and back-end verification

Public $ width = false; // set the image width.
Public $ height = false; // set the image height.
Public $ style = false; // set the image style
Public $ css = false; // sets the image style class.
Public $ alt = false; // Set the replacement text of the image.
Public $ format; // format

Public $ searchDefault; // default value of the search condition
Public $ searchMax; // maximum search range
Public $ searchMin; // lower limit of the search range

Private $ config; // save the original configuration

/**
* @ Param SCrud $ father main CRUD object
* @ Param array $ c database configuration
*/
Public function _ construct (SCrud $ father, array $ c ){
$ This-> crud = $ father;
$ This-> config = $ c;

// All configuration values are recorded in the attributes of this object.
Foreach ($ c as $ k => $ v ){
$ This-> $ k = $ v;
}

// Process the default values of some attributes
$ T = $ c ['simpletype'];
$ N = $ c ['name'];

$ Default = array (
'Title' => $ c ['description']? : $ N, // Default title value: Remarks/field name
'Input' => strpos ('> CIRNDT', $ t), // whether to participate in the sorting: C/I/N/D/T
'Failed' => strpos ('> CIRLNDT', $ t), // whether to participate in the list
'Ininsert' => strpos ('> CILNDTX', $ t) and! $ C ['primarykey'], // whether to participate in the creation
'Inupdate' => strpos ('> CILNDTX', $ t) and! $ C ['primarykey'], // whether to participate in editing
'Inview' => strpos ('> CIRLNDTX', $ t), // whether to display
'Iscreated '=> strpos ('> cidt', $ t) and ($ n = 'created 'or $ n = 'create _ time '), // whether it is the creation time field
'Isupdated' => strpos ('> cidt', $ t) and ($ n = 'updated' or $ n = 'update _ time '), // whether the time field is modified
);

Foreach ($ default as $ k => $ v ){
If (! Isset ($ c [$ k]) {
$ This-> $ k = $ v;
}
}

// Set the default CRUD type of the field
Switch ($ t ){
// Date
Case 'D ':
$ This-> showType = 'date ';
$ This-> updateType = 'date ';
$ This-> searchType = 'daterange ';
Break;
// Time
Case 'T ':
$ This-> showType = 'time ';
$ This-> updateType = 'time ';
$ This-> searchType = 'daterange ';
Break;
// Large Text
Case 'x ':
$ This-> showType = 'string ';
$ This-> updateType = 'text ';
$ This-> searchType = null;
Break;
// String
Case 'C ':
$ This-> showType = 'string ';
$ This-> updateType = 'string ';
$ This-> searchType = 'like ';
Break;
// Logic
Case 'L ':
$ This-> showType = 'string ';
$ This-> updateType = 'Radio ';
$ This-> searchType = 'list ';
$ This-> enum = array ('0' => 'no', '1' => 'Yes ');
Break;
// Integer
Case 'I ':
$ This-> showType = 'string ';
$ This-> updateType = 'string ';
$ This-> searchType = 'range ';
Break;
// Auto-increment integer
Case 'r ':
$ This-> showType = 'string ';
$ This-> updateType = 'string ';
$ This-> searchType = 'equal ';
Break;
// Floating point
Case 'n ':
$ This-> showType = 'string ';
$ This-> updateType = 'string ';
$ This-> searchType = 'range ';
Break;
Default:
$ This-> showType = 'string ';
$ This-> updateType = 'string ';
$ This-> searchType = null;
}
}

/**
* Process the field again before use.
*/
Public function process (){
// Process foreign keys into enumeration
If ($ this-> foreignKey ){
$ Fk = $ this-> foreignKey;
$ T = table ($ fk ['table']);
If (isset ($ fk ['where']) {
$ T = $ t-> where ($ fk ['where']);
}
If (isset ($ fk ['orderby']) {
$ T = $ t-> orderby ($ fk ['orderby']);
}
$ This-> enum = $ t-> col ($ fk ['field']);
}

// The password does not participate in the search. the password is displayed when the password is changed/created.
If ($ this-> isPassword ){
$ This-> searchType = null;
$ This-> updateType = 'password ';
}
}

/**
* Determines whether the field can be sorted.
* @ Return boolean
*/
Public function isSortable (){
If ($ this-> isAbandon or $ this-> simpleType = 'x' or $ this-> simpleType = 'B' or $ this-> simpleType = 'l' or $ this-> isPassword or! $ This-> inGrid ){
Return false;
}

Return $ this-> inSort;
}

/**
* Determine whether this field is involved in the creation
* @ Return boolean
*/
Public function isInsertable (){
If ($ this-> isAbandon or $ this-> simpleType = 'B' or $ this-> isCreated or $ this-> isUpdated or $ this-> autoIncrement or $ this-> primaryKey) {
Return false;
}
Return $ this-> inInsert;
}

/**
* Determine whether this field is used for editing
* @ Return boolean
*/
Public function isUpdatable (){
If ($ this-> isAbandon or $ this-> simpleType = 'B' or $ this-> isCreated or $ this-> isUpdated or $ this-> autoIncrement or $ this-> primaryKey) {
Return false;
}
Return $ this-> inInsert;
}

/**
* Determines whether this field is displayed in the list.
* @ Return boolean
*/
Public function isGridable (){
If ($ this-> isAbandon or $ this-> simpleType = 'x' or $ this-> simpleType = 'B' or $ this-> isPassword ){
Return false;
}

If ($ this-> primaryKey or $ this-> isSortable ()){
Return true;
}

Return $ this-> inGrid;
}

/**
* Check whether this field is used for viewing
* @ Return boolean
*/
Public function isViewable (){
If ($ this-> isAbandon or $ this-> simpleType = 'B' or $ this-> isPassword ){
Return false;
}
If ($ this-> primaryKey ){
Return true;
}
Return $ this-> inView;
}

/**
* Save the decoding function
* @ Var Closure
*/
Public $ decode;

/**
* Set decoding function/decoding
* @ Param Closure | mixed $ decode
* @ Return SCrudField
*/
Public function decode ($ decode ){
If ($ decode instanceof Closure ){
// Set the decoding function
$ This-> decode = $ decode;
Return $ this;
} Else {
// Specific decoding
$ Closure = $ this-> decode;
Return $ closure ($ decode );
}
}

/**
* Save the encoding function
* @ Var Closure
*/
Public $ encode;

/**
* Set the encoding function
* @ Param Closure | mixed $ encode
* @ Return SCrudField
*/
Public function encode ($ encode ){
If ($ encode instanceof Closure ){
// Sets the encoding function.
$ This-> encode = $ encode;
Return $ this;
} Else {
// Code
$ Closure = $ this-> encode;
Return $ closure ($ encode );
}
}

/**
* Display this field in the list/View
*
* @ Param $ value field value
* @ Return string
*/
Public function show ($ value ){
// Enumeration, displayed by performance value
If ($ this-> enum ){
$ Value = $ this-> enum [$ value];
}

Switch ($ this-> showType ){
Case 'image ':
Return $ this-> crud-> display ('grid _ image', array (
'Src' => $ value,
'Width' => $ this-> width,
'Height' => $ this-> height,
'Style' => $ this-> style,
'Css '=> $ this-> css,
'Alt' => $ this-> alt
));
Case 'Time ':
$ Format = $ this-> format? : 'Y-m-d H: I: s ';
Return date ($ format, $ value );
Case 'date ':
$ Format = $ this-> format? : 'Y-m-D ';
Return date ($ format, $ value );
Case 'text ':
Return $ this-> showString ($ value );
Default:
If ($ this-> decode ){
Return $ this-> decode ($ value );
}
Return $ value;
}
}

/**
* Displayed during creation/editing
* @ Param string $ default
*/
Public function showUpdate ($ v = ''){
$ Tpl = 'update _ '. strtolower ($ this-> updateType );
$ This-> crud-> display ($ tpl, array (
'Field' => $ this,
'Value' => $ v
));
}

/**
* Determine whether to participate in the search
* @ Return boolean
*/
Public function isSearchable (){
If ($ this-> isAbandon or! $ This-> searchType or $ this-> isPassword ){
Return false;
}
Return true;
}

/**
* Display a search condition
* @ Param string $ default
*/
Public function showSearch (){
If (! $ This-> isSearchable ()){
Return;
}

// Add an unlimited parameter for enumeration.
If ($ this-> enum ){
$ Enum = array_merge (array (null => 'unlimited '), $ this-> enum );
} Else {
$ Enum = null;
}

Return $ this-> crud-> display ('search _ '. strtolower ($ this-> searchType), array (
'Title' => $ this-> title,
'Name' => 'crud _ '. $ this-> name,
'Default' => $ this-> searchDefault,
'Min' => $ this-> searchMin,
'Max' => $ this-> searchMax,
'Enum' => $ enum,
));
}

/**
* Determine whether any character is not allowed
* @ Param unknown $ v
* @ Param unknown $ chars
* @ Return boolean
*/
Private function antiInject ($ v, $ chars ){
For ($ I = 0; $ I If (strpos ($ v, $ chars [$ I])! = False)
Return false;
}
Return true;
}

/**
* Construct fuzzy match query conditions
* @ Param SRequest $ req
* @ Return boolean | string
*/
Private function whereLike (SRequest $ req ){
// Request parameter name
$ Name = 'crud _ '. $ this-> name;

// If this request parameter does not exist
If (! $ Req-> exist ($ name )){
Return false;
}

// If the request parameter is null
$ V = trim ($ req-> $ name );
If (! $ V ){
Return false;
}

// If the request parameter contains invalid characters
If (! $ This-> antiInject ($ v, '\' "\ % _')){
Return false;
}

// Return condition
Return '''. $ this-> name. ''' like "% '. $ v.' % "';
}

/**
* Construct exact matching query conditions
* @ Param SRequest $ req
* @ Return boolean | multitype: string
*/
Private function whereEqual (SRequest $ req ){
// Request parameter name
$ Name = 'crud _ '. $ this-> name;

// If this request parameter does not exist
If (! $ Req-> exist ($ name )){
Return false;
}

// If the request parameter is null
$ V = trim ($ req-> $ name );

If (! Strlen ($ v )){
Return false;
}

// If the request parameter contains invalid characters
If (! $ This-> antiInject ($ v ,'\'"\\')){
Return false;
}

// Standardize parameters
Switch ($ this-> simpleType ){
Case 'I ':
Return array ($ this-> name => intval ($ v ));
Case 'L ':
Return array ($ this-> name => ($ v = '1' or strtolower ($ v) = 'true ')? 1-0 );
Case 'n ':
Return array ($ this-> name => floatval ($ v ));
Case 'D ':
$ P = strtotime ($ v );
If (! $ P ){
Return false;
}
Return array ($ this-> name => date ('Y-m-D', $ p ));
Case 'T ':
$ T = strtotime ($ v );
If (! $ T ){
Return false;
}
Return array ($ this-> name => date ('Y-m-d H: I: S', $ t ));
}

// Return condition
Return array ($ this-> name => $ v );
}

/**
* Construct a search condition for date matching
* @ Param SRequest $ req
* @ Return boolean | multitype: Ambigous
*/
Private function whereDate (SRequest $ req ){
// Request parameter name
$ Name = 'crud _ '. $ this-> name;

// If this request parameter does not exist
If (! $ Req-> exist ($ name )){
Return false;
}

// If the request parameter is null
$ V = trim ($ req-> $ name );
If (! $ V ){
Return false;
}

// If the request parameter contains invalid characters
If (! $ This-> antiInject ($ v ,'\'"\\')){
Return false;
}

// If it cannot be parsed by date
$ V = strtotime ($ v );
If ($ v ){
Return false;
}

// Standardize parameters
Switch ($ this-> simpleType ){
Case 'C ':
Case 'D ':
Return array ($ this-> name => date ('Y-m-D', $ v ));
Case 'T ':
Return array ($ this-> name => date ('Y-m-d H: I: S', $ v ));
}

// Return condition
Return array ($ this-> name => $ v );
}

/**
* Obtain a date range boundary parameter from the request parameter
* @ Param unknown $ name
* @ Param SRequest $ req
* @ Return boolean | string | number | Ambigous
*/
Private function whereOne ($ name, SRequest $ req ){
// If this request parameter does not exist
If (! $ Req-> exist ($ name )){
Return false;
}

// If the request parameter is null
$ V = trim ($ req-> $ name );
If (! $ V ){
Return false;
}

// If the request parameter contains invalid characters
If (! $ This-> antiInject ($ v ,'\'"\\')){
Return false;
}

// Standardize parameters
Switch ($ this-> simpleType ){
Case 'C ':
Return $ v;
Case 'I ':
Case 'r ':
Return intval ($ v );
Case 'n ':
Return floatval ($ v );
Case 'D ':
// If it cannot be parsed by date
$ V = strtotime ($ v );
If ($ v ){
Return false;
}
Return date ('Y-m-D', $ v );
Case 'T ':
// If it cannot be parsed by date
$ V = strtotime ($ v );
If ($ v ){
Return false;
}
Return date ('Y-m-d H: I: S', $ v );
}

Return $ v;
}

/**
* Create search criteria based on request parameters
*/
Private function whereRange (SRequest $ req ){
// Request parameter name
$ Name = 'crud _ '. $ this-> name;

// Obtain the boundary value
$ Min = $ this-> whereOne ($ name. '_ min', $ req );
$ Max = $ this-> whereOne ($ name. '_ max', $ req );

If (! $ Min and! $ Max ){
Return false;
}

If (! $ Max ){
Return '''. $ this-> name. ''> =" '. $ min .'"';
}

If (! $ Min ){
Return '''. $ this-> name. ''' <= "'. $ max .'"';
}

// Return condition
Return '''. $ this-> name. ''' BETWEEN "'. $ min.'" AND "'. $ max .'"';
}

/**
* Construct query conditions for the date range
* @ Param SRequest $ req
* @ Return boolean | string
*/
Private function whereDateRange (SRequest $ req ){
// Request parameter name
$ Name = 'crud _ '. $ this-> name;

// Calculate the boundary value
$ Min = $ this-> whereOne ($ name. '_ min', $ req );
$ Max = $ this-> whereOne ($ name. '_ max', $ req );

If (! $ Min and! $ Max ){
Return false;
}

If (! $ Max ){
Return '''. $ this-> name. ''> =" '. $ min .'"';
}

If (! $ Min ){
Return '''. $ this-> name. ''' <= "'. $ max .'"';
}

// Return condition
Return '''. $ this-> name. ''' BETWEEN "'. $ min.'" AND "'. $ max .'"';
}

Private function whereTime (SRequest $ req ){
// @ Todo: query condition for time matching
}

/**
* Construct query conditions for single-choice search
* @ Param SRequest $ req
* @ Return boolean | multitype: Ambigous
*/
Private function whereRadio (SRequest $ req ){
// Request parameter name
$ Name = 'crud _ '. $ this-> name;

// If this request parameter does not exist
If (! $ Req-> exist ($ name )){
Return false;
}

// If the request parameter is null
$ V = trim ($ req-> $ name );
If (! $ V ){
Return false;
}

// If the request parameter contains invalid characters
If (! $ This-> antiInject ($ v ,'\'"\\')){
Return false;
}

// Standardize parameters
Switch ($ this-> simpleType ){
Case 'I ':
Case 'r ':
Return array ($ this-> name => intval ($ v ));
Case 'L ':
Return array ($ this-> name => ($ v = '1' or strtolower ($ v) = 'true '));
}

// Return condition
Return array ($ this-> name => $ v );
}

/**
* Create multiple search query conditions based on user requests
* @ Param SRequest $ req
* @ Return boolean | multitype: Ambigous
*/
Private function whereCheck (SRequest $ req ){
// Request parameter name
$ Name = 'crud _ '. $ this-> name;

// If this request parameter does not exist
If (! $ Req-> exist ($ name )){
Return false;
}

// If the request parameter is null
$ V = trim ($ req-> $ name );
If (! $ V ){
Return false;
}

// If the request parameter contains invalid characters
If (! $ This-> antiInject ($ v ,'\'"\\')){
Return false;
}

// Standardize parameters
Switch ($ this-> simpleType ){
Case 'I ':
Case 'r ':
Return array ($ this-> name => intval ($ v ));
Break;
Case 'L ':
Return array ($ this-> name => ($ v = '1' or strtolower ($ v) = 'true '));
}

// Return condition
Return array ($ this-> name => $ v );
}

/**
* Construct query conditions based on user request parameters
*
* @ Param SRequest $ req
* @ Throws Exception
* @ Return Ambigous | Ambigous | Ambigous
*/
Public function where (SRequest $ req ){
Switch ($ this-> searchType ){
Case 'like ':
Return $ this-> whereLike ($ req );
Case 'equal ':
Return $ this-> whereEqual ($ req );
Case 'date ':
Return $ this-> whereDate ($ req );
Case 'Time ':
Return $ this-> whereTime ($ req );
Case 'List ':
Return $ this-> whereEqual ($ req );
Case 'tree ':
Return $ this-> whereEqual ($ req );
Case 'Radio ':
Return $ this-> whereRadio ($ req );
Case 'check ':
Return $ this-> whereCheck ($ req );
Case 'range ':
Return $ this-> whereRange ($ req );
Case 'daterange ':
Return $ this-> whereDateRange ($ req );
}
Throw new Exception ('The program flow should not arrive here ');
}

The above is the general CRUD function framework (6) SCrudField content 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.