[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 $
**/
/*-----------引入檔案區--------------*/
include "header.php";
$xoopsOption['template_main'] = set_bootstrap('phone_book_index_b3.html');
include_once XOOPS_ROOT_PATH . "/header.php";
/*-----------功能函數區--------------*/
//以流水號秀出某筆phone_book資料內容
function show_one_phone_book($sn = '')
{
global $xoopsDB, $xoopsTpl, $isAdmin;
if (empty($sn)) {
return;
} else {
$sn = intval($sn);
}
$myts = MyTextSanitizer::getInstance();
$sql = "select * from `" . $xoopsDB->prefix("phone_book") . "`
where `sn` = '{$sn}' ";
$result = $xoopsDB->query($sql)
or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
$all = $xoopsDB->fetchArray($result);
//以下會產生這些變數: $sn, $cate_sn, $name, $birthday, $phone, $email, $zip, $county, $city, $addr, $note
foreach ($all as $k => $v) {
$$k = $v;
}
//取得分類資料(phone_book_cate)
$phone_book_cate_arr = get_phone_book_cate($cate_sn);
//過濾讀出的變數值
$name = $myts->htmlSpecialChars($name);
$birthday = $myts->htmlSpecialChars($birthday);
$phone = $myts->htmlSpecialChars($phone);
$email = $myts->htmlSpecialChars($email);
$zip = $myts->htmlSpecialChars($zip);
$county = $myts->htmlSpecialChars($county);
$city = $myts->htmlSpecialChars($city);
$addr = $myts->htmlSpecialChars($addr);
$note = $myts->displayTarea($note, 0, 1, 0, 1, 1);
$xoopsTpl->assign('sn', $sn);
$xoopsTpl->assign('cate_sn', $cate_sn);
$xoopsTpl->assign('cate_sn_title', $phone_book_cate_arr['cate_title']);
$xoopsTpl->assign('name', $name);
$xoopsTpl->assign('birthday', $birthday);
$xoopsTpl->assign('phone', $phone);
$xoopsTpl->assign('email', $email);
$xoopsTpl->assign('zip', $zip);
$xoopsTpl->assign('county', $county);
$xoopsTpl->assign('city', $city);
$xoopsTpl->assign('addr', $addr);
$xoopsTpl->assign('note', nl2br($note));
$xoopsTpl->assign('action', $_SERVER['PHP_SELF']);
$xoopsTpl->assign('now_op', 'show_one_phone_book');
}
//以流水號取得某筆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資料
function list_phone_book($def_cate_sn = "", $def_city="")
{
global $xoopsDB, $xoopsTpl, $isAdmin;
//取得分類資料
$phone_book_cate_arr = get_phone_book_cate_arr();
$myts = MyTextSanitizer::getInstance();
$and_cate_sn = empty($def_cate_sn) ? "" : "and `cate_sn` = '{$def_cate_sn}'";
$and_city = empty($def_city) ? "" : "and `city` = '{$def_city}'";
$sql = "select * from `" . $xoopsDB->prefix("phone_book") . "` where 1 $and_cate_sn $and_city";
//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)) {
//以下會產生這些變數: $sn, $cate_sn, $name, $birthday, $phone, $email, $zip, $county, $city, $addr, $note
foreach ($all as $k => $v) {
$$k = $v;
}
//過濾讀出的變數值
$name = $myts->htmlSpecialChars($name);
$birthday = $myts->htmlSpecialChars($birthday);
$phone = $myts->htmlSpecialChars($phone);
$email = $myts->htmlSpecialChars($email);
$zip = $myts->htmlSpecialChars($zip);
$county = $myts->htmlSpecialChars($county);
$city = $myts->htmlSpecialChars($city);
$addr = $myts->htmlSpecialChars($addr);
$note = $myts->displayTarea($note, 0, 1, 0, 1, 1);
$all_content[$i]['sn'] = $sn;
$all_content[$i]['cate_sn'] = $cate_sn;
$all_content[$i]['cate_title'] = $phone_book_cate_arr[$cate_sn]['cate_title'];
$all_content[$i]['name'] = $name;
$all_content[$i]['birthday'] = $birthday;
$all_content[$i]['phone'] = $phone;
$all_content[$i]['email'] = $email;
$all_content[$i]['zip'] = $zip;
$all_content[$i]['county'] = $county;
$all_content[$i]['city'] = $city;
$all_content[$i]['addr'] = $addr;
$all_content[$i]['note'] = $note;
$i++;
}
$xoopsTpl->assign('bar', $bar);
$xoopsTpl->assign('action', $_SERVER['PHP_SELF']);
$xoopsTpl->assign('isAdmin', $isAdmin);
$xoopsTpl->assign('all_content', $all_content);
$xoopsTpl->assign('now_op', 'list_phone_book');
$xoopsTpl->assign('cate_sn', $cate_sn);
$xoopsTpl->assign('cate_arr', get_phone_book_cate_arr());
//擷取縣市陣列
$county_arr=get_county_arr();
//套用到樣板
$xoopsTpl->assign('county_arr', $county_arr);
}
//擷取縣市陣列
function get_county_arr(){
global $xoopsDB;
$sql = "select county from `" . $xoopsDB->prefix("phone_book") . "` group by county order by zip";
$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
while (list($county) = $xoopsDB->fetchRow($result)) {
$county_arr[] = $county;
}
return $county_arr;
}
//取得 phone_book_cate 的所有資料陣列
function get_phone_book_cate_arr()
{
global $xoopsDB;
$sql = "select * from `" . $xoopsDB->prefix("phone_book_cate") . "`
where `cate_enable` = '1'";
$result = $xoopsDB->query($sql)
or redirect_header($_SERVER['PHP_SELF'], 3, mysql_error());
while ($data = $xoopsDB->fetchArray($result)) {
$cate_sn = $data['cate_sn'];
$all[$cate_sn] = $data;
}
return $all;
}
/*-----------執行動作判斷區----------*/
include_once $GLOBALS['xoops']->path('/modules/system/include/functions.php');
$op = system_CleanVars($_REQUEST, 'op', '', 'string');
$city = system_CleanVars($_REQUEST, 'city', '', 'string');
$sn = system_CleanVars($_REQUEST, 'sn', '', 'int');
$cate_sn = system_CleanVars($_REQUEST, 'cate_sn', '', 'int');
switch ($op) {
/*---判斷動作請貼在下方---*/
default:
if (empty($sn)) {
list_phone_book($cate_sn,$city);
} else {
show_one_phone_book($sn);
}
break;
/*---判斷動作請貼在上方---*/
}
/*-----------秀出結果區--------------*/
$xoopsTpl->assign("toolbar", toolbar_bootstrap($interface_menu));
$xoopsTpl->assign("isAdmin", $isAdmin);
include_once XOOPS_ROOT_PATH . '/footer.php';