[1071]XOOPS模組開發
<?php
/*-----------引入檔案區--------------*/
include_once "header.php";
$xoopsOption['template_main'] = "snews_index.tpl";
include_once XOOPS_ROOT_PATH . "/header.php";
/*-----------function區--------------*/
//顯示內容
function snews_show($sn)
{
global $xoopsTpl, $xoopsDB, $TadUpFiles;
$myts = MyTextSanitizer::getInstance();
$tbl = $xoopsDB->prefix('snews');
$sql = "SELECT * FROM `$tbl` WHERE `sn` = '{$sn}'";
$result = $xoopsDB->query($sql) or web_error($sql);
$snews = $xoopsDB->fetchArray($result);
$snews['content'] = $myts->displayTarea($snews['content'], 1, 0, 0, 0, 0);
$snews['title'] = $myts->htmlSpecialChars($snews['title']);
$snews['username'] = $myts->htmlSpecialChars($snews['username']);
$TadUpFiles->set_col('sn', $sn);
$snews['cover'] = $TadUpFiles->show_files('pic', false);
$TadUpFiles->set_col('snews', $sn);
$snews['files'] = $TadUpFiles->show_files();
// die(var_dump($snews));
$xoopsTpl->assign('snews', $snews);
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 = new sweet_alert();
$sweet_alert->render("snews_del", "index.php?op=snews_delete&sn=", 'sn');
if (power_chk("", "1")) {
$xoopsTpl->assign('snews_post', true);
}
if (power_chk("", "2")) {
$xoopsTpl->assign('snews_delete', true);
}
}
//顯示列表
function snews_list()
{
global $xoopsTpl, $xoopsDB, $TadUpFiles, $xoopsModuleConfig;
$myts = MyTextSanitizer::getInstance();
$tbl = $xoopsDB->prefix('snews');
$sql = "SELECT * FROM `$tbl` ORDER BY `update_time` DESC";
//getPageBar($原sql語法, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
$PageBar = getPageBar($sql, $xoopsModuleConfig['show_num'], 10);
$bar = $PageBar['bar'];
$sql = $PageBar['sql'];
$total = $PageBar['total'];
$result = $xoopsDB->query($sql) or web_error($sql);
$all = array();
while ($snews = $xoopsDB->fetchArray($result)) {
$snews['content'] = $myts->displayTarea($snews['content'], 1, 0, 0, 0, 0);
$snews['title'] = $myts->htmlSpecialChars($snews['title']);
$snews['username'] = $myts->htmlSpecialChars($snews['username']);
$TadUpFiles->set_col('sn', $snews['sn']);
$snews['cover'] = $TadUpFiles->get_pic_file('thumb');
$all[] = $snews;
}
$xoopsTpl->assign('all', $all);
$xoopsTpl->assign('bar', $bar);
$xoopsTpl->assign('total', $total);
}
//顯示精選文章
function snews_focus()
{
global $xoopsTpl, $xoopsDB, $TadUpFiles;
$myts = MyTextSanitizer::getInstance();
$tbl = $xoopsDB->prefix('snews');
$sql = "SELECT * FROM `$tbl` where `focus`=1 ORDER BY `update_time` DESC";
$result = $xoopsDB->query($sql) or web_error($sql);
$all_focus = array();
while ($snews = $xoopsDB->fetchArray($result)) {
$snews['content'] = xoops_substr(strip_tags($snews['content']), 0, 240);
$snews['title'] = $myts->htmlSpecialChars($snews['title']);
$TadUpFiles->set_col('sn', $snews['sn']);
$snews['cover'] = $TadUpFiles->get_pic_file();
$all_focus[] = $snews;
}
$xoopsTpl->assign('all_focus', $all_focus);
}
//刪除內容
function snews_delete($sn)
{
global $xoopsTpl, $xoopsDB, $TadUpFiles;
if (!power_chk('snews', 2)) {
redirect_header('index.php', 3, '無操作權限');
}
$tbl = $xoopsDB->prefix('snews');
$sql = "DELETE FROM `$tbl` WHERE `sn` = '{$sn}'";
$xoopsDB->queryF($sql) or web_error($sql);
$TadUpFiles->set_col('sn', $sn);
$TadUpFiles->del_files();
$TadUpFiles->set_col('snews', $sn);
$TadUpFiles->del_files();
}
//新增文章的表單
function snews_form($sn)
{
global $xoopsTpl, $xoopsUser, $TadUpFiles, $xoopsDB;
if (!power_chk('snews', 1)) {
redirect_header('index.php', 3, '無操作權限');
}
$snews = array();
if ($sn) {
$tbl = $xoopsDB->prefix('snews');
$sql = "SELECT * FROM `$tbl` WHERE `sn` = '{$sn}'";
$result = $xoopsDB->query($sql) or web_error($sql);
$snews = $xoopsDB->fetchArray($result);
}
include_once XOOPS_ROOT_PATH . "/class/xoopsformloader.php";
//建立表單
$form = new XoopsThemeForm('編輯文章', 'myForm', 'index.php', 'post', true);
//建立類別選項
$select = new XoopsFormRadio('類別或主題', 'topic_sn', $snews['topic_sn']);
$options['1'] = '街巷故事';
$options['2'] = '市井觀點';
$options['3'] = '私房知識塾';
$select->addOptionArray($options);
$form->addElement($select);
//建立標題欄位
$form->addElement(new XoopsFormText('文章標題', 'title', 60, 100, $snews['title']), true);
//所見即所得編輯器
include_once XOOPS_ROOT_PATH . "/modules/tadtools/ck.php";
$ck = new CKEditor("snews", "content", $snews['content']);
$ck->setHeight(250);
$editor = $ck->render();
$form->addElement(new XoopsFormLabel('文章內容', $editor));
//精選(是否選單)
$form->addElement(new XoopsFormRadioYN('精選', 'focus', $snews['focus']));
//作者輸入框
$username = ($sn) ? $snews['username'] : $xoopsUser->name();
$form->addElement(new XoopsFormText('作者', 'username', 60, 100, $username));
//上傳表單
$form->setExtra("enctype='multipart/form-data'");
$TadUpFiles->set_col('sn', $sn);
$upform = $TadUpFiles->upform(true, 'pic', null, true, '.jpg,.png');
$form->addElement(new XoopsFormLabel('封面圖', $upform));
//上傳附檔表單
$TadUpFiles->set_col('snews', $sn);
$upform = $TadUpFiles->upform(true, 'snews');
$form->addElement(new XoopsFormLabel('附檔', $upform));
//使用者編號
if ($sn) {
$uid = $_SESSION['snewsAdmin'] ? $snews['uid'] : $xoopsUser->uid();
} else {
$uid = $xoopsUser->uid();
}
$form->addElement(new XoopsFormHidden('uid', $uid));
//下個動作
if ($sn) {
$form->addElement(new XoopsFormHidden('op', 'snews_update'));
$form->addElement(new XoopsFormHidden('sn', $sn));
} else {
$form->addElement(new XoopsFormHidden('op', 'snews_insert'));
}
//儲存按鈕
$form->addElement(new XoopsFormButton('', '', '儲存', 'submit'));
//產生程式碼
$snews_form = $form->render();
//將表單送到樣板
$xoopsTpl->assign('snews_form', $snews_form);
}
function snews_insert()
{
global $xoopsDB, $TadUpFiles;
if (!power_chk('snews', 1)) {
redirect_header('index.php', 3, '無操作權限');
}
//安全判斷
if (!$GLOBALS['xoopsSecurity']->check()) {
$error = implode("<br>", $GLOBALS['xoopsSecurity']->getErrors());
throw new Exception($error);
}
$myts = MyTextSanitizer::getInstance();
foreach ($_POST as $key => $value) {
$$key = $myts->addSlashes($value);
}
$tbl = $xoopsDB->prefix('snews');
$sql = "insert into `$tbl` (`focus`, `topic_sn`, `sort`, `title`, `content`, `username`, `create_time`, `update_time`, `uid`) values('{$focus}', '{$topic_sn}', '{$sort}', '{$title}', '{$content}', '{$username}', now(), now(), '{$uid}')";
$xoopsDB->query($sql) or web_error($sql);
$sn = $xoopsDB->getInsertId(); //取得最後新增的編號
$TadUpFiles->set_col('sn', $sn);
$TadUpFiles->upload_file('pic', 1920, 640, null, $title, true, false, null, 'png;jpg');
$TadUpFiles->set_col('snews', $sn);
$TadUpFiles->upload_file('snews', 1920, 640, null, null, true);
mk_html($sn);
return $sn;
}
function snews_update($sn)
{
global $xoopsDB, $TadUpFiles;
if (!power_chk('snews', 1)) {
redirect_header('index.php', 3, '無操作權限');
}
//安全判斷
if (!$GLOBALS['xoopsSecurity']->check()) {
$error = implode("<br>", $GLOBALS['xoopsSecurity']->getErrors());
throw new Exception($error);
}
$myts = MyTextSanitizer::getInstance();
foreach ($_POST as $key => $value) {
$$key = $myts->addSlashes($value);
}
$tbl = $xoopsDB->prefix('snews');
$sql = "update `$tbl` set `focus`='{$focus}', `topic_sn`='{$topic_sn}', `title`='{$title}', `content`='{$content}', `username`='{$username}',`update_time`= now(), `uid`='{$uid}' where `sn`='{$sn}'";
$xoopsDB->queryF($sql) or web_error($sql);
$TadUpFiles->set_col('sn', $sn);
$TadUpFiles->upload_file('pic', 1920, 640, null, $title, true, false, null, 'png;jpg');
$TadUpFiles->set_col('snews', $sn);
$TadUpFiles->upload_file('snews', 1920, 640, null, null, true);
mk_html($sn);
return $sn;
}
/*-----------執行動作判斷區----------*/
include_once $GLOBALS['xoops']->path('/modules/system/include/functions.php');
$op = system_CleanVars($_REQUEST, 'op', '', 'string');
$sn = system_CleanVars($_REQUEST, 'sn', 0, 'int');
switch ($op) {
case "snews_form":
snews_form($sn);
break;
case "snews_insert":
$sn = snews_insert();
header("location: index.php?sn={$sn}");
exit;
case "snews_update":
snews_update($sn);
header("location: index.php?sn={$sn}");
exit;
//下載檔案
case "tufdl":
$files_sn = isset($_GET['files_sn']) ? intval($_GET['files_sn']) : "";
$TadUpFiles->add_file_counter($files_sn, false, true);
exit;
case "snews_delete":
snews_delete($sn);
header("location: index.php");
exit;
default:
if ($sn) {
snews_show($sn);
$op = "snews_show";
} else {
snews_list();
snews_focus();
$op = "snews_list";
}
break;
}
/*-----------秀出結果區--------------*/
$xoopsTpl->assign('op', $op);
$xoopsTpl->assign("toolbar", toolbar_bootstrap($interface_menu));
include_once XOOPS_ROOT_PATH . '/footer.php';