線上書籍

Home

[1071]XOOPS模組開發

<?php /*-----------引入檔案區--------------*/ $xoopsOption['template_main'] = "snews_adm_main.tpl"; include_once "header.php"; include_once "../function.php"; /*-----------執行動作判斷區----------*/ 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; default: snews_list(); $op = 'snews_list'; break; } $xoopsTpl->assign('op', $op); include_once 'footer.php'; /*-----------function區--------------*/ //顯示預設頁面內容 function snews_list() { global $xoopsTpl; $main = "後台頁面"; $xoopsTpl->assign('content', $main); } //新增文章的表單 function snews_form($sn) { global $xoopsTpl, $xoopsUser, $TadUpFiles, $xoopsDB; $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', 'main.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 (!$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); return $sn; } function snews_update($sn) { global $xoopsDB, $TadUpFiles; //安全判斷 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); return $sn; }