PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.0
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.0
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
← All changes | app/Models/Person.php +57 -36 1.5.52.4.0 View file →
@@ -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,26 +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));
116 + $fallBackName = '';
117 + if (isset($this->attributes['first_name'])) {
118 + $fallBackName = $this->attributes['first_name'];
119 + }
115 120
116 - /*
117 - * Filter person profile avatar, by default it use gravatar profile picture
118 - *
119 - * @since v1.0.0
120 - * @param string $url link to the profile picture
121 - * @pram string $email user gravatar email address
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
122 136 */
123 - return apply_filters(
124 - 'fluent_support/get_avatar',
125 - "https://www.gravatar.com/avatar/${hash}?s=128",
126 - $email
137 + return apply_filters('fluent_support/get_avatar',
138 + "https://www.gravatar.com/avatar/{$hash}?s=128" . $fallback,
139 + $this->attributes['email']
127 140 );
128 141 }
129 142
130 143 /**
@@ -178,9 +191,9 @@
178 191 ->where('object_type', 'person_meta')
179 192 ->where('key', $metaKey)
180 193 ->first();
181 194 if ($meta) {
182 - $value = maybe_unserialize($meta->value);
195 + $value = Helper::safeUnserialize($meta->value);
183 196 if ($value) {
184 197 return $value;
185 198 }
186 199 }
@@ -195,12 +208,12 @@
195 208 ->where('key', $metaKey)
196 209 ->first();
197 210 if ($meta) {
198 211 $meta->value = maybe_serialize($metaValue);
199 - $meta->update();
212 + $meta->save();
200 213 }
201 214
202 - if (!$meta){
215 + if (!$meta) {
203 216 Meta::create([
204 217 'object_type' => 'person_meta',
205 218 'object_id' => $this->id,
206 219 'key' => $metaKey,
@@ -215,6 +228,14 @@
215 228 {
216 229 Meta::where('object_id', $this->id)
217 230 ->where('object_type', 'person_meta')
218 231 ->delete();
232 + }
233 +
234 + public function restoreAvatar()
235 + {
236 + $this->avatar = null;
237 + $this->save();
238 +
239 + return $this;
219 240 }
220 241 }