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 / Customer.php

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

160 lines 4.5 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
8 class Customer extends Person
9 {
10 protected static $type = 'customer';
11
12 protected $searchable = [
13 'id',
14 'first_name',
15 'last_name',
16 'email',
17 'address_line_1',
18 'address_line_2',
19 'country'
20 ];
21
22 public static function boot()
23 {
24 parent::boot();
25
26 static::creating(function ($model) {
27 $model->person_type = static::$type;
28 $model->hash = md5(time().wp_generate_uuid4());
29 });
30
31 static::addGlobalScope(function (Builder $builder) {
32 $builder->where('person_type', 'customer');
33 });
34
35 }
36
37 public static function mappables()
38 {
39 return [
40 'title' => __('Customer Title', 'fluent-support'),
41 'address_line_1' => __('Address Line 1', 'fluent-support'),
42 'address_line_2' => __('Address Line 2', 'fluent-support'),
43 'city' => __('City', 'fluent-support'),
44 'state' => __('State', 'fluent-support'),
45 'zip' => __('Zip Code', 'fluent-support'),
46 'country' => __('Country', 'fluent-support'),
47 ];
48 }
49
50 /**
51 * maybeCreateCustomer method will update existing customer or create new
52 * This method will get request to create customer, this will check existence, if exist it will update otherwise it will create new.
53 * @param $customerData
54 * @return false|Customer
55 */
56 public static function maybeCreateCustomer($customerData)
57 {
58 $customer = self::getCustomerFromData($customerData);
59 $user = get_user_by('email', $customerData['email']);
60 if($user) {
61 if($user->first_name) {
62 $customerData['first_name'] = $user->first_name;
63 }
64 if($user->last_name) {
65 $customerData['last_name'] = $user->last_name;
66 }
67 if(empty($customerData['first_name']) && empty($customerData['last_name'])) {
68 $customerData['first_name'] = $user->display_name;
69 }
70 $customerData['user_id'] = $user->ID;
71 }
72
73 if(!$customer) {
74 if(!empty($customerData['last_ip_address'])) {
75 $customerData['ip_address'] = $customerData['last_ip_address'];
76 }
77 // we have to create customer
78 $customer = self::create($customerData);
79
80 /*
81 * Action on customer create
82 *
83 * @since v1.0.0
84 * @param object $customer
85 */
86 do_action('fluent_support/customer_created', $customer);
87
88 } else {
89 if(!empty($customerData['user_id']) || !empty($customerData['remote_uid'])) {
90 $customerData = array_filter($customerData);
91 $customer->fill($customerData);
92 $customer->save();
93 }
94 }
95
96 return $customer;
97 }
98
99
100 /**
101 * One2Many: Customer has to many Click Tickets
102 * @return Model Collection
103 */
104 public function tickets()
105 {
106 $foreign_key = self::$type . '_id';
107
108 $class = __NAMESPACE__ . '\Ticket';
109
110 return $this->hasMany(
111 $class, $foreign_key, 'id'
112 );
113 }
114
115 /**
116 * getCustomerFromData method will return customer information by user id or email address
117 * @param $customerData
118 * @return false
119 */
120 public static function getCustomerFromData($customerData)
121 {
122 $remoteUid = Arr::get($customerData, 'remote_uid');
123 $email = $customerData['email'];
124
125 $customer = false;
126 if($remoteUid) {
127 $customer = self::where('remote_uid', $remoteUid)->first();
128 }
129
130 if(!$customer) {
131 if(!empty($customerData['user_id'])) {
132 $customer = self::where('user_id', $customerData['user_id'])->first();
133 }
134 if(!$customer) {
135 $customer = self::where('email', $email)->first();
136 }
137 }
138
139 return $customer;
140 }
141
142 /**
143 * getTicketCounts method will return the number of tickets by a customer
144 * @return mixed
145 */
146 public function getTicketCounts()
147 {
148 return Ticket::where('customer_id', $this->id)->count();
149 }
150
151 /**
152 * getResponseCounts will return the number of responses by a customer
153 * @return mixed
154 */
155 public function getResponseCounts()
156 {
157 return Conversation::where('person_id', $this->id)->count();
158 }
159 }
160