| 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 |
public function __construct($integId, $logID, $entryID) |
| 24 |
{ |
| 25 |
$this->_integrationID = $integId; |
| 26 |
$this->_logID = $logID; |
| 27 |
$this->_logResponse = new UtilApiResponse(); |
| 28 |
} |
| 29 |
|
| 30 |
public function insertRecord($data, $actions) |
| 31 |
{ |
| 32 |
$contact = Arr::only($data, ['first_name', 'last_name', 'email']); |
| 33 |
|
| 34 |
// for full name |
| 35 |
if (!$contact['first_name'] && !$contact['last_name']) { |
| 36 |
$fullName = Arr::get($data, 'full_name'); |
| 37 |
if ($fullName) { |
| 38 |
$nameArray = explode(' ', $fullName); |
| 39 |
if (count($nameArray) > 1) { |
| 40 |
$contact['last_name'] = array_pop($nameArray); |
| 41 |
$contact['first_name'] = implode(' ', $nameArray); |
| 42 |
} else { |
| 43 |
$contact['first_name'] = $fullName; |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// for exsist user |
| 49 |
$subscriber = Subscriber::where('email', $contact['email'])->first(); |
| 50 |
|
| 51 |
if ($subscriber && isset($actions->skip_if_exists) && $actions->skip_if_exists) { |
| 52 |
$response = [ |
| 53 |
'success' => false, |
| 54 |
'messages' => 'Contact already exists!' |
| 55 |
]; |
| 56 |
} else { |
| 57 |
// for subscirber |
| 58 |
if (!$subscriber) { |
| 59 |
if (isset($actions->double_opt_in) && $actions->double_opt_in) { |
| 60 |
$contact['status'] = 'pending'; |
| 61 |
} else { |
| 62 |
$contact['status'] = 'subscribed'; |
| 63 |
} |
| 64 |
|
| 65 |
if ($listId = Arr::get($data, 'list_id')) { |
| 66 |
$contact['lists'] = [$listId]; |
| 67 |
} |
| 68 |
$contact['tags'] = $data['tags']; |
| 69 |
|
| 70 |
$subscriber = FluentCrmApi('contacts')->createOrUpdate($contact, false, false); |
| 71 |
|
| 72 |
if ('pending' === $subscriber->status) { |
| 73 |
$subscriber->sendDoubleOptinEmail(); |
| 74 |
} |
| 75 |
if ($subscriber) { |
| 76 |
$response = [ |
| 77 |
'success' => true, |
| 78 |
'messages' => 'Insert successfully!' |
| 79 |
]; |
| 80 |
} else { |
| 81 |
$response = [ |
| 82 |
'success' => false, |
| 83 |
'messages' => 'Somthing wrong!' |
| 84 |
]; |
| 85 |
} |
| 86 |
} else { |
| 87 |
if ($listId = Arr::get($data, 'list_id')) { |
| 88 |
$contact['lists'] = [$listId]; |
| 89 |
} |
| 90 |
|
| 91 |
$contact['tags'] = $data['tags']; |
| 92 |
|
| 93 |
$hasDouBleOptIn = isset($actions->double_opt_in) && $actions->double_opt_in; |
| 94 |
|
| 95 |
$forceSubscribed = !$hasDouBleOptIn && ('subscribed' !== $subscriber->status); |
| 96 |
|
| 97 |
if ($forceSubscribed) { |
| 98 |
$contact['status'] = 'subscribed'; |
| 99 |
} |
| 100 |
$subscriber = FluentCrmApi('contacts')->createOrUpdate($contact, $forceSubscribed, false); |
| 101 |
|
| 102 |
if ($hasDouBleOptIn && ('pending' === $subscriber->status || 'unsubscribed' === $subscriber->status)) { |
| 103 |
$subscriber->sendDoubleOptinEmail(); |
| 104 |
} |
| 105 |
if ($subscriber) { |
| 106 |
$response = [ |
| 107 |
'success' => true, |
| 108 |
'messages' => 'Insert successfully!' |
| 109 |
]; |
| 110 |
} else { |
| 111 |
$response = [ |
| 112 |
'success' => false, |
| 113 |
'messages' => 'Something wrong!' |
| 114 |
]; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
return $response; |
| 119 |
} |
| 120 |
|
| 121 |
public function executeRecordApi( |
| 122 |
$fieldValues, |
| 123 |
$fieldMap, |
| 124 |
$actions, |
| 125 |
$list_id, |
| 126 |
$tags |
| 127 |
) { |
| 128 |
$fieldData = []; |
| 129 |
|
| 130 |
foreach ($fieldMap as $fieldKey => $fieldPair) { |
| 131 |
if (!empty($fieldPair->fluentCRMField)) { |
| 132 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 133 |
$fieldData[$fieldPair->fluentCRMField] = $fieldPair->customValue; |
| 134 |
} else { |
| 135 |
$fieldData[$fieldPair->fluentCRMField] = $fieldValues[$fieldPair->formField]; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
$fieldData['list_id'] = $list_id; |
| 140 |
$fieldData['tags'] = $tags; |
| 141 |
|
| 142 |
$recordApiResponse = $this->insertRecord($fieldData, $actions); |
| 143 |
|
| 144 |
if ($recordApiResponse['success']) { |
| 145 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'success', $recordApiResponse); |
| 146 |
} else { |
| 147 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', $recordApiResponse); |
| 148 |
} |
| 149 |
return $recordApiResponse; |
| 150 |
} |
| 151 |
} |