例子一:
这是一个文章投票的插件,以下是代码清单已经注释。
<?php
//First start with some information about the Content Plugin, its author and probably some other information
/**
* @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.
*/
//You can not directly access the file when you put this code at the beginning:
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
//下面的程序在系统中注册了插件,在显示正文之前,将触发plgContentVote函数调用
$mainframe->registerEvent( 'onBeforeDisplayContent', 'plgContentVote' );
function plgContentVote( &$row, &$params, $page=0 )
{
//获取URI
$uri = & JFactory::getURI();
$id = $row->id;
//初始化变量
$html = '';
//获取参数和rating_count
if (isset($row->rating_count) && $params->get( 'show_vote' ) && !$params->get( 'popup' ))
{
//加载语言文件
JPlugin::loadLanguage( 'plg_content_vote' );
//添加html输出
$html .= '<form method="post" action="' . $uri->toString( ) . '">';
$img = '';
//查找图片
$starImageOn = JHTML::_('image.site', 'rating_star.png', '/images/M_images/' );
$starImageOff = JHTML::_('image.site', 'rating_star_blank.png', '/images/M_images/' );
//如果评价大约0,显示几个星星
for ($i=0; $i < $row->rating; $i++) {
$img .= $starImageOn;
}
//如果rate大于5,不显示星星
for ($i=$row->rating; $i < 5; $i++) {
$img .= $starImageOff;
}
//HTML输出
$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="radio" alt="vote 1 star" name="user_rating" value="1" />';
$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 output; close the <form> tag
$html .= '</form>';
}
//返回html
return $html;
}