PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.5
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.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 1.5.5 All 68 releases
fluent-support / app / Models / Person.php

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

221 lines 5.7 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\Framework\Database\Orm\ScopeInterface;
6
7 class Person extends Model
8 {
9 protected $table = 'fs_persons';
10
11 protected $appends = ['full_name', 'photo'];
12
13 /**
14 * The attributes that are mass assignable.
15 *
16 * @var array
17 */
18 protected $fillable = [
19 'first_name',
20 'last_name',
21 'email',
22 'title',
23 'person_type',
24 'user_id',
25 'remote_uid',
26 'hash',
27 'last_response_at',
28 'status',
29 'ip_address',
30 'last_ip_address',
31 'address_line_1',
32 'address_line_2',
33 'city',
34 'state',
35 'zip',
36 'country',
37 'note'
38 ];
39
40
41 /**
42 * $searchable Columns in table to search
43 * @var array
44 */
45 protected $searchable = [
46 'first_name',
47 'last_name',
48 'email'
49 ];
50
51 /**
52 * Local scope to filter subscribers by search/query string
53 * @param ModelQueryBuilder $query
54 * @param string $search
55 * @return ModelQueryBuilder
56 */
57 public function scopeSearchBy($query, $search)
58 {
59 if ($search) {
60 $fields = $this->searchable;
61 $query->where(function ($query) use ($fields, $search) {
62 $query->where(array_shift($fields), 'LIKE', "%$search%");
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 }
73
74 foreach ($fields as $field) {
75 $query->orWhere($field, 'LIKE', "$search%");
76 }
77 });
78 }
79
80 return $query;
81 }
82
83 /**
84 * Local scope to filter subscribers by search/query string
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 $email = '';
110 if (isset($this->attributes['email'])) {
111 $email = trim($this->attributes['email']);
112 }
113
114 $hash = md5(strtolower($email));
115
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
122 */
123 return apply_filters(
124 'fluent_support/get_avatar',
125 "https://www.gravatar.com/avatar/${hash}?s=128",
126 $email
127 );
128 }
129
130 /**
131 * Accessor to get dynamic full_name attribute
132 * @return string
133 */
134 public function getFullNameAttribute()
135 {
136 $fname = isset($this->attributes['first_name']) ? $this->attributes['first_name'] : '';
137 $lname = isset($this->attributes['last_name']) ? $this->attributes['last_name'] : '';
138 return trim("{$fname} {$lname}");
139 }
140
141 public static function explodeFullName($record)
142 {
143 if (!empty($record['first_name']) || !empty($record['last_name'])) {
144 return $record;
145 }
146 if (!empty($record['full_name'])) {
147 $fullNameArray = explode(' ', $record['full_name']);
148 $record['first_name'] = array_shift($fullNameArray);
149 if ($fullNameArray) {
150 $record['last_name'] = implode(' ', $fullNameArray);
151 }
152 unset($record['full_name']);
153 }
154
155 return $record;
156 }
157
158 public function getUserProfileEditUrl()
159 {
160 $userEditUrl = '';
161 if ($this->user_id) {
162 $userEditUrl = get_edit_user_link($this->user_id);
163 }
164
165 /*
166 * Filter person profile edit url
167 *
168 * @since v1.0.0
169 * @param string $userEditUrl User profile edit link
170 * @param object $this Model object
171 */
172 return apply_filters('fluent_support/person_user_edit_url', $userEditUrl, $this);
173 }
174
175 public function getMeta($metaKey, $default = '')
176 {
177 $meta = Meta::where('object_id', $this->id)
178 ->where('object_type', 'person_meta')
179 ->where('key', $metaKey)
180 ->first();
181 if ($meta) {
182 $value = maybe_unserialize($meta->value);
183 if ($value) {
184 return $value;
185 }
186 }
187
188 return $default;
189 }
190
191 public function updateMeta($metaKey, $metaValue)
192 {
193 $meta = Meta::where('object_id', $this->id)
194 ->where('object_type', 'person_meta')
195 ->where('key', $metaKey)
196 ->first();
197 if ($meta) {
198 $meta->value = maybe_serialize($metaValue);
199 $meta->update();
200 }
201
202 if (!$meta){
203 Meta::create([
204 'object_type' => 'person_meta',
205 'object_id' => $this->id,
206 'key' => $metaKey,
207 'value' => maybe_serialize($metaValue)
208 ]);
209 }
210
211 return true;
212 }
213
214 public function deleteAllMeta()
215 {
216 Meta::where('object_id', $this->id)
217 ->where('object_type', 'person_meta')
218 ->delete();
219 }
220 }
221