PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.2
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.2
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.2, at app/Models/Customer.php

122 lines 3.2 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 maybeCreateCustomer($customerData)
38 {
39 $customer = self::getCustomerFromData($customerData);
40 $user = get_user_by('email', $customerData['email']);
41 if($user) {
42 if($user->first_name) {
43 $customerData['first_name'] = $user->first_name;
44 }
45 if($user->last_name) {
46 $customerData['last_name'] = $user->last_name;
47 }
48 if(empty($customerData['first_name']) && empty($customerData['last_name'])) {
49 $customerData['first_name'] = $user->display_name;
50 }
51 $customerData['user_id'] = $user->ID;
52 }
53
54 if(!$customer) {
55 if(!empty($customerData['last_ip_address'])) {
56 $customerData['ip_address'] = $customerData['last_ip_address'];
57 }
58 // we have to create customer
59 $customer = self::create($customerData);
60
61 do_action('fluent_support/customer_created', $customer);
62
63 } else {
64 if(!empty($customerData['user_id']) || !empty($customerData['remote_uid'])) {
65 $customerData = array_filter($customerData);
66 $customer->fill($customerData);
67 $customer->save();
68 }
69 }
70
71 return $customer;
72 }
73
74
75 /**
76 * One2Many: Customer has to many Click Tickets
77 * @return Model Collection
78 */
79 public function tickets()
80 {
81 $foreign_key = self::$type . '_id';
82
83 $class = __NAMESPACE__ . '\Ticket';
84
85 return $this->hasMany(
86 $class, $foreign_key, 'id'
87 );
88 }
89
90 public static function getCustomerFromData($customerData)
91 {
92 $remoteUid = Arr::get($customerData, 'remote_uid');
93 $email = $customerData['email'];
94
95 $customer = false;
96 if($remoteUid) {
97 $customer = self::where('remote_uid', $remoteUid)->first();
98 }
99
100 if(!$customer) {
101 if(!empty($customerData['user_id'])) {
102 $customer = self::where('user_id', $customerData['user_id'])->first();
103 }
104 if(!$customer) {
105 $customer = self::where('email', $email)->first();
106 }
107 }
108
109 return $customer;
110 }
111
112 public function getTicketCounts()
113 {
114 return Ticket::where('customer_id', $this->id)->count();
115 }
116
117 public function getResponseCounts()
118 {
119 return Conversation::where('person_id', $this->id)->count();
120 }
121 }
122