General CRUD functionality Framework (vi) Scrudfield field classes in the Quick backend in the icephp framework

Source: Internet
Author: User
/**
* CRUD Field class
* @author Bluehire
*
*/
Class Scrudfield extends Scrudsub {
The following attribute source database (config file, config/crud/*.config.php)
Public $name; Field name
Private $scale; Precision
Private $type; Type complete
Private $maxLength; Maximum length
Public $simpleType; Simple Type CILNDTXBR
Private $notNull; Null not allowed
Public $primaryKey; Whether the primary key
Private $autoIncrement; Whether self-growth
Private $binary; Whether to binary
Private $unsigned; No sign
Private $hasDefault; Whether there is a default value
Public $defaultValue; Default value
Public $description; Field Notes

Recalculate default values that can be modified
public $title; Field Header

The following properties, all Boolean, can be set
Public $isPassword; Whether the password field
Public $isAbandon; Whether to be abandoned
Public $inGrid; Whether to participate in the list
Public $inInsert; Whether to participate in creating
Public $inUpdate; Whether to participate in the modification
Public $inView; Whether to participate in viewing
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 an enumeration in which the field stores values corresponding to the displayed values
Public $foreignKey; This field is the foreign key for another table, where you set the main table name and the main field name

Public $regular; Regular expressions for front-and back-end validation

Public $width = false; Width setting of the picture
Public $height = false; Height setting of the picture
Public $style = false; Picture Style settings
Public $css = false; To set a style class for a picture
Public $alt = false; Set alternate text for a picture
Public $format; Format

Public $searchDefault; Default values for search criteria
Public $searchMax; Upper limit of search range
Public $searchMin; Lower limit of search range

Private $config; Save 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 properties of this object
foreach ($c as $k = = $v) {
$this $k = $v;
}

Handle default values for some properties
$t = $c [' SimpleType '];
$n = $c [' name '];

$default = Array (
' title ' = $c [' description ']? : $n,//default value for Title: Memo/Field Name
' Insort ' = Strpos (' >cirndt ', $t),//whether participate in sort: c/i/n/d/t
' InGrid ' + strpos (' >cirlndt ', $t),//whether to participate in the list
' Ininsert ' = Strpos (' >cilndtx ', $t) and! $c [' PrimaryKey '],//whether to participate in creating
' Inupdate ' = Strpos (' >cilndtx ', $t) and! $c [' PrimaryKey '],//whether to participate in editing
' InView ' = Strpos (' >cirlndtx ', $t),//whether participate in display
' Iscreated ' = Strpos (' >cidt ', $t) and ($n = = ' created ' or $n = = ' Create_time '),//Is the creation time field
' Isupdated ' = Strpos (' >cidt ', $t) and ($n = = ' Updated ' or $n = = ' Update_time '),//Is the modified Time field
);

foreach ($default as $k = = $v) {
if (! isset ($c [$k])) {
$this $k = $v;
}
}

Set the default crud type for a 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 ' = ' is ');
Break
Integer
Case ' I ':
$this->showtype= ' String ';
$this->updatetype= ' String ';
$this->searchtype= ' Range ';
Break
Self-growing integers
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;
}
}

/**
* Once the field is processed again before use
*/
Public Function process () {
Processing a foreign key as an enumeration
if ($this->foreignkey) {
$FK = $this->foreignkey;
$t = Table ($fk [' table ']);
if (Isset ($FK [' where ')]) {
$t = $t->where ($fk [' where ']);
}
if (Isset ($FK [' by ')]) {
$t = $t->orderby ($fk [' by ']);
}
$this->enum = $t->col ($fk [' field ']);
}

Password does not participate in search, modify/Create, display by password
if ($this->ispassword) {
$this->searchtype = null;
$this->updatetype = ' Password ';
}
}

/**
* Determine if this field is sortable
* @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 if this field participates 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 if this field participates in 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;
}

/**
* Determine if this field participates in list display
* @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;
}

/**
* Determine if this field participates in 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 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) {
Set the Encoding function
$this->encode = $encode;
return $this;
} else {
Specific code
$closure = $this->encode;
Return $closure ($encode);
}
}

/**
* Show this field in the list/view
*
* @param $value field values
* @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;
}
}

/**
* Display when creating/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;
}

/**
* Show a search condition
* @param string $default
*/
Public Function Showsearch () {
if (! $this->issearchable ()) {
Return
}

If it is an enumeration, add an unrestricted parameter
if ($this->enum) {
$enum =array_merge (Array (null=> ' unrestricted '), $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 if there are characters that are not allowed
* @param unknown $v
* @param unknown $chars
* @return Boolean
*/
Private Function Antiinject ($v, $chars) {
for ($i =0; $i <strlen ($chars); $i + +) {
if (Strpos ($v, $chars [$i])!==false)
return false;
}
return true;
}

/**
* Construction of fuzzy matching query conditions
* @param srequest $req
* @return boolean|string
*/
Private Function Wherelike (Srequest $req) {
Request name of parameter
$name = ' crud_ '. $this->name;

If this request parameter does not exist
if (! $req->exist ($name)) {
return false;
}

If the request parameter is empty
$v =trim ($req, $name);
if (! $v) {
return false;
}

If there are illegal characters in the request parameter
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 name of parameter
$name = ' crud_ '. $this->name;

If this request parameter does not exist
if (! $req->exist ($name)) {
return false;
}

If the request parameter is empty
$v =trim ($req, $name);

if (!strlen ($v)) {
return false;
}

If there are illegal characters in the request parameter
if (! $this->antiinject ($v, ' \ ' \ \ ')) {
return false;
}

Standardize the 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 date-matching search criteria
* @param srequest $req
* @return Boolean|multitype:ambigous
*/
Private Function Wheredate (Srequest $req) {
Request name of parameter
$name = ' crud_ '. $this->name;

If this request parameter does not exist
if (! $req->exist ($name)) {
return false;
}

If the request parameter is empty
$v =trim ($req, $name);
if (! $v) {
return false;
}

If there are illegal characters in the request parameter
if (! $this->antiinject ($v, ' \ ' \ \ ')) {
return false;
}

If you cannot resolve by date
$v =strtotime ($v);
if ($v) {
return false;
}

Standardize the 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);
}

/**
* Get 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 empty
$v = Trim ($req, $name);
if (! $v) {
return false;
}

If there are illegal characters in the request parameter
if (! $this->antiinject ($v, ' \ ' \ \ ')) {
return false;
}

Standardize the parameters
Switch ($this->simpletype) {
Case ' C ':
return $v;
Case ' I ':
Case ' R ':
Return Intval ($v);
Case ' N ':
Return Floatval ($v);
Case ' D ':
If you cannot resolve by date
$v = Strtotime ($v);
if ($v) {
return false;
}
Return date (' y-m-d ', $v);
Case ' T ':
If you cannot resolve 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 name of parameter
$name = ' crud_ '. $this->name;

Take 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 criteria for date range
* @param srequest $req
* @return boolean|string
*/
Private Function Wheredaterange (Srequest $req) {
Request name of parameter
$name = ' crud_ '. $this->name;

Calculate boundary values
$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: Time-matched query criteria
}

/**
* Construct query criteria for single-selection search
* @param srequest $req
* @return Boolean|multitype:ambigous
*/
Private Function Whereradio (Srequest $req) {
Request name of parameter
$name = ' crud_ '. $this->name;

If this request parameter does not exist
if (! $req->exist ($name)) {
return false;
}

If the request parameter is empty
$v =trim ($req, $name);
if (! $v) {
return false;
}

If there are illegal characters in the request parameter
if (! $this->antiinject ($v, ' \ ' \ \ ')) {
return false;
}

Standardize the 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);
}

/**
* Construct query criteria for multi-select search based on user request
* @param srequest $req
* @return Boolean|multitype:ambigous
*/
Private Function Wherecheck (Srequest $req) {
Request name of parameter
$name = ' crud_ '. $this->name;

If this request parameter does not exist
if (! $req->exist ($name)) {
return false;
}

If the request parameter is empty
$v =trim ($req, $name);
if (! $v) {
return false;
}

If there are illegal characters in the request parameter
if (! $this->antiinject ($v, ' \ ' \ \ ')) {
return false;
}

Standardize the 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);
}

/**
* Constructs the query condition according to the user request parameter
*
* @param srequest $req
* @throws Exception
* @return Ambigous | ambigous | Ambigous <boolean, multitype:ambigous= "" >| Ambigous <boolean, multitype: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 (' program flow should not arrive here ');
}

The above is the icephp framework of the General CRUD function framework in the background (vi) Scrudfield field class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.