【phpcms-v9】index.php文件分析-前台首页模板文件的解析过程分析

第一步:前台首页默认执行的是:index.php?m=content&c=index&a=init

[html]  view plain  copy
 print ?
  1. //首页  
  2. public function init() {  
  3.         if(isset($_GET['siteid'])) {  
  4.             $siteid = intval($_GET['siteid']);          //当前站点ID  
  5.         } else {  
  6.             $siteid = 1;                        //当前站点ID  
  7.         }  
  8.         $siteid = $GLOBALS['siteid'] = max($siteid,1);  
  9.         define('SITEID', $siteid);  
  10.         $_userid = $this->_userid;  
  11.         $_username = $this->_username;  
  12.         $_groupid = $this->_groupid;  
  13.         $SEO = seo($siteid);                        //查看第二步,获取当前站点当前栏目下生成的SEO信息  
  14.         $sitelist  = getcache('sitelist','commons');                //缓存后台设置的所有站点配置信息  
  15.         $default_style = $sitelist[$siteid]['default_style'];       //当前站点默认模板风格配置  
  16.         $CATEGORYS = getcache('category_content_'.$siteid,'commons');   //当前站点所有栏目详细配置信息  
  17.         include template('content','index',$default_style);     //查看第三步:模版调用  
  18. }  

第二步:获取SEO信息:phpcms/libs/functions/global.func.php

[html]  view plain  copy
 print ?
  1. /**  
  2.  * 生成SEO  
  3.  * @param $siteid       站点ID  
  4.  * @param $catid        栏目ID  
  5.  * @param $title        标题  
  6.  * @param $description  描述  
  7.  * @param $keyword      关键词  
  8.  */  
  9. function seo($siteid, $catid = '', $title = '', $description = '', $keyword = '') {  
  10.     if (!empty($title))$title = strip_tags($title);                     //过滤title  
  11.     if (!empty($description)) $description = strip_tags($description);  //过滤description  
  12.     if (!empty($keyword)) $keyword = str_replace(' ', ',', strip_tags($keyword));//过滤keyword  
  13.     $sites = getcache('sitelist', 'commons');                           //所有站点详细配置信息  
  14.     $site = $sites[$siteid];                                            //当前站点详细配置信息  
  15.     $cat = array();  
  16.     if (!empty($catid)) {                                               //栏目ID不为空  
  17.         $siteids = getcache('category_content','commons');              //所有栏目对应的站点ID缓存文件,格式:栏目ID=>站点ID  
  18.         $siteid = $siteids[$catid];                                     //当前栏目对应的站点ID  
  19.         $categorys = getcache('category_content_'.$siteid,'commons');   //当前站点下所有栏目的详细配置信息  
  20.         $cat = $categorys[$catid];                                      //当前站点下当前栏目的详细配置信息                      
  21.         $cat['setting'] = string2array($cat['setting']);                //当前站点当前栏目详细配置信息的setting设置信息,转化为数组  
  22.     }  
  23.     //站点title  
  24.     $seo['site_title'] =isset($site['site_title']) && !empty($site['site_title']) ? $site['site_title'] : $site['name'];  
  25.     //关键词  
  26.     $seo['keyword'] = !empty($keyword) ? $keyword : $site['keywords'];  
  27.     //描述  
  28.     $seo['description'] = isset($description) && !empty($description) ? $description : (isset($cat['setting']['meta_description']) && !empty($cat['setting']['meta_description']) ? $cat['setting']['meta_description'] : (isset($site['description']) && !empty($site['description']) ? $site['description'] : ''));  
  29.     //标题  
  30.     $seo['title'] =  (isset($title) && !empty($title) ? $title.' - ' : '').(isset($cat['setting']['meta_title']) && !empty($cat['setting']['meta_title']) ? $cat['setting']['meta_title'].' - ' : (isset($cat['catname']) && !empty($cat['catname']) ? $cat['catname'].' - ' : ''));  
  31.     foreach ($seo as $k=>$v) {  
  32.         $seo[$k] = str_replace(array("\n","\r"),    '', $v);            //将seo信息中\n和\r替换为空  
  33.     }  
  34.     return $seo;                                                        //返回seo数组信息  
  35. }  

第三步:模板调用:phpcms/libs/functions/global.func.php

