Home > Backend Development > PHP Tutorial > Universal CRUD function framework in the fast backend in the IcePHP framework (6) SCrudField field class

Universal CRUD function framework in the fast backend in the IcePHP framework (6) SCrudField field class

黄舟
Release: 2023-03-04 11:10:01
Original
1375 people have browsed it

/**
* CRUD field class
* @author bluehire
*
*/
class SCrudField extends SCrudSub {
// The following properties come 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
private $autoIncrement; // Whether it is auto-incrementing
private $binary; // Whether it is binary
private $unsigned; // Unsigned
private $hasDefault; // Is there a default value?
public $defaultValue; // Default value
public $description; // Field remarks

// Recalculated Default value, can be modified
public $title; // Field title

//The following attributes are 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 the creation
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 a time field

public $showType; //CRUD type of field Text/Image/Date/Time/String
public $updateType; //CRUD type of 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, here set the correspondence between the field storage value and the display value
public $foreignKey; //This field is a foreign key of another table. Set the main table name and main table field name here

public $regular; //Regular expression used for front-end and back-end verification

public $width = false; //Picture width settings
public $height = false; //Picture height settings
public $style = false; //Picture style settings
public $css = false; //Set the style class of the image
public $alt = false; //Set the replacement text of the image
public $format; //Format

public $searchDefault; / /Default value of search conditions
public $searchMax; //The upper limit of the search range
public $searchMin; //The 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 properties of this object
foreach($c as $k=>$v){
$this->$k=$ v;
}

//Handle the default values ​​of some attributes
$t=$c['simpleType'];
$n=$c['name'];

$default = array (
'title' => $c ['description'] ? : $n, //Default value of title: remarks/field name
'inSort' => strpos ( '>CIRNDT', $t ), //Whether to participate in sorting: 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 the creation
'inUpdate' => strpos ( ' >CILNDTX', $t ) and ! $c ['primaryKey'], //Whether to participate in editing
'inView' => strpos ( '>CIRLNDTX', $t ), //Whether to participate in display
'isCreated' => strpos ( '>CIDT', $t ) and ($n == 'created' or $n == 'create_time'), // Whether it is a creation time field
'isUpdated ' => strpos ( '>CIDT', $t ) and ($n == 'updated' or $n == 'update_time'), //Whether it is a modification time field
);

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'=>'is');
break;
//integer
case 'I':
$this->showType='String';
$this->updateType='String';
$this->searchType='Range';
break;
//自增长 整数
case 'R':
$this->showType='String';
$this->updateType='String';
$this->searchType='Equal';
break;
//浮点
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() {
// 将外键处理成枚举
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'] );
}

//密码不参与搜索,修改/创建时,按密码显示
if ($this->isPassword) {
$this->searchType = null;
$this->updateType = 'Password';
}
}

/**
* Determine whether this 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 participates in 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 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 whether 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 whether 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) {
//设置解码函数
$this->decode = $decode;
return $this;
} else {
//具体 解码
$closure = $this->decode;
return $closure ( $decode );
}
}

/**
* Save encoding function
* ​​@var Closure
*/
public $encode;

/**
* Set encoding function
* ​​@param Closure|mixed $encode
* @return SCrudField
*/
public function encode($encode) { 
if ($encode instanceof Closure) {
//设置编码函数
$this->encode = $encode;
return $this;
} else {
//具体 编码
$closure = $this->encode;
return $closure ( $encode );
}
}

/**
* Display this field in list/view
*
* @param $value field value
* @return string
*/
public function show($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 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;
}

/**
* Display a search condition
* ​​@param string $default
*/
public function showSearch() {
if(!$this->isSearchable()){
return;
}

//如果是枚举,增加一个 不限制 的参数
if($this->enum){
$enum=array_merge(array(null=>'不限制'),$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 there are disallowed characters
* @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 matching query conditions
* @param SRequest $req
* @return boolean|string
*/
private function whereLike(SRequest $req){
//请求参数名
$name='crud_'.$this->name;

//如果不存在此请求参数
if(!$req->exist($name)){
return false;
}

//如果请求参数为空
$v=trim($req->$name);
if(!$v){
return false;
}

//如果请求参数中有非法字符
if(!$this->antiInject($v, ''"\%_')){
return false;
}

//返回条件 
return '`'.$this->name.'` like "%'.$v.'%"';
}

/**
* Construct exact matching query conditions
* @param SRequest $req
* @return boolean|multitype:string
*/
private function whereEqual(SRequest $req){
//请求参数名
$name='crud_'.$this->name;

//如果不存在此请求参数
if(!$req->exist($name)){
return false;
}

//如果请求参数为空
$v=trim($req->$name);

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

//如果请求参数中有非法字符
if(!$this->antiInject($v, ''"\')){
return false;
}

//对参数进行标准化
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 array($this->name=>$v);
}

/**
* Construct search conditions for date matching
* @param SRequest $req
* @return boolean|multitype:Ambigous
*/
private function whereDate(SRequest $req){
//请求参数名
$name='crud_'.$this->name;

//如果不存在此请求参数
if(!$req->exist($name)){
return false;
}

//如果请求参数为空
$v=trim($req->$name);
if(!$v){
return false;
}

//如果请求参数中有非法字符
if(!$this->antiInject($v, ''"\')){
return false;
}

//如果无法按日期解析
$v=strtotime($v);
if($v){
return false;
}

//对参数进行标准化
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 parameters
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 date cannot be used Parse
$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;

//Get 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 date range
* @param SRequest $req
* @return boolean|string
*/
private function whereDateRange(SRequest $req){
//Request parameter name
$name='crud_'.$this-> name;

//Calculate 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 conditions
return '`'. $this->name.'` BETWEEN "'.$min.'" AND "'.$max.'"';
}

private function whereTime(SRequest $req){
//@todo: Query conditions for time matching
}

/**
* Construct query conditions for single-select 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 empty
$v=trim($req->$name);
if(!$v){
return false;
}

//If there are illegal characters in the request parameters
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);
}

/**
* Construct query conditions for multi-select search based on user request
* @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 empty
$v=trim($req->$name);
if(!$v){
return false;
}

//If there are illegal characters in the request parameters
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);
}

/**
* Construct query conditions based on user request parameters
*
* @param SRequest $req
* @throws Exception
* ​​@return Ambigous |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 reach here');
}

The above is the general CRUD function framework (6) in the fast backend in the IcePHP framework (6) The content of the SCrudField field class, please pay attention to more related content PHP Chinese website (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template