首页 后端开发 php教程 IcePHP框架中的快速后台中的通用CRUD功能框架(六) SCrudField 字段类

IcePHP框架中的快速后台中的通用CRUD功能框架(六) SCrudField 字段类

Dec 27, 2016 am 09:56 AM

/**
* CRUD字段类
* @author bluehire
*
*/ 
class SCrudField extends SCrudSub {
// 以下属性来源数据库(配置文件,config/crud/*.config.php)
public $name; // 字段名称
private $scale; // 精度
private $type; // 类型完整
private $maxLength; // 最大长度
public $simpleType; // 简单类型CILNDTXBR
private $notNull; // 不允许空
public $primaryKey; // 是否主键
private $autoIncrement; // 是否自增长
private $binary; // 是否二进制
private $unsigned; // 无符号
private $hasDefault; // 有否默认值
public $defaultValue; // 默认值
public $description; // 字段备注

// 重新计算的默认值,可修改
public $title; // 字段标题

//以下属性,均为Boolean,可设置 
public $isPassword; // 是否密码字段
public $isAbandon; // 是否被放弃 
public $inGrid; // 是否参与列表
public $inInsert; // 是否参与创建
public $inUpdate; // 是否参与修改
public $inView; // 是否参与查看
public $inSort; // 是否参与排序
public $isCreated; // 是否创建时间字段
public $isUpdated; // 是否修改时间字段

public $showType; //字段的CRUD类型 Text/Image/Date/Time/String
public $updateType; //字段的CRUD类型 Text/Image/Date/Time/String
public $searchType; //搜索类型 LIKE/EQUAL/DATE/TIME/RANGE/DATERANGE/CHECK/RADIO/TREE/LIST

public $enum; //本字段是枚举,在此设置字段 存储值与显示值的对应
public $foreignKey; //本字段是另一表的外键,在此设置主表名及主表字段名

public $regular; //用于前后端验证的正则表达式

public $width = false; //图片的宽度设置
public $height = false; //图片的高度设置
public $style = false; //图片样式设置
public $css = false; //设置图片的样式 类
public $alt = false; //设置图片的替换文字
public $format; // 格式

public $searchDefault; //搜索条件的默认值
public $searchMax; //搜索范围的上限
public $searchMin; //搜索范围的下限

private $config; // 保存原始配置

/**
* @param SCrud $father 主CRUD对象
* @param array $c 数据库配置
*/
public function __construct(SCrud $father, array $c) {
$this->crud = $father;
$this->config = $c;

//所有配置值记录到本对象的属性中
foreach($c as $k=>$v){
$this->$k=$v;
}

//处理一些属性的默认值
$t=$c['simpleType'];
$n=$c['name'];

$default = array (
'title' => $c ['description'] ? : $n, //标题的默认值: 备注/字段名
'inSort' => strpos ( '>CIRNDT', $t ), //是否参与排序: C/I/N/D/T 
'inGrid' => strpos ( '>CIRLNDT', $t ), //是否参与列表 
'inInsert' => strpos ( '>CILNDTX', $t ) and ! $c ['primaryKey'], //是否参与创建
'inUpdate' => strpos ( '>CILNDTX', $t ) and ! $c ['primaryKey'], //是否参与编辑
'inView' => strpos ( '>CIRLNDTX', $t ), //是否参与显示
'isCreated' => strpos ( '>CIDT', $t ) and ($n == 'created' or $n == 'create_time'), // 是否是创建时间字段
'isUpdated' => strpos ( '>CIDT', $t ) and ($n == 'updated' or $n == 'update_time'), //是否是修改时间字段
);

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

//设置字段的默认CRUD类型
switch($t){
//日期
case 'D':
$this->showType='Date';
$this->updateType='Date';
$this->searchType='DateRange';
break;
//时间
case 'T':
$this->showType='Time';
$this->updateType='Time';
$this->searchType='DateRange';
break;
//大文本
case 'X':
$this->showType='String';
$this->updateType='Text';
$this->searchType=null;
break;
//字符串
case 'C':
$this->showType='String';
$this->updateType='String';
$this->searchType='Like';
break;
//逻辑
case 'L':
$this->showType='String';
$this->updateType='Radio';
$this->searchType='List';
$this->enum=array('0'=>'否','1'=>'是');
break;
//整数
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;

}

/**
* 在使用前,对字段再进行一次处理
*/
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';
}
}

/**
* 判断本字段是否可排序
* @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;
}

/**
* 判断本字段是否参与创建
* @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;
}

/**
* 判断本字段是否参与编辑
* @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;
}

/**
* 判断本字段是否参与列表显示
* @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;
}

/**
* 判断本字段是否参与查看
* @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;
}

/**
* 保存解码函数
* @var Closure
*/
public $decode;

