1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models\Presenters; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute; |
5
|
|
|
use Xetaravel\Models\Session; |
6
|
|
|
use Xetaravel\Utility\UserUtility; |
7
|
|
|
|
8
|
|
|
trait UserPresenter |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The default avatar used when there is no avatar for the user. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $defaultAvatar = '/images/avatar.png'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The default primary color used when there is no primary color defined. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $defaultAvatarPrimaryColor = '#B4AEA4'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the user's username. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\Database\Eloquent\Casts\Attribute |
28
|
|
|
*/ |
29
|
|
|
protected function username(): Attribute |
30
|
|
|
{ |
31
|
|
|
return Attribute::make( |
32
|
|
|
get: fn ($value) => $this->trashed() ? 'Deleted' : $value |
|
|
|
|
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the account first name. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getFirstNameAttribute(): string |
42
|
|
|
{ |
43
|
|
|
return $this->parse($this->account, 'first_name'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the account last name. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getLastNameAttribute(): string |
52
|
|
|
{ |
53
|
|
|
return $this->parse($this->account, 'last_name'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get whatever the user has rubies or not. |
58
|
|
|
* |
59
|
|
|
* @return boolean |
60
|
|
|
*/ |
61
|
|
|
public function getHasRubiesAttribute(): bool |
62
|
|
|
{ |
63
|
|
|
return $this->rubies_total > 0 ? true : false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the account full name. Return the username if the user |
68
|
|
|
* has not set his first name and last name. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getFullNameAttribute(): string |
73
|
|
|
{ |
74
|
|
|
if ($this->trashed()) { |
75
|
|
|
return $this->username; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$fullName = $this->parse($this->account, 'first_name') . ' ' . $this->parse($this->account, 'last_name'); |
79
|
|
|
|
80
|
|
|
if (empty(trim($fullName))) { |
81
|
|
|
return $this->username; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $fullName; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the experiences total formated. |
89
|
|
|
* |
90
|
|
|
* @return integer |
91
|
|
|
*/ |
92
|
|
|
public function getExperiencesTotalAttribute($experiencesTotal): int |
93
|
|
|
{ |
94
|
|
|
return number_format($experiencesTotal, 0, ".", " "); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the account facebook. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getFacebookAttribute(): string |
103
|
|
|
{ |
104
|
|
|
return $this->parse($this->account, 'facebook'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the account twitter. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getTwitterAttribute(): string |
113
|
|
|
{ |
114
|
|
|
return $this->parse($this->account, 'twitter'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the account biography. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getBiographyAttribute(): string |
123
|
|
|
{ |
124
|
|
|
return $this->parse($this->account, 'biography'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the account signature. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getSignatureAttribute(): string |
133
|
|
|
{ |
134
|
|
|
return $this->parse($this->account, 'signature'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the small avatar. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function getAvatarSmallAttribute(): string |
143
|
|
|
{ |
144
|
|
|
return $this->parseMedia('thumbnail.small'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the medium avatar. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getAvatarMediumAttribute(): string |
153
|
|
|
{ |
154
|
|
|
return $this->parseMedia('thumbnail.medium'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the big avatar. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function getAvatarBigAttribute(): string |
163
|
|
|
{ |
164
|
|
|
return $this->parseMedia('thumbnail.big'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the profile background. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getProfileBackgroundAttribute(): string |
173
|
|
|
{ |
174
|
|
|
return UserUtility::getProfileBackground(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the profile url. |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getProfileUrlAttribute(): string |
183
|
|
|
{ |
184
|
|
|
if (!isset($this->slug)) { |
185
|
|
|
return ''; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ($this->trashed()) { |
189
|
|
|
return route('users.user.show', ['slug' => strtolower($this->username)]); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return route('users.user.show', ['slug' => $this->slug]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get the primary color detected in the avatar. |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getAvatarPrimaryColorAttribute(): string |
201
|
|
|
{ |
202
|
|
|
if (isset($this->getMedia('avatar')[0]) && $this->getMedia('avatar')[0]->hasCustomProperty('primaryColor')) { |
|
|
|
|
203
|
|
|
return $this->getMedia('avatar')[0]->getCustomProperty('primaryColor'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this->defaultAvatarPrimaryColor; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* We must decrement the post count due to the first post being counted. |
211
|
|
|
* |
212
|
|
|
* @param int $count The actual post count cache. |
213
|
|
|
* |
214
|
|
|
* @return int |
215
|
|
|
*/ |
216
|
|
|
public function getDiscussPostCountAttribute($count): int |
217
|
|
|
{ |
218
|
|
|
return $count - $this->discuss_conversation_count; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the status of the user : online or offline |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function getOnlineAttribute(): string |
227
|
|
|
{ |
228
|
|
|
$online = Session::expires()->where('user_id', $this->id)->first(); |
229
|
|
|
|
230
|
|
|
return is_null($online) ? false : true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Parse a mdedia and return it if isset or return the default avatar. |
235
|
|
|
* |
236
|
|
|
* @param string $type The type of the media to get. |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
protected function parseMedia(string $type): string |
241
|
|
|
{ |
242
|
|
|
if (isset($this->getMedia('avatar')[0])) { |
243
|
|
|
return $this->getMedia('avatar')[0]->getUrl($type); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $this->defaultAvatar; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Parse an attribute and return its value or empty if null. |
251
|
|
|
* |
252
|
|
|
* @param Object|null $relation The relation or the user object. |
253
|
|
|
* Can be `$this` or `$this->account` for exemple |
254
|
|
|
* @param string|null $attribute The attribute to parse. |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
protected function parse($relation, $attribute): string |
259
|
|
|
{ |
260
|
|
|
if ($relation === null || $relation->{$attribute} === null) { |
261
|
|
|
return ''; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $relation->{$attribute}; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|