1041 XOOPS模組開發
<?php
/*-----------引入檔案區--------------*/
$xoopsOption['template_main'] = "tad_ebook_adm_main.html";
include_once "header.php";
include_once "../function.php";
/*-----------function區--------------*/
//管理電子校刊表單
function ebook_form($ebook_sn=""){
global $xoopsTpl , $xoopsDB;
if($ebook_sn){
$tbl=$xoopsDB->prefix('tad_ebook');
$sql="select * from `{$tbl}` where ebook_sn='{$ebook_sn}'";
$result=$xoopsDB->query($sql) or die(mysql_error());
$all=$xoopsDB->fetchArray($result);
$ebook_title=$all['ebook_title'];
$ebook_post_date=strtotime($all['ebook_post_date']);
$ebook_publish_date=strtotime($all['ebook_publish_date']);
$op='update_ebook';
}else{
$ebook_title = "";
$ebook_post_date = time() + 3600 * 24 * 14;
$ebook_publish_date = time() + 3600 * 24 * 28;
$op='insert_ebook';
}
//引入表單物件
include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
$form = new XoopsThemeForm('電子校刊管理表單', 'ebook', 'main.php', 'post', true);
$Text = new XoopsFormText('電子校刊標題', 'ebook_title', 200 , 80 , $ebook_title);
//$Text->setExtra("style='width: 400px; color: white; background-color: #000000;'");
$Text->setExtra("style='width: 400px;'");
$Text->setExtra("placeholder='請輸入標題'");
$form->addElement($Text);
$DateSelect=new XoopsFormTextDateSelect('投稿截止日', 'ebook_post_date', 15, $ebook_post_date);
$DateSelect->setExtra("style='width: 100px;'");
$form->addElement($DateSelect);
$DateSelect1=new XoopsFormTextDateSelect('出刊日', 'ebook_publish_date', 15, $ebook_publish_date);
$DateSelect1->setExtra("style='width: 100px;'");
$form->addElement($DateSelect1);
$Tray=new XoopsFormElementTray('', ' ', '');
$Tray->addElement(new XoopsFormButton('', '', '送出', 'submit'));
$Tray->addElement(new XoopsFormButton('', '', '清除', 'reset'));
$form->addElement($Tray);
$form->addElement(new XoopsFormHidden('ebook_sn', $ebook_sn));
$form->addElement(new XoopsFormHidden('op', $op));
//安全機制
$form->addElement(new XoopsFormHiddenToken('XOOPS_TOKEN_REQUEST',360));
$ebook_form = $form->render();
$xoopsTpl->assign('ebook_form' , $ebook_form);
}
//寫入資料庫
function insert_ebook(){
global $xoopsDB;
//安全判斷
if(!$GLOBALS['xoopsSecurity']->check()){
//錯誤訊息
$error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
//轉向到首頁
redirect_header($_SERVER['PHP_SELF'],3, $error);
}
$tbl=$xoopsDB->prefix('tad_ebook');
$sql="insert into `{$tbl}` (`ebook_title` ,`ebook_post_date` , `ebook_publish_date`) values('{$_POST['ebook_title']}' , '{$_POST['ebook_post_date']}' ,'{$_POST['ebook_publish_date']}')";
$xoopsDB->query($sql) or die(mysql_error());
}
//列出所有書籍
function ebook_list(){
global $xoopsDB , $xoopsTpl;
$tbl=$xoopsDB->prefix('tad_ebook');
$sql="select * from `{$tbl}`";
$result=$xoopsDB->query($sql) or die(mysql_error());
$i=0;
$all_data="";
while($all=$xoopsDB->fetchArray($result)){
$all_data[$i]=$all;
$i++;
}
//die(var_export($all_data));
$xoopsTpl->assign('all_data' , $all_data);
}
//從資料庫刪除
function del_ebook($ebook_sn=""){
global $xoopsDB;
$tbl=$xoopsDB->prefix('tad_ebook');
$sql="delete from `{$tbl}` where ebook_sn='{$ebook_sn}'";
$xoopsDB->queryF($sql) or die(mysql_error());
}
//更新資料庫
function update_ebook($ebook_sn=""){
global $xoopsDB;
//安全判斷
if(!$GLOBALS['xoopsSecurity']->check()){
//錯誤訊息
$error=implode("<br />" , $GLOBALS['xoopsSecurity']->getErrors());
//轉向到首頁
redirect_header($_SERVER['PHP_SELF'],3, $error);
}
$tbl=$xoopsDB->prefix('tad_ebook');
$sql="
update `{$tbl}` set
`ebook_title`='{$_POST['ebook_title']}' ,
`ebook_post_date`='{$_POST['ebook_post_date']}' ,
`ebook_publish_date`='{$_POST['ebook_publish_date']}'
where ebook_sn='{$ebook_sn}'";
$xoopsDB->queryF($sql) or die(mysql_error());
}
/*-----------執行動作判斷區----------*/
$op=empty($_REQUEST['op'])?"":$_REQUEST['op'];
$ebook_sn=isset($_REQUEST['ebook_sn'])?intval($_REQUEST['ebook_sn']):"";
switch($op){
case "insert_ebook":
insert_ebook();
header("location:{$_SERVER['PHP_SELF']}");
exit;
break;
case "del_ebook":
del_ebook($ebook_sn);
header("location:{$_SERVER['PHP_SELF']}");
exit;
break;
case "update_ebook":
update_ebook($ebook_sn);
header("location:{$_SERVER['PHP_SELF']}");
exit;
break;
case "edit_ebook":
ebook_form($ebook_sn);
break;
default:
ebook_form();
ebook_list();
break;
}
/*-----------引入檔案區--------------*/
include_once 'footer.php';
?>