PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.96
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.96
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Models / XProfile.php

XProfile.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.96, at app/Models/XProfile.php

273 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Models;
4
5
6 use FluentCommunity\App\Functions\Utility;
7 use FluentCommunity\App\Models\Contact;
8 use FluentCommunity\App\Models\Feed;
9 use FluentCommunity\App\Models\User;
10 use FluentCommunity\Framework\Support\Arr;
11 use FluentCommunity\Modules\Course\Model\Course;
12 use FluentCrm\App\Models\Subscriber;
13
14 /**
15 * FluentCommunity XProfile Model
16 *
17 * Database Model
18 *
19 * @package FluentCommunity\App\Models
20 *
21 * @version 1.1.0
22 */
23 class XProfile extends Model
24 {
25 protected $table = 'fcom_xprofile';
26
27 protected $guarded = ['id'];
28
29 protected $primaryKey = 'user_id';
30
31 protected $casts = [
32 'user_id' => 'integer',
33 'total_points' => 'integer',
34 'is_verified' => 'integer',
35 ];
36
37 protected $fillable = [
38 'user_id',
39 'total_points',
40 'username',
41 'status',
42 'is_verified',
43 'display_name',
44 'avatar',
45 'short_description',
46 'last_activity',
47 'meta',
48 'created_at'
49 ];
50
51 protected $searchable = [
52 'display_name',
53 'username'
54 ];
55
56 protected $appends = ['badge'];
57
58 public function scopeSearchBy($query, $search)
59 {
60 if ($search) {
61 $fields = $this->searchable;
62
63 $query->where(function ($q) use ($fields, $search) {
64 $q->where(array_shift($fields), 'LIKE', "%$search%");
65 foreach ($fields as $field) {
66 $q->orWhere($field, 'LIKE', "%$search%");
67 }
68 });
69 }
70
71 return $query;
72 }
73
74 public function scopeMentionBy($query, $search)
75 {
76 if ($search) {
77 $query->where(function ($q) use ($search) {
78 $q->where('display_name', 'LIKE', "$search%")
79 ->orWhere('username', 'LIKE', "$search%");
80 });
81 }
82
83 return $query;
84 }
85
86 public function user()
87 {
88 return $this->belongsTo(User::class, 'user_id', 'ID');
89 }
90
91 public function contact()
92 {
93 return $this->hasOne(Contact::class, 'user_id', 'user_id');
94 }
95
96 public function spaces()
97 {
98 return $this->belongsToMany(BaseSpace::class, 'fcom_space_user', 'user_id', 'space_id')
99 ->withPivot(['role', 'status', 'created_at']);
100 }
101
102 public function posts()
103 {
104 return $this->hasMany(Feed::class, 'user_id', 'user_id');
105 }
106
107 public function courses()
108 {
109 return $this->belongsToMany(Course::class, 'fcom_space_user', 'user_id', 'space_id')
110 ->withPivot(['role', 'status', 'created_at']);
111 }
112
113 public function space_pivot()
114 {
115 return $this->belongsTo(SpaceUserPivot::class, 'user_id', 'user_id')->withoutGlobalScopes();
116 }
117
118 public function community_role()
119 {
120 return $this->belongsTo(Meta::class, 'user_id', 'object_id')
121 ->where('meta_key', '_user_community_roles');
122 }
123
124 /**
125 * Accessor to get dynamic photo attribute
126 * @return string
127 */
128 public function getAvatarAttribute()
129 {
130 if (!empty($this->attributes['avatar'])) {
131 return $this->attributes['avatar'];
132 }
133
134 $url = Utility::getFromCache('user_avatar_' . $this->user_id, function () {
135 return get_avatar_url($this->user_id, [
136 'size' => 128,
137 'default' => apply_filters('fluent_community/default_avatar', 'https://ui-avatars.com/api/?name=' . $this->display_name . '/128', $this)
138 ]);
139 }, WEEK_IN_SECONDS);
140
141 if (!$url) {
142 $url = apply_filters('fluent_community/default_avatar', FLUENT_COMMUNITY_PLUGIN_URL . 'assets/images/placeholder.png', $this);
143 }
144
145 return $url;
146 }
147
148 public function getBadgeAttribute()
149 {
150 return apply_filters('fluent_community/xprofile/badge', null, $this);
151 }
152
153 public function getCrmContact()
154 {
155 if (!defined('FLUENTCRM')) {
156 return null;
157 }
158
159 if ($this->user_email) {
160 return Subscriber::where('user_id', $this->ID)
161 ->orWhere('email', $this->user_email)
162 ->first();
163 }
164
165 return Subscriber::where('user_id', $this->ID)
166 ->first();
167 }
168
169 public function getMetaAttribute($value)
170 {
171 $settings = maybe_unserialize($value);
172
173 if (!$settings) {
174 $settings = [
175 'cover_photo' => '',
176 'website' => ''
177 ];
178 }
179
180 return $settings;
181 }
182
183 public function setMetaAttribute($value)
184 {
185 if (!$value) {
186 $value = [
187 'cover_photo' => '',
188 'website' => ''
189 ];
190 }
191
192 $this->attributes['meta'] = maybe_serialize($value);
193 }
194
195 public function getFirstName()
196 {
197 if (!$this->display_name) {
198 return '';
199 }
200 $fullName = explode(' ', $this->display_name);
201 return $fullName[0];
202 }
203
204 public function getLastName()
205 {
206
207 if (!$this->display_name) {
208 return '';
209 }
210
211 $fullName = explode(' ', $this->display_name);
212
213 // remove the first name
214 array_shift($fullName);
215
216 if (count($fullName) == 0) {
217 return '';
218 }
219 return implode(' ', $fullName);
220 }
221
222 public function getCompletionScore()
223 {
224 $scores = [
225 'first_name' => 20,
226 'last_name' => 20,
227 'website' => 30,
228 'cover_photo' => 20,
229 'avatar' => 20,
230 'short_description' => 30,
231 'social_links' => 20
232 ];
233
234 $score = 0;
235
236 if ($this->getFirstName()) {
237 $score += $scores['first_name'];
238 }
239
240 if ($this->getLastName()) {
241 $score += $scores['last_name'];
242 }
243
244 $meta = $this->meta;
245 if (Arr::get($meta, 'website')) {
246 $score += $scores['website'];
247 }
248
249 if (Arr::get($meta, 'cover_photo')) {
250 $score += $scores['cover_photo'];
251 }
252
253 if ($this->short_description) {
254 $score += $scores['short_description'];
255 }
256
257 if ($score >= 100) {
258 return 100;
259 }
260
261 if (!empty($meta['social_links']) && array_filter(Arr::get($meta, 'social_links', []))) {
262 $score += $scores['social_links'];
263 }
264
265 if (!empty($this->attributes['avatar'])) {
266 $score += $scores['avatar'];
267 }
268
269 return $score > 100 ? 100 : $score;
270 }
271
272 }
273