| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Bit CRM Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\BitCRM; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 15 |
|
| 16 |
/** |
| 17 |
* Provide functionality for lead insert into Bit CRM |
| 18 |
*/ |
| 19 |
class RecordApiHelper |
| 20 |
{ |
| 21 |
private $_integrationID; |
| 22 |
|
| 23 |
private $_logID; |
| 24 |
|
| 25 |
private $_logResponse; |
| 26 |
|
| 27 |
private $_entryID; |
| 28 |
|
| 29 |
public function __construct($integId, $logID, $entryID) |
| 30 |
{ |
| 31 |
$this->_integrationID = $integId; |
| 32 |
$this->_logID = $logID; |
| 33 |
$this->_logResponse = new UtilApiResponse(); |
| 34 |
$this->_entryID = $entryID; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Build the LeadService payload and create the lead |
| 39 |
* |
| 40 |
* @param array $fieldValues submitted field values (smart tags already merged) |
| 41 |
* @param object $integrationDetails decoded integration_details JSON |
| 42 |
* @param int $formId current form id |
| 43 |
* |
| 44 |
* @return array LeadService result ['success' => bool, ...] |
| 45 |
*/ |
| 46 |
public function executeRecordApi($fieldValues, $integrationDetails, $formId) |
| 47 |
{ |
| 48 |
$fieldMap = $integrationDetails->field_map; |
| 49 |
$crmFieldMeta = $this->crmFieldMeta($integrationDetails); |
| 50 |
|
| 51 |
$systemDefinedFieldsValues = []; |
| 52 |
$customFieldsValues = []; |
| 53 |
$skippedFields = []; |
| 54 |
|
| 55 |
foreach ($fieldMap as $fieldPair) { |
| 56 |
if (empty($fieldPair->crmFormField)) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 60 |
$value = $fieldPair->customValue; |
| 61 |
} else { |
| 62 |
$value = isset($fieldValues[$fieldPair->formField]) ? $fieldValues[$fieldPair->formField] : null; |
| 63 |
} |
| 64 |
if (is_null($value) || '' === $value || (is_array($value) && 0 === count($value))) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
if (!isset($crmFieldMeta[$fieldPair->crmFormField])) { |
| 69 |
// mapped field no longer exists in CRM (e.g. CRM Pro deactivated) - drop, don't fail the lead |
| 70 |
$skippedFields[] = $fieldPair->crmFormField; |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
$meta = $crmFieldMeta[$fieldPair->crmFormField]; |
| 75 |
if (!empty($meta->isCustom)) { |
| 76 |
// Multi-value custom fields (multi-select, checkbox) must be stored JSON-encoded: |
| 77 |
// Bit CRM decodes field_value with JSON::is() on read, so an array round-trips as |
| 78 |
// an array. Single values stay plain strings. |
| 79 |
$customFieldsValues[$fieldPair->crmFormField] = [ |
| 80 |
'field_id' => $meta->fieldId, |
| 81 |
'field_value' => is_array($value) ? wp_json_encode(array_values($value)) : (string) $value, |
| 82 |
]; |
| 83 |
} else { |
| 84 |
// System columns are scalar; join a multi-value form field into a readable string. |
| 85 |
$systemDefinedFieldsValues[$fieldPair->crmFormField] = is_array($value) ? implode(', ', $value) : (string) $value; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$payload = ['systemDefinedFieldsValues' => $systemDefinedFieldsValues]; |
| 90 |
|
| 91 |
if (!empty($customFieldsValues)) { |
| 92 |
$payload['customFieldsValues'] = $customFieldsValues; |
| 93 |
} |
| 94 |
if (!empty($integrationDetails->tagIds)) { |
| 95 |
$payload['tagIds'] = array_map('intval', (array) $integrationDetails->tagIds); |
| 96 |
} |
| 97 |
if (!empty($integrationDetails->newTagTitles)) { |
| 98 |
$payload['newTagTitles'] = array_map('strval', (array) $integrationDetails->newTagTitles); |
| 99 |
} |
| 100 |
|
| 101 |
$recordApiResponse = (new \BitApps\Crm\Services\LeadService())->store($payload); |
| 102 |
|
| 103 |
if (!empty($skippedFields)) { |
| 104 |
$recordApiResponse['skipped_fields'] = 'Unknown Bit CRM field(s) skipped: ' . implode(', ', $skippedFields); |
| 105 |
} |
| 106 |
|
| 107 |
$entryDetails = [ |
| 108 |
'formId' => $formId, |
| 109 |
'entryId' => $this->_entryID, |
| 110 |
'fieldValues' => $fieldValues |
| 111 |
]; |
| 112 |
|
| 113 |
if (!empty($recordApiResponse['success'])) { |
| 114 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'Lead-create'], 'success', $recordApiResponse, $entryDetails); |
| 115 |
} else { |
| 116 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'Lead-create'], 'error', $recordApiResponse, $entryDetails); |
| 117 |
} |
| 118 |
return $recordApiResponse; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Authoritative CRM field metadata keyed by field key. Live fetch first |
| 123 |
* (fresh isCustom/fieldId), saved crmFields snapshot as fallback. |
| 124 |
* |
| 125 |
* @return array<string, object> |
| 126 |
*/ |
| 127 |
private function crmFieldMeta($integrationDetails) |
| 128 |
{ |
| 129 |
$fields = []; |
| 130 |
try { |
| 131 |
$fields = BitCRMHandler::getLeadFields(); |
| 132 |
} catch (\Throwable $th) { |
| 133 |
$fields = []; |
| 134 |
} |
| 135 |
if (empty($fields) && !empty($integrationDetails->crmFields)) { |
| 136 |
$fields = $integrationDetails->crmFields; |
| 137 |
} |
| 138 |
|
| 139 |
$meta = []; |
| 140 |
foreach ($fields as $field) { |
| 141 |
$meta[$field->key] = $field; |
| 142 |
} |
| 143 |
return $meta; |
| 144 |
} |
| 145 |
} |
| 146 |
|