本教程不适用于整合Discuz用户,止在调用Discuz最新主题,由此实战来学习Joomla1.5模块开发原理。

autoit之前开发过两套针对Discuz的模块,但是基于Joomla1.0下的,而且作者也未再更新过,Discuz的优秀不言而喻,网上对此需求颇高!便在此把学习研究过程记下来。

做之前需要知道Joomla1.5的官方的模块开发参考文档(英文):http://docs.joomla.org/How_to_create_a_module

先就依葫芦画瓢(按照上述英文文档所译),掌握MVC的开发原理,做一个简单的查询主题并显示,尚未添加帖子链接,在后面的学习过程中逐渐完善。

1、创建文件 /modules/mod_dz_latest_post/mod_dz_latest_post.xml:

<?xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
<!-- 模块名称 -->
<name>DZ-Latest-Post</name>

<!-- 作者名称 -->
<author>vanshion</author>

<!-- 创建日期 -->
<creationDate>2011-01-01</creationDate>

<!-- 版权信息 -->
<copyright>All rights reserved by Joomla178 2010.</copyright>

<!-- 遵循规范 -->
<license>GPL 2.0</license>

<!-- 作者邮箱,将#替换为@ -->
<authorEmail>shion.van#gmail.com</authorEmail>

<!-- 作者主页 -->
<authorUrl>www.Joomla178.com</authorUrl>

<!-- 模块版本 -->
<version>1.0.0</version>

<!-- 模块用途(简介) -->
<description>Provides a listing of Discuz latest post</description>

<!-- 需要安装的模块相关文件列表 -->
<files>
<!-- 模块控制文件 -->
<filename module="mod_dz_latest_post">mod_dz_latest_post.php</filename>
<filename>index.html</filename>
<filename>helper.php</filename>
<filename>tmpl/default.php</filename>
<filename>tmpl/index.html</filename>
</files>

<languages>
<!-- 模块包含的语言文件 -->
<language tag="en-GB">en-GB.mod_dz_latest_post.ini</language>
<language tag="zh-CN">zh-CN.mod_dz_latest_post.ini</language>
</languages>

<!-- 后台控制参数 -->
<params>
<!-- 允许为table/xhtml自定义样式显示 -->
<param name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" description="PARAMMODULECLASSSUFFIX" />

<!-- 给定一个后台显示分割符,类似readmore的一条线 -->
<param name="@spacer" type="spacer" default="" label="" description="" />

<!-- 设定Discuz的最新主题 -->
<param name="dz_lpcount" type="text" default="5" label="LABEL DISCUZ LATEST POST COUNT" description="DESC DISCUZ LATEST POST COUNT" />
</params>
</install>

上述代码主要设定了一个后台传入参数:Discuz主题显示条数。

2、创建文件 /modules/mod_dz_latest_post/mod_dz_latest_post.php:

<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');

// 包含helper.php文件
require_once(dirname(__FILE__).DS.'helper.php');

// 获取后台配置参数dz_lpcount
$Dz_LpCount = $params->get('dz_lpcount');

// 按照下面函数显示items
$items = ModDzLatestPostHelper::getItems($Dz_LpCount);

// 包含模板显示
require(JModuleHelper::getLayoutPath('mod_dz_latest_post'));
?>

3、创建文件 /modules/mod_dz_latest_post/helper.php:

<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');

class ModDzLatestPostHelper/*注意命名规范*/
{
/**
* 返回一个最新主题列表
*/
public function getItems($dz_lpcount)
{
// 获取数据库
$db = &JFactory::getDBO();

// 获取由主题数量限制的主题列表
// 前提是discuz的数据库安装在Joomla的数据库上,用数据库软件找到discuz的主题是在表cdb_threads中,执行SQL语句,查询subject字段并按dateline降序排列,并仅显示$dz_lpcount条。
$query = 'SELECT `subject` FROM `cdb_threads` ORDER BY `dateline` DESC LIMIT ' . $dz_lpcount  . '';

$db->setQuery($query);
$items = ($items = $db->loadObjectList())?$items:array();

return $items;
} //结束获取主题

} //end ModDzLatestPostHelper
?>

4、创建文件 /modules/mod_dz_latest_post/tmpl/default.php:

<?php defined('_JEXEC') or die('Restricted access'); // no direct access ?>
<?php echo JText::_('DISCUZ LATEST POST'); ?>
<ul>
<?php foreach ($items as $item) { ?>
<li>
<?php echo JText::sprintf('DISCUZ LATEST POST LABEL', $item->subject); ?>
</li>
<?php } ?>
</ul>

5、创建文件 /modules/mod_dz_latest_post/index.html:

   创建文件 /modules/mod_dz_latest_post/tmpl/index.html:

<html><body bgcolor="#FFFFFF"></body></html>

6、创建英文标准包 /modules/mod_dz_latest_post/en-GB.mod_dz_latest_post.ini:

LABEL DISCUZ LATEST POST COUNT=Latest posts count from discuz! 
DESC DISCUZ LATEST POST COUNT=Discription of latest posts count from discuz!
DISCUZ LATEST POST=Latest Posts
DISCUZ LATEST POST LABEL=%s is lates posts

6、创建中文标准包 /modules/mod_dz_latest_post/zh-CN.mod_dz_latest_post.ini:

LABEL DISCUZ LATEST POST COUNT=调用Discuz!的最新主题数
DESC DISCUZ LATEST POST COUNT=这是一个调用Discuz!最新主题的数量统计
DISCUZ LATEST POST=最新主题
DISCUZ LATEST POST LABEL=%s 是最新的

7、切记以上所有文件均以utf-8编码保存!最后打包文件为zip格式,便于后续开发,在此打包为

网站调整,暂时无法下载,将发布beta版

除特殊标明文章转自第三方网站,文章均由JOOMLASK.COM原创提供
欢迎友情转载,请务必保留本文出处并引用本文链接: 跟我学Joomla模块开发-Discuz最新主题模块【一】