用户名 密码 记住我 还未注册?

文章管理 - XOOPS模块的开发设计教程

文章管理 :: Xoops 模块

XOOPS模块的开发设计教程


本文档主要介绍XOOPS模块的开发设计及安装配置。Xoops的模块的便于安装在xoops系统下,主要作用由其自身的功能决定,现在www.xoops.org.cn下有很多免费的模块下载。开发xoops的模块就要参照它官方的标准来制作,后面我主要通过eclipse制作一个例子来介绍xoops模块的开发。
一、概述
本文档主要介绍XOOPS模块的开发设计及安装配置。Xoops的模块的便于安装在xoops系统下,主要作用由其自身的功能决定,现在www.xoops.org.cn下有很多免费的模块下载。开发xoops的模块就要参照它官方的标准来制作,后面我主要通过eclipse制作一个例子来介绍xoops模块的开发。

二、开发设计
文件结构

module
  
|-目录 admin (管理文件存放文件夹)
  
|-目录 blocks (区块程序存放目录)
  
|-目录 images (图片存放文件夹)
  
|-目录 language (模块语言文件)
  
|-目录 sql (数据库表结构存放目录)
  
|-目录 tmpelates (smarty模板存放目录)
  
|-文件 index.php (模块标准首页文件)
  
|-文件 xoops_version.php (模块核心配置文件)


设计前,请按以上标准来建立文件夹。区块程序文件夹也要建立一个模板文件夹来放置blocks的模板 /templates/blocks。

2.编写xoops_version.php文件
xoops_version.php是一个模块的核心配置文件,是和主程序连接的纽带。
<?php// 模块名称
$modversion['name'] = '文件管理';
// 版本号
$modversion['version'] = '1.00';
// 模块说明
$modversion['description'] = '一个测试的模块';
// 版权所有者
$modversion['credits'] = 'MyRice.Dep';
// 作者
$modversion['author'] = 'Alex Jia';
// 帮助页面
$modversion['help'] = 'help.html';
// 版权列号
$modversion['license'] = 'GPL see LICENSE';
// 模块 logo 图片
$modversion['image'] = 'images/logo.png';
// 模块目录名称
$modversion['dirname'] = 'test';
// 模版文件 
$modversion['templates'][1]['file'] = 'test_index.html';
// 模版说明
$modversion['templates'][1]['description'] = 'Test Form';
?>

3.语言模块文件夹文件结构设置
以简体中文为例在language下chinese文件夹 language/Chinese,需要的程序文件请见下

module
  
|-目录 admin (管理文件存放文件夹)
  
|-目录 blocks (区块程序存放目录)
  
|-目录 images (图片存放文件夹)
  
|-目录 language (模块语言文件)
     
|-目录 chinese 
        
|- 文件 admin.php (管理程序语言信息部分)
        
|- 文件 blocks.php (区块语言信息部分)
        
|- 文件 main.php (主程序信息部分)
        
|- 文件 modinfo.php (模块组信息部分)
  
|-目录 sql (数据库表结构存放目录)
  
|-目录 tmpelates (smarty模板存放目录)
  
|-文件 index.php (模块标准首页文件)
  
|-文件 xoops_version.php (模块核心配置文件)

然后对需要设置的部分的对应的语言文件进行编写
例如:modinfo.php
<?php
//先建一个 modinfo.php 代码如下, define为定义常数函数,xoops 的习惯,常數第一個字母为 _ ,
//並以大写字母为常数名。
define("_NAME","文件管理");           
define("_DESC","一个简单的文件程序");
?>


4.制作一个logo.png的模块logo图片文件放到images文件夹下。(这里的logo后缀一定要用.png,也就是说logo必须为png格式的文件)

5.编辑index.php文件

系统默认读取web发布文件
<?php
// 插入系统设定文挡
include "../../mainfile.php";
// 插入头文件
include "../../header.php";
echo 
"文件管理模块";
// 插入尾文件
include "../../footer.php";
?>


OK,现在这个测试的安装小模块已经完成。

6.接下来我们写test.index.html文件 此文件放到templates文件夹下。
应用到php的部分应该用smarty格式编写<{$var}>


