/**
* 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
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
*/
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)!

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP可以轻松创建互动网页内容。1)通过嵌入HTML动态生成内容,根据用户输入或数据库数据实时展示。2)处理表单提交并生成动态输出,确保使用htmlspecialchars防XSS。3)结合MySQL创建用户注册系统,使用password_hash和预处理语句增强安全性。掌握这些技巧将提升Web开发效率。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。