1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yoeunes\Voteable\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
7
|
|
|
use Illuminate\Database\Query\JoinClause; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Yoeunes\Voteable\VoteBuilder; |
10
|
|
|
use Yoeunes\Voteable\VoteQueryBuilder; |
11
|
|
|
|
12
|
|
|
trait Voteable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* This model has many votes. |
16
|
|
|
* |
17
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
18
|
|
|
*/ |
19
|
|
|
public function votes() |
20
|
|
|
{ |
21
|
|
|
return $this->morphMany(config('voteable.vote'), 'voteable'); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function votesCount() |
25
|
|
|
{ |
26
|
|
|
return $this->votes()->sum('amount'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
|
|
public function upVotesCount() |
33
|
|
|
{ |
34
|
|
|
return $this->votes()->where('amount', '>=', 0)->sum('amount'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function downVotesCount() |
41
|
|
|
{ |
42
|
|
|
return $this->votes()->where('amount', '<', 0)->sum('amount'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function isVoted() |
46
|
|
|
{ |
47
|
|
|
return $this->votes()->exists(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function isUpVoted() |
54
|
|
|
{ |
55
|
|
|
return $this->votes()->where('amount', '>=', 0)->exists(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function isDownVoted() |
62
|
|
|
{ |
63
|
|
|
return $this->votes()->where('amount', '<', 0)->exists(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isVotedByUser(int $user_id) |
67
|
|
|
{ |
68
|
|
|
return $this->votes()->where('user_id', $user_id)->exists(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $user_id |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function isUpVotedByUser(int $user_id) |
77
|
|
|
{ |
78
|
|
|
return $this->votes()->where('amount', '>=', 0)->where('user_id', $user_id)->exists(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $user_id |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isDownVotedByUser(int $user_id) |
87
|
|
|
{ |
88
|
|
|
return $this->votes()->where('amount', '<', 0)->where('user_id', $user_id)->exists(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Builder $query |
93
|
|
|
* @param string $direction |
94
|
|
|
* @param string $type |
|
|
|
|
95
|
|
|
* |
96
|
|
|
* @return Builder |
97
|
|
|
*/ |
98
|
|
|
public function scopeOrderByVotes(Builder $query, string $direction = 'asc') |
99
|
|
|
{ |
100
|
|
|
return $query |
101
|
|
|
->leftJoin('votes', function (JoinClause $join) { |
102
|
|
|
$join |
103
|
|
|
->on('votes.voteable_id', $this->getTable() . '.id') |
|
|
|
|
104
|
|
|
->where('votes.voteable_type', in_array(__CLASS__, Relation::morphMap()) ? array_search(__CLASS__, Relation::morphMap()) : __CLASS__); |
105
|
|
|
}) |
106
|
|
|
->addSelect(DB::raw('SUM(votes.value) as count_votes')) |
107
|
|
|
->groupBy($this->getTable(). '.id') |
|
|
|
|
108
|
|
|
->orderBy('count_votes', $direction); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Builder $query |
113
|
|
|
* @param string $direction |
114
|
|
|
* @param string $type |
115
|
|
|
* |
116
|
|
|
* @return Builder |
117
|
|
|
*/ |
118
|
|
|
public function scopeOrderByUpVotes(Builder $query, string $direction = 'asc', string $type = '>=') |
119
|
|
|
{ |
120
|
|
|
return $query |
121
|
|
|
->leftJoin('votes', function (JoinClause $join) { |
122
|
|
|
$join |
123
|
|
|
->on('votes.voteable_id', $this->getTable() . '.id') |
|
|
|
|
124
|
|
|
->where('votes.voteable_type', in_array(__CLASS__, Relation::morphMap()) ? array_search(__CLASS__, Relation::morphMap()) : __CLASS__); |
125
|
|
|
}) |
126
|
|
|
->where('amount', $type, 0) |
127
|
|
|
->addSelect(DB::raw('SUM(votes.value) as count_votes')) |
128
|
|
|
->groupBy($this->getTable(). '.id') |
|
|
|
|
129
|
|
|
->orderBy('count_votes', $direction); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param Builder $query |
134
|
|
|
* @param string $direction |
135
|
|
|
* |
136
|
|
|
* @return Builder |
137
|
|
|
*/ |
138
|
|
|
public function scopeOrderByDownVotes(Builder $query, string $direction = 'asc') |
139
|
|
|
{ |
140
|
|
|
return $this->scopeOrderByUpVotes($query, $direction, '<'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param int $vote_id |
145
|
|
|
* |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
|
public function cancelVote(int $vote_id) |
149
|
|
|
{ |
150
|
|
|
return $this->votes()->where('id', $vote_id)->delete(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
|
|
public function resetVotes() |
157
|
|
|
{ |
158
|
|
|
return $this->votes()->delete(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param int $user_id |
163
|
|
|
* |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
public function cancelVotesForUser(int $user_id) |
167
|
|
|
{ |
168
|
|
|
return $this->votes()->where('user_id', $user_id)->delete(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param int $user_id |
173
|
|
|
* @param int $amount |
174
|
|
|
* |
175
|
|
|
* @return int |
176
|
|
|
*/ |
177
|
|
|
public function updateVotesForUser(int $user_id, int $amount) |
178
|
|
|
{ |
179
|
|
|
return $this->votes()->where('user_id', $user_id)->update(['amount' => $amount]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param int $vote_id |
184
|
|
|
* @param int $amount |
185
|
|
|
* |
186
|
|
|
* @return int |
187
|
|
|
*/ |
188
|
|
|
public function updateVote(int $vote_id, int $amount) |
189
|
|
|
{ |
190
|
|
|
return $this->votes()->where('id', $vote_id)->update(['amount' => $amount]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return VoteBuilder |
195
|
|
|
* |
196
|
|
|
* @throws \Throwable |
197
|
|
|
*/ |
198
|
|
|
public function getVoteBuilder() |
199
|
|
|
{ |
200
|
|
|
return (new VoteBuilder()) |
201
|
|
|
->voteable($this); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return VoteQueryBuilder |
206
|
|
|
*/ |
207
|
|
|
public function getVoteQueryBuilder() |
208
|
|
|
{ |
209
|
|
|
return new VoteQueryBuilder($this->votes()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function voters() |
213
|
|
|
{ |
214
|
|
|
return $this->morphToMany(config('voteable.user'), 'voteable', 'votes'); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function countVotesByDate($from = null, $to = null) |
218
|
|
|
{ |
219
|
|
|
$query = $this->votes(); |
220
|
|
|
|
221
|
|
|
if (! empty($from) && empty($to)) { |
222
|
|
|
$query->where('created_at', '>=', date_transformer($from)); |
223
|
|
|
} elseif (empty($from) && ! empty($to)) { |
224
|
|
|
$query->where('created_at', '<=', date_transformer($to)); |
225
|
|
|
} elseif (! empty($from) && ! empty($to)) { |
226
|
|
|
$query->whereBetween('created_at', [date_transformer($from), date_transformer($to)]); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $query->sum('amount'); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.