Repository navigation
Expand file tree
/
Copy pathArticleClass.php
More file actions
123 lines (105 loc) · 4.65 KB
/
Copy pathArticleClass.php
File metadata and controls
123 lines (105 loc) · 4.65 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
<?php
require_once 'config.php';
class ArticleClass
{
private $conn;
public function __construct($conn)
{
$this->conn = $conn;
}
public function insertArticle($user_id, $title, $content, $img)
{
try {
$img_path = null;
// Handle image upload
if (!empty($img)) {
// Check if image is uploaded via form
if (is_array($img) && isset($img['tmp_name']) && is_uploaded_file($img['tmp_name'])) {
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
$imgExtension = strtolower(pathinfo($img['name'], PATHINFO_EXTENSION));
if (in_array($imgExtension, $allowedExtensions)) {
$timestamp = time();
$img_path = "/final_project/src/img/" . $timestamp . "." . $imgExtension;
$output_file = $_SERVER["DOCUMENT_ROOT"] . $img_path;
if (!move_uploaded_file($img['tmp_name'], $output_file)) {
return "Failed to move uploaded file.";
}
} else {
return "Invalid image file extension.";
}
} else {
return "Invalid image file.";
}
}
$sql = "INSERT INTO Articles (user_id, title, content, img, art_date, rate)
VALUES (:user_id, :title, :content, :img, NOW(), 0)";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':content', $content, PDO::PARAM_STR);
$stmt->bindParam(':img', $img_path, PDO::PARAM_STR);
$stmt->execute();
return "文章發佈成功!";
} catch (PDOException $e) {
return "Error: " . $e->getMessage();
}
}
public function getUserArticles($user_id)
{
try {
$sql = "SELECT article_id, title, content, img FROM Articles WHERE user_id = :user_id";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return [];
}
}
public function updateArticle($article_id, $title, $content, $img)
{
try {
$img_path = null;
if (!empty($img)) {
if (is_array($img) && isset($img['tmp_name']) && is_uploaded_file($img['tmp_name'])) {
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
$imgExtension = strtolower(pathinfo($img['name'], PATHINFO_EXTENSION));
if (in_array($imgExtension, $allowedExtensions)) {
$timestamp = time();
$img_path = "/final_project/src/img/" . $timestamp . "." . $imgExtension;
$output_file = $_SERVER["DOCUMENT_ROOT"] . $img_path;
if (!move_uploaded_file($img['tmp_name'], $output_file)) {
return "Failed to move uploaded file.";
}
} else {
return "Invalid image file extension.";
}
} else {
return "Invalid image file.";
}
}
$sql = "UPDATE Articles SET title = :title, content = :content, img = COALESCE(:img, img) WHERE article_id = :article_id";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':content', $content, PDO::PARAM_STR);
$stmt->bindParam(':img', $img_path, PDO::PARAM_STR);
$stmt->bindParam(':article_id', $article_id, PDO::PARAM_INT);
$stmt->execute();
return "文章更新成功!";
} catch (PDOException $e) {
return "Error: " . $e->getMessage();
}
}
public function deleteArticle($article_id)
{
try {
$sql = "DELETE FROM Articles WHERE article_id = :article_id";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':article_id', $article_id, PDO::PARAM_INT);
$stmt->execute();
return "文章刪除成功!";
} catch (PDOException $e) {
return "Error: " . $e->getMessage();
}
}
}