PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.93
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.93
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.93, at app/Models/XProfile.php

252 lines 5.8 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 FluentCrm\App\Models\Subscriber;
12
13 /**
14 * FluentCommunity XProfile Model
15 *
16 * Database Model
17 *
18 * @package FluentCommunity\App\Models
19 *
20 * @version 1.1.0
21 */
22 class XProfile extends Model
23 {
24 protected $table = 'fcom_xprofile';
25
26 protected $guarded = ['id'];
27
28 protected $primaryKey = 'user_id';
29
30 protected $casts = [
31 'user_id' => 'integer',
32 'total_points' => 'integer',
33 'is_verified' => 'integer',
34 ];
35
36 protected $fillable = [
37 'user_id',
38 'total_points',
39 'username',
40 'total_points',
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 $query->where(function ($query) use ($fields, $search) {
63 $query->where(array_shift($fields), 'LIKE', "%$search%");
64 foreach ($fields as $field) {
65 $query->orWhere($field, 'LIKE', "%$search%");
66 }
67 });
68 }
69
70 return $query;
71 }
72
73 public function scopeMentionBy($query, $search)
74 {
75 if ($search) {
76 $query->where(function ($query) use ($search) {
77 $query->where('display_name', 'LIKE', "$search%")
78 ->orWhere('username', 'LIKE', "$search%");
79 });
80 }
81
82 return $query;
83 }
84
85 public function user()
86 {
87 return $this->belongsTo(User::class, 'user_id', 'ID');
88 }
89
90 public function contact()
91 {
92 return $this->hasOne(Contact::class, 'user_id', 'user_id');
93 }
94
95 public function spaces()
96 {
97 return $this->belongsToMany(BaseSpace::class, 'fcom_space_user', 'user_id', 'space_id')
98 ->withPivot(['role', 'created_at']);
99 }
100
101 public function space_pivot()
102 {
103 return $this->belongsTo(SpaceUserPivot::class, 'user_id', 'user_id')->withoutGlobalScopes();
104 }
105
106 public function community_role()
107 {
108 return $this->belongsTo(Meta::class, 'user_id', 'object_id')
109 ->where('meta_key', '_user_community_roles');
110 }
111
112 /**
113 * Accessor to get dynamic photo attribute
114 * @return string
115 */
116 public function getAvatarAttribute()
117 {
118 if (!empty($this->attributes['avatar'])) {
119 return $this->attributes['avatar'];
120 }
121
122 return Utility::getFromCache('user_avatar_' . $this->user_id, function () {
123 return $this->user ? $this->user->photo : 'https://ui-avatars.com/api/?name=' . $this->display_name . '/128';
124 }, WEEK_IN_SECONDS);
125 }
126
127 public function getBadgeAttribute()
128 {
129 return apply_filters('fluent_community/xprofile/badge', null, $this);
130 }
131
132 public function getCrmContact()
133 {
134 if (!defined('FLUENTCRM')) {
135 return null;
136 }
137
138 if ($this->user_email) {
139 return Subscriber::where('user_id', $this->ID)
140 ->orWhere('email', $this->user_email)
141 ->first();
142 }
143
144 return Subscriber::where('user_id', $this->ID)
145 ->first();
146 }
147
148 public function getMetaAttribute($value)
149 {
150 $settings = maybe_unserialize($value);
151
152 if (!$settings) {
153 $settings = [
154 'cover_photo' => '',
155 'website' => ''
156 ];
157 }
158
159 return $settings;
160 }
161
162 public function setMetaAttribute($value)
163 {
164 if (!$value) {
165 $value = [
166 'cover_photo' => '',
167 'website' => ''
168 ];
169 }
170
171 $this->attributes['meta'] = maybe_serialize($value);
172 }
173
174 public function getFirstName()
175 {
176 if (!$this->display_name) {
177 return '';
178 }
179 $fullName = explode(' ', $this->display_name);
180 return $fullName[0];
181 }
182
183 public function getLastName()
184 {
185
186 if (!$this->display_name) {
187 return '';
188 }
189
190 $fullName = explode(' ', $this->display_name);
191
192 // remove the first name
193 array_shift($fullName);
194
195 if (count($fullName) == 0) {
196 return '';
197 }
198 return implode(' ', $fullName);
199 }
200
201 public function getCompletionScore()
202 {
203 $scores = [
204 'first_name' => 20,
205 'last_name' => 20,
206 'website' => 30,
207 'cover_photo' => 20,
208 'avatar' => 20,
209 'short_description' => 30,
210 'social_links' => 20
211 ];
212
213 $score = 0;
214
215 if ($this->getFirstName()) {
216 $score += $scores['first_name'];
217 }
218
219 if ($this->getLastName()) {
220 $score += $scores['last_name'];
221 }
222
223 $meta = $this->meta;
224 if (Arr::get($meta, 'website')) {
225 $score += $scores['website'];
226 }
227
228 if (Arr::get($meta, 'cover_photo')) {
229 $score += $scores['cover_photo'];
230 }
231
232 if ($this->short_description) {
233 $score += $scores['short_description'];
234 }
235
236 if ($score >= 100) {
237 return 100;
238 }
239
240 if (!empty($meta['social_links']) && array_filter(Arr::get($meta, 'social_links', []))) {
241 $score += $scores['social_links'];
242 }
243
244 if (!empty($this->attributes['avatar'])) {
245 $score += $scores['avatar'];
246 }
247
248 return $score > 100 ? 100 : $score;
249 }
250
251 }
252