XOOPS模組開發記錄
一、共同引入
- header.php (不用修改)
<?php
//載入XOOPS主設定檔(必要)
include_once "../../mainfile.php";
//載入自訂的共同函數檔
include_once "function.php";
//載入工具選單設定檔(亦可將 interface_menu.php 的內容複製到此檔下方,並刪除 interface_menu.php)
include_once "interface_menu.php";
- function.php (不用修改)
<?php
//引入TadTools的函式庫
if (!file_exists(XOOPS_ROOT_PATH . "/modules/tadtools/tad_function.php")) {
redirect_header("http://www.tad0616.net/modules/tad_uploader/index.php?of_cat_sn=50", 3, _TAD_NEED_TADTOOLS);
}
include_once XOOPS_ROOT_PATH . "/modules/tadtools/tad_function.php";
#引入ugmTools.php
include_once XOOPS_ROOT_PATH . "/modules/ugm_tools2/ugmTools.php";
//其他自訂的共同的函數
- interface_menu.php
01-16行是取得權限的方法
24行,是使用權限的方法,本身是二維陣列 <?php #引入權限檔(引入權限函數與x、y軸,權限則由使用程式處理) include XOOPS_ROOT_PATH . "/modules/" . "tncu_cart/groupperm.php" ; //---- 權限設定 ---- */ // 得到指定模組 mid $module_handler =& xoops_gethandler('module'); $xoopsModule =& $module_handler->getByDirname("tncu_cart"); $module_id = $xoopsModule->getVar('mid'); $isAdmin=false; if ($xoopsUser){ //判斷是否對該模組有管理權限 $isAdmin=$xoopsUser->isAdmin($module_id); } $gperm = get_gperm($module_id,$isAdmin,$gperm_itemid_arr,$gperm_name_arr); //---- 權限設定 ---- */ #工具列設定 $mod_name = $xoopsModule->name();//模組中文名 $module_name = $xoopsModule->dirname();//模組 $moduleMenu = array(); if($gperm['tncu_cartAdmin'][4]){ $i = 0; $moduleMenu[$i]['url']="index.php"; $moduleMenu[$i]['title']="回首頁"; $moduleMenu[$i]['icon']="fa-home"; $i++; $moduleMenu[$i]['url']="menu.php"; $moduleMenu[$i]['title']="選單管理"; $moduleMenu[$i]['icon']="fa-bars"; } if($isAdmin) { $i++; $moduleMenu[$i]['url']="admin/index.php"; $moduleMenu[$i]['title']=sprintf(_TAD_ADMIN,$mod_name);//管理後台 $moduleMenu[$i]['icon']="fa-wrench"; $i++; $moduleMenu[$i]['url']=XOOPS_URL."/modules/system/admin.php?fct=preferences&op=showmod&mod={$module_id}"; $moduleMenu[$i]['title']=sprintf(_TAD_CONFIG,$mod_name);//」偏好設定 $moduleMenu[$i]['icon']="fa-edit"; $i++; $moduleMenu[$i]['url']=XOOPS_URL."/modules/system/admin.php?fct=modulesadmin&op=update&module={$module_name}"; $moduleMenu[$i]['title']=sprintf(_TAD_UPDATE,$mod_name);//更新 $moduleMenu[$i]['icon']="fa-refresh"; $i++; $moduleMenu[$i]['url']=XOOPS_URL."/modules/system/admin.php?fct=blocksadmin&op=list&filter=1&selgen={$module_id}&selmod=-2&selgrp=-1&selvis=-1"; $moduleMenu[$i]['title']=sprintf(_TAD_BLOCKS,$mod_name);//」區塊管理 $moduleMenu[$i]['icon']="fa-th"; }
二、自動更新,增加資料表
- include/onInstall.php
go_update() //資料表 #---- 增加資料表 tncu_cart_kind $tbl = "tncu_cart_kind"; if(!chk_isTable($tbl)){ $sql=" CREATE TABLE `" . $xoopsDB->prefix($tbl) . "` ( `sn` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT 'sn', `ofsn` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '父類別', `kind` varchar(255) NOT NULL DEFAULT 'prod' COMMENT '分類', `title` varchar(255) NOT NULL DEFAULT '' COMMENT '標題', `sort` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '排序', `enable` enum('1','0') NOT NULL DEFAULT '1' COMMENT '狀態', `url` varchar(255) NOT NULL DEFAULT '' COMMENT '網址', `target` enum('1','0') NOT NULL DEFAULT '0' COMMENT '外連', `col_sn` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'col_sn', `content` text COMMENT '內容', `ps` varchar(255) DEFAULT NULL COMMENT '備註', PRIMARY KEY (`sn`) ) ENGINE=InnoDB; "; $xoopsDB->queryF($sql); } - sql/mysql.sql (安裝模組時使用,如使用InnoDB,則要注意關聯)
-- 2
CREATE TABLE `tncu_cart_kind` (
`sn` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT 'sn',
`ofsn` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '父類別',
`kind` varchar(255) NOT NULL DEFAULT 'prod' COMMENT '分類',
`title` varchar(255) NOT NULL DEFAULT '' COMMENT '標題',
`sort` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
`enable` enum('1','0') NOT NULL DEFAULT '1' COMMENT '狀態',
`url` varchar(255) NOT NULL DEFAULT '' COMMENT '網址',
`target` enum('1','0') NOT NULL DEFAULT '0' COMMENT '外連',
`col_sn` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'col_sn',
`content` text COMMENT '內容',
`ps` varchar(255) DEFAULT NULL COMMENT '備註',
PRIMARY KEY (`sn`)
) ENGINE=InnoDB;
- xoops_version.php
$modversion['tables'][2] = 'tncu_cart_kind';
三、選單管理
- xoops_version.php
$i++;
$modversion['templates'][$i]['file'] = 'tncu_cart_menu.tpl';
$modversion['templates'][$i]['description'] = 'tncu_cart_menu.tpl';
- menu.php
<?php
/*-----------引入檔案區--------------*/
require_once "header.php";
$xoopsOption['template_main'] = 'tncu_cart_menu.tpl';
include_once XOOPS_ROOT_PATH . "/header.php";
#權限設定
if(!$gperm['tncu_cartAdmin'][4])redirect_header(XOOPS_URL,3,_NOPERM);//瀏覽
#引入類別物件---------------------------------
require_once XOOPS_ROOT_PATH . "/modules/ugm_tools2/ugmKind3.php";
#引入上傳物件
require_once XOOPS_ROOT_PATH . "/modules/ugm_tools2/ugmUpFiles3.php";
/*-----------執行動作判斷區----------*/
#system_CleanVars (&$global, $key, $default= '', $type= 'int')
require_once $GLOBALS['xoops']->path('/modules/system/include/functions.php');
$op = system_CleanVars($_REQUEST, 'op', '', 'string');
$sn = system_CleanVars($_REQUEST, 'sn', '', 'int');
$kind = system_CleanVars($_REQUEST, 'kind', 'menuMain', 'string');
#foreign key
$foreign = array(
"menuMain" => array("title" => "主選單", "stopLevel" => 3),
);
#防呆
$kind = in_array($kind, array_keys($foreign))?$kind:"menuMain";
#關閉左右區塊
$xoopsTpl->assign( 'xoops_showlblock', 0 );
$xoopsTpl->assign( 'xoops_showrblock', 0 );
#引入列表與表單陣列-----------------------
require_once XOOPS_ROOT_PATH . "/modules/ugm_tools2/ugm_kind3_menuArray.php";
#引入類別物件---------------------------------
require_once XOOPS_ROOT_PATH . "/modules/ugm_tools2/ugm_kind3_main.php";
###########################################################
# 異動後,要執行的動作
###########################################################
function transaction(){
global $xoopsDB, $ugmKind,$op;
#---- 過濾讀出的變數值 ----
$myts = MyTextSanitizer::getInstance();
$kind = $ugmKind->get_kind();
$moduleName =$ugmKind->get_moduleName();
$stopLevel =$ugmKind->get_stopLevel();
$ofsn=0;
$level=1;
$enable=1;
if($kind == "menuMain"){
}
}
###########################################################
# 刪除資料 額外檢查
###########################################################
function opDeleteCheck($sn = "") {
global $xoopsDB, $ugmKind;
}
- tncu_cart_menu.tpl
<{includeq file="$xoops_rootpath/modules/ugm_tools2/templates/ugm_tools_toolbar.tpl"}>
<{if $op=="opList"}>
<link href="<{xoAppUrl}>modules/tadtools/treeTable/stylesheets/jquery.treetable.css" rel="stylesheet">
<link href="<{xoAppUrl}>modules/tadtools/treeTable/stylesheets/jquery.treetable.theme.default.css" rel="stylesheet">
<script type="text/javascript" src="<{xoAppUrl}>modules/tadtools/treeTable/javascripts/src/jquery.treetable.js"></script>
<!-- sweet-alert -->
<link rel="stylesheet" href="<{xoAppUrl}>modules/tadtools/sweet-alert/sweet-alert.css" type="text/css">
<script src="<{xoAppUrl}>modules/tadtools/sweet-alert/sweet-alert.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
//可以展開,預設展開
$('#form_table').treetable({ expandable: true ,initialState: 'expanded' });
// 配置拖動節點
$('#form_table .folder').draggable({
helper: 'clone',
opacity: .75,
refreshPositions: true, // Performance?
revert: 'invalid',
revertDuration: 300,
scroll: true
});
// Configure droppable rows
$('#form_table .folder').each(function() {
$(this).parents('#form_table tr').droppable({
accept: '.folder',
drop: function(e, ui) {
var droppedEl = ui.draggable.parents('tr');
console.log(droppedEl[0]);
$('#form_table').treetable('move', droppedEl.data('ttId'), $(this).data('ttId'));
//alert( droppedEl.data('ttId'));
//目地的sn :$(this).data('ttId')
//自己的sn:droppedEl.data('ttId')
$.ajax({
type: 'POST',
url: '?op=opSaveDrag',
data: { ofsn: $(this).data('ttId'), sn: droppedEl.data('ttId'),kind :"<{$kind}>" },
success: function(theResponse) {
swal({
title: theResponse,
text: '<{$smarty.const._MD_TREETABLE_MOVE_RETURN}>',
type: 'success',
showCancelButton: 0,
confirmButtonColor: '#3085d6',
confirmButtonText: '確定',
closeOnConfirm: false ,
allowOutsideClick: true
},
function(){
location.href="<{$smarty.session.returnUrl}>";
});
}
});
},
hoverClass: 'accept',
over: function(e, ui) {
var droppedEl = ui.draggable.parents('tr');
if(this != droppedEl[0] && !$(this).is('.expanded')) {
$('#form_table').treetable('expandNode', $(this).data('ttId'));
}
}
});
});
//排序
$('#sort').sortable({ opacity: 0.6, cursor: 'move', update: function() {
var order = $(this).sortable('serialize') + '&op=opUpdateSort';
$.post('<{$action}>', order, function(theResponse){
swal({
title: theResponse,
text: '<{$smarty.const._MD_TREETABLE_MOVE_RETURN}>',
type: 'success',
showCancelButton: 0,
confirmButtonColor: '#3085d6',
confirmButtonText: '確定',
closeOnConfirm: false ,
allowOutsideClick: true
},
function(){
location.href="<{$smarty.session.returnUrl}>";
});
});
}
});
//每行的删除操作注册脚本事件
$(".btnDel").bind("click", function(){
var vbtnDel=$(this);//得到点击的按钮对象
var vTr=vbtnDel.parents("tr");//得到父tr对象;
var sn=vTr.attr("sn");//取得 sn
var kind=vTr.attr("kind");//取得 sn
var title=$("#title_"+sn).val();//取得 title
//警告視窗
swal({
title: '確定要刪除此資料?',
text: title,
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#c9302c',
cancelButtonColor: '#ec971f',
confirmButtonText: '確定刪除!',
cancelButtonText: '取消!'
},
function(){
location.href="?op=opDelete&sn=" + sn+"&kind="+kind;
});
});
});
</script>
<div class="panel panel-primary">
<div class="panel-heading"><h3 class="panel-title"><{$listTitle}></h3></div>
<div class="panel-body">
<form action='<{$action}>' method='post' id='myForm'>
<{$foreignForm}>
<{$listHtml}>
<{$token}>
</form>
</div>
</div>
<{/if}>
<{if $op=="opForm"}>
<!-- bootstrap 驗證 -->
<link rel="stylesheet" href="<{xoAppUrl}>modules/ugm_tools2/class/bootstrapValidator/css/bootstrapValidator.css"/>
<script type="text/javascript" src="<{xoAppUrl}>modules/ugm_tools2/class/bootstrapValidator/js/bootstrapValidator.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#myForm').bootstrapValidator({
live: 'disabled',//
message: '此值無效',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
title: {
validators: {
notEmpty: {
message: '必填'
}
}
}
}
});
});
</script>
<div class="panel panel-primary">
<div class="panel-heading"><h3 class="panel-title"><{$row.formTitle}></h3></div>
<div class="panel-body">
<form role="form" action="<{$action}>" method="post" id="myForm" enctype="multipart/form-data">
<{foreach from=$forms item=form key=r}>
<div class="row">
<{foreach from=$form item=cell key=col}>
<{if $cell.type != "hidden"}>
<div class="col-sm-<{$cell.width}>">
<div class="form-group">
<label for="<{$col}>"><{$cell.label}></label>
<{if $cell.type == "text"}>
<input type='text' name='<{$col}>' value='<{$row.$col}>' id='<{$col}>' class="form-control">
<{elseif $cell.type == "radio"}>
<div>
<input type='radio' name='<{$col}>' id='<{$col}>_1' value='1' <{if $row.$col==1}>checked<{/if}>>
<label for='<{$col}>_1'>啟用</label>
<input type='radio' name='<{$col}>' id='<{$col}>_0' value='0' <{if $row.$col==0}>checked<{/if}>>
<label for='<{$col}>_0'>停用</label>
</div>
<{elseif $cell.type == "select"}>
<select name="<{$col}>" id="<{$col}>" class="form-control" size="1" >
<{$row.$col}>
</select>
<{elseif $cell.type == "icon"}>
<link rel="stylesheet" type="text/css" href="<{xoAppUrl}>modules/ugm_tools2/class/fontawesome-iconpicker/css/fontawesome-iconpicker.min.css">
<script type="text/javascript" src="<{xoAppUrl}>modules/ugm_tools2/class/fontawesome-iconpicker/js/fontawesome-iconpicker.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#<{$col}>').iconpicker();
});
</script>
<div class="input-group iconpicker-container">
<input type="text" data-placement="bottomRight" class="form-control icp icp-auto iconpicker-element iconpicker-input" id="<{$col}>" name="<{$col}>" value="<{$row.$col}>">
<span class="input-group-addon"><i class="fa <{$row.$col}>"></i></span>
</div>
<{elseif $cell.type == "single_img"}>
<{$row.$col}>
<{/if}>
</div>
</div>
<{else}>
<input type='hidden' name='<{$col}>' value='<{$row.$col}>'>
<{/if}>
<{/foreach}>
</div>
<{/foreach}>
<hr>
<div class="form-group text-center">
<button type="submit" class="btn btn-primary"><{$smarty.const._SUBMIT}></button>
<{if !$row.sn}>
<button type="reset" class="btn btn-danger"><{$smarty.const._RESET}></button>
<{/if}>
<button type="button" class="btn btn-warning" onclick="location.href='<{$smarty.session.return_url}>'">返回</button>
<input type='hidden' name='op' value='<{$row.op}>'>
<input type='hidden' name='sn' value='<{$row.sn}>'>
<input type='hidden' name='kind' value='<{$row.kind}>'>
<{$token}>
</div>
</form>
</div>
</div>
<{/if}>