| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Autonami Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Autonami; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use BWF_Contacts; |
| 12 |
use BWFCRM_Contact; |
| 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_obj = BWF_Contacts::get_instance(); |
| 33 |
$contact = $contact_obj->get_contact_by('email', $data['email']); |
| 34 |
$userExist = (absint($contact->get_id()) > 0); |
| 35 |
|
| 36 |
if ($userExist && isset($actions->skip_if_exists) && $actions->skip_if_exists) { |
| 37 |
$response = ['success' => false, 'messages' => 'Contact already exists!']; |
| 38 |
} else { |
| 39 |
foreach ($data as $key => $item) { |
| 40 |
$obj = 'set_' . $key; |
| 41 |
$contact->$obj($item); |
| 42 |
} |
| 43 |
$contact->set_status(1); |
| 44 |
$contact->save(); |
| 45 |
|
| 46 |
$customContact = new BWFCRM_Contact($data['email']); |
| 47 |
foreach ($data as $key => $item) { |
| 48 |
if ('address' === $key) { |
| 49 |
$key = 'address-1'; |
| 50 |
} |
| 51 |
$customContact->set_field_by_slug($key, $item); |
| 52 |
} |
| 53 |
$customContact->save_fields(); |
| 54 |
|
| 55 |
if (absint($contact->get_id()) > 0) { |
| 56 |
$response = ['success' => true, 'messages' => 'Insert successfully!']; |
| 57 |
} else { |
| 58 |
$response = ['success' => false, 'messages' => 'Something wrong!']; |
| 59 |
} |
| 60 |
} |
| 61 |
return $response; |
| 62 |
} |
| 63 |
|
| 64 |
public function executeRecordApi($fieldValues, $fieldMap, $actions, $lists, $tags) |
| 65 |
{ |
| 66 |
$fieldData = []; |
| 67 |
foreach ($fieldMap as $fieldPair) { |
| 68 |
if (!empty($fieldPair->autonamiField)) { |
| 69 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 70 |
$fieldData[$fieldPair->autonamiField] = $fieldPair->customValue; |
| 71 |
} else { |
| 72 |
$fieldData[$fieldPair->autonamiField] = is_array($fieldValues[$fieldPair->formField]) ? json_encode($fieldValues[$fieldPair->formField]) : $fieldValues[$fieldPair->formField]; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
$fieldData['lists'] = $lists; |
| 77 |
$fieldData['tags'] = $tags; |
| 78 |
|
| 79 |
$recordApiResponse = $this->insertRecord($fieldData, $actions); |
| 80 |
|
| 81 |
if ($recordApiResponse['success']) { |
| 82 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'success', $recordApiResponse); |
| 83 |
} else { |
| 84 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', $recordApiResponse); |
| 85 |
} |
| 86 |
return $recordApiResponse; |
| 87 |
} |
| 88 |
} |
| 89 |
|