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