| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoCrm Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoCRM; |
| 9 |
|
| 10 |
use WP_Error; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use BitCode\BitForm\Core\Util\DateTimeHelper; |
| 13 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 14 |
use BitCode\BitFormPro\Integration\ZohoCRM\TagApiHelper; |
| 15 |
|
| 16 |
/** |
| 17 |
* Provide functionality for Record insert,upsert |
| 18 |
*/ |
| 19 |
class RecordApiHelper |
| 20 |
{ |
| 21 |
protected $_defaultHeader; |
| 22 |
protected $_apiDomain; |
| 23 |
protected $_tokenDetails; |
| 24 |
|
| 25 |
public function __construct($tokenDetails) |
| 26 |
{ |
| 27 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 28 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 29 |
$this->_tokenDetails = $tokenDetails; |
| 30 |
$this->_logResponse = new UtilApiResponse(); |
| 31 |
} |
| 32 |
|
| 33 |
public function upsertRecord($module, $data) |
| 34 |
{ |
| 35 |
$insertRecordEndpoint = "{$this->_apiDomain}/crm/v2/{$module}/upsert"; |
| 36 |
$data = \is_string($data) ? $data : \json_encode($data); |
| 37 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 38 |
} |
| 39 |
|
| 40 |
public function insertRecord($module, $data) |
| 41 |
{ |
| 42 |
$insertRecordEndpoint = "{$this->_apiDomain}/crm/v2/{$module}"; |
| 43 |
$data = \is_string($data) ? $data : \json_encode($data); |
| 44 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 45 |
} |
| 46 |
|
| 47 |
public function executeRecordApi($formID, $entryID, $integId, $logID, $defaultConf, $module, $layout, $fieldValues, $fieldMap, $actions, $required, $fileMap = [], $isRelated = false) |
| 48 |
{ |
| 49 |
|
| 50 |
foreach ($fieldMap as $fieldKey => $fieldPair) { |
| 51 |
if (!empty($fieldPair->zohoFormField)) { |
| 52 |
if (empty($defaultConf->layouts->{$module}->{$layout}->fields->{$fieldPair->zohoFormField})) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
if ($fieldPair->formField === 'custom' && isset($fieldPair->customValue)) { |
| 56 |
$fieldData[$fieldPair->zohoFormField] = $this->formatFieldValue($fieldPair->customValue, $defaultConf->layouts->{$module}->{$layout}->fields->{$fieldPair->zohoFormField}); |
| 57 |
} else { |
| 58 |
$fieldData[$fieldPair->zohoFormField] = $this->formatFieldValue($fieldValues[$fieldPair->formField], $defaultConf->layouts->{$module}->{$layout}->fields->{$fieldPair->zohoFormField}); |
| 59 |
} |
| 60 |
if (empty($fieldData[$fieldPair->zohoFormField]) && \in_array($fieldPair->zohoFormField, $required)) { |
| 61 |
$error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%s is required for zoho crm, %s module', 'bitform'), $fieldPair->zohoFormField, $module)); |
| 62 |
$this->_logResponse->apiResponse($logID, $integId, ['type' => 'record', 'type_name' => 'field'], 'validation', $error); |
| 63 |
return $error; |
| 64 |
} |
| 65 |
|
| 66 |
if (!empty($fieldData[$fieldPair->zohoFormField])) { |
| 67 |
$requiredLength = $defaultConf->layouts->{$module}->{$layout}->fields->{$fieldPair->zohoFormField}->length; |
| 68 |
$currentLength = is_array($fieldData[$fieldPair->zohoFormField]) || is_object($fieldData[$fieldPair->zohoFormField]) ? |
| 69 |
@count($fieldData[$fieldPair->zohoFormField]) |
| 70 |
: strlen($fieldData[$fieldPair->zohoFormField]); |
| 71 |
if ($currentLength > $requiredLength) { |
| 72 |
$error = new WP_Error('REQ_FIELD_LENGTH_EXCEEDED', wp_sprintf(__('zoho crm field %s\'s maximum length is %s, Given %s', 'bitform'), $fieldPair->zohoFormField, $module)); |
| 73 |
$this->_logResponse->apiResponse($logID, $integId, ['type' => 'record', 'type_name' => 'field'], 'validation', $error); |
| 74 |
return $error; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
if (!empty($defaultConf->layouts->{$module}->{$layout}->id)) { |
| 80 |
$fieldData['Layout']['id'] = $defaultConf->layouts->{$module}->{$layout}->id; |
| 81 |
} |
| 82 |
$requestParams['data'][] = (object) $fieldData; |
| 83 |
$requestParams['trigger'] = []; |
| 84 |
$recordApiResponse = $this->upsertRecord($module, (object) $requestParams); |
| 85 |
if ($recordApiResponse->status === 'error') { |
| 86 |
$this->_logResponse->apiResponse($logID, $integId, 'record', 'error', $recordApiResponse); |
| 87 |
} else { |
| 88 |
$this->_logResponse->apiResponse($logID, $integId, 'record', 'success', $recordApiResponse); |
| 89 |
} |
| 90 |
return $recordApiResponse; |
| 91 |
} |
| 92 |
|
| 93 |
public function formatFieldValue($value, $formatSpecs) |
| 94 |
{ |
| 95 |
if (empty($value)) { |
| 96 |
return ''; |
| 97 |
} |
| 98 |
|
| 99 |
switch ($formatSpecs->json_type) { |
| 100 |
case 'jsonarray': |
| 101 |
$apiFormat = 'array'; |
| 102 |
break; |
| 103 |
case 'jsonobject': |
| 104 |
$apiFormat = 'object'; |
| 105 |
break; |
| 106 |
|
| 107 |
default: |
| 108 |
$apiFormat = $formatSpecs->json_type; |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
$formatedValue = ''; |
| 113 |
$fieldFormat = gettype($value); |
| 114 |
if ($fieldFormat === $apiFormat && $formatSpecs->data_type !== 'datetime') { |
| 115 |
$formatedValue = $value; |
| 116 |
} else { |
| 117 |
if ($apiFormat === 'array' || $apiFormat === 'object') { |
| 118 |
if ($fieldFormat === 'string') { |
| 119 |
if (strpos($value, ',') === -1) { |
| 120 |
$formatedValue = json_decode($value); |
| 121 |
} else { |
| 122 |
$formatedValue = explode(',', $value); |
| 123 |
} |
| 124 |
$formatedValue = is_null($formatedValue) && !is_null($value) ? [$value] : $formatedValue; |
| 125 |
} else { |
| 126 |
$formatedValue = $value; |
| 127 |
} |
| 128 |
|
| 129 |
if ($apiFormat === 'object') { |
| 130 |
$formatedValue = (object) $formatedValue; |
| 131 |
} |
| 132 |
} elseif ($apiFormat === 'string' && $formatSpecs->data_type !== 'datetime') { |
| 133 |
$formatedValue = !is_string($value) ? json_encode($value) : $value; |
| 134 |
} elseif ($formatSpecs->data_type === 'datetime') { |
| 135 |
$dateTimeHelper = new DateTimeHelper(); |
| 136 |
$formatedValue = $dateTimeHelper->getFormated($value, 'Y-m-d\TH:i', wp_timezone(), 'Y-m-d\TH:i:sP', null); |
| 137 |
} else { |
| 138 |
$stringyfiedValue = !is_string($value) ? json_encode($value) : $value; |
| 139 |
|
| 140 |
switch ($apiFormat) { |
| 141 |
case 'double': |
| 142 |
$formatedValue = (float) $stringyfiedValue; |
| 143 |
break; |
| 144 |
|
| 145 |
case 'boolean': |
| 146 |
$formatedValue = (bool) $stringyfiedValue; |
| 147 |
break; |
| 148 |
|
| 149 |
case 'integer': |
| 150 |
$formatedValue = (int) $stringyfiedValue; |
| 151 |
break; |
| 152 |
|
| 153 |
default: |
| 154 |
$formatedValue = $stringyfiedValue; |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
$formatedValueLenght = $apiFormat === 'array' || $apiFormat === 'object' ? (is_countable($formatedValue) ? \count($formatedValue) : @count($formatedValue)) : \strlen($formatedValue); |
| 160 |
if ($formatedValueLenght > $formatSpecs->length) { |
| 161 |
$formatedValue = $apiFormat === 'array' || $apiFormat === 'object' ? array_slice($formatedValue, 0, $formatSpecs->length) : substr($formatedValue, 0, $formatSpecs->length); |
| 162 |
} |
| 163 |
|
| 164 |
return $formatedValue; |
| 165 |
} |
| 166 |
} |
| 167 |
|