# fluent-support/2.4.5/app/Services/FluentCRMServices.php

Fluent Support – Helpdesk &amp; Customer Support Ticket System, version 2.4.5. 147 lines.

- Page: https://pluginprobe.com/plugins/fluent-support/2.4.5/code/app/Services/FluentCRMServices.php
- Raw: https://pluginprobe.com/plugins/fluent-support/2.4.5/raw/app/Services/FluentCRMServices.php
- Modified: 2026-10-09T15:31:16+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/fluent-support/2.4.5/code/app/Services/FluentCRMServices.php#L10-L20`.

```php
<?php

namespace FluentSupport\App\Services;

use Exception;

class FluentCRMServices
{
    /**
     * Set the FluentCRM contact's tags to exactly $data['tags'].
     *
     * @param array $data
     * @return array
     * @throws Exception
     */
    public function syncCrmTags ( $data )
    {

        $this->isCrmEnabled();
        $this->isContactAvailable( $data['contact_id'] );

        $tags = $data['tags'] ?? [];

        $tagIds = array_filter( $tags, 'absint' );
        $this->checkCrmPermission();

        $contact = \FluentCrm\App\Models\Subscriber::findOrFail( $data['contact_id'] );

        $this->addOrRemoveTag( $contact, $tagIds );

        return [
            'tags'    => $contact->tags,
            'message' => __('FluentCRM contact tags has been updated', 'fluent-support')
        ];
    }

    /**
     * Set the FluentCRM contact's lists to exactly $data['lists'].
     *
     * @param array $data
     * @return array
     * @throws Exception
     */
    public function syncCrmLists ( $data )
    {
        $this->isCrmEnabled();
        $this->isContactAvailable( $data['contact_id'] );

        $lists = $data['lists'] ?? [];

        $listIds = array_filter( $lists, 'absint' );
        $this->checkCrmPermission();

        $contact = \FluentCrm\App\Models\Subscriber::findOrFail( $data['contact_id'] );

        $this->addOrRemoveList( $contact, $listIds );

        return [
            'lists'    => $contact->lists,
            'message' => __('FluentCRM contact lists has been updated', 'fluent-support')
        ];
    }

    /**
     * @param \FluentCrm\App\Models\Subscriber $contact
     * @param array $tagIds
     * @return mixed
     */
    public function addOrRemoveTag ( $contact, $tagIds )
    {
        $existingTags = $contact->tags;
        $existingTagIds = [];
        foreach ($existingTags as $tag) {
            $existingTagIds[] = $tag->id;
        }
        $newTagIds = array_diff($tagIds, $existingTagIds);
        $removedTagIds = array_diff($existingTagIds, $tagIds);

        if ($newTagIds) {
            $contact->attachTags($newTagIds);
        }

        if ($removedTagIds) {
            $contact->detachTags($removedTagIds);
        }

        return $contact;
    }

    /**
     * @param \FluentCrm\App\Models\Subscriber $contact
     * @param array $listIds
     * @return mixed
     */
    public function addOrRemoveList ( $contact, $listIds )
    {
        $existingLists = $contact->lists;
        $existingListIds = [];

        foreach ($existingLists as $list) {
            $existingListIds[] = $list->id;
        }
        $newListIds = array_diff($listIds, $existingListIds);
        $removedListIds = array_diff($existingListIds, $listIds);

        if ($newListIds) {
            $contact->attachLists( $newListIds );
        }

        if ($removedListIds) {
            $contact->detachLists( $removedListIds );
        }

        return $contact;
    }

    private function checkCrmPermission ()
    {
        $canAddTags = \FluentCrm\App\Services\PermissionManager::currentUserCan('fcrm_manage_contacts');
        $canAddTags = apply_filters('fluent_support/can_user_add_tags_to_customer', $canAddTags);

        if (!$canAddTags) {
            throw new \Exception(esc_html__('Sorry you do not have permission to add contact tags', 'fluent-support'));
        } else {
            return true;
        }
    }

    private function isCrmEnabled ()
    {
        if ( !defined('FLUENTCRM') ) {
           throw new \Exception(esc_html__('FluentCRM is not installed or enabled', 'fluent-support'));
        } else {
            return true;
        }
    }

    private function isContactAvailable ( $contactId )
    {
        if (!$contactId) {
            throw new \Exception(esc_html__('Contact could not be found', 'fluent-support'));
        } else {
            return true;
        }
    }
}

```