[html]  view plain  copy
 print ?
  1. /**  
  2.  * 模板调用  
  3.  *  
  4.  * @param $module  
  5.  * @param $template  
  6.  * @param $istag  
  7.  * @return unknown_type  
  8.  */  
  9. function template($module = 'content', $template = 'index', $style = '') {  
  10.   
  11.     if(strpos($module, 'plugin/')!== false) { //一般情况下不会执行if里面的代码  
  12.         $plugin = str_replace('plugin/', '', $module);  
  13.         return p_template($plugin, $template,$style);  
  14.     }  
  15.     $module = str_replace('/', DIRECTORY_SEPARATOR, $module);  
  16.     if(!empty($style) && preg_match('/([a-z0-9\-_]+)/is',$style)) {//如果模板风格不为空  
  17.     } elseif (empty($style) && !defined('STYLE')) {                //如果模板风格为空  
  18.         if(defined('SITEID')) {                                    //是否定义了SITEID常量  
  19.             $siteid = SITEID;  
  20.         } else {  
  21.             $siteid = param::get_cookie('siteid');  
  22.         }  
  23.         if (!$siteid) $siteid = 1;  
  24.         $sitelist = getcache('sitelist','commons');                 //获取所有站点的详细配置信息  
  25.         if(!empty($siteid)) {  
  26.             $style = $sitelist[$siteid]['default_style'];           //获取当前站点的默认模板风格  
  27.         }  
  28.     } elseif (empty($style) && defined('STYLE')) {  
  29.         $style = STYLE;  
  30.     } else {  
  31.         $style = 'default';  
  32.     }  
  33.     if(!$style) $style = 'default';  
  34.     $template_cache = pc_base::load_sys_class('template_cache');//模板解析类,路径:phpcms/libs/classes/template_cache.class.php  
  35.     //编译文件缓存路径:根目录/caches/caches_template/default/content/index.php  
  36.     $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';  
  37.     //路径:phpcms/templates/dafault/content/index.html ,如:首页模板文件  
  38.     if(file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {  
  39.         //如果编译文件不存在或者说模板文件的创建时间大于编译文件的生成时间,则重新编译  
  40.         if(!file_exists($compiledtplfile) || (@filemtime(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > @filemtime($compiledtplfile))) {  
  41.             $template_cache->template_compile($module, $template, $style);//查看第四步:适用模板风格不是default的情况  
  42.         }  
  43.     } else {  
  44.         //编译文件缓存路径:根目录/caches/caches_template/default/content/index.php  
  45.         $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';  
  46.         //如果编译文件不存在或者说前台公共的模板文件存在,并且前台公共模板文件的创建时间大于编译文件的生成时间  
  47.         if(!file_exists($compiledtplfile) || (file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') && filemtime(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > filemtime($compiledtplfile))) {  
  48.             //重新编译  
  49.             $template_cache->template_compile($module, $template, 'default');//查看第四步:适用于模板风格为default的情况  
  50.         } elseif (!file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {  
  51.             //如果前台公共的模板文件不存在的话,则提示模板不存在  
  52.             showmessage('Template does not exist.'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html');  
  53.         }  
  54.     }  
  55.     //返回编译文件  
  56.     return $compiledtplfile;  
  57. }  

第四步:模板解析:phpcms/libs/classes/template_cache.class.php

[html]  view plain  copy
 print ?
  1.        /**  
  2.  * 编译模板  
  3.  *  
  4.  * @param $module   模块名称  
  5.  * @param $template 模板文件名  
  6.  * @param $istag    是否为标签模板  
  7.  * @return unknown  
  8.  */  
  9.   
  10. public function template_compile($module, $template, $style = 'default') {  
  11.     if(strpos($module, '/')=== false) {//如果"/"不存在  
  12.         //路径:phpcms/templates/default/content/index.html ,如:首页公共模板文件  
  13.         $tplfile = $_tpl = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';  
  14.     } elseif (strpos($module, 'yp/') !== false) {  
  15.         $module = str_replace('/', DIRECTORY_SEPARATOR, $module);  
  16.         $tplfile = $_tpl = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';  
  17.     } else {  
  18.         $plugin = str_replace('plugin/', '', $module);  
  19.         $module = str_replace('/', DIRECTORY_SEPARATOR, $module);  
  20.         $tplfile = $_tpl = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html';  
  21.     }  
  22.     if ($style != 'default' && !file_exists ( $tplfile )) {  
  23.         $style = 'default';  
  24.         $tplfile = PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';  
  25.     }  
  26.     if (! file_exists ( $tplfile )) {  
  27.         //如果公共模板文件不存在,则提示模板文件不存在,如:/templates/default/content/index.html is not exists!  
  28.         showmessage ( "templates".DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.".html is not exists!" );  
  29.     }  
  30.     //获取公共模板文件中的内容  
  31.     $content = @file_get_contents ( $tplfile );  
  32.     //要生成的编译文件所在目录  
  33.     $filepath = CACHE_PATH.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;  
  34.     if(!is_dir($filepath)) {  
  35.         //如果目录不存在,则层级创建所有目录  
  36.         mkdir($filepath, 0777, true);  
  37.     }  
  38.     //编译文件的全路径  
  39.     $compiledtplfile = $filepath.$template.'.php';  
  40.     //解析公共模板文件中的内容及标签,并返回解析后的内容  
  41.     $content = $this->template_parse($content);//查看第五步  
  42.     //将解析后的公共模板文件内容写入到要生成的编译文件中  
  43.     $strlen = file_put_contents ( $compiledtplfile, $content );  
  44.     //给生成的编译文件设置权限  
  45.     chmod ( $compiledtplfile, 0777 );  
  46.     return $strlen;//返回写入编译文件的字节数  
  47. }  

第五步:模板解析:phpcms/libs/classes/template_cache.class.php

[html]  view plain  copy
 print ?
  1.        /**  
  2.  * 解析模板  
  3.  *  
  4.  * @param $str  模板内容  
  5.  * @return ture  
  6.  */  
  7. public function template_parse($str) {  
  8.     $str = preg_replace ( "/\{template\s+(.+)\}/", "<?php include template(\\1); ?>", $str );  
  9.     $str = preg_replace ( "/\{include\s+(.+)\}/", "<?php include \\1; ?>", $str );  
  10.     $str = preg_replace ( "/\{php\s+(.+)\}/", "<?php \\1?>", $str );  
  11.     $str = preg_replace ( "/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str );  
  12.     $str = preg_replace ( "/\{else\}/", "<?php } else { ?>", $str );  
  13.     $str = preg_replace ( "/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str );  
  14.     $str = preg_replace ( "/\{\/if\}/", "<?php } ?>", $str );  
  15.     //for 循环  
  16.     $str = preg_replace("/\{for\s+(.+?)\}/","<?php for(\\1) { ?>",$str);  
  17.     $str = preg_replace("/\{\/for\}/","<?php } ?>",$str);  
  18.     //++ --  
  19.     $str = preg_replace("/\{\+\+(.+?)\}/","<?php ++\\1; ?>",$str);  
  20.     $str = preg_replace("/\{\-\-(.+?)\}/","<?php ++\\1; ?>",$str);  
  21.     $str = preg_replace("/\{(.+?)\+\+\}/","<?php \\1++; ?>",$str);  
  22.     $str = preg_replace("/\{(.+?)\-\-\}/","<?php \\1--; ?>",$str);  
  23.     $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\}/", "<?php \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) { ?>", $str );  
  24.     $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>", $str );  
  25.     $str = preg_replace ( "/\{\/loop\}/", "<?php \$n++;}unset(\$n); ?>", $str );  
  26.     $str = preg_replace ( "/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]* ([]) )\}/", "<?php echo \\1;?>", $str );  
  27.     $str = preg_replace ( "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]* ([]) )\}/", "<?php echo \\1;?>", $str );  
  28.     $str = preg_replace ( "/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str );  
  29.     $str = preg_replace("/\{(\\$[a-zA-Z0-9_
    \'\"\$\x7f-\xff]+)\}/es", "\$this->addquote('<?php echo \\1;?>')",$str);  
  30.     $str = preg_replace ( "/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $str );  
  31.     $str = preg_replace("/\{pc:(\w+)\s+([^}]+)\}/ie", "self::pc_tag('$1','$2', '$0')", $str);//查看第六步:解析pc标签的开始标签  
  32.     $str = preg_replace("/\{\/pc\}/ie", "self::end_pc_tag()", $str);//查看第六步:解析pc标签的结束标签  
  33.     $str = "<?php defined('IN_PHPCMS') or exit('No permission resources.'); ?>" . $str;  
  34.     return $str;  
  35. }  

第六步:pc标签的解析:

[html]  view plain  copy
 print ?
  1. /**  
  2.      * 解析PC标签  
  3.      * @param string $op 操作方式  
  4.      * @param string $data 参数  
  5.      * @param string $html 匹配到的所有的HTML代码  
  6.      */  
  7.     public static function pc_tag($op, $data, $html) {  
  8.         preg_match_all("/([a-z]+)\=[\"]?([^\"]+)[\"]?/i", stripslashes($data), $matches, PREG_SET_ORDER);  
  9.         $arr = array('action','num','cache','page', 'pagesize', 'urlrule', 'return', 'start');  
  10.         $tools = array('json', 'xml', 'block', 'get');  
  11.         $datas = array();  
  12.         $tag_id = md5(stripslashes($html));  
  13.         //可视化条件  
  14.         $str_datas = 'op='.$op.'&tag_md5='.$tag_id;  
  15.         foreach ($matches as $v) {  
  16.             $str_datas .= $str_datas ? "&$v[1]=".($op == 'block' && strpos($v[2], '$') === 0 ? $v[2] : urlencode($v[2])) : "$v[1]=".(strpos($v[2], '$') === 0 ? $v[2] : urlencode($v[2]));  
  17.             if(in_array($v[1], $arr)) {  
  18.                 $$v[1] = $v[2];//如果pc标签中参数在默认参数数组$arr中存在,则将参数转换为变量,如:$page=value等  
  19.                 continue;  
  20.             }  
  21.             $datas[$v[1]] = $v[2];//如果pc标签中参数不在默认参数数组$arr中存在,则直接将其放置到$datas[参数名]=value中  
  22.         }  
  23.         $str = '';  
  24.         $num = isset($num) && intval($num) ? intval($num) : 20;  
  25.         $cache = isset($cache) && intval($cache) ? intval($cache) : 0;  
  26.         $return = isset($return) && trim($return) ? trim($return) : 'data';  
  27.         if (!isset($urlrule)) $urlrule = '';  
  28.         if (!empty($cache) && !isset($page)) {  
  29.             $str .'$tag_cache_name = md5(implode(\'&\','.self::arr_to_html($datas).').\''.$tag_id.'\');if(!$'.$return.' = tpl_cache($tag_cache_name,'.$cache.')){';  
  30.         }  
  31.         if (in_array($op,$tools)) {//pc标签分两大类:工具类和模块类                工具类执行如下代码  
  32.             switch ($op) {  
  33.                 case 'json':  
  34.                         if (isset($datas['url']) && !empty($datas['url'])) {  
  35.                             $str .= '$json = @file_get_contents(\''.$datas['url'].'\');';  
  36.                             $str .'$'.$return.' = json_decode($json, true);';  
  37.                         }  
  38.                     break;  
  39.                       
  40.                 case 'xml':  
  41.                         $str .= '$xml = pc_base::load_sys_class(\'xml\');';  
  42.                         $str .= '$xml_data = @file_get_contents(\''.$datas['url'].'\');';  
  43.                         $str .'$'.$return.' = $xml->xml_unserialize($xml_data);';  
  44.                     break;  
  45.                       
  46.                 case 'get':  
  47.                         $str .'pc_base::load_sys_class("get_model", "model", 0);';  
  48.                         if ($datas['dbsource']) {  
  49.                             $dbsource = getcache('dbsource', 'commons');  
  50.                             if (isset($dbsource[$datas['dbsource']])) {  
  51.                                 $str .'$get_db = new get_model('.var_export($dbsource,true).', \''.$datas['dbsource'].'\');';  
  52.                             } else {  
  53.                                 return false;  
  54.                             }  
  55.                         } else {  
  56.                             $str .'$get_db = new get_model();';  
  57.                         }  
  58.                         $num = isset($num) && intval($num) > 0 ? intval($num) : 20;  
  59.                         if (isset($start) && intval($start)) {  
  60.                             $limit = intval($start).','.$num;  
  61.                         } else {  
  62.                             $limit = $num;  
  63.                         }  
  64.                         if (isset($page)) {  
  65.                             $str .'$pagesize = '.$num.';';  
  66.                             $str .'$page = intval('.$page.') ? intval('.$page.') : 1;if($page<=0){$page=1;}';  
  67.                             $str .'$offset = ($page - 1) * $pagesize;';  
  68.                             $limit = '$offset,$pagesize';  
  69.                             if ($sql = preg_replace('/select([^from].*)from/i', "SELECT COUNT(*) as count FROM ", $datas['sql'])) {  
  70.                                 $str .'$r = $get_db->sql_query("'.$sql.'");$s = $get_db->fetch_next();$pages=pages($s[\'count\'], $page, $pagesize, $urlrule);';  
  71.                             }  
  72.                         }  
  73.                           
  74.                           
  75.                         $str .'$r = $get_db->sql_query("'.$datas['sql'].' LIMIT '.$limit.'");while(($s = $get_db->fetch_next()) != false) {$a[] = $s;}$'.$return.' = $a;unset($a);';  
  76.                     break;  
  77.                       
  78.                 case 'block':  
  79.                     $str .= '$block_tag = pc_base::load_app_class(\'block_tag\', \'block\');';  
  80.                     $str .'echo $block_tag->pc_tag('.self::arr_to_html($datas).');';  
  81.                     break;  
  82.             }  
  83.         } else {//pc标签分两大类:工具类和模块类                模块类执行如下代码  
  84.             if (!isset($action) || empty($action)) return false;  
  85.             //content模块:phpcms/modules/content/classes/content_tag.class.php  
  86.             if (module_exists($op) && file_exists(PC_PATH.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.$op.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.$op.'_tag.class.php')) {  
  87.                 //content_tag.class.php               检查content_tag类中是否存在的某方法  
  88.                 $str .'$'.$op.'_tag = pc_base::load_app_class("'.$op.'_tag", "'.$op.'");if (method_exists($'.$op.'_tag, \''.$action.'\')) {';   
  89.                 if (isset($start) && intval($start)) {  
  90.                     $datas['limit'] = intval($start).','.$num;//如:limit 0 , 10  
  91.                 } else {  
  92.                     $datas['limit'] = $num; //如:limit 10  
  93.                 }  
  94.                 if (isset($page)) {//分页参数  
  95.                     $str .'$pagesize = '.$num.';';//每页显示数据量  
  96.                     $str .'$page = intval('.$page.') ? intval('.$page.') : 1;if($page<=0){$page=1;}';//当前页码  
  97.                     $str .'$offset = ($page - 1) * $pagesize;';//要查询数据的开始位置  
  98.                     $datas['limit'] = '$offset.",".$pagesize';  
  99.                     $datas['action'] = $action;//方法,如,content_tag.class.php中的lists方法  
  100.                     $str .'$'.$op.'_total = $'.$op.'_tag->count('.self::arr_to_html($datas).');';//分页方法  
  101.                     $str .'$pages = pages($'.$op.'_total, $page, $pagesize, $urlrule);';  
  102.                 }  
  103.                 $str .'$'.$return.' = $'.$op.'_tag->'.$action.'('.self::arr_to_html($datas).');';//查看第七步:content_tag.class.php中方法  
  104.                 $str .'}';  
  105.             }   
  106.         }  
  107.         if (!empty($cache) && !isset($page)) {  
  108.             $str .'if(!empty($'.$return.')){setcache($tag_cache_name, $'.$return.', \'tpl_data\');}';  
  109.             $str .'}';  
  110.         }  
  111.         /**  
  112.          * 解析结果大概如下所示:  
  113.          <?php  
  114.          if(defined('IN_ADMIN')  && !defined('HTML')) {  
  115.             echo "<div class=\"admin_piao\" pc_action=\"content\" data=\"op=content&tag_md5=2d4b9e3c7c2cc4bd0cec8b1fac9ae764&action=position&posid=12&thumb=1&order=listorder+DESC&num=10\">  
  116.             <a href=\"javascript:void(0)\" class=\"admin_piao_edit\">编辑</a>";  
  117.          }  
  118.          $content_tag = pc_base::load_app_class("content_tag", "content");  
  119.          if (method_exists($content_tag, 'position')) {  
  120.             $data = $content_tag->position(array('posid'=>'12','thumb'=>'1','order'=>'listorder DESC','limit'=>'10',));  
  121.          }  
  122.         ?>  
  123.          */  
  124.         return "<"."?php if(defined('IN_ADMIN')  && !defined('HTML')) {echo \"<div class=\\\"admin_piao\\\" pc_action=\\\"".$op."\\\" data=\\\"".$str_datas."\\\"><a href=\\\"javascript:void(0)\\\" class=\\\"admin_piao_edit\\\">".($op=='block' ? L('block_add') : L('edit'))."</a>\";}".$str."?".">";  
  125.     }  

第七步:pc标签类,路径:phpcms/modules/content/classes/content_tag.class.php

[html]  view plain  copy
 print ?
  1.        private $db;  
  2. public function __construct() {  
  3.     $this->db = pc_base::load_model('content_model');//查看第八步:数据模型,对应数据表news 和 news_data  
  4.     $this->position = pc_base::load_model('position_data_model');//数据模型  
  5. }  
  6. /**  
  7.  * 初始化模型  
  8.  * @param $catid  
  9.  */  
  10. public function set_modelid($catid) {  
  11.     $siteids = getcache('category_content','commons');//获取所有栏目所属的站点id  
  12.     if(!$siteids[$catid]) return false;//不存在此栏目,返回false  
  13.     $siteid = $siteids[$catid];//当前栏目所属站点id  
  14.     $this->category = getcache('category_content_'.$siteid,'commons');//获取当前站点id下所有栏目的配置信息  
  15.     if($this->category[$catid]['type']!=0) return false;//如果不为内部栏目,返回false  0-内部栏目 1-单网页 2-外部链接  
  16.     $this->modelid = $this->category[$catid]['modelid'];//获取当前栏目所属模型id  
  17.     $this->db->set_model($this->modelid);//查看第八步  
  18.     $this->tablename = $this->db->table_name;//数据表名  
  19.     if(empty($this->category)) {//当前站点id下所有栏目的配置信息  
  20.         return false;  
  21.     } else {  
  22.         return true;  
  23.     }  
  24. }  

[html]  view plain  copy
 print ?
  1.        /**  
  2.  * 列表页标签  
  3.  * @param $data  
  4.  */  
  5. public function lists($data) {  
  6.     $catid = intval($data['catid']);  
  7.     if(!$this->set_modelid($catid)) return false;  
  8.     if(isset($data['where'])) {//如果pc标签中设置了条件  
  9.         $sql = $data['where'];//pc标签中的条件  
  10.     } else {//如果pc标签中没有设置条件  
  11.         $thumb = intval($data['thumb']) ? " AND thumb != ''" : '';  
  12.         if($this->category[$catid]['child']) {  
  13.             $catids_str = $this->category[$catid]['arrchildid'];  
  14.             $pos = strpos($catids_str,',')+1;  
  15.             $catids_str = substr($catids_str, $pos);  
  16.             $sql = "status=99 AND catid IN ($catids_str)".$thumb;  
  17.         } else {  
  18.             $sql = "status=99 AND catid='$catid'".$thumb;  
  19.         }  
  20.     }  
  21.     $order = $data['order'];//pc标签中排序字段  
  22.   
  23.     $return = $this->db->select($sql, '*', $data['limit'], $order, '', 'id');//从数据库中获取主表数据,使用的也是sql语句查询  
  24.                       
  25.     //调用副表的数据  
  26.     if (isset($data['moreinfo']) && intval($data['moreinfo']) == 1) {  
  27.         $ids = array();  
  28.         foreach ($return as $v) {  
  29.             if (isset($v['id']) && !empty($v['id'])) {  
  30.                 $ids[] = $v['id'];  
  31.             } else {  
  32.                 continue;  
  33.             }  
  34.         }  
  35.         if (!empty($ids)) {  
  36.             $this->db->table_name = $this->db->table_name.'_data';//副表名  
  37.             $ids = implode('\',\'', $ids);  
  38.             $r = $this->db->select("`id` IN ('$ids')", '*', '', '', '', 'id');  
  39.             if (!empty($r)) {  
  40.                 foreach ($r as $k=>$v) {  
  41.                     if (isset($return[$k])) $return[$k] = array_merge($v, $return[$k]);//主表中数据与副表中数据合并  
  42.                 }  
  43.             }  
  44.         }  
  45.     }  
  46.     return $return;//返回查询到的数据  
  47. }  


第八步:content_model类,路径:phpcms/model/content_model.class.php

[html]  view plain  copy
 print ?
  1.        public $table_name = '';//数据库表名  
  2. public $category = '';  
  3. public function __construct() {  
  4.     $this->db_config = pc_base::load_config('database');//加载数据库配置信息  
  5.     $this->db_setting = 'default';//加载数据库默认的配置信息  
  6.     parent::__construct();  
  7.     $this->url = pc_base::load_app_class('url', 'content');  
  8.     $this->siteid = get_siteid();//得到当前站点id  
  9. }  
  10. public function set_model($modelid) {  
  11.     $this->model = getcache('model', 'commons');//获取所有模型的配置信息  1-文档模型 2-下载模型 3-图片模型    跟后台设置有关  
  12.     $this->modelid = $modelid;//当前模型id  
  13.                $this->table_name = $this->db_tablepre.$this->model[$modelid]['tablename'];//模型所对应的数据表 文档模型-news  图片模型-picture 下载模型-download  
  14.     $this->model_tablename = $this->model[$modelid]['tablename'];  
  15. }  

总结:pc标签内部机制也是通过sql语句来返回数据的
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值