【phpcms-v9】content_form.calss.php文件分析-内容添加页面动态表单的生成原理

[html]  view plain  copy
 print ?
  1. <?php  
  2. //此文件主要根据caches/caches_model/caches_data/model_field_1.php文件中的模型字段来动态的生成表单  
  3. //路径:phpcms/caches/caches_model/caches_data/content_form.class.php文件,主要用来返回内容添加页面左侧动态生成的表单  
  4. //此类主要用来动态生成内容添加页面的所有表单:内容添加页面左侧表单不是直接写在html中的,而是通过程序动态生成的,现将其分析一下  
  5. //此文件需要参考:model_feild_模型ID.cache.php文件,因为通过后台 模型管理->字段管理 中所有已存在或新加的字段都会被缓存在这个文件中,如【下图2所示】  
  6.  class content_form {  
  7.     var $modelid;//模型id  1-文章模型 2-图片模型 3-下载模型  ,不同的模型在添加内容时会动态生成不同的表单  
  8.     var $fields;//所有模型字段信息  
  9.     var $id;  
  10.     var $formValidator;//表单验证  
  11.   
  12.     function __construct($modelid,$catid = 0,$categorys = array()) {  
  13.         $this->modelid = $modelid;  
  14.         $this->catid = $catid;  
  15.         $this->categorys = $categorys;  
  16.         $this->fields = getcache('model_field_'.$modelid,'model');//此缓存文件主要用来缓存模型字段信息  
  17.         $this->siteid = get_siteid();  
  18.     }  
  19.     //此方法主要用来获取所有动态生成好的表单,以便于在前台循环显示  
  20.     function get($data = array()) {  
  21.         $_groupid = param::get_cookie('_groupid');  
  22.         $this->data = $data;  
  23.         if(isset($data['id'])) $this->id = $data['id'];  
  24.         $info = array();  
  25.         $this->content_url = $data['url'];  
  26.           
  27.         /**  
  28.          * $this->fields:主要存放从model_field_模型id.cache.php文件中获取过来的所有模型字段信息  
  29.          * $field:单个模型字段信息  
  30.          */  
  31.         foreach($this->fields as $field=>$v) {  
  32.             if(defined('IN_ADMIN')) {  
  33.                 if($v['iscore'] || check_in($_SESSION['roleid'], $v['unsetroleids'])) continue;  
  34.             } else {  
  35.                 if($v['iscore'] || !$v['isadd'] || check_in($_groupid, $v['unsetgroupids'])) continue;  
  36.             }  
  37.             /**  
  38.              * 表单类型:formtype  
  39.              * 对应   后台->模型管理->字段管理->类型  
  40.              * 对应  content_form.class.php文件中方法的名称  
  41.              * 对应  caches/caches_model/caches_data/model_field_模型id.cache.php文件中键名  
  42.              * 注意:通过 后台->模型管理->字段管理->添加字段  ,添加的新字段都将被缓存到model_field_模型id.cache.php文件中  
  43.              */  
  44.             $func = $v['formtype'];//表单类型  
  45.               
  46.             $value = isset($data[$field]) ? htmlspecialchars($data[$field], ENT_QUOTES) : '';//表单值  
  47.             if($func=='pages' && isset($data['maxcharperpage'])) {  
  48.                 $value = $data['paginationtype'].'|'.$data['maxcharperpage'];  
  49.             }  
  50.             if(!method_exists($this, $func)) continue;  
  51.               
  52.             /**  
  53.              * 1.$func:假设模型字段名称为text,则会调用$this->text()方法  
  54.              * 2.$this->text()方法会生成并返回一个text类型的表单,如:<input type="text" name="info['keywords']" value="">  
  55.              * 3.后面程序会将生成的表单放到$info[][]二维数组中,前台只需要循环此数组即可将网页所有的表单都呈现出来  
  56.              */  
  57.             $form = $this->$func($field, $value, $v);  
  58.               
  59.             if($form !== false) {  
  60.                 //默认情况下此常量已经被定义了  
  61.                 if(defined('IN_ADMIN')) {  
  62.                     //基本信息字段:基本信息字段将在添加页面左侧显示  
  63.                     if($v['isbase']) {  
  64.                         //添加内容页面:左侧部分的表单名称  
  65.                         $star = $v['minlength'] || $v['pattern'] ? 1 : 0;  
  66.                         $info['base'][$field] = array('name'=>$v['name'], 'tips'=>$v['tips'], 'form'=>$form, 'star'=>$star,'isomnipotent'=>$v['isomnipotent'],'formtype'=>$v['formtype']);  
  67.                     } else {  
  68.                         //添加内容页面:右侧部分的表单名称  
  69.                         //$field:thumb、relation、inputtime、islink、template、allow_comment、readpoint  
  70.                         $star = $v['minlength'] || $v['pattern'] ? 1 : 0;  
  71.                         $info['senior'][$field] = array('name'=>$v['name'], 'tips'=>$v['tips'], 'form'=>$form, 'star'=>$star,'isomnipotent'=>$v['isomnipotent'],'formtype'=>$v['formtype']);  
  72.                     }  
  73.                 } else {  
  74.                     $star = $v['minlength'] || $v['pattern'] ? 1 : 0;  
  75.                     $info[$field] = array('name'=>$v['name'], 'tips'=>$v['tips'], 'form'=>$form, 'star'=>$star,'isomnipotent'=>$v['isomnipotent'],'formtype'=>$v['formtype']);  
  76.                 }  
  77.             }  
  78.         }  
  79.         return $info;  
  80.     }  
  81.   
  82.          //text方法主要用来生成一个text类型的表单:<input type="text" name="info[]" value="" />  
  83.     function text($field, $value, $fieldinfo) {  
  84.   
  85.     }  
  86.   
  87.     //textarea方法主要用来生成一个textarea类型的表单:<textarea name="info[]" ></textarea>  
  88.     function textarea($field, $value, $fieldinfo) {  
  89.           
  90.     }  
  91.   
  92.     //editor方法主要用来生成一个在线编辑器,如:ckeditor编辑器  
  93.   
  94.     function editor($field, $value, $fieldinfo) {  
  95.       
  96.         }  
  97.     function catid($field, $value, $fieldinfo) {  
  98.           
  99.     }  
  100.     function title($field, $value, $fieldinfo) {  
  101.           
  102.     }  
  103.   
  104.     //box方法主要用来生成radio、select、checkbox类型的表单:<input type="radio" name="info[]" value="" />  
  105.     function box($field, $value, $fieldinfo) {  
  106.   
  107.           
  108.     }  
  109.   
  110.     //image方法主要用来生成上传文件的输入框和上传文件按钮  
  111.     function image($field, $value, $fieldinfo) {  
  112.           
  113.     }  
  114.     function images($field, $value, $fieldinfo) {  
  115.           
  116.     }     
  117.         function number($field, $value, $fieldinfo) {  
  118.           
  119.     }  
  120.   
  121.  }   
  122. ?>  
  123.    


