PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
V3.3.2 3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 All 139 releases
bit-form / includes / Core / Integration / FluentCrm / RecordApiHelper.php

RecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Integration/FluentCrm/RecordApiHelper.php

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