今天lewis 网友在论坛中问如何利用论坛投票实现digg的一样的效果,具体像http://www.autoit.cn/index.php/content/view/85/25.html 这篇文章中提到的一样,具体实现的代码如下:
首先来修改文章最终页面投票插件的显示位置,具体就是将最终页模板中的这几行:
<?php if (!$this->params->get('show_intro')) :
echo $this->article->event->afterDisplayTitle;
endif; ?>
<?php echo $this->article->event->beforeDisplayContent; ?>
放在文章标题显示之前,如果你是Joomla! 1.5.2应该是components/com_content/views/article/tmpl/default.php,第10行的位置:
<td class="contentheading<?php echo $this->params->get( 'pageclass_sfx' ); ?>" width="100%">
就是这一行之后,如果你是自己安装的其他模板,应该在相应的模板目录中。
接下来,我们来修改plugins/content/vote.php文件,源代码如下:
<?php
/**
* @version $Id: vote.php 9764 2007-12-30 07:48:11Z ircmaxell $
* @package Joomla
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );$mainframe->registerEvent( 'onBeforeDisplayContent', 'plgContentVote' );function plgContentVote( &$row, &$params, $page=0 )
{
$uri = & JFactory::getURI();
$db = & JFactory::getDBO(); $id = $row->id;
$html = '';
if (isset($row->rating_count) && $params->get( 'show_vote' ) && !$params->get( 'popup' ))
{
$query = 'select rating_count from #__content_rating where content_id='.$row->id;
$db->setQuery($query);
$rating_count = $db->loadResult();
JPlugin::loadLanguage( 'plg_content_vote' );
$html .= '<span style="background-image: url(/digg.png);background-repeat: no-repeat;widht:100px">';
$html .= '<form method="post" action="' . $uri->toString( ) . '">';
$img = ''; // look for images in template if available
//$starImageOn = JHTML::_('image.site', 'rating_star.png', '/images/M_images/' );
//$starImageOff = JHTML::_('image.site', 'rating_star_blank.png', '/images/M_images/' ); //for ($i=0; $i < $row->rating; $i++) {
// $img .= $starImageOn;
//}
//for ($i=$row->rating; $i < 5; $i++) {
// $img .= $starImageOff;
//}
//$html .= '<span class="content_rating">';
//$html .= JText::_( 'User Rating' ) .':'. $img .' / ';
//$html .= intval( $row->rating_count );
//$html .= "</span>\n<br />\n"; if (!$params->get( 'intro_only' ))
{
$html .= '<span class="content_vote">';
//$html .= JText::_( 'Poor' );
$html .= '<input type="submit" style="background-color:transparent;border:0pt none;font-size:14pt;height:62px;padding-bottom:20px;width:55px;" value="'.$rating_count.'" alt="Digg it!" name="submit_vote"/>';
$html .= '<input type="radio" style="display:none" alt="vote 1 star" name="user_rating" value="1" checked="checked"/>';
//$html .= '<input type="radio" alt="vote 2 star" name="user_rating" value="2" />';
//$html .= '<input type="radio" alt="vote 3 star" name="user_rating" value="3" />';
//$html .= '<input type="radio" alt="vote 4 star" name="user_rating" value="4" />';
//$html .= '<input type="radio" alt="vote 5 star" name="user_rating" value="5" checked="checked" />';
//$html .= JText::_( 'Best' );
//$html .= ' <input class="button" type="submit" name="submit_vote" value="'. JText::_( 'Rate' ) .'" />';
$html .= '<input type="hidden" name="task" value="vote" />';
$html .= '<input type="hidden" name="option" value="com_content" />';
$html .= '<input type="hidden" name="cid" value="'. $id .'" />';
$html .= '<input type="hidden" name="url" value="'. $uri->toString( ) .'" />';
$html .= '</span>';
}
$html .= '</form>';
$html .= '</span>';
}
return $html;
}
Joomla! 1.5.2 的源代码中有问题,根本就没有查到rating_count ,所以在插件中读了一遍。
这样就可以实现了。不过因为是移动了afterDisplayTitle 的位置,不是很理想。正式的做法应该是trigger一个自己的时间,比如 beforeDisplayTitle,可惜Joomla! 竟然没有这个事件。
别忘了下载本站 http://www.maycode.com/digg.png 放到你站点的根目录下。另外因为不同模板显示的style有所不同,可能要根据具体情况调一下style,让计数与标题显示在同一行。