比如说:要查看title模型字段生成的表单,我们需要了解以下几处

第一处:caches/caches_model/caches_data/content_form.class.php文件中:$form = $this->$func($field, $value, $v);

这行代码其实就是在调用:function title($field, $value, $fieldinfo){}//参数1:模型字段或表单字段名称,如title    参数2:模型字段或表单字段的值    参数3:模型字段相关信息

函数如下:

[html]  view plain  copy
 print ?
  1. function title($field, $value, $fieldinfo) {  
  2.         extract($fieldinfo);//此行代码很重要  
  3.         $style_arr = explode(';',$this->data['style']);  
  4.         $style_color = $style_arr[0];  
  5.         $style_font_weight = $style_arr[1] ? $style_arr[1] : '';  
  6.   
  7.         $style = 'color:'.$this->data['style'];  
  8.         if(!$value) $value = $defaultvalue;  
  9.         $errortips = $this->fields[$field]['errortips'];  
  10.         $errortips_max = L('title_is_empty');  
  11.         if($errortips) $this->formValidator .'$("#'.$field.'").formValidator({onshow:"",onfocus:"'.$errortips.'"}).inputValidator({min:'.$minlength.',max:'.$maxlength.',onerror:"'.$errortips_max.'"});';  
  12.         $str = '<input type="text" style="width:400px;'.($style_color ? 'color:'.$style_color.';' : '').($style_font_weight ? 'font-weight:'.$style_font_weight.';' : '').'" name="info['.$field.']" id="'.$field.'" value="'.$value.'" style="'.$style.'" class="measure-input " onBlur="$.post(\'api.php?op=get_keywords&number=3&sid=\'+Math.random()*5, {data:$(\'#title\').val()}, function(data){if(data && $(\'#keywords\').val()==\'\') $(\'#keywords\').val(data); })" onkeyup="strlen_verify(this, \'title_len\', '.$maxlength.');"/><input type="hidden" name="style_color" id="style_color" value="'.$style_color.'">  
  13.         <input type="hidden" name="style_font_weight" id="style_font_weight" value="'.$style_font_weight.'">';  
  14.         if(defined('IN_ADMIN')) $str .'<input type="button" class="button" id="check_title_alt" value="'.L('check_title','','content').'" onclick="$.get(\'?m=content&c=content&a=public_check_title&catid='.$this->catid.'&sid=\'+Math.random()*5, {data:$(\'#title\').val()}, function(data){if(data==\'1\') {$(\'#check_title_alt\').val(\''.L('title_repeat').'\');$(\'#check_title_alt\').css(\'background-color\',\'#FFCC66\');} else if(data==\'0\') {$(\'#check_title_alt\').val(\''.L('title_not_repeat').'\');$(\'#check_title_alt\').css(\'background-color\',\'#F8FFE1\')}})" style="width:73px;"/><img src="'.IMG_PATH.'icon/colour.png" width="15" height="16" onclick="colorpicker(\''.$field.'_colorpanel\',\'set_title_color\');" style="cursor:hand"/>  
  15.         <img src="'.IMG_PATH.'icon/bold.png" width="10" height="10" onclick="input_font_bold()" style="cursor:hand"/> <span id="'.$field.'_colorpanel" style="position:absolute;" class="colorpanel"></span>';  
  16.         $str .L('can_enter').'<B><span id="title_len">'.$maxlength.'</span></B> '.L('characters');  
  17.         return $str;//返回的html表单  
  18.     }  



