| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
class FluentCRMServices |
| 8 |
{ |
| 9 |
/** |
| 10 |
* This `syncCrmTags` method is used to sync tags from FluentCRM to Fluent Support. |
| 11 |
* @param array $data |
| 12 |
* @return array |
| 13 |
* @throws Exception |
| 14 |
*/ |
| 15 |
public function syncCrmTags ( $data ) |
| 16 |
{ |
| 17 |
|
| 18 |
$this->isCrmEnabled(); |
| 19 |
$this->isContactAvailable( $data['contact_id'] ); |
| 20 |
|
| 21 |
$tags = $data['tags'] ?? []; |
| 22 |
|
| 23 |
$tagIds = array_filter( $tags, 'absint' ); |
| 24 |
$this->checkCrmPermission(); |
| 25 |
|
| 26 |
$contact = \FluentCrm\App\Models\Subscriber::findOrFail( $data['contact_id'] ); |
| 27 |
|
| 28 |
$this->addOrRemoveTag( $contact, $tagIds ); |
| 29 |
|
| 30 |
return [ |
| 31 |
'tags' => $contact->tags, |
| 32 |
'message' => __('FluentCRM contact tags has been updated', 'fluent-support') |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* This `syncCrmLists` method is used to sync lists from FluentCRM to Fluent Support. |
| 38 |
* @param array $data |
| 39 |
* @return array |
| 40 |
* @throws Exception |
| 41 |
*/ |
| 42 |
public function syncCrmLists ( $data ) |
| 43 |
{ |
| 44 |
$this->isCrmEnabled(); |
| 45 |
$this->isContactAvailable( $data['contact_id'] ); |
| 46 |
|
| 47 |
$lists = $data['lists'] ?? []; |
| 48 |
|
| 49 |
$listIds = array_filter( $lists, 'absint' ); |
| 50 |
$this->checkCrmPermission(); |
| 51 |
|
| 52 |
$contact = \FluentCrm\App\Models\Subscriber::findOrFail( $data['contact_id'] ); |
| 53 |
|
| 54 |
$this->addOrRemoveList( $contact, $listIds ); |
| 55 |
|
| 56 |
return [ |
| 57 |
'lists' => $contact->lists, |
| 58 |
'message' => __('FluentCRM contact lists has been updated', 'fluent-support') |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* This `addOrRemoveTag` method will add or remove tags from FluentCRM contact from Fluent Support. |
| 64 |
* @param array $contact |
| 65 |
* @param array $tagIds |
| 66 |
* @return mixed |
| 67 |
*/ |
| 68 |
public function addOrRemoveTag ( $contact, $tagIds ) |
| 69 |
{ |
| 70 |
$existingTags = $contact->tags; |
| 71 |
$existingTagIds = []; |
| 72 |
foreach ($existingTags as $tag) { |
| 73 |
$existingTagIds[] = $tag->id; |
| 74 |
} |
| 75 |
$newTagIds = array_diff($tagIds, $existingTagIds); |
| 76 |
$removedTagIds = array_diff($existingTagIds, $tagIds); |
| 77 |
|
| 78 |
if ($newTagIds) { |
| 79 |
$contact->attachTags($newTagIds); |
| 80 |
} |
| 81 |
|
| 82 |
if ($removedTagIds) { |
| 83 |
$contact->detachTags($removedTagIds); |
| 84 |
} |
| 85 |
|
| 86 |
return $contact; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* This `addOrRemoveList` method will add or remove lists from FluentCRM contact from Fluent Support. |
| 91 |
* @param array $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 |
// This `checkCrmPermission` method will check current agent has permission to access to modify customer's FluentCRM data |
| 118 |
private function checkCrmPermission () |
| 119 |
{ |
| 120 |
$canAddTags = \FluentCrm\App\Services\PermissionManager::currentUserCan('fcrm_manage_contacts'); |
| 121 |
$canAddTags = apply_filters('fluent_support/can_user_add_tags_to_customer', $canAddTags); |
| 122 |
|
| 123 |
if (!$canAddTags) { |
| 124 |
throw new \Exception( 'Sorry you do not have permission to add contact tags' ); |
| 125 |
} else { |
| 126 |
return true; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// This `isCrmEnabled` method will check FluentCRM is enabled or not |
| 131 |
private function isCrmEnabled () |
| 132 |
{ |
| 133 |
if ( !defined('FLUENTCRM') ) { |
| 134 |
throw new \Exception('FluentCRM is not installed or Enabled'); |
| 135 |
} else { |
| 136 |
return true; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// This `isContactAvailable` method will check if contact is valid or not |
| 141 |
private function isContactAvailable ( $contactId ) |
| 142 |
{ |
| 143 |
if (!$contactId) { |
| 144 |
throw new \Exception('Contact could not be found'); |
| 145 |
} else { |
| 146 |
return true; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|