phpcms-v9】前台模板文件中{pc}标签的执行流程

前台pc标签的使用:{pc:content 参数名="参数值" 参数名="参数值" 参数名="参数值"}
如: {pc:content action="lists" catid="9" cache="3600" num="20" page="$page"}{/pc}
执行流程如下:
     ①模块名:content
     ②模块类文件:modules/content/classes/content_tag.class.php,便于二次开发或标签的扩展
     ③文件中的方法:lists
     ④action后面的参数以数组的形式传递给lists方法,如:function lists($data=array())
此标签实际获取的是:content_tag.class.php文件中lists方法返回的数据
注意:此标签会根据catid的值去寻找对应的模型id,然后再根据对应的模型id寻找到对应的模型表,如:news
注意:几乎所有的模块都有一个标签类,格式如下:模块名_tag.class.php
注意:前台模板文件中出现{pc}标签的位置,都可在"碎片管理"中进行编辑修改,非常方便

content_tag.class.php文件分析-pc标签调用的方法

<?php
class content_tag {
    private $db;                                                            //主要针对v9_news表
    public function __construct() {
        $this->db = pc_base::load_model('content_model');                    //数据模型,对应数据表news 和 news_data
        $this->position = pc_base::load_model('position_data_model');        //position_data表
    }
    /**
     * 初始化模型,其实就是为了根据栏目id设置对应对应的模型表及数据表:news-模型表、v9_news-数据表
     * @param $catid
     */
    public function set_modelid($catid) {
        $siteids = getcache('category_content','commons');    //获取所有栏目所属的站点id
        if(!$siteids[$catid]) return false;                    //不存在此栏目,返回false
        $siteid = $siteids[$catid];                            //当前栏目所属站点id  
        $this->category = getcache('category_content_'.$siteid,'commons');//获取当前站点id下所有栏目的配置信息
        if($this->category[$catid]['type']!=0) return false;//如果不为内部栏目,返回false  0-内部栏目 1-单网页 2-外部链接
        $this->modelid = $this->category[$catid]['modelid'];//获取当前栏目所属模型id  
        $this->db->set_model($this->modelid);                //根据模型id获取当前模型所对应的模型表和数据表 1:文档模型-news  3:图片模型-picture   2:下载模型-download
        $this->tablename = $this->db->table_name;            //数据表:v9_news
        if(empty($this->category)) {                        //如果当前站点下不存在栏目配置信息,则返回false
            return false;
        } else {                                            //如果当前站点下存在栏目配置信息,则返回true
            return true;
        }
    }
    /**
     * 分页统计
     * @param $data
     */
    public function count($data) {
        if($data['action'] == 'lists') {
            $catid = intval($data['catid']);
            if(!$this->set_modelid($catid)) return false;
            if(isset($data['where'])) {
                $sql = $data['where'];
            } else {
                if($this->category[$catid]['child']) {
                    $catids_str = $this->category[$catid]['arrchildid'];
                    $pos = strpos($catids_str,',')+1;
                    $catids_str = substr($catids_str, $pos);
                    $sql = "status=99 AND catid IN ($catids_str)";
                } else {
                    $sql = "status=99 AND catid='$catid'";
                }
            }
            return $this->db->count($sql);
        }
    }
    
