/**
* 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<strlen($chars);$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 <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 ( '程序流程不应该到达这里' );
}
以上就是IcePHP框架中的快速后台中的通用CRUD功能框架(六) SCrudField 字段类的内容,更多相关内容请关注PHP中文网(www.php.cn)!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号