第二处:此模型字段在调用title($field, $value, $fieldinfo)函数后返回的html表单在模板中的表现:

[html]  view plain  copy
 print ?
  1. <td>  
  2. <input type="text" style="width:400px;" name="info[title]" id="title" value="" style="color:" class="measure-input " onBlur="$.post('api.php?op=get_keywords&number=3&sid='+Math.random()*5, {data:$('#title').val()}, function(data){if(data && $('#keywords').val()=='') $('#keywords').val(data); })" onkeyup="strlen_verify(this, 'title_len', 80);"/>  
  3. <input type="hidden" name="style_color" id="style_color" value="">  
  4. <input type="hidden" name="style_font_weight" id="style_font_weight" value="">  
  5. <input type="button" class="button" id="check_title_alt" value="检测重复" onclick="$.get('?m=content&c=content&a=public_check_title&catid=18&sid='+Math.random()*5, {data:$('#title').val()}, function(data){if(data=='1') {$('#check_title_alt').val('标题重复');$('#check_title_alt').css('background-color','#FFCC66');} else if(data=='0') {$('#check_title_alt').val('标题不重复');$('#check_title_alt').css('background-color','#F8FFE1')}})" style="width:73px;"/>  
  6. <img src="http://zhencms.com/statics/images/icon/colour.png" width="15" height="16" onclick="colorpicker('title_colorpanel','set_title_color');" style="cursor:hand"/>   
  7. <img src="http://zhencms.com/statics/images/icon/bold.png" width="10" height="10" onclick="input_font_bold()" style="cursor:hand"/>  
  8. <span id="title_colorpanel" style="position:absolute;" class="colorpanel"></span>还可输入<B><span id="title_len">80</span></B> 个字符  
  9. </td>  

第三处:第一处中的title函数是动态缓存到content_form.class.php文件中的,那么它是从哪里copy了一份缓存到content_form.class.php文件中的呢?从这里copy了一份后再动态缓存到content_form.class.php文件中的:phpcms/modules/content/fields/title/form.inc.php文件

