PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.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 All 69 releases
fluent-support / app / Services / FluentCRMServices.php

FluentCRMServices.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Services/FluentCRMServices.php

147 lines 3.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\Services;
4
5 use Exception;
6
7 class FluentCRMServices
8 {
9 /**
10 * Set the FluentCRM contact's tags to exactly $data['tags'].
11 *
12 * @param array $data
13 * @return array
14 * @throws Exception
15 */
16 public function syncCrmTags ( $data )
17 {
18
19 $this->isCrmEnabled();
20 $this->isContactAvailable( $data['contact_id'] );
21
22 $tags = $data['tags'] ?? [];
23
24 $tagIds = array_filter( $tags, 'absint' );
25 $this->checkCrmPermission();
26
27 $contact = \FluentCrm\App\Models\Subscriber::findOrFail( $data['contact_id'] );
28
29 $this->addOrRemoveTag( $contact, $tagIds );
30
31 return [
32 'tags' => $contact->tags,
33 'message' => __('FluentCRM contact tags has been updated', 'fluent-support')
34 ];
35 }
36
37 /**
38 * Set the FluentCRM contact's lists to exactly $data['lists'].
39 *
40 * @param array $data
41 * @return array
42 * @throws Exception
43 */
44 public function syncCrmLists ( $data )
45 {
46 $this->isCrmEnabled();
47 $this->isContactAvailable( $data['contact_id'] );
48
49 $lists = $data['lists'] ?? [];
50
51 $listIds = array_filter( $lists, 'absint' );
52 $this->checkCrmPermission();
53
54 $contact = \FluentCrm\App\Models\Subscriber::findOrFail( $data['contact_id'] );
55
56 $this->addOrRemoveList( $contact, $listIds );
57
58 return [
59 'lists' => $contact->lists,
60 'message' => __('FluentCRM contact lists has been updated', 'fluent-support')
61 ];
62 }
63
64 /**
65 * @param \FluentCrm\App\Models\Subscriber $contact
66 * @param array $tagIds
67 * @return mixed
68 */
69 public function addOrRemoveTag ( $contact, $tagIds )
70 {
71 $existingTags = $contact->tags;
72 $existingTagIds = [];
73 foreach ($existingTags as $tag) {
74 $existingTagIds[] = $tag->id;
75 }
76 $newTagIds = array_diff($tagIds, $existingTagIds);
77 $removedTagIds = array_diff($existingTagIds, $tagIds);
78
79 if ($newTagIds) {
80 $contact->attachTags($newTagIds);
81 }
82
83 if ($removedTagIds) {
84 $contact->detachTags($removedTagIds);
85 }
86
87 return $contact;
88 }
89
90 /**
91 * @param \FluentCrm\App\Models\Subscriber $contact
92 * @param array $listIds
93 * @return mixed
94 */
95 public function addOrRemoveList ( $contact, $listIds )
96 {
97 $existingLists = $contact->lists;
98 $existingListIds = [];
99
100 foreach ($existingLists as $list) {
101 $existingListIds[] = $list->id;
102 }
103 $newListIds = array_diff($listIds, $existingListIds);
104 $removedListIds = array_diff($existingListIds, $listIds);
105
106 if ($newListIds) {
107 $contact->attachLists( $newListIds );
108 }
109
110 if ($removedListIds) {
111 $contact->detachLists( $removedListIds );
112 }
113
114 return $contact;
115 }
116
117 private function checkCrmPermission ()
118 {
119 $canAddTags = \FluentCrm\App\Services\PermissionManager::currentUserCan('fcrm_manage_contacts');
120 $canAddTags = apply_filters('fluent_support/can_user_add_tags_to_customer', $canAddTags);
121
122 if (!$canAddTags) {
123 throw new \Exception(esc_html__('Sorry you do not have permission to add contact tags', 'fluent-support'));
124 } else {
125 return true;
126 }
127 }
128
129 private function isCrmEnabled ()
130 {
131 if ( !defined('FLUENTCRM') ) {
132 throw new \Exception(esc_html__('FluentCRM is not installed or enabled', 'fluent-support'));
133 } else {
134 return true;
135 }
136 }
137
138 private function isContactAvailable ( $contactId )
139 {
140 if (!$contactId) {
141 throw new \Exception(esc_html__('Contact could not be found', 'fluent-support'));
142 } else {
143 return true;
144 }
145 }
146 }
147