<form action="<{$smarty.server.PHP_SELF}>" method="post" enctype="multipart/form-data">
<
table width='100%' class='outer' cellspacing='1'>
<
tr><th colspan='2'>文件管理</th></tr>
<
tr>
<
td class='head'>标题</td>
<
td class='even'><input type="text" name="d_name" size="30"></td>
</
tr>
<
tr>
<
td class='head'>类别</td>
<
td class='even'>
<
select name="d_kind">
<{
html_options values=$kind_arr output=$kind_arr }>
</
select>
</
td>
</
tr>
<
tr>
<
td class='head'>说明</td>
<
td class='even'><input type="text" name="d_memo" size="60"></td>
</
tr>
<
tr>
<
td class='head'>文件</td>
<
td class='even'><input type="file" name="infile"></td>
</
tr>
<
tr>
<
td class='head'></td>
<
td class='head'><input type="submit" name="op" value="上传"></td>
</
table>
</
form>

7.更新模块让系统读入新写的模板页

这有个小技巧,在开发模块的时候,也许我们得经常的更新模板页,每次更新一次就得按上面的程序操作一次,有写麻烦,xoops之所以这样设计是为了加快读取摸板的速度,在摸板开发阶段,可以把自动更新模块设置为“是”,这样就可以减少麻烦了。

8.修改index.php,插入test_index.html
<?php
// 插入系统设定文挡
include "../../mainfile.php";
// 插入头文件
include "../../header.php";
// 插入模板
$xoopsOption['template_main'] = "test_index.html";
// 插入尾文件
include "../../footer.php";
?>


9.接下来我们要求模块对数据库进行一下操作建立一个表
写一个mysql.sql的文件如下 ,放到test/sql文件夹下