    /**
     * 列表页标签:主要返回的是主表中数据与附表中数据, {pc:content action="lists"} {/pc}标签调用的都是lists方法
     * @param $data
     */
    public function lists($data) {
        $catid = intval($data['catid']);                                    //pc标签中catid属性
        if(!$this->set_modelid($catid)) return false;                        //会根据栏目id->对应的模型id->对应的模型表
        if(isset($data['where'])) {                                            //如果pc标签中设置了where属性,一般情况下不存在
            $sql = $data['where'];                                            
        } else {                                                            //如果pc标签中没有设置where属性
            $thumb = intval($data['thumb']) ? " AND thumb != ''" : '';        //如果有thumb属性
            if($this->category[$catid]['child']) {                            //当前栏目下是否存在子栏目
                $catids_str = $this->category[$catid]['arrchildid'];        //所有子栏目id,包括当前栏目自身id
                $pos = strpos($catids_str,',')+1;                            
                $catids_str = substr($catids_str, $pos);                    //所有子栏目id,不包括当前栏目自身id
                $sql = "status=99 AND catid IN ($catids_str)".$thumb;        //拼接成一个sql语句
            } else {
                $sql = "status=99 AND catid='$catid'".$thumb;                //如果当前栏目下不存在子栏目
            }
        }
        $order = $data['order'];                                            //pc标签中order属性
        //$sql作为一个条件出现,调用的是model.calss.php文件中的select方法,返回结果集数组,并按照键名'id'排序
        $return = $this->db->select($sql, '*', $data['limit'], $order, '', 'id');//从数据库中获取主表数据,使用的也是sql语句查询
                        
        //调用副表的数据
        if (isset($data['moreinfo']) && intval($data['moreinfo']) == 1) {    //如果pc标签中设置了moreinfo属性:是否调用附表数据
            $ids = array();
            //$return为返回的主表数据
            foreach ($return as $v) {                                        //循环主表中的记录信息:$v-主表中的每条记录
                if (isset($v['id']) && !empty($v['id'])) {
                    $ids[] = $v['id'];                                        //ids[]是主表中排序完成的文章id数组
                } else {
                    continue;
                }
            }
            if (!empty($ids)) {
                $this->db->table_name = $this->db->table_name.'_data';        //副表名
                $ids = implode('\',\'', $ids);                                //以逗号拼接成一个字符串
                $r = $this->db->select("`id` IN ('$ids')", '*', '', '', '', 'id');//查询与主表中数据有关的副表中的数据
                if (!empty($r)) {
                    foreach ($r as $k=>$v) {                                //副表中的数据
                        //$return:返回的主表中的数据,主表中的id字段值与附表中的id字段值是对应的,也就是说一篇文章在主表中存储的id跟在附表中存储的id的值是相等的
                        if (isset($return[$k])) $return[$k] = array_merge($v, $return[$k]);//主表中数据与副表中数据合并
                    }
                }
            }
        }
        return $return;
    }
    
    /**
     * 相关文章标签,{pc:content action="relation"} {/pc}标签调用的都是relation方法
     * @param $data
     */
    public function relation($data) {
        $catid = intval($data['catid']);                                    //pc标签的catid属性:栏目id
        if(!$this->set_modelid($catid)) return false;
        $order = $data['order'];                                            //pc标签的order属性
        $sql = "`status`=99";                                                //拼接sql
        $limit = $data['id'] ? $data['limit']+1 : $data['limit'];
        if($data['relation']) {                                                //pc标签的relation属性:相关文章id   格式如下:2|7|5
            $relations = explode('|',trim($data['relation'],'|'));            //以 | 分割成一个数组
            $relations = array_diff($relations, array(null));
            $relations = implode(',',$relations);
            $sql = " `id` IN ($relations)";                                    //拼接sql
            $key_array = $this->db->select($sql, '*', $limit, $order,'','id');//关联的文章
        } elseif($data['keywords']) {                                        //相关关键词
            $keywords = str_replace('%', '',$data['keywords']);
            $keywords_arr = explode(' ',$keywords);                            //以空格将关键词分割为一个数组
            $key_array = array();
            $number = 0;                                                    //相关热词文章数量
            $i =1;                                                            //控制返回文章数量的标识
            foreach ($keywords_arr as $_k) {                                
                $sql2 = $sql." AND `keywords` LIKE '%$_k%'".(isset($data['id']) && intval($data['id']) ? " AND `id` != '".abs(intval($data['id']))."'" : '');
                $r = $this->db->select($sql2, '*', $limit, '','','id');
                $number += count($r);                                        //所有热词文章的总量
                foreach ($r as $id=>$v) {
                    if($i<= $data['limit'] && !in_array($id, $key_array)) $key_array[$id] = $v;//$key_array[文章id]=文章记录
                    $i++;
                }
                if($data['limit']<$number) break;
            }
        }
        if($data['id']) unset($key_array[$data['id']]);//针对性的屏蔽某篇文章
        return $key_array;
    }
    
