PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.3.2
Fluent Support – Helpdesk & Customer Support Ticket System v2.3.2
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 1.5.6 All 67 releases
fluent-support / app / Models / Customer.php

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

174 lines 4.8 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\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 * maybeCreateCustomer method will update existing customer or create new
63 * This method will get request to create customer, this will check existence, if exist it will update otherwise it will create new.
64 * @param $customerData
65 * @return false|Customer
66 */
67 public static function maybeCreateCustomer($customerData)
68 {
69 $customer = self::getCustomerFromData($customerData);
70 $user = get_user_by('email', $customerData['email']);
71 if ($user) {
72 if ($user->first_name) {
73 $customerData['first_name'] = $user->first_name;
74 }
75 if ($user->last_name) {
76 $customerData['last_name'] = $user->last_name;
77 }
78 if (empty($customerData['first_name']) && empty($customerData['last_name'])) {
79 $customerData['first_name'] = $user->display_name;
80 }
81 $customerData['user_id'] = $user->ID;
82 }
83
84 if (!$customer) {
85 if (!empty($customerData['last_ip_address'])) {
86 $customerData['ip_address'] = $customerData['last_ip_address'];
87 }
88
89 $customerData = self::explodeFullName($customerData);
90
91 // we have to create customer
92 $customer = self::create($customerData);
93
94 /*
95 * Action on customer create
96 *
97 * @since v1.0.0
98 * @param object $customer
99 */
100 do_action('fluent_support/customer_created', $customer);
101
102 } else {
103 if (!empty($customerData['user_id']) || !empty($customerData['remote_uid'])) {
104 $customerData = array_filter($customerData);
105 $customer->fill($customerData);
106 $customer->save();
107 }
108 }
109
110 return $customer;
111 }
112
113
114 /**
115 * One2Many: Customer has to many Click Tickets
116 * @return Model Collection
117 */
118 public function tickets()
119 {
120 $foreign_key = self::$type . '_id';
121
122 $class = __NAMESPACE__ . '\Ticket';
123
124 return $this->hasMany(
125 $class, $foreign_key, 'id'
126 );
127 }
128
129 /**
130 * getCustomerFromData method will return customer information by user id or email address
131 * @param $customerData
132 * @return false
133 */
134 public static function getCustomerFromData($customerData)
135 {
136 $remoteUid = Arr::get($customerData, 'remote_uid');
137 $email = $customerData['email'];
138
139 $customer = false;
140 if ($remoteUid) {
141 $customer = self::where('remote_uid', $remoteUid)->first();
142 }
143
144 if (!$customer) {
145 if (!empty($customerData['user_id'])) {
146 $customer = self::where('user_id', $customerData['user_id'])->first();
147 }
148 if (!$customer) {
149 $customer = self::where('email', $email)->first();
150 }
151 }
152
153 return $customer;
154 }
155
156 /**
157 * getTicketCounts method will return the number of tickets by a customer
158 * @return mixed
159 */
160 public function getTicketCounts()
161 {
162 return Ticket::where('customer_id', $this->id)->count();
163 }
164
165 /**
166 * getResponseCounts will return the number of responses by a customer
167 * @return mixed
168 */
169 public function getResponseCounts()
170 {
171 return Conversation::where('person_id', $this->id)->count();
172 }
173 }
174