|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* NewsFeed Class |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
7
|
|
|
* @author Omar El Gabry <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
class NewsFeed extends Model{ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* get all news feed. |
|
13
|
|
|
* |
|
14
|
|
|
* @access public |
|
15
|
|
|
* @param integer $pageNum |
|
16
|
|
|
* @return array |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
View Code Duplication |
public function getAll($pageNum = 1){ |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
$pagination = Pagination::pagination("newsfeed", "", [], $pageNum); |
|
22
|
|
|
$offset = $pagination->getOffset(); |
|
23
|
|
|
$limit = $pagination->perPage; |
|
24
|
|
|
|
|
25
|
|
|
$database = Database::openConnection(); |
|
26
|
|
|
$query = "SELECT newsfeed.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, newsfeed.content, newsfeed.date "; |
|
27
|
|
|
$query .= "FROM users, newsfeed "; |
|
28
|
|
|
$query .= "WHERE users.id = newsfeed.user_id "; |
|
29
|
|
|
$query .= "ORDER BY newsfeed.date DESC "; |
|
30
|
|
|
$query .= "LIMIT $limit OFFSET $offset"; |
|
31
|
|
|
|
|
32
|
|
|
$database->prepare($query); |
|
33
|
|
|
$database->execute(); |
|
34
|
|
|
$newsfeed = $database->fetchAllAssociative(); |
|
35
|
|
|
|
|
36
|
|
|
return array("newsfeed" => $newsfeed, "pagination" => $pagination); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* get news feed by Id. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $newsfeedId |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
public function getById($newsfeedId){ |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$database = Database::openConnection(); |
|
48
|
|
|
$query = "SELECT newsfeed.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, newsfeed.content, newsfeed.date "; |
|
49
|
|
|
$query .= "FROM users, newsfeed "; |
|
50
|
|
|
$query .= "WHERE newsfeed.id = :id "; |
|
51
|
|
|
$query .= "AND users.id = newsfeed.user_id LIMIT 1 "; |
|
52
|
|
|
|
|
53
|
|
|
$database->prepare($query); |
|
54
|
|
|
$database->bindValue(':id', (int)$newsfeedId); |
|
55
|
|
|
$database->execute(); |
|
56
|
|
|
|
|
57
|
|
|
$feed = $database->fetchAllAssociative(); |
|
58
|
|
|
return $feed; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* create news feed. |
|
63
|
|
|
* |
|
64
|
|
|
* @param integer $userId |
|
65
|
|
|
* @param string $content |
|
66
|
|
|
* @return array feed created |
|
67
|
|
|
* @throws Exception if feed couldn't be created |
|
68
|
|
|
*/ |
|
69
|
|
View Code Duplication |
public function create($userId, $content){ |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$validation = new Validation(); |
|
72
|
|
|
if(!$validation->validate(['Content' => [$content, "required|minLen(4)|maxLen(300)"]])) { |
|
73
|
|
|
$this->errors = $validation->errors(); |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$database = Database::openConnection(); |
|
78
|
|
|
$query = "INSERT INTO newsfeed (user_id, content) VALUES (:user_id, :content)"; |
|
79
|
|
|
|
|
80
|
|
|
$database->prepare($query); |
|
81
|
|
|
$database->bindValue(':user_id', $userId); |
|
82
|
|
|
$database->bindValue(':content', $content); |
|
83
|
|
|
$database->execute(); |
|
84
|
|
|
|
|
85
|
|
|
if($database->countRows() !== 1){ |
|
86
|
|
|
throw new Exception("Couldn't add news feed"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$newsfeedId = $database->lastInsertedId(); |
|
90
|
|
|
$feed = $this->getById($newsfeedId); |
|
91
|
|
|
return $feed; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* update news feed. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $newsfeedId |
|
98
|
|
|
* @param string $content |
|
99
|
|
|
* @return array feed created |
|
100
|
|
|
* @throws Exception if feed couldn't be updated |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
public function update($newsfeedId, $content){ |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$validation = new Validation(); |
|
105
|
|
|
if(!$validation->validate(['Content' => [$content, "required|minLen(4)|maxLen(300)"]])) { |
|
106
|
|
|
$this->errors = $validation->errors(); |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$database = Database::openConnection(); |
|
111
|
|
|
$query = "UPDATE newsfeed SET content = :content WHERE id = :id LIMIT 1"; |
|
112
|
|
|
|
|
113
|
|
|
$database->prepare($query); |
|
114
|
|
|
$database->bindValue(':content', $content); |
|
115
|
|
|
$database->bindValue(':id', $newsfeedId); |
|
116
|
|
|
$result = $database->execute(); |
|
117
|
|
|
|
|
118
|
|
|
if(!$result){ |
|
119
|
|
|
throw new Exception("Couldn't update newsfeed of ID: " . $newsfeedId); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$feed = $this->getById($newsfeedId); |
|
123
|
|
|
return $feed; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
} |
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.