d_id 流水号
-- uname  用户名
-- d_name 标题
-- d_kind 类别
-- d_memo 说明
-- d_time 插入时间
-- d_file 文件名
CREATE TABLE 
`test` (
  `
d_idint(11NOT NULL auto_increment,
  `
unamevarchar(25NOT NULL default '',
  `
d_namevarchar(40NOT NULL default '',
  `
d_kindvarchar(20NOT NULL default '',
  `
d_memovarchar(100NOT NULL default '',
  `
d_timetimestamp(14),
  `
d_filevarchar(100NOT NULL default '',
  
PRIMARY KEY  (`d_id`)
ENGINE=MyISAM;.


10.修改 xoops_version.php,加入数据库设置相关代码,如下


// 数据表结构
$modversion['sqlfile']['mysql'] = "sql/mysql.sql";    
// 数据表名称
$modversion['tables'][0] = "test";
$modversion['tables'][n] , n从0开始编写,但名称必须和sql/mysql.sql中的语句想符合,用到那个表都要在这里声明,我们这里只是使用CREATE TABLE `test`所以$modversion['tables'][0] = "test";


11. 如果第一次安装时没有建立数据库表结构,必须反安装模块组,再重新安装,让系统重新载入数据库。(这里步骤不在重复)

12.在test/主目录建立upload.php文件
<?php
// 插入系统设定文挡
include "../../mainfile.php";
// 插入头文件
include "../../header.php";
//判断使用者身份
if (! $xoopsUser ) {
     
redirect_header(XOOPS_URL,3,'没有权限使用被模块');
     exit;
}
// 判斷使用者是否按傳送按鈕
if ($_POST['op'] == '上传'){
    
// 取得使用者帳號
    
$uname $xoopsUser->getVar("uname");
    
$d_name $_POST['d_name'];
    
$d_memo $_POST['d_memo'];
    if (
is_uploaded_file($_FILES['infile']['tmp_name'])){
        
// 不允许上传php程序文件
        
$no_arr = array("php","php3","exe","dll");
        
$temp_arr explode(".",$_FILES['infile']['name']);       
        if (
in_array(strtolower(end($temp_arr)),$no_arr)){
               
redirect_header('index.php',2,"不允许上传 php 文件");
               exit;
        }
        
$file_path XOOPS_UPLOAD_PATH."/test";
        
// 如果未建立目录则在uploads下建立test的目录
       
if (!is_dir($file_path)){
          
// 建立目录
          
mkdir($file_path);
       }
       
// 存档名称
       
$d_file time()."_".$_FILES['infile']['name'];       
       
// copy 上传文件到 uploads 下,上传文件名称加上时间避免同名覆盖
       
copy($_FILES['infile']['tmp_name'],$file_path."/".$d_file);
   }
   
// 插入到数据库
   
$sql "INSERT INTO ".$xoopsDB->prefix("test")." (uname,d_name,d_memo,d_file)VALUES('$uname','$d_name','$d_memo','$d_file')";
   if ( !
$result $xoopsDB->query($sql) ) {
       
// 删除文档
       
unlink($file_path."/".$d_file);
       
redirect_header('index.php',2,"添加失败!");      
       exit();      
  }
      
redirect_header('index.php',2,"新增成功!");
}
// 插入模板
$xoopsOption['template_main'] = 'test_index.html';
// 插入尾文件
include "../../footer.php";
?>


13.在 xoops_version.php 加入第二个模板并增加子菜单,如下例,再由系统管理处更新模块,重新载入模板文件到系统。


// 模板文件
$modversion['templates'][2]['file'] = 'test_view.html';
$modversion['templates'][2]['description'] = '列出文件';
$modversion['templates'][3]['file'] = 'test_sel.html';
$modversion['templates'][3]['description'] = '查看文件';
// 加入子選單
$modversion['sub'][1]['name'] = '文件列表';
$modversion['sub'][1]['url'] = "view.php";
$modversion['sub'][2]['name'] = '类别管理';
$modversion['sub'][2]['url'] = "kind.php"


14.编辑view.php 放到test/ 主目录

<?php
include "../../mainfile.php";
include 
"../../header.php";
if (
$_REQUEST['op']=='delete'){
    
$d_id $_REQUEST['d_id'];
    
$uname $xoopsUser->getVar('uname');
    
$sql "SELECT d_file FROM ".$xoopsDB->prefix('test')." WHERE d_id='$d_id' AND uname='$uname'";
    
$res $xoopsDB->query($sql);
    if (
$xoopsDB->getRowsNum($res) >){
            
$rows $xoopsDB->fetchArray($res);
            
$d_file $rows['d_file'];
            
$sql "DELETE FROM ".$xoopsDB->prefix('test')." WHERE d_id=$d_id";
            
$xoopsDB->query($sql);
            
// 删除文件
            
$d_file XOOPS_UPLOAD_PATH."/test/$d_file";
            
unlink($d_file);
    }
}
// 载如模板
$xoopsOption['template_main'] = 'test_view.html';
// 查询 test 文件
$query "SELECT * FROM ".$xoopsDB->prefix("test");
$res $xoopsDB->query($query);
// 将查询结果放回数组
$arr = array();
while(
$rows $xoopsDB->fetchArray($res)){
    
$arr[] = $rows;
}
// 将结果数组传给模板
$xoopsTpl->assign("docdata",$arr);
// 将用户帐号传回模板
$xoopsTpl->assign("uname",$xoopsUser->getVar('uname'));
include 
"../../footer.php";
?>


15.编辑test_view.html 放到templates/文件夹


<table class='outer' cellpadding='4' cellspacing='1'>
<
tr valign='middle'><th>标题</th><th>上传人</th><th>文件</th>
<{if 
$uname}><th>管理</th><{/if}>
</
tr>
<!-- 
显示所有上传的文件 -->
<{foreach 
from=$docdata item=data}>
<
tr class="<{cycle values="even,odd"}>"><td><a href='sel.php?d_id=<{$data.d_id}>'><{$data.d_name}></a></td><td><{$data.uname}></td>
<
td><a href='<{$smarty.const.XOOPS_URL}>/uploads/test/<{$data.d_file}>' target='_blank'><{$data.d_file}></a></td>
<{if 
$uname}><td><{if $uname eq $data.uname}><a href='<{$smarty.server.PHP_SELF}>?op=delete&d_id=<{$data.d_id}>'>删除</a><{/if}></td><{/if}>
</
tr>
<{/foreach}>
</
table>


16.编辑sel.php 放到test/主文件夹

<?php
include "../../mainfile.php";
include 
"../../header.php";
$xoopsOption['template_main'] = 'test_sel.html';
// 查询 test 资料
$query "SELECT * FROM ".$xoopsDB->prefix("test")." WHERE d_id=".$_REQUEST['d_id'];
$res $xoopsDB->query($query);
$rows $xoopsDB->fetchArray($res);
// 将结果传给模板
$xoopsTpl->assign("data",$rows);
include 
"../../footer.php";
?>

17.编辑test_sel.html 放到templates/文件夹

<table width='100%' class='outer' cellspacing='1'>
<
tr><th colspan='2'>文件管理</th></tr>
<
tr>
<
td class='head'>序号</td>
<
td class='even'><{$data.d_id}></td>
</
tr>
<
tr>
<
td class='head'>上传时间</td>
<
td class='even'><{$data.d_time}></td>
</
tr>
<
tr>
<
td class='head'>标题</td>
<
td class='even'><{$data.d_name}></td>
</
tr>
<
tr>
<
td class='head'>说明</td>
<
td class='even'><{$data.d_memo}></td>
</
tr>
<
tr>
<
td class='head'>文件</td>
<
td class='even'><{$data.d_file}></td>
</
tr>
</
table>


18. 在 xoops_version.php 加入区块设置,如下:(其实xoops里的区块就是指首页的样式,只要进入区块设置看一下就知道了)

// 区块模板
$modversion['templates'][4]['file'] = 'test_new.html';
$modversion['templates'][4]['description'] = 'lastest file ';
// 区块设置
$modversion['blocks'][1]['file'] = "news_block.php";
$modversion['blocks'][1]['name'] = "新加入文件";
$modversion['blocks'][1]['description'] = "显示新加入文件";
$modversion['blocks'][1]['show_func'] = "b_news_show";
$modversion['blocks'][1]['template'] = 'news_block.html';

19. 在 test/blocks 目录添加 news_block.php
<?php
// 显示最新的5个新增文件
function b_news_show() {
        global 
$xoopsDB;        
        
$sql "SELECT d_id,d_name,d_time FROM ".$xoopsDB->prefix('test')." ORDER BY d_time LIMIT 5 ";
        
$res $xoopsDB->queryF($sql);
        
$block = array();
            while(
$rows $xoopsDB->fetchArray($res)){
                
$block[] = $rows;
            }             
        return 
$block;
}
?>


20. 在 templates/blocks 目录建立 news_block.html

<ul>
  <{foreach 
item=docs from=$block}>
    <
li><a href="<{$xoops_url}>/modules/docman/sel.php?d_id=<{$docs.d_id}>"><{$docs.d_name}></a> (<{$docs.d_time|date_format:"%Y-%m-%d"}> )</li>
  <{/foreach}>
</
ul>


21.添加区块
现在我们把刚刚完成的区块添加到首页

22.制作管理文件页面
下面需要做一个管理页面把以上写过的三个文件关联起来 ,也就是在管理的下拉菜单中显示出来。需要在admin文件夹下建立两个文件
test/admin/admin.php 管理页面首页
test/admin/menu.php 下拉菜单显示
而且还修改xoops_version.php配置文件

建立admin.php文件 代码如下:
<?
include '../../../include/cp_header.php';
xoops_cp_header();
$xTheme->loadModuleAdminMenu(1_PROFILE_MI_INDEX);
xoops_cp_footer();?>
建立menu.php文件代码如下
<?php
$adminmenu
[1]['title'] ="上传文件";
$adminmenu[1]['link'] = "upload.php";
$adminmenu[2]['title'] = "文件管理";
$adminmenu[2]['link'] = "view.php";
?>

修改xoops_version.php配置文件在最后添加

// 管理接口
$modversion['hasAdmin'] = 1//是否有管理接口
$modversion['adminindex'] = "admin/admin.php"//管理接口的首页位置
$modversion['adminmenu'] = "admin/menu.php"//管理接口的选单程式
// Menu
$modversion['hasMain'] = 1//是否有菜单项

ok现在程序就已经基本完成我们更新一下,看一下结果

总结:xoops模块的安装制作过程到此就完成,本文档主要是介绍模块开发的过程,不针对本例程序的实用性。由于xoops功能涉及面广,其中xoops的很多功能还未提及说明,希望各位同僚可以进行补充。
程序代码:同本文档所在压缩包 testfile.zip
安装图标:pm_logo.psd 在包内images/下 方便以后开发使用
<< 让article模块拥有实用的Web 2.0风格翻页页码 菩提猫笔记:XOOPS源码分析之--表单组件浅析(一) >>
跟踪网址
  • 文章地址: http://xoops.org.cn/modules/article/view.article.php/c7/27
  • 跟踪地址: http://xoops.org.cn/modules/article/trackback.php/27
API: 工具箱 短消息 Email PDF 书签 打印 | RSS | RDF | ATOM
Copyright© gfek2001zz & XOOPS CHINA
网友个人意见,不代表本站立场。对于发言内容,由发表者自负责任。
发表者 树状展开