1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Post Class |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
7
|
|
|
* @author Omar El Gabry <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class Post extends Model{ |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* get all posts |
14
|
|
|
* |
15
|
|
|
* @access public |
16
|
|
|
* @param integer $pageNum |
17
|
|
|
* @return array Associative array of the posts, and Pagination Object. |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function getAll($pageNum = 1){ |
|
|
|
|
21
|
|
|
|
22
|
|
|
$pagination = Pagination::pagination("posts", "", [], $pageNum); |
23
|
|
|
$offset = $pagination->getOffset(); |
24
|
|
|
$limit = $pagination->perPage; |
25
|
|
|
|
26
|
|
|
$database = Database::openConnection(); |
27
|
|
|
$query = "SELECT posts.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, posts.title, posts.content, posts.date "; |
28
|
|
|
$query .= "FROM users, posts "; |
29
|
|
|
$query .= "WHERE users.id = posts.user_id "; |
30
|
|
|
$query .= "ORDER BY posts.date DESC "; |
31
|
|
|
$query .= "LIMIT $limit OFFSET $offset"; |
32
|
|
|
|
33
|
|
|
$database->prepare($query); |
34
|
|
|
$database->execute(); |
35
|
|
|
$posts = $database->fetchAllAssociative(); |
36
|
|
|
|
37
|
|
|
$this->appendNumberOfComments($posts, $database); |
|
|
|
|
38
|
|
|
|
39
|
|
|
return array("posts" => $posts, "pagination" => $pagination); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* append number of comments to the array of posts for each post. |
44
|
|
|
* |
45
|
|
|
* @access private |
46
|
|
|
* @param array |
47
|
|
|
* |
48
|
|
|
*/ |
49
|
|
|
private function appendNumberOfComments(&$posts){ |
50
|
|
|
|
51
|
|
|
$postId = 0; |
52
|
|
|
$database = Database::openConnection(); |
53
|
|
|
|
54
|
|
|
$query = "SELECT COUNT(*) AS comments FROM comments WHERE post_id = :post_id "; |
55
|
|
|
$database->prepare($query); |
56
|
|
|
$database->bindParam(':post_id', $postId); |
57
|
|
|
|
58
|
|
|
foreach($posts as $key => $post){ |
59
|
|
|
$postId = (int)$posts[$key]["id"]; |
|
|
|
|
60
|
|
|
$database->execute(); |
61
|
|
|
$posts[$key]["comments"] = $database->fetchAssociative()["comments"]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* get post by Id. |
67
|
|
|
* |
68
|
|
|
* @access public |
69
|
|
|
* @param integer $postId |
70
|
|
|
* @return array Array holds the data of the post |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function getById($postId){ |
|
|
|
|
73
|
|
|
|
74
|
|
|
$database = Database::openConnection(); |
75
|
|
|
$query = "SELECT posts.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, posts.title, posts.content, posts.date "; |
76
|
|
|
$query .= "FROM users, posts "; |
77
|
|
|
$query .= "WHERE posts.id = :id "; |
78
|
|
|
$query .= "AND users.id = posts.user_id LIMIT 1 "; |
79
|
|
|
|
80
|
|
|
$database->prepare($query); |
81
|
|
|
$database->bindValue(':id', $postId); |
82
|
|
|
$database->execute(); |
83
|
|
|
|
84
|
|
|
$post = $database->fetchAssociative(); |
85
|
|
|
return $post; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* create post |
90
|
|
|
* |
91
|
|
|
* @access public |
92
|
|
|
* @param integer $userId |
93
|
|
|
* @param string $title |
94
|
|
|
* @param string $content |
95
|
|
|
* @return bool |
96
|
|
|
* @throws Exception If post couldn't be created |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
|
|
public function create($userId, $title, $content){ |
100
|
|
|
|
101
|
|
|
$validation = new Validation(); |
102
|
|
View Code Duplication |
if(!$validation->validate([ |
|
|
|
|
103
|
|
|
'Title' => [$title, "required|minLen(2)|maxLen(60)"], |
104
|
|
|
'Content' => [$content, "required|minLen(4)|maxLen(1800)"]])) { |
105
|
|
|
$this->errors = $validation->errors(); |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$database = Database::openConnection(); |
110
|
|
|
$query = "INSERT INTO posts (user_id, title, content) VALUES (:user_id, :title, :content)"; |
111
|
|
|
|
112
|
|
|
$database->prepare($query); |
113
|
|
|
$database->bindValue(':user_id', $userId); |
114
|
|
|
$database->bindValue(':title', $title); |
115
|
|
|
$database->bindValue(':content', $content); |
116
|
|
|
$database->execute(); |
117
|
|
|
|
118
|
|
|
if($database->countRows() !== 1){ |
119
|
|
|
throw new Exception ("Couldn't add news feed"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* update Post |
127
|
|
|
* |
128
|
|
|
* @access public |
129
|
|
|
* @static static method |
130
|
|
|
* @param string $postId |
131
|
|
|
* @param string $title |
132
|
|
|
* @param string $content |
133
|
|
|
* @return array Array of the updated post |
134
|
|
|
* @throws Exception If post couldn't be updated |
135
|
|
|
* |
136
|
|
|
*/ |
137
|
|
|
public function update($postId, $title, $content){ |
138
|
|
|
|
139
|
|
|
$validation = new Validation(); |
140
|
|
View Code Duplication |
if(!$validation->validate([ |
|
|
|
|
141
|
|
|
'Title' => [$title, "required|minLen(2)|maxLen(60)"], |
142
|
|
|
'Content' => [$content, "required|minLen(4)|maxLen(1800)"]])) { |
143
|
|
|
$this->errors = $validation->errors(); |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$database = Database::openConnection(); |
148
|
|
|
$query = "UPDATE posts SET title = :title, content = :content WHERE id = :id LIMIT 1"; |
149
|
|
|
|
150
|
|
|
$database->prepare($query); |
151
|
|
|
$database->bindValue(':title', $title); |
152
|
|
|
$database->bindValue(':content', $content); |
153
|
|
|
$database->bindValue(':id', $postId); |
154
|
|
|
$result = $database->execute(); |
155
|
|
|
|
156
|
|
|
if(!$result){ |
157
|
|
|
throw new Exception("Couldn't update post of ID: " . $postId); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$post = $this->getById($postId); |
161
|
|
|
return $post; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.