/**
* 设置解码函数/解码
* @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 );
}
}

/**
* 保存编码函数
* @var Closure
*/
public $encode;

/**
* 设置编码函数
* @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 );
}
}

/**
* 在列表/查看显示本字段

* @param $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;
}
}

/**
* 在创建/编辑 时显示
* @param string $default
*/
public function showUpdate($v=''){ 
$tpl = 'update_' . strtolower ( $this->updateType );
$this->crud->display ( $tpl, array (
'field' => $this,
'value'=>$v
) );
}

/**
* 判断是否参与搜索
* @return boolean
*/
public function isSearchable(){
if($this->isAbandon or !$this->searchType or $this->isPassword){
return false;
}
return true;
}

/**
* 显示一个搜索条件
* @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, 
) );
}

/**
* 判断是否有不允许的字符
* @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;
}

/**
* 构造 模糊匹配的查询条件
* @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.'%"';
}

/**
* 构造 精确匹配的查询条件
* @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);
}

/**
* 构造日期匹配的搜索条件
* @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 array($this->name=>$v);
}

/**
* 从请求参数中获取一个日期范围边界参数
* @param unknown $name
* @param SRequest $req
* @return boolean|string|number|Ambigous 
*/
private function whereOne($name, SRequest $req) {
// 如果不存在此请求参数
if (! $req->exist ( $name )) {
return false;
}

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

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

// 对参数进行标准化
switch ($this->simpleType) {
case 'C' :
return $v;
case 'I' :
case 'R':
return intval ( $v );
case 'N' :
return floatval ( $v );
case 'D' :
// 如果无法按日期解析
$v = strtotime ( $v );
if ($v) {
return false;
}
return date ( 'Y-m-d', $v );
case 'T' :
// 如果无法按日期解析
$v = strtotime ( $v );
if ($v) {
return false;
}
return date ( 'Y-m-d H:i:s', $v );
}

return $v;
}

/**
* 根据请求参数创建搜索条件
*/
private function whereRange(SRequest $req){
//请求参数名
$name='crud_'.$this->name;

//取边界值
$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 '`'.$this->name.'` BETWEEN "'.$min.'" AND "'.$max.'"';
}

/**
* 构造日期范围的查询条件
* @param SRequest $req
* @return boolean|string
*/
private function whereDateRange(SRequest $req){
//请求参数名
$name='crud_'.$this->name;

//计算边界值
$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 '`'.$this->name.'` BETWEEN "'.$min.'" AND "'.$max.'"';
}

private function whereTime(SRequest $req){
//@todo:时间匹配的查询条件
}

/**
* 构造 单选搜索的查询条件
* @param SRequest $req
* @return boolean|multitype:Ambigous 
*/
private function whereRadio(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;
}

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

/**
* 根据用户请求构造多选搜索的查询条件
* @param SRequest $req
* @return boolean|multitype:Ambigous 
*/
private function whereCheck(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;
}

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

/**
* 根据用户请求参数,构造 查询条件

* @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 ( '程序流程不应该到达这里' );
}

 以上就是IcePHP框架中的快速后台中的通用CRUD功能框架(六) SCrudField 字段类的内容,更多相关内容请关注PHP中文网(www.php.cn)!


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

解释PHP中晚期静态结合的概念。 解释PHP中晚期静态结合的概念。 Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章讨论了框架中的基本安全功能,以防止漏洞,包括输入验证,身份验证和常规更新。

自定义/扩展框架:如何添加自定义功能。 自定义/扩展框架:如何添加自定义功能。 Mar 28, 2025 pm 05:12 PM

本文讨论了将自定义功能添加到框架上,专注于理解体系结构,识别扩展点以及集成和调试的最佳实践。

如何用PHP的cURL库发送包含JSON数据的POST请求? 如何用PHP的cURL库发送包含JSON数据的POST请求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...

描述扎实的原则及其如何应用于PHP的开发。 描述扎实的原则及其如何应用于PHP的开发。 Apr 03, 2025 am 12:04 AM

SOLID原则在PHP开发中的应用包括:1.单一职责原则(SRP):每个类只负责一个功能。2.开闭原则(OCP):通过扩展而非修改实现变化。3.里氏替换原则(LSP):子类可替换基类而不影响程序正确性。4.接口隔离原则(ISP):使用细粒度接口避免依赖不使用的方法。5.依赖倒置原则(DIP):高低层次模块都依赖于抽象,通过依赖注入实现。

会话如何劫持工作,如何在PHP中减轻它? 会话如何劫持工作,如何在PHP中减轻它? Apr 06, 2025 am 12:02 AM

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

See all articles