PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 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 All 69 releases
fluent-support / app / Models / Person.php

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

239 lines 6.0 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 * @var array
44 */
45 protected $searchable = [
46 'first_name',
47 'last_name',
48 'email'
49 ];
50
51 /**
52 * @param ModelQueryBuilder $query
53 * @param string $search
54 * @return ModelQueryBuilder
55 */
56 public function scopeSearchBy($query, $search = '')
57 {
58 if (!$search) {
59 return $query;
60 }
61
62 $fields = $this->searchable;
63 $query->where(function ($query) use ($fields, $search) {
64 $query->where(array_shift($fields), 'LIKE', "%$search%");
65
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 }
75
76 foreach ($fields as $field) {
77 $query->orWhere($field, 'LIKE', "%$search%");
78 }
79 });
80
81 return $query;
82 }
83
84 /**
85 * @param ModelQueryBuilder $query
86 * @param array $statuses
87 * @return ModelQueryBuilder
88 */
89 public function scopeFilterByStatues($query, $statuses)
90 {
91 if ($statuses) {
92 $query->whereIn('status', $statuses);
93 }
94
95 return $query;
96 }
97
98
99 /**
100 * Accessor to get dynamic photo attribute
101 * @return string
102 */
103 public function getPhotoAttribute()
104 {
105 if (!empty($this->attributes['avatar'])) {
106 return $this->attributes['avatar'];
107 }
108
109 if (empty($this->attributes['email'])) {
110 return '';
111 }
112
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']
137 );
138 }
139
140 /**
141 * Accessor to get dynamic full_name attribute
142 * @return string
143 */
144 public function getFullNameAttribute()
145 {
146 $fname = isset($this->attributes['first_name']) ? $this->attributes['first_name'] : '';
147 $lname = isset($this->attributes['last_name']) ? $this->attributes['last_name'] : '';
148 return trim("{$fname} {$lname}");
149 }
150
151 public static function explodeFullName($record)
152 {
153 if (!empty($record['first_name']) || !empty($record['last_name'])) {
154 return $record;
155 }
156 if (!empty($record['full_name'])) {
157 $fullNameArray = explode(' ', $record['full_name']);
158 $record['first_name'] = array_shift($fullNameArray);
159 if ($fullNameArray) {
160 $record['last_name'] = implode(' ', $fullNameArray);
161 }
162 unset($record['full_name']);
163 }
164
165 return $record;
166 }
167
168 public function getUserProfileEditUrl()
169 {
170 $userEditUrl = '';
171 if ($this->user_id) {
172 $userEditUrl = get_edit_user_link($this->user_id);
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 */
182 return apply_filters('fluent_support/person_user_edit_url', $userEditUrl, $this);
183 }
184
185 public function getMeta($metaKey, $default = '')
186 {
187 $meta = Meta::where('object_id', $this->id)
188 ->where('object_type', 'person_meta')
189 ->where('key', $metaKey)
190 ->first();
191 if ($meta) {
192 $value = Helper::safeUnserialize($meta->value);
193 if ($value) {
194 return $value;
195 }
196 }
197
198 return $default;
199 }
200
201 public function updateMeta($metaKey, $metaValue)
202 {
203 $meta = Meta::where('object_id', $this->id)
204 ->where('object_type', 'person_meta')
205 ->where('key', $metaKey)
206 ->first();
207 if ($meta) {
208 $meta->value = maybe_serialize($metaValue);
209 $meta->save();
210 }
211
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
221 return true;
222 }
223
224 public function deleteAllMeta()
225 {
226 Meta::where('object_id', $this->id)
227 ->where('object_type', 'person_meta')
228 ->delete();
229 }
230
231 public function restoreAvatar()
232 {
233 $this->avatar = null;
234 $this->save();
235
236 return $this;
237 }
238 }
239