代码: 全选
<?php
/**
* 情侣博客插件, 可以把文章按作者成不同的栏目
*
* @package Couple Blog
* @author qining
* @version 1.0.0
* @dependence 9.11.25-*
* @link http://typecho.org
*/
class CoupleBlog implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->query = array('CoupleBlog', 'query');
Typecho_Plugin::factory('Widget_Archive')->___isLeft = array('CoupleBlog', '___isLeft');
Typecho_Plugin::factory('Widget_Archive')->___isRight = array('CoupleBlog', '___isRight');
return _t('请到插件配置里设置双栏分别显示的作者');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$db = Typecho_Db::get();
$result = $db->fetchAll($db->select('uid', 'screenName')->from('table.users'));
$authors = array();
foreach ($result as $row) {
$authors[$row['uid']] = $row['screenName'];
}
$leftAuthor = new Typecho_Widget_Helper_Form_Element_Select('leftAuthorId', $authors, 0, '第一栏显示的作者');
$form->addInput($leftAuthor);
$rightAuthor = new Typecho_Widget_Helper_Form_Element_Select('rightAuthorId', $authors, 0, '第二栏显示的作者');
$form->addInput($rightAuthor);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 是否为左侧
*
* @access public
* @param Typecho_Archive $widget archive组件
* @return void
*/
public static function ___isLeft($widget)
{
return $widget->authorId == Helper::options()->plugin('CoupleBlog')->leftAuthorId;
}
/**
* 是否为右侧
*
* @access public
* @param Typecho_Archive $widget archive组件
* @return void
*/
public static function ___isRight($widget)
{
return $widget->authorId == Helper::options()->plugin('CoupleBlog')->rightAuthorId;
}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function query($widget, $select)
{
$db = Typecho_Db::get();
/** 排除一些特殊情况 */
if ($widget->is('single') || $widget->is('author')) {
$db->fetchAll($select, array($widget, 'push'));
return;
}
/** 精彩部分来了 */
$leftCountSql = $widget->getCountSql();
$rightCountSql = clone $leftCountSql;
$leftCountSql->where('table.contents.authorId = ?', Helper::options()->plugin('CoupleBlog')->leftAuthorId);
$rightCountSql->where('table.contents.authorId = ?', Helper::options()->plugin('CoupleBlog')->rightAuthorId);
$leftCount = $widget->size($leftCountSql);
$rightCount =$widget->size($rightCountSql);
/** 翻页依据文章数最多的那一位 */
$widget->setTotal(max($leftCount, $rightCount));
$leftSql = $select;
$rightSql = clone $select;
$leftSql->where('table.contents.authorId = ?', Helper::options()->plugin('CoupleBlog')->leftAuthorId);
$rightSql->where('table.contents.authorId = ?', Helper::options()->plugin('CoupleBlog')->rightAuthorId);
$db->fetchAll($leftSql, array($widget, 'push'));
$db->fetchAll($rightSql, array($widget, 'push'));
}
}
以上是你的插件,但是我用了莫名其妙的有个BUG,右边比左边少一篇文章,但是左右同一作者的话,文章数又相同。