    /**
     * 排行榜标签{pc:content action="hits"} {/pc}标签调用的都是hits方法
     * @param $data
     */
    public function hits($data) {
        $catid = intval($data['catid']);                                    //栏目id,pc标签的catid属性
        if(!$this->set_modelid($catid)) return false;                        //根据栏目id->对应的模型id->模型表,如:news

        $this->hits_db = pc_base::load_model('hits_model');                    //hits:点击量表            
        $sql = $desc = $ids = '';
        $array = $ids_array = array();
        $order = $data['order'];                                            //pc标签中order属性
        $hitsid = 'c-'.$this->modelid.'-%';                                    //hits点击量表中hitsid字段的组成:c-模型id-文章id
        $sql = "hitsid LIKE '$hitsid'";                                        //拼接sql
        if(isset($data['day'])) {                                            //pc标签中day属性:表示调用多少天内的排行
            $updatetime = SYS_TIME-intval($data['day'])*86400;                //表示调用多少天内的排行
            $sql .= " AND updatetime>'$updatetime'";                        //拼接sql
        }
        if($this->category[$catid]['child']) {                                //当前栏目是否有子栏目
            $catids_str = $this->category[$catid]['arrchildid'];            //当前栏目下所有子栏目id,包括当前栏目自身的id
            $pos = strpos($catids_str,',')+1;
            $catids_str = substr($catids_str, $pos);                        //当前栏目下所有子栏目id,不包括当前栏目自身id
            $sql .= " AND catid IN ($catids_str)";                            //拼接sql
        } else {
            $sql .= " AND catid='$catid'";                                    //如果当前栏目不存在子栏目
        }
        $hits = array();
        $result = $this->hits_db->select($sql, '*', $data['limit'], $order);//查询v9_hits点击量表
        foreach ($result as $r) {                                            //$result:v9_hits表中返回的数据
            $pos = strpos($r['hitsid'],'-',2) + 1;                            //格式:c-模型id-文章id,如,c-1-1 ,查找第二个 "-"的位置
            $ids_array[] = $id = substr($r['hitsid'],$pos);                    //文章的id
            $hits[$id] = $r;                                                //每篇文章的点击量信息 ,格式:$hits[文章id]= 文章点击记录信息
        }
        $ids = implode(',', $ids_array);                                    //以逗号拼接成一个字符串
        if($ids) {                                    
            $sql = "status=99 AND id IN ($ids)";                            //拼接sql
        } else {
            $sql = '';
        }
        $this->db->table_name = $this->tablename;                            //内容主表:v9_news
        $result = $this->db->select($sql, '*', $data['limit'],'','','id');  //查询内容主表:v9_news
        foreach ($ids_array as $id) {
            if($result[$id]['title']!='') {
                $array[$id] = $result[$id];
                $array[$id] = array_merge($array[$id], $hits[$id]);            //将内容主表v9_news中数据与点击量表v9_hits中数据合并,合并桥梁为id
            }
        }
        return $array;
    }
    /**
     * 栏目标签:主要用来返回当前栏目下的所有子栏目及子栏目的url链接地址等信息:{pc:content action="category"} {/pc}标签调用的都是category方法
     * @param $data
     */
    public function category($data) {
        $data['catid'] = intval($data['catid']);                            //pc标签的catid属性:栏目id
        $array = array();
        $siteid = $data['siteid'] && intval($data['siteid']) ? intval($data['siteid']) : get_siteid();//pc标签的siteid属性:默认调用系统站点
        $categorys = getcache('category_content_'.$siteid,'commons');        //获取当前站点下所有栏目的详细配置信息
        $site = siteinfo($siteid);                                            //获取当前站点的信息
        $i = 1;
        foreach ($categorys as $catid=>$cat) {
            if($i>$data['limit']) break;
            if((!$cat['ismenu']) || $siteid && $cat['siteid']!=$siteid) continue;//ismenu:是否显示栏目,1-显示栏目   0-不显示栏目
            if (strpos($cat['url'], '://') === false) {                        //当前栏目的url链接地址中是否存在 "://"
                $cat['url'] = substr($site['domain'],0,-1).$cat['url'];        //当前栏目的url链接地址
            }
            if($cat['parentid']==$data['catid']) {                            //默认为0,调用一级栏目,$cat['parentid']只有一个值
                $array[$catid] = $cat;                                        //所有子栏目信息
                $i++;
            }
        }
        return $array;
    }
    
