| 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 |
return apply_filters( |
| 116 |
'fluent_support/get_avatar', |
| 117 |
"https://www.gravatar.com/avatar/${hash}?s=128", |
| 118 |
$email |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Accessor to get dynamic full_name attribute |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
public function getFullNameAttribute() |
| 127 |
{ |
| 128 |
$fname = isset($this->attributes['first_name']) ? $this->attributes['first_name'] : ''; |
| 129 |
$lname = isset($this->attributes['last_name']) ? $this->attributes['last_name'] : ''; |
| 130 |
return trim("{$fname} {$lname}"); |
| 131 |
} |
| 132 |
|
| 133 |
public static function explodeFullName($record) |
| 134 |
{ |
| 135 |
if (!empty($record['first_name']) || !empty($record['last_name'])) { |
| 136 |
return $record; |
| 137 |
} |
| 138 |
if (!empty($record['full_name'])) { |
| 139 |
$fullNameArray = explode(' ', $record['full_name']); |
| 140 |
$record['first_name'] = array_shift($fullNameArray); |
| 141 |
if ($fullNameArray) { |
| 142 |
$record['last_name'] = implode(' ', $fullNameArray); |
| 143 |
} |
| 144 |
unset($record['full_name']); |
| 145 |
} |
| 146 |
|
| 147 |
return $record; |
| 148 |
} |
| 149 |
|
| 150 |
public function getUserProfileEditUrl() |
| 151 |
{ |
| 152 |
$userEditUrl = ''; |
| 153 |
if ($this->user_id) { |
| 154 |
$userEditUrl = get_edit_profile_url($this->user_id); |
| 155 |
} |
| 156 |
return apply_filters('fluent_support/person_user_edit_url', $userEditUrl, $this); |
| 157 |
} |
| 158 |
|
| 159 |
public function getMeta($metaKey, $default = '') |
| 160 |
{ |
| 161 |
$meta = Meta::where('object_id', $this->id) |
| 162 |
->where('object_type', 'person_meta') |
| 163 |
->where('key', $metaKey) |
| 164 |
->first(); |
| 165 |
if ($meta) { |
| 166 |
$value = maybe_unserialize($meta->value); |
| 167 |
if ($value) { |
| 168 |
return $value; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $default; |
| 173 |
} |
| 174 |
|
| 175 |
public function updateMeta($metaKey, $metaValue) |
| 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 |
$meta->value = maybe_serialize($metaValue); |
| 183 |
$meta->save(); |
| 184 |
} |
| 185 |
|
| 186 |
Meta::create([ |
| 187 |
'object_type' => 'person_meta', |
| 188 |
'object_id' => $this->id, |
| 189 |
'key' => $metaKey, |
| 190 |
'value' => maybe_serialize($metaValue) |
| 191 |
]); |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
public function deleteAllMeta() |
| 196 |
{ |
| 197 |
Meta::where('object_id', $this->id) |
| 198 |
->where('object_type', 'person_meta') |
| 199 |
->delete(); |
| 200 |
} |
| 201 |
} |
| 202 |
|