-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplanedit.php
More file actions
311 lines (307 loc) · 11.4 KB
/
planedit.php
File metadata and controls
311 lines (307 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<?php
require_once __DIR__ . '/vendor/owasp/csrf-protector-php/libs/csrf/csrfprotector.php';
csrfProtector::init();
require('config/config.php');
require('func/plantype.php');
$showform = true;
switch ($_GET["type"]) {
case 'add':
$type = "add";
$typename = "新增";
break;
case 'edit':
$type = "edit";
$typename = "編輯";
break;
default :
$showform = false;
break;
}
$action = $_POST["action"] ?? "view";
$planid = $_GET["id"] ?? "";
?>
<html lang="zh-Hant-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php
require __DIR__ . '/commonhead.php';
?>
<title><?=$C["titlename"]?>/<?=$typename?>教案</title>
<style type="text/css">
body {
padding-top: 4.5rem;
}
.itemicon {
width: 18px;
text-align: center;
}
</style>
</head>
<body>
<?php
require("header.php");
$showform = true;
if (!$U["islogin"]) {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
此功能需要驗證帳號,請<a href="<?=$C["path"]?>/login/">登入</a>
</div>
<?php
$showform = false;
} else if ($action == "edit") {
if ($type == "add") {
$planid = substr(md5(uniqid(rand(),true)), 0, 8);
$sth = $G["db"]->prepare("INSERT INTO `plan` (`year`, `type`, `name`, `description`, `volume`, `tag`, `file`, `inuse`, `id`) VALUES (:year, :type, :name, :description, :volume, :tag, :file, :inuse, :id)");
} else if ($type == "edit") {
$sth = $G["db"]->prepare("UPDATE `plan` SET `year` = :year, type = :type, `name` = :name, `description` = :description, `volume` = :volume, `tag` = :tag, `file` = :file, `inuse` = :inuse WHERE `id` = :id");
}
$tag = $_POST["tag"] ?? array();
foreach ($_POST["newtag"] as $newtag) {
$newtag = trim($newtag);
$newtag = preg_replace("/[[:cntrl:]]/", "", $newtag);
if ($newtag != "" && !in_array($newtag, $tag)) {
$tag[] = $newtag;
}
}
$file = $_POST["file"] ?? array();
foreach ($_POST["newfile"] as $newfile) {
$newfile = trim($newfile);
$newfile = preg_replace("/[[:cntrl:]]/", "", $newfile);
if ($newfile != "" && !in_array($newfile, $file)) {
$sthfile = $G["db"]->prepare("SELECT * FROM `file` WHERE `id` = :id");
$sthfile->bindValue(":id", $newfile);
$sthfile->execute();
$D["file"][$newfile] = $sthfile->fetch(PDO::FETCH_ASSOC);
if ($D["file"][$newfile] === false) {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
附加檔案失敗,找不到檔案編號 <?=$newfile?>,<a href="<?=$C["path"]?>/managefiles/" target="_blank">查看檔案列表</a>
</div>
<?php
} else {
$file[] = $newfile;
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
附加檔案成功 <?=htmlentities($D["file"][$newfile]["name"])?>,<a href="<?=$C["path"]?>/file/<?=$newfile?>/" target="_blank">查看此檔案</a>
</div>
<?php
}
}
}
$_POST["name"] = trim($_POST["name"]);
$_POST["name"] = preg_replace("/[[:cntrl:]]/", "", $_POST["name"]);
if ($_POST["name"] == "") {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
標題為空,沒有進行任何動作
</div>
<?php
} else {
$sth->bindValue(":year", $_POST["year"]);
$sth->bindValue(":type", $_POST["type"]);
$sth->bindValue(":name", $_POST["name"]);
$sth->bindValue(":description", $_POST["description"]);
$sth->bindValue(":volume", $_POST["volume"]);
$sth->bindValue(":tag", json_encode($tag));
$sth->bindValue(":file", json_encode($file));
$sth->bindValue(":inuse", $_POST["inuse"]);
$sth->bindValue(":id", $planid);
$sth->execute();
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<?=htmlentities($_POST["name"])?> <?=$typename?>成功,<a href="<?=$C["path"]?>/plan/<?=$planid?>/" target="_blank">查看</a>
</div>
<?php
}
} else if ($action == "del") {
if (isset($_POST["del"])) {
$sth = $G["db"]->prepare("DELETE FROM `plan` WHERE `id` = :id");
$sth->bindValue(":id", $planid);
$sth->execute();
$showform = false;
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
已刪除,<a href="<?=$C["path"]?>/manageplans/">回到教案列表</a>
</div>
<?php
} else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
請勾選「確認刪除」
</div>
<?php
}
}
if ($showform && $type == "add") {
$D["plan"] = array("year"=>$G["schoolyear"], "type"=>"0", "name"=>"", "description"=>"", "volume"=>"", "tag"=>array(), "file"=>array(), "inuse"=>"1");
} else if ($showform && $type == "edit") {
$sth = $G["db"]->prepare("SELECT * FROM `plan` WHERE `id` = :id");
$sth->bindValue(":id", $planid);
$sth->execute();
$D["plan"] = $sth->fetch(PDO::FETCH_ASSOC);
if ($D["plan"] === false) {
?>
<div class="alert alert-danger" role="alert">
找不到教案,<a href="<?=$C["path"]?>/manageplans/">請回到列表重新選擇</a>
</div>
<?php
$showform = false;
} else {
$D["plan"]["tag"] = json_decode($D["plan"]["tag"], true);
$D["plan"]["file"] = json_decode($D["plan"]["file"], true);
foreach ($D["plan"]["file"] as $file) {
if (!isset($D["file"][$file])) {
$sthfile = $G["db"]->prepare("SELECT * FROM `file` WHERE `id` = :id");
$sthfile->bindValue(":id", $file);
$sthfile->execute();
$D["file"][$file] = $sthfile->fetch(PDO::FETCH_ASSOC);
}
}
}
}
require("func/tag.php");
if ($showform) {
?>
<div class="container">
<h2><?=$typename?>教案 <?php if($type=="edit"){ ?><a class="btn btn-sm btn-info" href="<?=$C["path"]?>/plan/<?=$planid?>/" role="button" target="_blank"><i class="fa fa-eye" aria-hidden="true"></i> 查看</a><?php } ?></h2>
<form action="" method="post">
<div class="row">
<label class="col-sm-3 col-md-2 form-control-label"><i class="fa fa-calendar itemicon" aria-hidden="true"></i> 學年度</label>
<div class="col-sm-9 col-md-10">
<input class="form-control" type="number" name="year" value="<?=$D["plan"]["year"]?>" min="0" max="99999999" required>
</div>
</div>
<div class="row">
<label class="col-sm-3 col-md-2 form-control-label"><i class="fa fa-bookmark itemicon" aria-hidden="true"></i> 分類</label>
<div class="col-sm-9 col-md-10">
<select name="type" class="form-control">
<?php
foreach ($D['plantype'] as $id => $plantype) {
?><option value="<?=$id?>" <?=($D["plan"]["type"] == $id?"selected":"")?>><?=$plantype?></option><?php
}
?>
</select>
</div>
</div>
<div class="row">
<label class="col-sm-3 col-md-2 form-control-label"><i class="fa fa-header itemicon" aria-hidden="true"></i> 標題</label>
<div class="col-sm-9 col-md-10">
<input class="form-control" type="text" name="name" value="<?=$D["plan"]["name"]?>" required>
</div>
</div>
<div class="row">
<label class="col-sm-3 col-md-2 form-control-label"><i class="fa fa-info itemicon" aria-hidden="true"></i> 說明</label>
<div class="col-sm-9 col-md-10">
<textarea class="form-control" name="description"><?=$D["plan"]["description"]?></textarea>
</div>
</div>
<div class="row">
<label class="col-sm-3 col-md-2 form-control-label"><i class="fa fa-header itemicon" aria-hidden="true"></i> 冊別</label>
<div class="col-sm-9 col-md-10">
<input class="form-control" type="text" name="volume" value="<?=$D["plan"]["volume"]?>" required>
</div>
</div>
<div class="row">
<label class="col-sm-3 col-md-2 form-control-label"><i class="fa fa-tags itemicon" aria-hidden="true"></i> 標籤</label>
<div class="col-sm-9 col-md-10">
<div class="checkbox">
<?php
foreach ($D['tag'] as $tag => $cnt) {
?><label><input type="checkbox" name="tag[]" value="<?=htmlentities($tag)?>" <?=(in_array($tag, $D["plan"]["tag"])?"checked":"")?>><mark><?=htmlentities($tag)?>(<?=$cnt?>)</mark></label> <?php
}
?>
<div id="taglist">
<input type="text" name="newtag[]" placeholder="新增" maxlength="15">
</div>
<button type="button" class="btn btn-default btn-sm" onclick="moretag()"><i class="fa fa-tag" aria-hidden="true"></i> 更多</button>
</div>
</div>
</div>
<div class="row">
<label class="col-sm-3 col-md-2 form-control-label"><i class="fa fa-files-o itemicon" aria-hidden="true"></i> 檔案</label>
<div class="col-sm-9 col-md-10">
<div class="checkbox">
<?php
foreach ($D["plan"]["file"] as $file) {
?>
<div id="file_<?=$file?>">
<a href="<?=$C["path"]?>/file/<?=$file?>/" target="_blank"><?=htmlentities($D["file"][$file]["name"])?></a>
<input type="hidden" name="file[]" value="<?=$file?>">
<button type="button" class="btn btn-danger btn-sm" onclick="removefile('<?=$file?>')"><i class="fa fa-trash-o" aria-hidden="true"></i> 移除</button>
</div>
<?php
}
?>
<div id="filelist">
<input type="text" name="newfile[]" placeholder="新檔案" hidden>
</div>
<button type="button" class="btn btn-default btn-sm" onclick="pickfile()"><i class="fa fa-file" aria-hidden="true"></i> 更多檔案</button>
</div>
</div>
</div>
<div class="row">
<label class="col-sm-3 col-md-2 form-control-label"><i class="fa fa-eye itemicon" aria-hidden="true"></i> 狀態</label>
<div class="col-sm-9 col-md-10">
<label>
<input type="radio" name="inuse" value="1" <?=($D["plan"]["inuse"] == 1?"checked":"")?>> <i class="fa fa-eye" aria-hidden="true"></i> <?=$G["inuse"][1]?>
</label>
<label>
<input type="radio" name="inuse" value="0" <?=($D["plan"]["inuse"] == 0?"checked":"")?>> <i class="fa fa-eye-slash" aria-hidden="true"></i> <?=$G["inuse"][0]?>
</label>
</div>
</div>
<div class="row">
<div class="col-sm-9 col-md-10 offset-sm-2">
<button type="submit" name="action" value="edit" class="btn btn-success"><i class="fa fa-pencil" aria-hidden="true"></i> <?=$typename?></button>
</div>
</div>
<?php
if ($type == "edit") {
?>
<div class="row">
<div class="col-sm-9 col-md-10 offset-sm-2">
<button type="submit" name="action" value="del" class="btn btn-danger"><i class="fa fa-trash-o" aria-hidden="true"></i> 刪除</button>
<label>
<input type="checkbox" name="del">確認刪除
</label>
</div>
</div>
<?php
}
?>
</form>
</div>
<script type="text/javascript">
function moretag(){
var temp=taglist.children[0].cloneNode(true);
temp.value="";
taglist.appendChild(temp);
}
function morefile(id=""){
var temp=filelist.children[0].cloneNode(true);
temp.value=id;
temp.hidden=false;
filelist.appendChild(temp);
}
function removefile(id){
document.all["file_"+id].remove();
}
function pickfile(id){
window.open("<?=$C["path"]?>/pickfile/", "_blank");
}
</script>
<?php
}
require("footer.php");
?>
</body>
</html>