| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Fluent CRM Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\FluentCrm; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use FluentCrm\App\Models\Subscriber; |
| 12 |
use FluentCrm\Includes\Helpers\Arr; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for Record insert |
| 16 |
*/ |
| 17 |
class RecordApiHelper |
| 18 |
{ |
| 19 |
private $_integrationID; |
| 20 |
private $_logID; |
| 21 |
private $_logResponse; |
| 22 |
|
| 23 |
private $_entryID; |
| 24 |
|
| 25 |
public function __construct($integId, $logID, $entryID) |
| 26 |
{ |
| 27 |
$this->_integrationID = $integId; |
| 28 |
$this->_logID = $logID; |
| 29 |
$this->_logResponse = new UtilApiResponse(); |
| 30 |
$this->_entryID = $entryID; |
| 31 |
} |
| 32 |
|
| 33 |
public function insertRecord($data, $actions) |
| 34 |
{ |
| 35 |
// $contact = Arr::only($data, ['first_name', 'last_name', 'email']); |
| 36 |
|
| 37 |
$contact = $data; |
| 38 |
unset($contact['list_id'], $contact['tags']); |
| 39 |
|
| 40 |
// for full name |
| 41 |
if (!$contact['first_name'] && !$contact['last_name']) { |
| 42 |
$fullName = Arr::get($data, 'full_name'); |
| 43 |
if ($fullName) { |
| 44 |
$nameArray = explode(' ', $fullName); |
| 45 |
if (count($nameArray) > 1) { |
| 46 |
$contact['last_name'] = array_pop($nameArray); |
| 47 |
$contact['first_name'] = implode(' ', $nameArray); |
| 48 |
} else { |
| 49 |
$contact['first_name'] = $fullName; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// for exsist user |
| 55 |
$subscriber = Subscriber::where('email', $contact['email'])->first(); |
| 56 |
|
| 57 |
if ($subscriber && isset($actions->skip_if_exists) && $actions->skip_if_exists) { |
| 58 |
$response = [ |
| 59 |
'success' => false, |
| 60 |
'messages' => 'Contact already exists!' |
| 61 |
]; |
| 62 |
} else { |
| 63 |
$hasDoubleOptIn = isset($actions->double_opt_in) && $actions->double_opt_in; |
| 64 |
$forceSubscribed = false; |
| 65 |
|
| 66 |
if (!$subscriber) { |
| 67 |
$contact['status'] = $hasDoubleOptIn ? 'pending' : 'subscribed'; |
| 68 |
} else { |
| 69 |
$forceSubscribed = !$hasDoubleOptIn && ('subscribed' !== $subscriber->status); |
| 70 |
if ($forceSubscribed) { |
| 71 |
$contact['status'] = 'subscribed'; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ($listId = Arr::get($data, 'list_id')) { |
| 76 |
$contact['lists'] = [$listId]; |
| 77 |
} |
| 78 |
$contact['tags'] = $data['tags']; |
| 79 |
|
| 80 |
$isNewSubscriber = !$subscriber; |
| 81 |
$subscriber = FluentCrmApi('contacts')->createOrUpdate($contact, $forceSubscribed, false); |
| 82 |
|
| 83 |
$shouldSendDoubleOptIn = $isNewSubscriber |
| 84 |
? ('pending' === $subscriber->status) |
| 85 |
: ($hasDoubleOptIn && in_array($subscriber->status, ['pending', 'unsubscribed'], true)); |
| 86 |
|
| 87 |
if ($shouldSendDoubleOptIn) { |
| 88 |
$subscriber->sendDoubleOptinEmail(); |
| 89 |
} |
| 90 |
|
| 91 |
$response = $subscriber |
| 92 |
? ['success' => true, 'messages' => 'Insert successfully!'] |
| 93 |
: ['success' => false, 'messages' => 'Something wrong!']; |
| 94 |
} |
| 95 |
return $response; |
| 96 |
} |
| 97 |
|
| 98 |
public function executeRecordApi( |
| 99 |
$fieldValues, |
| 100 |
$fieldMap, |
| 101 |
$actions, |
| 102 |
$list_id, |
| 103 |
$tags, |
| 104 |
$formId |
| 105 |
) { |
| 106 |
$fieldData = []; |
| 107 |
|
| 108 |
foreach ($fieldMap as $fieldPair) { |
| 109 |
if (!empty($fieldPair->fluentCRMField)) { |
| 110 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 111 |
$fieldData[$fieldPair->fluentCRMField] = $fieldPair->customValue; |
| 112 |
} else { |
| 113 |
$fieldData[$fieldPair->fluentCRMField] = $fieldValues[$fieldPair->formField]; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
$fieldData['list_id'] = $list_id; |
| 118 |
$fieldData['tags'] = $tags; |
| 119 |
|
| 120 |
$recordApiResponse = $this->insertRecord($fieldData, $actions); |
| 121 |
|
| 122 |
$entryDetails = [ |
| 123 |
'formId' => $formId, |
| 124 |
'entryId' => $this->_entryID, |
| 125 |
'fieldValues' => $fieldValues |
| 126 |
]; |
| 127 |
|
| 128 |
if ($recordApiResponse['success']) { |
| 129 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'success', $recordApiResponse, $entryDetails); |
| 130 |
} else { |
| 131 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', $recordApiResponse, $entryDetails); |
| 132 |
} |
| 133 |
return $recordApiResponse; |
| 134 |
} |
| 135 |
} |
| 136 |
|