[html]  view plain  copy
 print ?
  1. function title($field, $value, $fieldinfo) {  
  2.         extract($fieldinfo);//这行代码很重要,请查看第四处  
  3.         $style_arr = explode(';',$this->data['style']);  
  4.         $style_color = $style_arr[0];  
  5.         $style_font_weight = $style_arr[1] ? $style_arr[1] : '';  
  6.   
  7.         $style = 'color:'.$this->data['style'];  
  8.         if(!$value) $value = $defaultvalue;  
  9.         $errortips = $this->fields[$field]['errortips'];  
  10.         $errortips_max = L('title_is_empty');  
  11.         if($errortips) $this->formValidator .'$("#'.$field.'").formValidator({onshow:"",onfocus:"'.$errortips.'"}).inputValidator({min:'.$minlength.',max:'.$maxlength.',onerror:"'.$errortips_max.'"});';  
  12.         $str = '<input type="text" style="width:400px;'.($style_color ? 'color:'.$style_color.';' : '').($style_font_weight ? 'font-weight:'.$style_font_weight.';' : '').'" name="info['.$field.']" id="'.$field.'" value="'.$value.'" style="'.$style.'" class="measure-input " onBlur="$.post(\'api.php?op=get_keywords&number=3&sid=\'+Math.random()*5, {data:$(\'#title\').val()}, function(data){if(data && $(\'#keywords\').val()==\'\') $(\'#keywords\').val(data); })" onkeyup="strlen_verify(this, \'title_len\', '.$maxlength.');"/><input type="hidden" name="style_color" id="style_color" value="'.$style_color.'">  
  13.         <input type="hidden" name="style_font_weight" id="style_font_weight" value="'.$style_font_weight.'">';  
  14.         if(defined('IN_ADMIN')) $str .'<input type="button" class="button" id="check_title_alt" value="'.L('check_title','','content').'" onclick="$.get(\'?m=content&c=content&a=public_check_title&catid='.$this->catid.'&sid=\'+Math.random()*5, {data:$(\'#title\').val()}, function(data){if(data==\'1\') {$(\'#check_title_alt\').val(\''.L('title_repeat').'\');$(\'#check_title_alt\').css(\'background-color\',\'#FFCC66\');} else if(data==\'0\') {$(\'#check_title_alt\').val(\''.L('title_not_repeat').'\');$(\'#check_title_alt\').css(\'background-color\',\'#F8FFE1\')}})" style="width:73px;"/><img src="'.IMG_PATH.'icon/colour.png" width="15" height="16" onclick="colorpicker(\''.$field.'_colorpanel\',\'set_title_color\');" style="cursor:hand"/>   
  15.         <img src="'.IMG_PATH.'icon/bold.png" width="10" height="10" onclick="input_font_bold()" style="cursor:hand"/> <span id="'.$field.'_colorpanel" style="position:absolute;" class="colorpanel"></span>';  
  16.         $str .L('can_enter').'<B><span id="title_len">'.$maxlength.'</span></B> '.L('characters');  
  17.         return $str;  
  18.     }  

第四处:title()函数中的$filedinfo其实就是:caches/caches_model/caches_data/model_field_1.cache.php文件中如下代码:

[html]  view plain  copy
 print ?
  1. 'title' =>   
  2.  array (  
  3.    'fieldid' => '3',  
  4.    'modelid' => '1',  
  5.    'siteid' => '1',  
  6.    'field' => 'title',  
  7.    'name' => '标题',  
  8.    'tips' => '',  
  9.    'css' => 'inputtitle',  
  10.    'minlength' => '1',  
  11.    'maxlength' => '80',  
  12.    'pattern' => '',  
  13.    'errortips' => '请输入标题',  
  14.    'formtype' => 'title',  
  15.    'setting' => '',  
  16.    'formattribute' => '',  
  17.    'unsetgroupids' => '',  
  18.    'unsetroleids' => '',  
  19.    'iscore' => '0',  
  20.    'issystem' => '1',  
  21.    'isunique' => '0',  
  22.    'isbase' => '1',  
  23.    'issearch' => '1',  
  24.    'isadd' => '1',  
  25.    'isfulltext' => '1',  
  26.    'isposition' => '1',  
  27.    'listorder' => '4',  
  28.    'disabled' => '0',  
  29.    'isomnipotent' => '0',  
  30.  ),  



                                                                                                                              【图1】


来源:http://blog.csdn.net/yanhui_wei/article/details/7901129

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值