    /**
     * 推荐位,主要用来返回当前推荐位置所有文章的标题和url链接地址等信息:{pc:content action="position"} {/pc}标签调用的都是position方法
     * @param $data
     */
    public function position($data) {
        $sql = '';
        $array = array();
        $posid = intval($data['posid']);                                    //pc标签中posid属性:推荐位id
        $order = $data['order'];                                            //pc标签中order属性
        $thumb = (empty($data['thumb']) || intval($data['thumb']) == 0) ? 0 : 1;//pc标签中thumb属性
        $siteid = $GLOBALS['siteid'] ? $GLOBALS['siteid'] : 1;                //当前站点id
        $catid = (empty($data['catid']) || $data['catid'] == 0) ? '' : intval($data['catid']);//pc标签中的catid属性
        if($catid) {                                                        //如果栏目id存在
            $siteids = getcache('category_content','commons');                //获取所有栏目所对应的站点id
            if(!$siteids[$catid]) return false;                                //当前站点下不存在当前栏目,则返回false
            $siteid = $siteids[$catid];                                        //当前站点id
            $this->category = getcache('category_content_'.$siteid,'commons');//当前站点id下所有栏目的详细配置信息
        }
        if($catid && $this->category[$catid]['child']) {                    //当前栏目是否存在子栏目
            $catids_str = $this->category[$catid]['arrchildid'];            //当前栏目下所有子栏目id,包括当前栏目自身id
            $pos = strpos($catids_str,',')+1;
            $catids_str = substr($catids_str, $pos);                        //当前栏目下所有子栏目id,不包括当前栏目自身id
            $sql = "`catid` IN ($catids_str) AND ";                            //拼接sql
        }  elseif($catid && !$this->category[$catid]['child']) {            //如果当前栏目不存在子栏目
                $sql = "`catid` = '$catid' AND ";
        }
        if($thumb) $sql .= "`thumb` = '1' AND ";                            //有缩略图的情况
        if(isset($data['where'])) $sql .= $data['where'].' AND ';            //pc标签的where属性:一般情况下没有此属性
        if(isset($data['expiration']) && $data['expiration']==1) $sql .= '(`expiration` >= \''.SYS_TIME.'\' OR `expiration` = \'0\' ) AND ';
        $sql .= "`posid` = '$posid' AND `siteid` = '".$siteid."'";            //拼接sql
        $pos_arr = $this->position->select($sql, '*', $data['limit'],$order);//查询v9_position_data表
        if(!empty($pos_arr)) {
            foreach ($pos_arr as $info) {                                    //循环查询到的v9_position_data表中的记录
                $key = $info['catid'].'-'.$info['id'];                        //格式:栏目id-文章id
                $array[$key] = string2array($info['data']);                    //将v9_position_data表中data字段的值转换为数组
                $array[$key]['url'] = go($info['catid'],$info['id']);        //url链接地址
                $array[$key]['id'] = $info['id'];                            //文章id
                $array[$key]['catid'] = $info['catid'];                        //栏目id
                $array[$key]['listorder'] = $info['listorder'];                //排序
            }
        }
        return $array;
    }
    /**
     * 可视化标签,主要用于在后台可视化的对pc标签进行编辑
     */
    public function pc_tag() {
        $positionlist = getcache('position','commons');
        $sites = pc_base::load_app_class('sites','admin');
        $sitelist = $sites->pc_tag_list();
        
        foreach ($positionlist as $_v) if($_v['siteid'] == get_siteid() || $_v['siteid'] == 0) $poslist[$_v['posid']] = $_v['name'];
        return array(
            'action'=>array('lists'=>L('list','', 'content'),'position'=>L('position','', 'content'), 'category'=>L('subcat', '', 'content'), 'relation'=>L('related_articles', '', 'content'), 'hits'=>L('top', '', 'content')),
            'lists'=>array(
                'catid'=>array('name'=>L('catid', '', 'content'),'htmltype'=>'input_select_category','data'=>array('type'=>0),'validator'=>array('min'=>1)),
                'order'=>array('name'=>L('sort', '', 'content'), 'htmltype'=>'select','data'=>array('id DESC'=>L('id_desc', '', 'content'), 'updatetime DESC'=>L('updatetime_desc', '', 'content'), 'listorder ASC'=>L('listorder_asc', '', 'content'))),
                'thumb'=>array('name'=>L('thumb', '', 'content'), 'htmltype'=>'radio','data'=>array('0'=>L('all_list', '', 'content'), '1'=>L('thumb_list', '', 'content'))),
                'moreinfo'=>array('name'=>L('moreinfo', '', 'content'), 'htmltype'=>'radio', 'data'=>array('1'=>L('yes'), '0'=>L('no')))
            ),
            'position'=>array(
                'posid'=>array('name'=>L('posid', '', 'content'),'htmltype'=>'input_select','data'=>$poslist,'validator'=>array('min'=>1)),
                'catid'=>array('name'=>L('catid', '', 'content'),'htmltype'=>'input_select_category','data'=>array('type'=>0),'validator'=>array('min'=>0)),
                'thumb'=>array('name'=>L('thumb', '', 'content'), 'htmltype'=>'radio','data'=>array('0'=>L('all_list', '', 'content'), '1'=>L('thumb_list', '', 'content'))),            
                'order'=>array('name'=>L('sort', '', 'content'), 'htmltype'=>'select','data'=>array('listorder DESC'=>L('listorder_desc', '', 'content'),'listorder ASC'=>L('listorder_asc', '', 'content'),'id DESC'=>L('id_desc', '', 'content'))),
            ),
            'category'=>array(
                'siteid'=>array('name'=>L('siteid'), 'htmltype'=>'input_select', 'data'=>$sitelist),
                'catid'=>array('name'=>L('catid', '', 'content'), 'htmltype'=>'input_select_category', 'data'=>array('type'=>0))
            ),
            'relation'=>array(
                'catid'=>array('name'=>L('catid', '', 'content'), 'htmltype'=>'input_select_category', 'data'=>array('type'=>0), 'validator'=>array('min'=>1)),
                'order'=>array('name'=>L('sort', '', 'content'), 'htmltype'=>'select','data'=>array('id DESC'=>L('id_desc', '', 'content'), 'updatetime DESC'=>L('updatetime_desc', '', 'content'), 'listorder ASC'=>L('listorder_asc', '', 'content'))),
                'relation'=>array('name'=>L('relevant_articles_id', '', 'content'), 'htmltype'=>'input'),
                'keywords'=>array('name'=>L('key_word', '', 'content'), 'htmltype'=>'input')
            ),
            'hits'=>array(
                'catid'=>array('name'=>L('catid', '', 'content'), 'htmltype'=>'input_select_category', 'data'=>array('type'=>0), 'validator'=>array('min'=>1)),
                'day'=>array('name'=>L('day_select', '', 'content'), 'htmltype'=>'input', 'data'=>array('type'=>0)),
            ),
                
        );
    }
}

posted @ 2013-07-26 13:53  gzm2012  阅读(327)  评论(0编辑  收藏  举报