[1042] XOOPS模組開發進階
<?php
/**
* Phone Book module
*
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
* @license http://www.fsf.org/copyleft/gpl.html GNU public license
* @package Phone Book
* @since 2.5
* @author tad
* @version $Id $
**/
/*-----------引入檔案區--------------*/
$xoopsOption['template_main'] = 'phone_book_adm_cate_b3.html';
include_once "header.php";
include_once "../function.php";
/*-----------功能函數區--------------*/
//phone_book_cate編輯表單
function phone_book_cate_form($cate_sn = '')
{
global $xoopsDB, $xoopsTpl;
//抓取預設值
if (!empty($cate_sn)) {
$DBV = get_phone_book_cate($cate_sn);
} else {
$DBV = array();
}
//預設值設定
//設定 cate_sn 欄位的預設值
$cate_sn = !isset($DBV['cate_sn']) ? $cate_sn : $DBV['cate_sn'];
$xoopsTpl->assign('cate_sn', $cate_sn);
//設定 cate_title 欄位的預設值
$cate_title = !isset($DBV['cate_title']) ? '' : $DBV['cate_title'];
$xoopsTpl->assign('cate_title', $cate_title);
//設定 cate_sort 欄位的預設值
$cate_sort = !isset($DBV['cate_sort']) ? phone_book_cate_max_sort() : $DBV['cate_sort'];
$xoopsTpl->assign('cate_sort', $cate_sort);
//設定 cate_enable 欄位的預設值
$cate_enable = !isset($DBV['cate_enable']) ? '1' : $DBV['cate_enable'];
$xoopsTpl->assign('cate_enable', $cate_enable);
$op = empty($cate_sn) ? "insert_phone_book_cate" : "update_phone_book_cate";
//$op = "replace_phone_book_cate";
$xoopsTpl->assign('action', $_SERVER["PHP_SELF"]);
$xoopsTpl->assign('now_op', 'phone_book_cate_form');
$xoopsTpl->assign('next_op', $op);
}
//自動取得phone_book_cate的最新排序
function phone_book_cate_max_sort()
{
global $xoopsDB;
$sql = "select max(`cate_sort`) from `" . $xoopsDB->prefix("phone_book_cate") . "`";
$result = $xoopsDB->query($sql)
or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
list($sort) = $xoopsDB->fetchRow($result);
return ++$sort;
}
//以流水號取得某筆phone_book_cate資料
function get_phone_book_cate($cate_sn = '')
{
global $xoopsDB;
if (empty($cate_sn)) {
return;
}
$sql = "select * from `" . $xoopsDB->prefix("phone_book_cate") . "`
where `cate_sn` = '{$cate_sn}'";
$result = $xoopsDB->query($sql)
or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
$data = $xoopsDB->fetchArray($result);
return $data;
}
//新增資料到phone_book_cate中
function insert_phone_book_cate()
{
global $xoopsDB, $xoopsUser;
$myts = MyTextSanitizer::getInstance();
$cate_sn = intval($_POST['cate_sn']);
$cate_title = $myts->addSlashes($_POST['cate_title']);
$cate_sort = intval($_POST['cate_sort']);
$cate_enable = intval($_POST['cate_enable']);
$sql = "insert into `" . $xoopsDB->prefix("phone_book_cate") . "` (
`cate_title`,
`cate_sort`,
`cate_enable`
) values(
'{$cate_title}',
'{$cate_sort}',
'{$cate_enable}'
)";
$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
//取得最後新增資料的流水編號
$cate_sn = $xoopsDB->getInsertId();
return $cate_sn;
}
//更新phone_book_cate某一筆資料
function update_phone_book_cate($cate_sn = '')
{
global $xoopsDB, $xoopsUser;
$myts = MyTextSanitizer::getInstance();
$cate_sn = intval($_POST['cate_sn']);
$cate_title = $myts->addSlashes($_POST['cate_title']);
$cate_sort = intval($_POST['cate_sort']);
$cate_enable = intval($_POST['cate_enable']);
$sql = "update `" . $xoopsDB->prefix("phone_book_cate") . "` set
`cate_title` = '{$cate_title}',
`cate_sort` = '{$cate_sort}',
`cate_enable` = '{$cate_enable}'
where `cate_sn` = '$cate_sn'";
$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
return $cate_sn;
}
//刪除phone_book_cate某筆資料資料
function delete_phone_book_cate($cate_sn = '')
{
global $xoopsDB;
if (empty($cate_sn)) {
return;
}
$sql = "delete from `" . $xoopsDB->prefix("phone_book_cate") . "`
where `cate_sn` = '{$cate_sn}'";
$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
}
//列出所有phone_book_cate資料
function list_phone_book_cate()
{
global $xoopsDB, $xoopsTpl;
$myts = MyTextSanitizer::getInstance();
$sql = "select * from `" . $xoopsDB->prefix("phone_book_cate") . "` order by `cate_sort`";
//getPageBar($原sql語法, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
$PageBar = getPageBar($sql, 20, 10, null, null, $bootstrap);
$bar = $PageBar['bar'];
$sql = $PageBar['sql'];
$total = $PageBar['total'];
$result = $xoopsDB->query($sql)
or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
$all_content = '';
$i = 0;
while ($all = $xoopsDB->fetchArray($result)) {
//以下會產生這些變數: $cate_sn, $cate_title, $cate_sort, $cate_enable
foreach ($all as $k => $v) {
$$k = $v;
}
//將是/否選項轉換為圖示
$cate_enable = $cate_enable == 1 ? '<img src="' . XOOPS_URL . '/modules/phone_book/images/yes.gif" alt="' . _YES . '" title="' . _YES . '">' : '<img src="' . XOOPS_URL . '/modules/phone_book/images/no.gif" alt="' . _NO . '" title="' . _NO . '">';
//過濾讀出的變數值
$cate_title = $myts->htmlSpecialChars($cate_title);
$all_content[$i]['cate_sn'] = $cate_sn;
$all_content[$i]['cate_title'] = $cate_title;
$all_content[$i]['cate_sort'] = $cate_sort;
$all_content[$i]['cate_enable'] = $cate_enable;
$i++;
}
$xoopsTpl->assign('bar', $bar);
$xoopsTpl->assign('action', $_SERVER['PHP_SELF']);
$xoopsTpl->assign('all_content', $all_content);
$xoopsTpl->assign('now_op', 'list_phone_book_cate');
get_jquery(true);
if (!file_exists(XOOPS_ROOT_PATH . "/modules/tadtools/sweet_alert.php")) {
redirect_header("index.php", 3, _MA_NEED_TADTOOLS);
}
include_once XOOPS_ROOT_PATH . "/modules/tadtools/sweet_alert.php";
$sweet_alert_obj = new sweet_alert();
$delete_phone_book_func = $sweet_alert_obj->render('delete_cate_sn_func', "{$_SERVER['PHP_SELF']}?op=delete_phone_book_cate&cate_sn=", "cate_sn");
}
/*-----------執行動作判斷區----------*/
include_once $GLOBALS['xoops']->path('/modules/system/include/functions.php');
$op = system_CleanVars($_REQUEST, 'op', '', 'string');
$sn = system_CleanVars($_REQUEST, 'sn', '', 'int');
$cate_sn = system_CleanVars($_REQUEST, 'cate_sn', '', 'int');
switch ($op) {
/*---判斷動作請貼在下方---*/
//替換資料
//case "replace_phone_book_cate":
// replace_phone_book_cate();
// header("location: {$_SERVER['PHP_SELF']}?cate_sn=$cate_sn");
// exit;
//break;
//新增資料
case "insert_phone_book_cate":
$cate_sn = insert_phone_book_cate();
header("location: {$_SERVER['PHP_SELF']}");
exit;
break;
//更新資料
case "update_phone_book_cate":
update_phone_book_cate($cate_sn);
header("location: {$_SERVER['PHP_SELF']}");
exit;
break;
case "phone_book_cate_form":
phone_book_cate_form($cate_sn);
break;
case "delete_phone_book_cate":
delete_phone_book_cate($cate_sn);
header("location: {$_SERVER['PHP_SELF']}");
exit;
break;
default:
list_phone_book_cate();
break;
/*---判斷動作請貼在上方---*/
}
/*-----------秀出結果區--------------*/
$xoopsTpl->assign("isAdmin", true);
$xoTheme->addStylesheet(XOOPS_URL . '/modules/tadtools/css/xoops_adm.css');
include_once 'footer.php';