PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.1.2
Fluent Support – Helpdesk & Customer Support Ticket System v2.1.2
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
fluent-support / app / Models / Person.php

Person.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.1.2, at app/Models/Person.php

242 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Models;
4
5 use FluentSupport\App\Services\Helper;
6 use FluentSupport\Framework\Database\Orm\ScopeInterface;
7
8 class Person extends Model
9 {
10 protected $table = 'fs_persons';
11
12 protected $appends = ['full_name', 'photo'];
13
14 /**
15 * The attributes that are mass assignable.
16 *
17 * @var array
18 */
19 protected $fillable = [
20 'first_name',
21 'last_name',
22 'email',
23 'title',
24 'person_type',
25 'user_id',
26 'remote_uid',
27 'hash',
28 'last_response_at',
29 'status',
30 'ip_address',
31 'last_ip_address',
32 'address_line_1',
33 'address_line_2',
34 'city',
35 'state',
36 'zip',
37 'country',
38 'note'
39 ];
40
41
42 /**
43 * $searchable Columns in table to search
44 * @var array
45 */
46 protected $searchable = [
47 'first_name',
48 'last_name',
49 'email'
50 ];
51
52 /**
53 * Local scope to filter subscribers by search/query string
54 * @param ModelQueryBuilder $query
55 * @param string $search
56 * @return ModelQueryBuilder
57 */
58 public function scopeSearchBy($query, $search = '')
59 {
60 if (!$search) {
61 return $query;
62 }
63
64 $fields = $this->searchable;
65 $query->where(function ($query) use ($fields, $search) {
66 $query->where(array_shift($fields), 'LIKE', "%$search%");
67
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 }
77
78 foreach ($fields as $field) {
79 $query->orWhere($field, 'LIKE', "%$search%");
80 }
81 });
82
83 return $query;
84 }
85
86 /**
87 * Local scope to filter subscribers by search/query string
88 * @param ModelQueryBuilder $query
89 * @param array $statuses
90 * @return ModelQueryBuilder
91 */
92 public function scopeFilterByStatues($query, $statuses)
93 {
94 if ($statuses) {
95 $query->whereIn('status', $statuses);
96 }
97
98 return $query;
99 }
100
101
102 /**
103 * Accessor to get dynamic photo attribute
104 * @return string
105 */
106 public function getPhotoAttribute()
107 {
108 if (!empty($this->attributes['avatar'])) {
109 return $this->attributes['avatar'];
110 }
111
112 if (empty($this->attributes['email'])) {
113 return '';
114 }
115
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']
140 );
141 }
142
143 /**
144 * Accessor to get dynamic full_name attribute
145 * @return string
146 */
147 public function getFullNameAttribute()
148 {
149 $fname = isset($this->attributes['first_name']) ? $this->attributes['first_name'] : '';
150 $lname = isset($this->attributes['last_name']) ? $this->attributes['last_name'] : '';
151 return trim("{$fname} {$lname}");
152 }
153
154 public static function explodeFullName($record)
155 {
156 if (!empty($record['first_name']) || !empty($record['last_name'])) {
157 return $record;
158 }
159 if (!empty($record['full_name'])) {
160 $fullNameArray = explode(' ', $record['full_name']);
161 $record['first_name'] = array_shift($fullNameArray);
162 if ($fullNameArray) {
163 $record['last_name'] = implode(' ', $fullNameArray);
164 }
165 unset($record['full_name']);
166 }
167
168 return $record;
169 }
170
171 public function getUserProfileEditUrl()
172 {
173 $userEditUrl = '';
174 if ($this->user_id) {
175 $userEditUrl = get_edit_user_link($this->user_id);
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 */
185 return apply_filters('fluent_support/person_user_edit_url', $userEditUrl, $this);
186 }
187
188 public function getMeta($metaKey, $default = '')
189 {
190 $meta = Meta::where('object_id', $this->id)
191 ->where('object_type', 'person_meta')
192 ->where('key', $metaKey)
193 ->first();
194 if ($meta) {
195 $value = Helper::safeUnserialize($meta->value);
196 if ($value) {
197 return $value;
198 }
199 }
200
201 return $default;
202 }
203
204 public function updateMeta($metaKey, $metaValue)
205 {
206 $meta = Meta::where('object_id', $this->id)
207 ->where('object_type', 'person_meta')
208 ->where('key', $metaKey)
209 ->first();
210 if ($meta) {
211 $meta->value = maybe_serialize($metaValue);
212 $meta->save();
213 }
214
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
224 return true;
225 }
226
227 public function deleteAllMeta()
228 {
229 Meta::where('object_id', $this->id)
230 ->where('object_type', 'person_meta')
231 ->delete();
232 }
233
234 public function restoreAvatar()
235 {
236 $this->avatar = null;
237 $this->save();
238
239 return $this;
240 }
241 }
242