4-4
admin.php
<?php
//引入檔案(設定)
require_once "header.php";
$action_id = isset($_REQUEST['action_id']) ? intval($_REQUEST['action_id']) : '';
try
{
if (!isset($_SESSION['group']) or $_SESSION['group'] != 'admin') {
throw new Exception("僅限管理員使用");
}
//跑流程
switch ($op) {
case "insert_action":
$action_id = insert_action();
header("location: index.php?action_id=$action_id");
exit;
case "update_action":
update_action($action_id);
header("location: index.php?action_id=$action_id");
exit;
case "delete_action":
delete_action($action_id);
header("location: index.php");
exit;
default:
$content = action_form($action_id);
break;
}
} catch (exception $e) {
$error = $e->getMessage();
}
//函數區
function action_form($action_id = "")
{
global $db;
$values = [];
$op = 'insert_action';
if ($action_id) {
$sql = "SELECT * FROM `actions` WHERE `action_id`='$action_id'";
if (!$result = $db->query($sql)) {
throw new Exception($db->error);
}
if ($data = $result->fetch_assoc()) {
$values = $data;
} else {
throw new Exception("讀取 $action_id 失敗");
}
$op = 'update_action';
}
require_once "class/php-bootstrap-form/PFBC/Form.php";
ob_start();
echo '<script type="text/javascript" src="class/My97DatePicker/WdatePicker.js"></script>';
Form::open("action", $values);
Form::Hidden("op", $op);
if ($action_id) {
Form::Hidden("action_id", $action_id);
}
Form::Textbox("活動名稱", "title", ['required' => 1]);
Form::Textbox("活動日期", "action_date", ['required' => 1, 'onClick' => "WdatePicker()"]);
Form::Textbox("截止日期", "end_date", ['required' => 1, 'onClick' => "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:00'})"]);
Form::YesNo("使否啟用", "enable");
Form::CKEditor("活動內容", "content");
Form::Button('儲存', 'submit');
Form::close(false);
$form = ob_get_contents();
ob_end_clean();
return $form;
}
//新增活動
function insert_action()
{
global $db;
$title = clean_var('title', '活動名稱');
$content = clean_var('content', '活動說明');
$action_date = clean_var('action_date', '活動日期');
$end_date = clean_var('end_date', '報名截止日');
$uid = $_SESSION['uid'];
$enable = clean_var('enable', '是否啟用');
$sql = "INSERT INTO `actions` (`title`, `content`, `action_date`, `end_date`, `uid`, `enable`) VALUES('{$title}', '{$content}','{$action_date}','{$end_date}','{$uid}','{$enable}')";
if (!$db->query($sql)) {
throw new Exception($db->error);
}
$action_id = $db->insert_id;
return $action_id;
}
//更新活動
function update_action($action_id)
{
global $db;
$title = clean_var('title', '活動名稱');
$content = clean_var('content', '活動說明');
$action_date = clean_var('action_date', '活動日期');
$end_date = clean_var('end_date', '報名截止日');
$uid = $_SESSION['uid'];
$enable = clean_var('enable', '是否啟用');
$sql = "UPDATE `actions` SET
`title`='{$title}',
`content`='{$content}',
`action_date`='{$action_date}',
`end_date`='{$end_date}',
`enable`='{$enable}',
`uid`='{$uid}'
WHERE `action_id`='{$action_id}'";
if (!$db->query($sql)) {
throw new Exception($db->error);
}
}
//刪除某個活動
function delete_action($action_id)
{
global $db, $smarty;
$sql = "DELETE FROM `actions` WHERE `action_id`='$action_id'";
if (!$result = $db->query($sql)) {
throw new Exception($db->error);
}
}
//結果送至樣板
$page_title = "活動管理";
require_once "footer.php";