| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use FluentSupport\Framework\Database\Orm\Builder; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
use FluentSupport\App\Models\Traits\CustomerTrait; |
| 8 |
|
| 9 |
class Customer extends Person |
| 10 |
{ |
| 11 |
use CustomerTrait; |
| 12 |
|
| 13 |
protected static $type = 'customer'; |
| 14 |
|
| 15 |
protected $searchable = [ |
| 16 |
'id', |
| 17 |
'first_name', |
| 18 |
'last_name', |
| 19 |
'email', |
| 20 |
'address_line_1', |
| 21 |
'address_line_2', |
| 22 |
'country' |
| 23 |
]; |
| 24 |
|
| 25 |
/** |
| 26 |
* @return array |
| 27 |
*/ |
| 28 |
public function getSearchableFields() |
| 29 |
{ |
| 30 |
return $this->searchable; |
| 31 |
} |
| 32 |
|
| 33 |
public static function boot() |
| 34 |
{ |
| 35 |
parent::boot(); |
| 36 |
|
| 37 |
static::creating(function ($model) { |
| 38 |
$model->person_type = static::$type; |
| 39 |
$model->hash = md5(time() . wp_generate_uuid4()); |
| 40 |
}); |
| 41 |
|
| 42 |
static::addGlobalScope(function (Builder $builder) { |
| 43 |
$builder->where('person_type', 'customer'); |
| 44 |
}); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
public static function mappables() |
| 49 |
{ |
| 50 |
return [ |
| 51 |
'title' => __('Customer Title', 'fluent-support'), |
| 52 |
'address_line_1' => __('Address Line 1', 'fluent-support'), |
| 53 |
'address_line_2' => __('Address Line 2', 'fluent-support'), |
| 54 |
'city' => __('City', 'fluent-support'), |
| 55 |
'state' => __('State', 'fluent-support'), |
| 56 |
'zip' => __('Zip Code', 'fluent-support'), |
| 57 |
'country' => __('Country', 'fluent-support'), |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Find the customer for this data or create one. An existing record is only |
| 63 |
* updated when the data carries a user_id or remote_uid. |
| 64 |
* |
| 65 |
* @param array $customerData |
| 66 |
* @return false|Customer |
| 67 |
*/ |
| 68 |
public static function maybeCreateCustomer($customerData) |
| 69 |
{ |
| 70 |
$customer = self::getCustomerFromData($customerData); |
| 71 |
$email = Arr::get($customerData, 'email'); |
| 72 |
$user = $email ? get_user_by('email', $email) : false; |
| 73 |
if ($user) { |
| 74 |
if ($user->first_name) { |
| 75 |
$customerData['first_name'] = $user->first_name; |
| 76 |
} |
| 77 |
if ($user->last_name) { |
| 78 |
$customerData['last_name'] = $user->last_name; |
| 79 |
} |
| 80 |
if (empty($customerData['first_name']) && empty($customerData['last_name'])) { |
| 81 |
$customerData['first_name'] = $user->display_name; |
| 82 |
} |
| 83 |
$customerData['user_id'] = $user->ID; |
| 84 |
} |
| 85 |
|
| 86 |
if (!$customer) { |
| 87 |
if (!empty($customerData['last_ip_address'])) { |
| 88 |
$customerData['ip_address'] = $customerData['last_ip_address']; |
| 89 |
} |
| 90 |
|
| 91 |
$customerData = self::explodeFullName($customerData); |
| 92 |
|
| 93 |
$customer = self::create($customerData); |
| 94 |
|
| 95 |
if ($customer && $customer->exists) { |
| 96 |
/* |
| 97 |
* Action on customer create |
| 98 |
* |
| 99 |
* @since v1.0.0 |
| 100 |
* @param object $customer |
| 101 |
*/ |
| 102 |
do_action('fluent_support/customer_created', $customer); |
| 103 |
} |
| 104 |
|
| 105 |
return $customer; |
| 106 |
} |
| 107 |
|
| 108 |
// Never link an existing row to a different WP account by email match. |
| 109 |
// Linking happens on create, on registration, or by an agent. |
| 110 |
$incomingUserId = (int) Arr::get($customerData, 'user_id'); |
| 111 |
|
| 112 |
if ($incomingUserId && $incomingUserId !== (int) $customer->user_id) { |
| 113 |
unset($customerData['user_id']); |
| 114 |
} |
| 115 |
|
| 116 |
// The incoming email is unverified (WP lets users change theirs without |
| 117 |
// confirmation), so it must not overwrite a linked customer's address. |
| 118 |
// That only changes via EmailClaimService or an agent edit. |
| 119 |
if ((int) $customer->user_id) { |
| 120 |
unset($customerData['email']); |
| 121 |
} |
| 122 |
|
| 123 |
if (!empty($customerData['user_id']) || !empty($customerData['remote_uid'])) { |
| 124 |
$customerData = array_filter($customerData); |
| 125 |
$customer->fill($customerData); |
| 126 |
$customer->save(); |
| 127 |
} |
| 128 |
|
| 129 |
return $customer; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* @return Model Collection |
| 135 |
*/ |
| 136 |
public function tickets() |
| 137 |
{ |
| 138 |
$foreign_key = self::$type . '_id'; |
| 139 |
|
| 140 |
$class = __NAMESPACE__ . '\Ticket'; |
| 141 |
|
| 142 |
return $this->hasMany( |
| 143 |
$class, $foreign_key, 'id' |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Look up by remote_uid, then user_id, then email. |
| 149 |
* |
| 150 |
* @param array $customerData |
| 151 |
* @return false|Customer |
| 152 |
*/ |
| 153 |
public static function getCustomerFromData($customerData) |
| 154 |
{ |
| 155 |
$remoteUid = Arr::get($customerData, 'remote_uid'); |
| 156 |
$email = Arr::get($customerData, 'email'); |
| 157 |
$userId = (int) Arr::get($customerData, 'user_id'); |
| 158 |
|
| 159 |
if ($remoteUid) { |
| 160 |
$customer = self::where('remote_uid', $remoteUid)->first(); |
| 161 |
|
| 162 |
if ($customer) { |
| 163 |
return $customer; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// When a user id is given, don't fall back to email: a WP account's email |
| 168 |
// is unverified (the REST users endpoint skips confirmation), so it must |
| 169 |
// not reach another person's row. Email isn't unique, so take the oldest |
| 170 |
// match to keep replies threading onto the original record. |
| 171 |
if ($userId) { |
| 172 |
return self::where('user_id', $userId)->orderBy('id', 'ASC')->first() ?: false; |
| 173 |
} |
| 174 |
|
| 175 |
if ($email) { |
| 176 |
return self::where('email', $email)->orderBy('id', 'ASC')->first() ?: false; |
| 177 |
} |
| 178 |
|
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @return mixed |
| 184 |
*/ |
| 185 |
public function getTicketCounts() |
| 186 |
{ |
| 187 |
return Ticket::where('customer_id', $this->id)->count(); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @return mixed |
| 192 |
*/ |
| 193 |
public function getResponseCounts() |
| 194 |
{ |
| 195 |
return Conversation::where('person_id', $this->id)->count(); |
| 196 |
} |
| 197 |
} |
| 198 |
|