|
@@ -1,8 +1,9 @@ |
|
1
|
1
|
<?php |
|
2
|
2
|
|
|
3
|
3
|
namespace FluentSupport\App\Models; |
|
4
|
4
|
|
|
5
|
+use FluentSupport\App\Services\Helper; |
|
5
|
6
|
use FluentSupport\Framework\Database\Orm\ScopeInterface; |
|
6
|
7
|
|
|
7
|
8
|
class Person extends Model |
|
8
|
9
|
{ |
|
@@ -53,31 +54,33 @@ |
|
53
|
54
|
* @param ModelQueryBuilder $query |
|
54
|
55
|
* @param string $search |
|
55
|
56
|
* @return ModelQueryBuilder |
|
56
|
57
|
*/ |
|
57
|
|
- public function scopeSearchBy($query, $search) |
|
58
|
+ public function scopeSearchBy($query, $search = '') |
|
58
|
59
|
{ |
|
59
|
|
- if ($search) { |
|
60
|
|
- $fields = $this->searchable; |
|
61
|
|
- $query->where(function ($query) use ($fields, $search) { |
|
62
|
|
- $query->where(array_shift($fields), 'LIKE', "%$search%"); |
|
60
|
+ if (!$search) { |
|
61
|
+ return $query; |
|
62
|
+ } |
|
63
|
63
|
|
|
64
|
|
- $nameArray = explode(' ', $search); |
|
65
|
|
- if (count($nameArray) >= 2) { |
|
66
|
|
- $query->orWhere(function ($q) use ($nameArray) { |
|
67
|
|
- $fname = array_shift($nameArray); |
|
68
|
|
- $lastName = implode(' ', $nameArray); |
|
69
|
|
- $q->where('first_name', 'LIKE', "$fname%"); |
|
70
|
|
- $q->where('last_name', 'LIKE', "$lastName%"); |
|
71
|
|
- }); |
|
72
|
|
- } |
|
64
|
+ $fields = $this->searchable; |
|
65
|
+ $query->where(function ($query) use ($fields, $search) { |
|
66
|
+ $query->where(array_shift($fields), 'LIKE', "%$search%"); |
|
73
|
67
|
|
|
74
|
|
- foreach ($fields as $field) { |
|
75
|
|
- $query->orWhere($field, 'LIKE', "$search%"); |
|
76
|
|
- } |
|
77
|
|
- }); |
|
78
|
|
- } |
|
68
|
+ $nameArray = explode(' ', (string) $search); |
|
69
|
+ if (count($nameArray) >= 2) { |
|
70
|
+ $query->orWhere(function ($q) use ($nameArray) { |
|
71
|
+ $fname = array_shift($nameArray); |
|
72
|
+ $lastName = implode(' ', $nameArray); |
|
73
|
+ $q->where('first_name', 'LIKE', "%$fname%"); |
|
74
|
+ $q->where('last_name', 'LIKE', "%$lastName%"); |
|
75
|
+ }); |
|
76
|
+ } |
|
79
|
77
|
|
|
78
|
+ foreach ($fields as $field) { |
|
79
|
+ $query->orWhere($field, 'LIKE', "%$search%"); |
|
80
|
+ } |
|
81
|
+ }); |
|
82
|
+ |
|
80
|
83
|
return $query; |
|
81
|
84
|
} |
|
82
|
85
|
|
|
83
|
86
|
/** |
|
@@ -105,18 +108,36 @@ |
|
105
|
108
|
if (!empty($this->attributes['avatar'])) { |
|
106
|
109
|
return $this->attributes['avatar']; |
|
107
|
110
|
} |
|
108
|
111
|
|
|
109
|
|
- $email = ''; |
|
110
|
|
- if (isset($this->attributes['email'])) { |
|
111
|
|
- $email = trim($this->attributes['email']); |
|
112
|
+ if (empty($this->attributes['email'])) { |
|
113
|
+ return ''; |
|
112
|
114
|
} |
|
113
|
115
|
|
|
114
|
|
- $hash = md5(strtolower($email)); |
|
115
|
|
- return apply_filters( |
|
116
|
|
- 'fluent_support/get_avatar', |
|
117
|
|
- "https://www.gravatar.com/avatar/${hash}?s=128", |
|
118
|
|
- $email |
|
116
|
+ $fallBackName = ''; |
|
117
|
+ if (isset($this->attributes['first_name'])) { |
|
118
|
+ $fallBackName = $this->attributes['first_name']; |
|
119
|
+ } |
|
120
|
+ |
|
121
|
+ if (isset($this->attributes['last_name'])) { |
|
122
|
+ $fallBackName .= '+' . $this->attributes['last_name']; |
|
123
|
+ } |
|
124
|
+ |
|
125
|
+ $hash = md5(strtolower(trim($this->attributes['email'] ?? ''))); |
|
126
|
+ |
|
127
|
+ $fallback = ''; |
|
128
|
+ if ($fallBackName) { |
|
129
|
+ $fallback = '&d=https%3A%2F%2Fui-avatars.com%2Fapi%2F' . urlencode($fallBackName) . '/128'; |
|
130
|
+ } |
|
131
|
+ |
|
132
|
+ /** |
|
133
|
+ * Gravatar URL by Email |
|
134
|
+ * @param string $gravatar Gravatar URL |
|
135
|
+ * @param string $email Email address |
|
136
|
+ */ |
|
137
|
+ return apply_filters('fluent_support/get_avatar', |
|
138
|
+ "https://www.gravatar.com/avatar/{$hash}?s=128" . $fallback, |
|
139
|
+ $this->attributes['email'] |
|
119
|
140
|
); |
|
120
|
141
|
} |
|
121
|
142
|
|
|
122
|
143
|
/** |
|
@@ -150,10 +171,18 @@ |
|
150
|
171
|
public function getUserProfileEditUrl() |
|
151
|
172
|
{ |
|
152
|
173
|
$userEditUrl = ''; |
|
153
|
174
|
if ($this->user_id) { |
|
154
|
|
- $userEditUrl = get_edit_profile_url($this->user_id); |
|
175
|
+ $userEditUrl = get_edit_user_link($this->user_id); |
|
155
|
176
|
} |
|
177
|
+ |
|
178
|
+ /* |
|
179
|
+ * Filter person profile edit url |
|
180
|
+ * |
|
181
|
+ * @since v1.0.0 |
|
182
|
+ * @param string $userEditUrl User profile edit link |
|
183
|
+ * @param object $this Model object |
|
184
|
+ */ |
|
156
|
185
|
return apply_filters('fluent_support/person_user_edit_url', $userEditUrl, $this); |
|
157
|
186
|
} |
|
158
|
187
|
|
|
159
|
188
|
public function getMeta($metaKey, $default = '') |
|
@@ -162,9 +191,9 @@ |
|
162
|
191
|
->where('object_type', 'person_meta') |
|
163
|
192
|
->where('key', $metaKey) |
|
164
|
193
|
->first(); |
|
165
|
194
|
if ($meta) { |
|
166
|
|
- $value = maybe_unserialize($meta->value); |
|
195
|
+ $value = Helper::safeUnserialize($meta->value); |
|
167
|
196
|
if ($value) { |
|
168
|
197
|
return $value; |
|
169
|
198
|
} |
|
170
|
199
|
} |
|
@@ -182,14 +211,17 @@ |
|
182
|
211
|
$meta->value = maybe_serialize($metaValue); |
|
183
|
212
|
$meta->save(); |
|
184
|
213
|
} |
|
185
|
214
|
|
|
186
|
|
- Meta::create([ |
|
187
|
|
- 'object_type' => 'person_meta', |
|
188
|
|
- 'object_id' => $this->id, |
|
189
|
|
- 'key' => $metaKey, |
|
190
|
|
- 'value' => maybe_serialize($metaValue) |
|
191
|
|
- ]); |
|
215
|
+ if (!$meta) { |
|
216
|
+ Meta::create([ |
|
217
|
+ 'object_type' => 'person_meta', |
|
218
|
+ 'object_id' => $this->id, |
|
219
|
+ 'key' => $metaKey, |
|
220
|
+ 'value' => maybe_serialize($metaValue) |
|
221
|
+ ]); |
|
222
|
+ } |
|
223
|
+ |
|
192
|
224
|
return true; |
|
193
|
225
|
} |
|
194
|
226
|
|
|
195
|
227
|
public function deleteAllMeta() |
|
@@ -196,6 +228,14 @@ |
|
196
|
228
|
{ |
|
197
|
229
|
Meta::where('object_id', $this->id) |
|
198
|
230
|
->where('object_type', 'person_meta') |
|
199
|
231
|
->delete(); |
|
232
|
+ } |
|
233
|
+ |
|
234
|
+ public function restoreAvatar() |
|
235
|
+ { |
|
236
|
+ $this->avatar = null; |
|
237
|
+ $this->save(); |
|
238
|
+ |
|
239
|
+ return $this; |
|
200
|
240
|
} |
|
201
|
241
|
} |