前言:
增加/修改字段時可以設置“后臺增加信息處理函數”、“后臺修改信息處理函數”、“前臺增加信息處理函數”、“前臺修改信息處理函數”,可以分別設置對字段內容處理的函數,對于對字段內容存放格式有特殊要求的模型用得比較多。今天我們再簡單講解下處理函數制作格式。
基本設置步驟:
1、編寫處理函數;
2、將函數復制到e/class/userfun.php文件內容里;
3、修改字段設置處理函數名稱。
字段處理函數格式:
1 2 3 | function user_FieldFun( $mid , $f , $isadd , $isq , $value , $cs ){ return $value ; } |
參數說明:
user_FieldFun:函數名
$mid:系統模型ID
$f:字段名
$isadd:值為1時是增加信息;值為0時是修改信息
$isq:值為0時是后臺處理;值為1時是前臺處理
$value:字段原內容
$cs:字段附加參數,字段處理函數處設置的參數內容
字段處理函數范例:
例子1:自動在標題前面加“[EmpireCMS]”字樣
后臺字段函數設置:user_AddTitle
1 2 3 4 | function user_AddTitle( $mid , $f , $isadd , $isq , $value , $cs ){ $value = '[EmpireCMS]' . $value ; return $value ; } |
例子2:標題內容由writer和befrom字段的組合
后臺字段函數設置:user_TogTitle
標題字段顯示HTML代碼:<input type="hidden" name="title" value="test">
(說明:因為標題是必填項,所以要給初始值才不會提示內容空)
1 2 3 4 | function user_TogTitle( $mid , $f , $isadd , $isq , $value , $cs ){ $value = $_POST [ 'writer' ]. $_POST [ 'befrom' ]; return $value ; } |
例子3:上傳圖片并自動生成縮圖
后臺字段函數設置:user_TranImgAuto##170,120
(說明:后臺的參數170表示縮圖寬度,120為縮圖高度)
上傳圖片字段顯示HTML代碼:<input type="file" name="titlepicimgrs" size="45">
(說明:變量名用“字段名”+imgrs,即跟函數中的“$filetf”變量對應)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | function user_TranImgAuto( $mid , $f , $isadd , $isq , $value , $cs ){ global $empire , $dbtbpre , $public_r , $emod_r , $class_r , $tranpicturetype , $musername ; $filetf = $f . 'imgrs' ; //變量名 if (! $_FILES [ $filetf ][ 'name' ]) { return $value ; } $classid =(int) $_POST [ 'classid' ]; $id =(int) $_POST [ 'id' ]; $filepass =(int) $_POST [ 'filepass' ]; $filetype =GetFiletype( $_FILES [ $filetf ][ 'name' ]); $pr = $empire ->fetch1( "select qaddtran,qaddtransize,qaddtranimgtype from {$dbtbpre}enewspublic limit 1" ); if (! $pr [ 'qaddtran' ]) { printerror( "CloseQTranPic" , "" ,1); } if (! strstr ( $pr [ 'qaddtranimgtype' ], "|" . $filetype . "|" )) { printerror( "NotQTranFiletype" , "" ,1); } if ( $_FILES [ $filetf ][ 'size' ]> $pr [ 'qaddtransize' ]*1024) { printerror( "TooBigQTranFile" , "" ,1); } if (! strstr ( $tranpicturetype , ',' . $filetype . ',' )) { printerror( "NotQTranFiletype" , "" ,1); } $tfr =DoTranFile( $_FILES [ $filetf ][ 'tmp_name' ], $_FILES [ $filetf ][ 'name' ], $_FILES [ $filetf ][ 'type' ], $_FILES [ $filetf ][ 'size' ], $classid ); if ( $tfr [ 'tran' ]) { $csr = explode ( ',' , $cs ); $maxwidth = $csr [0]; $maxheight = $csr [1]; $yname = $tfr [ 'yname' ]; $name = $tfr [ 'name' ]; include_once (ECMS_PATH. 'e/class/gd.php' ); //生成縮圖 $filer =ResizeImage( $yname , $name , $maxwidth , $maxheight , $public_r [ 'spickill' ]); DelFiletext( $yname ); if ( $filer [ 'file' ]) { //寫入數據庫 $type =1; $filetime = date ( "Y-m-d H:i:s" ); $filesize =@ filesize ( $filer [ 'file' ]); $filename =GetFilename( str_replace (ECMS_PATH, '' , $filer [ 'file' ])); $adduser = '[Member]' . $musername ; $infoid = $isadd ==1?0: $id ; $empire ->query( "insert into {$dbtbpre}enewsfile(filename,filesize,adduser,path,filetime,classid,no,type,id,cjid,fpath) values('$filename','$filesize','$adduser','$tfr[filepath]','$filetime','$classid','[" . $f . "]" . addslashes (RepPostStr( $_POST [title])). "','$type','$infoid','$filepass','$public_r[fpath]');" ); if ( $isadd ==0) { $tbname = $emod_r [ $mid ][ 'tbname' ]; if ( strstr ( $emod_r [ $mid ][ 'tbdataf' ], ',' . $f . ',' )) { $ir = $empire ->fetch1( "select stb from {$dbtbpre}ecms_" . $tbname . " where id='$id'" ); $ifr = $empire ->fetch1( "select " . $f . " from {$dbtbpre}ecms_" . $tbname . "_data_" . $ir [stb]. " where id='$id'" ); $ifval = $ifr [ $f ]; } else { $ir = $empire ->fetch1( "select " . $f . " from {$dbtbpre}ecms_" . $tbname . " where id='$id'" ); $ifval = $ir [ $f ]; } if ( $ifval ) { DelYQTranFile( $classid , $id , $ifval , $f ); } } $value = str_replace ( $tfr [ 'filename' ], $filename , $tfr [ 'url' ]); } } else { $value = '' ; } return $value ; } |
處理函數可以實現很多非常復雜的字段內容存放格式需求,上面只是舉了幾個簡單的例子,更多需要用戶去實踐。
新聞熱點
疑難解答