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