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

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