导航菜单
登录 注册

预览

评论列表预览
评论列表预览

思路

查库,

typecho_comments

表,这次我没有用之前的 push 方式来获取

Widget

对象,继承了

Widget_Abstract_Comments

类,可以直接使用

Typecho_Widget::widget()

方法来调用对象,比较符合Typecho的设计思路。重写了翻页代码,完美支持翻页。

核心代码

回复可见
此处内容已隐藏,回复后(需要填写邮箱)可见

调用方式

和平常的

class Widget_Comments_List extends Widget_Abstract_Comments
{
    /**
     * 全局选项
     *
     * @access protected
     * @var Widget_Options
     */
    protected $options;

    /**
     * 用户对象
     *
     * @access protected
     * @var Widget_User
     */
    protected $user;

    /**
     * 数据库对象
     *
     * @access protected
     * @var Typecho_Db
     */
    protected $db;

    /**
     * 当前页
     *
     * @access private
     * @var integer
     */
    private $_currentPage;

    /**
     * 生成分页的内容
     *
     * @access private
     * @var array
     */
    private $_pageRow = array();

    /**
     * 分页计算对象
     *
     * @access private
     * @var Typecho_Db_Query
     */
    private $_countSql;

    /**
     * 所有文章个数
     *
     * @access private
     * @var integer
     */
    private $_total = false;
    /**
     * 构造函数,初始化组件
     *
     * @access public
     * @param mixed $request request对象
     * @param mixed $response response对象
     * @param mixed $params 参数列表
     * @return void
     */
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);

        /** 初始化数据库 */
        $this->db = Typecho_Db::get();

        /** 初始化常用组件 */
        $this->options = $this->widget('Widget_Options');
        $this->user = $this->widget('Widget_User');

        $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'ignoreAuthor' => false));
    }

    /**
     * 执行函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {
        $this->_currentPage = $this->request->get('page', 1);

        $select  = $this->select()->limit($this->parameter->pageSize)
            ->where('table.comments.status = ?', 'approved')
            ->order('table.comments.coid', Typecho_Db::SORT_DESC);

        if ($this->parameter->parentId) {
            $select->where('cid = ?', $this->parameter->parentId);
        }

        if ($this->options->commentsShowCommentOnly) {
            $select->where('type = ?', 'comment');
        }

        /** 忽略作者评论 */
        if ($this->parameter->ignoreAuthor) {
            $select->where('ownerId <> authorId');
        }


        /** 仅输出文章 */
        $this->_countSql = clone $select;


        $select->page($this->_currentPage, $this->parameter->pageSize);
        $this->db->fetchAll($select, array($this, 'push'));
    }
    /**
     * 输出分页
     *
     * @access public
     * @param string $prev 上一页文字
     * @param string $next 下一页文字
     * @param int $splitPage 分割范围
     * @param string $splitWord 分割字符
     * @param string $template 展现配置信息
     * @return void
     */
    public function pageNav()
    {
        $query = $this->request->makeUriByRequest('page={page}');
        /** 使用盒状分页 */
        $nav = new Typecho_Widget_Helper_PageNavigator_Box(
            false === $this->_total ? $this->_total = $this->size($this->_countSql) : $this->_total,
            $this->_currentPage,
            $this->parameter->pageSize,
            $query
        );
        $nav->render('&lt;', '&gt;');
    }

    /**
     * @return int
     */
    public function getTotal()
    {
        if (false === $this->_total) {
            $this->_total = $this->size($this->_countSql);
        }

        return $this->_total;
    }

    public function size(Typecho_Db_Query $condition)
    {
        return $this->db->fetchObject($condition
            ->select(array('COUNT(DISTINCT table.comments.coid)' => 'num'))
            ->from('table.comments')
            ->cleanAttribute('group'))->num;
    }
}

对象是一样的。

Widget_Abstract_Comments
JOE:简单两栏博客主题
上一篇
Moz: 一款自适应typecho主题
下一篇

精选留言

已有 19 条评论

  1. a
    辽宁省

    去问问企鹅村

  2. Armstrong
    广西

    我来学习一下,然后把它举一反三。

  3. 狐狸
    安徽省

    sadsadrazzrazz我来看看!

  4. jungle
    北京市

    我来学习一下

  5. 呆窝云
    上海市

    我来学习一下,然后把它举二反六。

  6. 23234
    河北省

    是得分点分v

  7. lus
    浙江省

    抄代码。

  8. 001
    广东省

    去问问企鹅村

  9. 目的地-Destination
    江苏省

    能举二反六吗?获取获取某个用户的所有评论及相关回复评论。。。

  10. 皮皮社
    广东省

    学习下,看看如何实现。