| @@ 58-76 (lines=19) @@ | ||
| 55 | * |
|
| 56 | * @return bool |
|
| 57 | */ |
|
| 58 | public function like($user, $parentId, $parentType) |
|
| 59 | { |
|
| 60 | $likes = $this->findAllWhere("parentId = ? AND parentType = ?", [$parentId, $parentType]); |
|
| 61 | // Return users of the ones who have upvoted. |
|
| 62 | $users = array_map(function ($like) { |
|
| 63 | return $like->upVote != null ? $like->user : ""; |
|
| 64 | }, $likes); |
|
| 65 | ||
| 66 | if (in_array($user, $users)) { |
|
| 67 | return false; |
|
| 68 | } |
|
| 69 | $this->user = $user; |
|
| 70 | $this->parentId = $parentId; |
|
| 71 | $this->parentType = $parentId; |
|
| 72 | $this->upVote = 1; |
|
| 73 | $this->downVote = null; |
|
| 74 | $this->save(); |
|
| 75 | return true; |
|
| 76 | } |
|
| 77 | ||
| 78 | /** |
|
| 79 | * Control if user has already liked or not |
|
| @@ 85-103 (lines=19) @@ | ||
| 82 | * |
|
| 83 | * @return bool |
|
| 84 | */ |
|
| 85 | public function dislike($user, $parentId, $parentType) |
|
| 86 | { |
|
| 87 | $likes = $this->findAllWhere("parentId = ? AND parentType = ?", [$parentId, $parentType]); |
|
| 88 | // Return users of the ones who have upvoted. |
|
| 89 | $users = array_map(function ($like) { |
|
| 90 | return $like->downVote != null ? $like->user : ""; |
|
| 91 | }, $likes); |
|
| 92 | ||
| 93 | if (in_array($user, $users)) { |
|
| 94 | return false; |
|
| 95 | } |
|
| 96 | $this->user = $user; |
|
| 97 | $this->parentId = $parentId; |
|
| 98 | $this->parentType = $parentId; |
|
| 99 | $this->upVote = null; |
|
| 100 | $this->downVote = 1; |
|
| 101 | $this->save(); |
|
| 102 | return true; |
|
| 103 | } |
|
| 104 | } |
|
| 105 | ||