首頁 後端開發 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; publicprivate $hasDefault; // 預設值
public $defaultValue; $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' 或 $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';
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' 或 $this->simpleType=='B' 或 $this->simpleType=='L' or $this->isPassword or !$this->inGrid){
return false;
}
this
return $this ->inSort;
}

/**
* 判斷本欄位是否參與建立
* @return boolean
*/
public function isInsertable(){
if($this->isAbandon or $this->simpleType=='B' or $this->isCreated or $ this->isUpdated or $this->autoIncrement 或 $this->primaryKey){
return false;
}
return $this->inInsert;
}

/**
* 判斷本欄位是否參與編輯
* @return boolean
*/
{
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' 或 $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 function decode($decode) {
if ($decode instanceof Closure) {
//設定解碼函數
$this->decode = $decode;
return $this;this;this;this; } else {
//具體解碼
$closure = $this->decode;
return $closure ( $decode );
}
}

/**
* 設定解碼函數/解碼
* @param Closure|mixed $decode 
* @return SCrudField
*/
public $encode;
*
*
* *
* 保存編碼函數
* @var Closure
*/
public function encode($encode) { 
if ($encode instanceof Closure) {
//設定編碼函數
$this->encode = $encode;
return $this;
} else$this->encode = $encode;
return $this;
} else //具體編碼
$closure = $this->encode;
return $closure ( $encode );
}
}

/**
* 設定編碼函數
* @param Closure|mixed $encode
* @return SCrudField
*/
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';
返回日期( $format, $value );
case '日期' :
$format = $this->format ? : 'Y-m- d';
回傳日期( $format, $value );
case 'Text' :
回傳$this->showString ( $value );
預設值:
if ($this->decode) {
return $this ->decode ( $value );
}
return $value;
}
}

/**
* 在建立/編輯 時顯示
* @param string $default
*/
public function showUpdate($v=''){ 
$tpl ='更新更新_'。 strtolower ( $this->updateType );
$this->crud->display ( $tpl, array (
'field' => $this,
'value'=>$v
) );
}
'value'=>$v
) );
}

/ **
* 判斷是否參與搜尋
* @return boolean
*/
public function isSearchable(){
if($this->isAbandon 或!$this->searchType 或$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
*/
私有函數antiInject($v,$chars){
for($i=0;$i if(strpos($ v,$chars[$i])!==false)
回傳false;
}
回傳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->; exit($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' 或strtolower($v)=='true')?1:0);
case 'N' :
回傳陣列($this->name =>floatval($v));
case 'D':
$p=strtotime($v);
if(!$p){
return false;
}
if(!$p){
return false;
}
if(!$p){
return false;
}
if(!$p){
return false;
}
if(!$p){
return false;
}
if(!$p){
return false;
}
if(!$p){
return false;
}
if(!$p){
return false;
}
if(!$p){
return false;
}
if return array($this->name =>date('Y-m-d',$p));
case 'T':
$t=strtotime($v);
if(!$t){
return false ;
}
回傳陣列( $this->name=>date('Y-m-d H:i:s',$t));
}

//回傳條件
return array($this->name=>$this->name=>$this->name=>$this->name=>$this->name=>$this->name=>$this->name=>$this->name=>$this->name=>$this->name=>$this->name=>$this->name=>$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->;簡單類型){🎜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 $vv;
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.'`}

//回傳條件
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->`'.$this->名.'`>="'.$min.'"';
}

if(!$min){
return '`'.$this->name.'`}

//回傳條件
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':
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 |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'turn :
re $ 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 ( $ree ); case 'DateRange' :
return $this->whereDateRange ( $req );
}
throw new Exception ( '程式流程不應該到達這裡' );
}

框架以上是IcePHP 框架以上是IcePHP 功能架構(六) 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脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++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中減輕它? Apr 06, 2025 am 12:02 AM

會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。

描述紮實的原則及其如何應用於PHP的開發。 描述紮實的原則及其如何應用於PHP的開發。 Apr 03, 2025 am 12:04 AM

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

PHP 8.1中的枚舉(枚舉)是什麼? PHP 8.1中的枚舉(枚舉)是什麼? Apr 03, 2025 am 12:05 AM

PHP8.1中的枚舉功能通過定義命名常量增強了代碼的清晰度和類型安全性。 1)枚舉可以是整數、字符串或對象,提高了代碼可讀性和類型安全性。 2)枚舉基於類,支持面向對象特性,如遍歷和反射。 3)枚舉可用於比較和賦值,確保類型安全。 4)枚舉支持添加方法,實現複雜邏輯。 5)嚴格類型檢查和錯誤處理可避免常見錯誤。 6)枚舉減少魔法值,提升可維護性,但需注意性能優化。

在PHPStorm中如何進行CLI模式的調試? 在PHPStorm中如何進行CLI模式的調試? Apr 01, 2025 pm 02:57 PM

在PHPStorm中如何進行CLI模式的調試?在使用PHPStorm進行開發時,有時我們需要在命令行界面(CLI)模式下調試PHP�...

如何在系統重啟後自動設置unixsocket的權限? 如何在系統重啟後自動設置unixsocket的權限? Mar 31, 2025 pm 11:54 PM

如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...

如何用PHP的cURL庫發送包含JSON數據的POST請求? 如何用PHP的cURL庫發送包含JSON數據的POST請求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

See all articles