1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\Modules; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A database driven model. |
7
|
|
|
*/ |
8
|
|
|
class Vote extends ActiveRecordModelExtender |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string $tableName name of the database table. |
13
|
|
|
*/ |
14
|
|
|
protected $tableName = "ramverk1_votes"; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Columns in the table. |
18
|
|
|
* |
19
|
|
|
* @var integer $id primary key auto incremented. |
20
|
|
|
*/ |
21
|
|
|
public $id; |
22
|
|
|
public $user; # question/answer/comment |
23
|
|
|
public $parentId; # All posts have different ids |
24
|
|
|
public $parentType; # post or comment |
25
|
|
|
public $upVote; |
26
|
|
|
public $downVote; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return a vote |
31
|
|
|
* @param string $sql |
32
|
|
|
* @param array $params |
33
|
|
|
* |
34
|
|
|
* @return object |
35
|
|
|
*/ |
36
|
1 |
|
public function getVote($sql, $params) |
37
|
|
|
{ |
38
|
1 |
|
$likes = $this->findAllWhere("$sql", $params); |
39
|
|
|
|
40
|
|
|
// Return users of the ones who have upvoted. |
41
|
|
|
$score = array_reduce($likes, function ($carry, $item) { |
42
|
1 |
|
$carry += $item->upVote - $item->downVote; |
43
|
1 |
|
return $carry; |
44
|
1 |
|
}); |
45
|
|
|
|
46
|
1 |
|
$this->likes = $likes; |
|
|
|
|
47
|
1 |
|
$this->score = $score; |
|
|
|
|
48
|
1 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Control if user has already liked or not |
53
|
|
|
* @param string $user |
54
|
|
|
* @param array $params |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
1 |
View Code Duplication |
public function like($user, $parentId, $parentType) |
|
|
|
|
59
|
|
|
{ |
60
|
1 |
|
$likes = $this->findAllWhere("parentId = ? AND parentType = ?", [$parentId, $parentType]); |
61
|
|
|
// Return users of the ones who have upvoted. |
62
|
|
|
$users = array_map(function ($like) { |
63
|
1 |
|
return $like->upVote != null ? $like->user : ""; |
64
|
1 |
|
}, $likes); |
65
|
|
|
|
66
|
1 |
|
if (in_array($user, $users)) { |
67
|
1 |
|
return false; |
68
|
|
|
} |
69
|
1 |
|
$this->user = $user; |
70
|
1 |
|
$this->parentId = $parentId; |
71
|
1 |
|
$this->parentType = $parentId; |
72
|
1 |
|
$this->upVote = 1; |
73
|
1 |
|
$this->downVote = null; |
74
|
1 |
|
$this->save(); |
75
|
1 |
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Control if user has already liked or not |
80
|
|
|
* @param string $user |
81
|
|
|
* @param array $params |
|
|
|
|
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
1 |
View Code Duplication |
public function dislike($user, $parentId, $parentType) |
|
|
|
|
86
|
|
|
{ |
87
|
1 |
|
$likes = $this->findAllWhere("parentId = ? AND parentType = ?", [$parentId, $parentType]); |
88
|
|
|
// Return users of the ones who have upvoted. |
89
|
1 |
|
$users = array_map(function ($like) { |
90
|
1 |
|
return $like->downVote != null ? $like->user : ""; |
91
|
1 |
|
}, $likes); |
92
|
|
|
|
93
|
1 |
|
if (in_array($user, $users)) { |
94
|
1 |
|
return false; |
95
|
|
|
} |
96
|
1 |
|
$this->user = $user; |
97
|
1 |
|
$this->parentId = $parentId; |
98
|
1 |
|
$this->parentType = $parentId; |
99
|
1 |
|
$this->upVote = null; |
100
|
1 |
|
$this->downVote = 1; |
101
|
1 |
|
$this->save(); |
102
|
1 |
|
return true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: