← All changes
|
includes/Core/Integration/ZohoCRM/ZohoCRMHandler.php
+727
-389
1.4.18
→
2.15.3
View file →
| @@ -6,14 +6,13 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\ZohoCRM; |
| 9 | 9 | |
| 10 | -use WP_Error; | |
| 11 | -use BitCode\BitForm\Core\Util\IpTool; | |
| 12 | -use BitCode\BitForm\Core\Util\HttpHelper; | |
| 13 | 10 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 14 | -use BitCode\BitForm\Core\Integration\ZohoCRM\RecordApiHelper; | |
| 15 | 11 | use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 12 | +use BitCode\BitForm\Core\Util\HttpHelper; | |
| 13 | +use BitCode\BitForm\Core\Util\IpTool; | |
| 14 | +use WP_Error; | |
| 16 | 15 | |
| 17 | 16 | /** |
| 18 | 17 | * Provide functionality for ZohoCrm integration |
| 19 | 18 | */ |
| @@ -18,423 +17,762 @@ | ||
| 18 | 17 | * Provide functionality for ZohoCrm integration |
| 19 | 18 | */ |
| 20 | 19 | class ZohoCRMHandler |
| 21 | 20 | { |
| 22 | - private $_formID; | |
| 23 | - private $_integrationID; | |
| 21 | + private $_formID; | |
| 22 | + private $_integrationID; | |
| 24 | 23 | |
| 25 | - public function __construct($integrationID, $fromID) | |
| 26 | - { | |
| 27 | - $this->_formID = $fromID; | |
| 28 | - $this->_integrationID = $integrationID; | |
| 29 | - $this->_logResponse = new UtilApiResponse(); | |
| 24 | + private $_logResponse; | |
| 25 | + | |
| 26 | + public function __construct($integrationID, $fromID) | |
| 27 | + { | |
| 28 | + $this->_formID = $fromID; | |
| 29 | + $this->_integrationID = $integrationID; | |
| 30 | + $this->_logResponse = new UtilApiResponse(); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Helps to register ajax function's with wp | |
| 35 | + * | |
| 36 | + * @return null | |
| 37 | + */ | |
| 38 | + public static function registerAjax() | |
| 39 | + { | |
| 40 | + add_action('wp_ajax_bitforms_zcrm_generate_token', [__CLASS__, 'generateTokens']); | |
| 41 | + add_action('wp_ajax_bitforms_zcrm_refresh_modules', [__CLASS__, 'refreshModulesAjaxHelper']); | |
| 42 | + add_action('wp_ajax_bitforms_zcrm_refresh_layouts', [__CLASS__, 'refreshLayoutsAjaxHelper']); | |
| 43 | + add_action('wp_ajax_bitforms_zcrm_get_users', [__CLASS__, 'refreshUsersAjaxHelper']); | |
| 44 | + add_action('wp_ajax_bitforms_zcrm_get_tags', [__CLASS__, 'refreshTagListAjaxHelper']); | |
| 45 | + add_action('wp_ajax_bitforms_zcrm_get_assignment_rules', [__CLASS__, 'getAssignmentRulesAjaxHelper']); | |
| 46 | + add_action('wp_ajax_nopriv_bitforms_zcrm_get_assignment_rules', [__CLASS__, 'getAssignmentRulesAjaxHelper']); | |
| 47 | + add_action('wp_ajax_bitforms_zcrm_get_related_lists', [__CLASS__, 'getRelatedListsAjaxHelper']); | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Process ajax request for generate_token | |
| 52 | + * | |
| 53 | + * @return JSON zoho crm api response and status | |
| 54 | + */ | |
| 55 | + public static function generateTokens() | |
| 56 | + { | |
| 57 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) { | |
| 58 | + $inputJSON = file_get_contents('php://input'); | |
| 59 | + $requestsParams = json_decode($inputJSON); | |
| 60 | + if ( | |
| 61 | + empty($requestsParams->{'accounts-server'}) | |
| 62 | + || empty($requestsParams->dataCenter) | |
| 63 | + || empty($requestsParams->clientId) | |
| 64 | + || empty($requestsParams->clientSecret) | |
| 65 | + || empty($requestsParams->redirectURI) | |
| 66 | + || empty($requestsParams->code) | |
| 67 | + ) { | |
| 68 | + wp_send_json_error( | |
| 69 | + __( | |
| 70 | + 'Requested parameter is empty', | |
| 71 | + 'bit-form' | |
| 72 | + ), | |
| 73 | + 400 | |
| 74 | + ); | |
| 75 | + } | |
| 76 | + $apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token'; | |
| 77 | + $requestParams = [ | |
| 78 | + 'grant_type' => 'authorization_code', | |
| 79 | + 'client_id' => $requestsParams->clientId, | |
| 80 | + 'client_secret' => $requestsParams->clientSecret, | |
| 81 | + 'redirect_uri' => \urldecode($requestsParams->redirectURI), | |
| 82 | + 'code' => $requestsParams->code | |
| 83 | + ]; | |
| 84 | + $apiResponse = HttpHelper::post($apiEndpoint, $requestParams); | |
| 85 | + if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { | |
| 86 | + wp_send_json_error( | |
| 87 | + empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, | |
| 88 | + 400 | |
| 89 | + ); | |
| 90 | + } | |
| 91 | + $apiResponse->generates_on = \time(); | |
| 92 | + wp_send_json_success($apiResponse, 200); | |
| 93 | + } else { | |
| 94 | + wp_send_json_error( | |
| 95 | + __( | |
| 96 | + 'Token expired', | |
| 97 | + 'bit-form' | |
| 98 | + ), | |
| 99 | + 401 | |
| 100 | + ); | |
| 30 | 101 | } |
| 102 | + } | |
| 31 | 103 | |
| 32 | - /** | |
| 33 | - * Helps to register ajax function's with wp | |
| 34 | - * | |
| 35 | - * @return null | |
| 36 | - */ | |
| 37 | - public static function registerAjax() | |
| 38 | - { | |
| 39 | - add_action('wp_ajax_bitforms_zcrm_generate_token', array(__CLASS__, 'generateTokens')); | |
| 40 | - add_action('wp_ajax_bitforms_zcrm_refresh_modules', array(__CLASS__, 'refreshModulesAjaxHelper')); | |
| 41 | - add_action('wp_ajax_bitforms_zcrm_refresh_layouts', array(__CLASS__, 'refreshLayoutsAjaxHelper')); | |
| 42 | - } | |
| 104 | + /** | |
| 105 | + * Process ajax request for refresh crm modules | |
| 106 | + * | |
| 107 | + * @return JSON crm module data | |
| 108 | + */ | |
| 109 | + public static function refreshModulesAjaxHelper() | |
| 110 | + { | |
| 111 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) { | |
| 112 | + $inputJSON = file_get_contents('php://input'); | |
| 113 | + $queryParams = json_decode($inputJSON); | |
| 114 | + if ( | |
| 115 | + empty($queryParams->tokenDetails) | |
| 116 | + || empty($queryParams->dataCenter) | |
| 117 | + || empty($queryParams->clientId) | |
| 118 | + || empty($queryParams->clientSecret) | |
| 119 | + ) { | |
| 120 | + wp_send_json_error( | |
| 121 | + __( | |
| 122 | + 'Requested parameter is empty', | |
| 123 | + 'bit-form' | |
| 124 | + ), | |
| 125 | + 400 | |
| 126 | + ); | |
| 127 | + } | |
| 128 | + $response = []; | |
| 129 | + if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 130 | + $response['tokenDetails'] = ZohoCRMHandler::_refreshAccessToken($queryParams); | |
| 131 | + } | |
| 132 | + $zohosIntegratedModules = [ | |
| 133 | + 'zohosign__ZohoSign_Document_Events', | |
| 134 | + 'zohoshowtime__ShowTime_Sessions', | |
| 135 | + 'zohoshowtime__Zoho_ShowTime', | |
| 136 | + 'zohosign__ZohoSign_Documents', | |
| 137 | + 'zohosign__ZohoSign_Recipients' | |
| 138 | + ]; | |
| 139 | + $modulesMetaApiEndpoint = "{$queryParams->tokenDetails->api_domain}/crm/v2/settings/modules"; | |
| 140 | + $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; | |
| 141 | + $modulesMetaResponse = HttpHelper::get($modulesMetaApiEndpoint, null, $authorizationHeader); | |
| 142 | + if (!is_wp_error($modulesMetaResponse) && (empty($modulesMetaResponse->status) || (!empty($modulesMetaResponse->status) && 'error' !== $modulesMetaResponse->status))) { | |
| 143 | + $retriveModuleData = $modulesMetaResponse->modules; | |
| 43 | 144 | |
| 44 | - /** | |
| 45 | - * Process ajax request for generate_token | |
| 46 | - * | |
| 47 | - * @return JSON zoho crm api response and status | |
| 48 | - */ | |
| 49 | - public static function generateTokens() | |
| 50 | - { | |
| 51 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) { | |
| 52 | - $inputJSON = file_get_contents('php://input'); | |
| 53 | - $requestsParams = json_decode($inputJSON); | |
| 54 | - if ( | |
| 55 | - empty($requestsParams->{'accounts-server'}) | |
| 56 | - || empty($requestsParams->dataCenter) | |
| 57 | - || empty($requestsParams->clientId) | |
| 58 | - || empty($requestsParams->clientSecret) | |
| 59 | - || empty($requestsParams->redirectURI) | |
| 60 | - || empty($requestsParams->code) | |
| 61 | - ) { | |
| 62 | - wp_send_json_error( | |
| 63 | - __( | |
| 64 | - 'Requested parameter is empty', | |
| 65 | - 'bitforms' | |
| 66 | - ), | |
| 67 | - 400 | |
| 68 | - ); | |
| 69 | - } | |
| 70 | - $apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token'; | |
| 71 | - $requestParams = array( | |
| 72 | - "grant_type" => "authorization_code", | |
| 73 | - "client_id" => $requestsParams->clientId, | |
| 74 | - "client_secret" => $requestsParams->clientSecret, | |
| 75 | - "redirect_uri" => \urldecode($requestsParams->redirectURI), | |
| 76 | - "code" => $requestsParams->code | |
| 77 | - ); | |
| 78 | - $apiResponse = HttpHelper::post($apiEndpoint, $requestParams); | |
| 79 | - if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { | |
| 80 | - wp_send_json_error( | |
| 81 | - empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, | |
| 82 | - 400 | |
| 83 | - ); | |
| 84 | - } | |
| 85 | - $apiResponse->generates_on = \time(); | |
| 86 | - wp_send_json_success($apiResponse, 200); | |
| 87 | - } else { | |
| 88 | - wp_send_json_error( | |
| 89 | - __( | |
| 90 | - 'Token expired', | |
| 91 | - 'bitform' | |
| 92 | - ), | |
| 93 | - 401 | |
| 94 | - ); | |
| 145 | + $allModules = []; | |
| 146 | + foreach ($retriveModuleData as $key => $value) { | |
| 147 | + if ((!empty($value->inventory_template_supported) && $value->inventory_template_supported) || \in_array($value->api_name, $zohosIntegratedModules)) { | |
| 148 | + continue; | |
| 149 | + } | |
| 150 | + if (!empty($value->api_supported) && $value->api_supported && !empty($value->editable) && $value->editable) { | |
| 151 | + $allModules[$value->api_name] = (object) [ | |
| 152 | + 'plural_label' => $value->plural_label, | |
| 153 | + 'triggers_supported' => $value->triggers_supported, | |
| 154 | + 'quick_create' => $value->quick_create, | |
| 155 | + ]; | |
| 156 | + } | |
| 95 | 157 | } |
| 158 | + uksort($allModules, 'strnatcasecmp'); | |
| 159 | + $response['modules'] = $allModules; | |
| 160 | + } else { | |
| 161 | + wp_send_json_error( | |
| 162 | + empty($modulesMetaResponse->error) ? 'Unknown' : $modulesMetaResponse->error, | |
| 163 | + 400 | |
| 164 | + ); | |
| 165 | + } | |
| 166 | + if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { | |
| 167 | + ZohoCRMHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['modules']); | |
| 168 | + } | |
| 169 | + wp_send_json_success($response, 200); | |
| 170 | + } else { | |
| 171 | + wp_send_json_error( | |
| 172 | + __( | |
| 173 | + 'Token expired', | |
| 174 | + 'bit-form' | |
| 175 | + ), | |
| 176 | + 401 | |
| 177 | + ); | |
| 96 | 178 | } |
| 97 | - /** | |
| 98 | - * Process ajax request for refresh crm modules | |
| 99 | - * | |
| 100 | - * @return JSON crm module data | |
| 101 | - */ | |
| 102 | - public static function refreshModulesAjaxHelper() | |
| 103 | - { | |
| 104 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) { | |
| 105 | - $inputJSON = file_get_contents('php://input'); | |
| 106 | - $queryParams = json_decode($inputJSON); | |
| 107 | - if ( | |
| 108 | - empty($queryParams->tokenDetails) | |
| 109 | - || empty($queryParams->dataCenter) | |
| 110 | - || empty($queryParams->clientId) | |
| 111 | - || empty($queryParams->clientSecret) | |
| 112 | - ) { | |
| 113 | - wp_send_json_error( | |
| 114 | - __( | |
| 115 | - 'Requested parameter is empty', | |
| 116 | - 'bitforms' | |
| 117 | - ), | |
| 118 | - 400 | |
| 119 | - ); | |
| 120 | - } | |
| 121 | - $response = []; | |
| 122 | - if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 123 | - $response['tokenDetails'] = ZohoCRMHandler::_refreshAccessToken($queryParams); | |
| 124 | - } | |
| 125 | - $zohosIntegratedModules = array( | |
| 126 | - 'zohosign__ZohoSign_Document_Events', | |
| 127 | - 'zohoshowtime__ShowTime_Sessions', | |
| 128 | - 'zohoshowtime__Zoho_ShowTime', | |
| 129 | - 'zohosign__ZohoSign_Documents', | |
| 130 | - 'zohosign__ZohoSign_Recipients' | |
| 131 | - ); | |
| 132 | - $modulesMetaApiEndpoint = "{$queryParams->tokenDetails->api_domain}/crm/v2/settings/modules"; | |
| 133 | - $authorizationHeader["Authorization"] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; | |
| 134 | - $modulesMetaResponse = HttpHelper::get($modulesMetaApiEndpoint, null, $authorizationHeader); | |
| 135 | - if (!is_wp_error($modulesMetaResponse) && (empty($modulesMetaResponse->status) || (!empty($modulesMetaResponse->status) && $modulesMetaResponse->status !== 'error'))) { | |
| 136 | - $retriveModuleData = $modulesMetaResponse->modules; | |
| 179 | + } | |
| 137 | 180 | |
| 138 | - $allModules = []; | |
| 139 | - foreach ($retriveModuleData as $key => $value) { | |
| 140 | - if ((!empty($value->inventory_template_supported) && $value->inventory_template_supported) || \in_array($value->api_name, $zohosIntegratedModules)) { | |
| 141 | - continue; | |
| 142 | - } | |
| 143 | - if (!empty($value->api_supported) && $value->api_supported && !empty($value->editable) && $value->editable) { | |
| 144 | - $allModules[$value->api_name] = (object) array( | |
| 145 | - 'plural_label' => $value->plural_label, | |
| 146 | - 'triggers_supported' => $value->triggers_supported, | |
| 147 | - 'quick_create' => $value->quick_create, | |
| 148 | - ); | |
| 149 | - } | |
| 181 | + /** | |
| 182 | + * Process ajax request for refesh crm layouts | |
| 183 | + * | |
| 184 | + * @return JSON crm layout data | |
| 185 | + */ | |
| 186 | + public static function refreshLayoutsAjaxHelper() | |
| 187 | + { | |
| 188 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) { | |
| 189 | + $inputJSON = file_get_contents('php://input'); | |
| 190 | + $queryParams = json_decode($inputJSON); | |
| 191 | + if ( | |
| 192 | + empty($queryParams->module) | |
| 193 | + || empty($queryParams->tokenDetails) | |
| 194 | + || empty($queryParams->dataCenter) | |
| 195 | + || empty($queryParams->clientId) | |
| 196 | + || empty($queryParams->clientSecret) | |
| 197 | + ) { | |
| 198 | + wp_send_json_error( | |
| 199 | + __( | |
| 200 | + 'Requested parameter is empty', | |
| 201 | + 'bit-form' | |
| 202 | + ), | |
| 203 | + 400 | |
| 204 | + ); | |
| 205 | + } | |
| 206 | + $response = []; | |
| 207 | + if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 208 | + $response['tokenDetails'] = ZohoCRMHandler::_refreshAccessToken($queryParams); | |
| 209 | + } | |
| 210 | + $layoutsMetaApiEndpoint = "{$queryParams->tokenDetails->api_domain}/crm/v2/settings/layouts"; | |
| 211 | + $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; | |
| 212 | + $requiredParams['module'] = $queryParams->module; | |
| 213 | + $layoutsMetaResponse = HttpHelper::get($layoutsMetaApiEndpoint, $requiredParams, $authorizationHeader); | |
| 214 | + if (!is_wp_error($layoutsMetaResponse) && (empty($layoutsMetaResponse->status) || (!empty($layoutsMetaResponse->status) && 'error' !== $layoutsMetaResponse->status))) { | |
| 215 | + $retriveLayoutsData = $layoutsMetaResponse->layouts; | |
| 216 | + $layouts = []; | |
| 217 | + foreach ($retriveLayoutsData as $layoutKey => $layoutValue) { | |
| 218 | + $fields = []; | |
| 219 | + $fileUploadFields = []; | |
| 220 | + $requiredFields = []; | |
| 221 | + $requiredFileUploadFiles = []; | |
| 222 | + $uniqueFields = []; | |
| 223 | + foreach ($layoutValue->sections as $sectionKey => $sectionValue) { | |
| 224 | + foreach ($sectionValue->fields as $fieldKey => $fieldDetails) { | |
| 225 | + if (empty($fieldDetails->subform) && !empty($fieldDetails->api_name) && !empty($fieldDetails->view_type->create) && $fieldDetails->view_type->create && 'ownerlookup' !== $fieldDetails->data_type) { | |
| 226 | + if ('fileupload' === $fieldDetails->data_type) { | |
| 227 | + $fileUploadFields[$fieldDetails->api_name] = (object) [ | |
| 228 | + 'display_label' => $fieldDetails->display_label, | |
| 229 | + 'length' => $fieldDetails->length, | |
| 230 | + 'visible' => $fieldDetails->visible, | |
| 231 | + 'json_type' => $fieldDetails->json_type, | |
| 232 | + 'data_type' => $fieldDetails->data_type, | |
| 233 | + 'required' => $fieldDetails->required | |
| 234 | + ]; | |
| 235 | + } else { | |
| 236 | + $fields[$fieldDetails->api_name] = (object) [ | |
| 237 | + 'display_label' => $fieldDetails->display_label, | |
| 238 | + 'length' => $fieldDetails->length, | |
| 239 | + 'visible' => $fieldDetails->visible, | |
| 240 | + 'json_type' => $fieldDetails->json_type, | |
| 241 | + 'data_type' => $fieldDetails->data_type, | |
| 242 | + 'required' => $fieldDetails->required | |
| 243 | + ]; | |
| 150 | 244 | } |
| 151 | - uksort($allModules, 'strnatcasecmp'); | |
| 152 | - $response["modules"] = $allModules; | |
| 153 | - } else { | |
| 154 | - wp_send_json_error( | |
| 155 | - empty($modulesMetaResponse->error) ? 'Unknown' : $modulesMetaResponse->error, | |
| 156 | - 400 | |
| 157 | - ); | |
| 245 | + | |
| 246 | + if (!empty($fieldDetails->required) && $fieldDetails->required) { | |
| 247 | + if ('fileupload' === $fieldDetails->data_type) { | |
| 248 | + $requiredFileUploadFiles[] = $fieldDetails->api_name; | |
| 249 | + } elseif ('Parent_Id' !== $fieldDetails->api_name) { | |
| 250 | + $requiredFields[] = $fieldDetails->api_name; | |
| 251 | + } | |
| 252 | + } | |
| 253 | + if (!empty($fieldDetails->unique) && count((array)$fieldDetails->unique)) { | |
| 254 | + $uniqueFields[] = $fieldDetails->api_name; | |
| 255 | + } | |
| 256 | + } | |
| 158 | 257 | } |
| 159 | - if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { | |
| 160 | - // var_dump($queryParams->formID, $queryParams->id); | |
| 161 | - ZohoCRMHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['modules']); | |
| 162 | - } | |
| 163 | - wp_send_json_success($response, 200); | |
| 164 | - } else { | |
| 165 | - wp_send_json_error( | |
| 166 | - __( | |
| 167 | - 'Token expired', | |
| 168 | - 'bitform' | |
| 169 | - ), | |
| 170 | - 401 | |
| 171 | - ); | |
| 258 | + } | |
| 259 | + uksort($fields, 'strnatcasecmp'); | |
| 260 | + uksort($fileUploadFields, 'strnatcasecmp'); | |
| 261 | + usort($requiredFields, 'strnatcasecmp'); | |
| 262 | + usort($requiredFileUploadFiles, 'strnatcasecmp'); | |
| 263 | + | |
| 264 | + $layouts[$layoutValue->name] = (object) [ | |
| 265 | + 'visible' => $layoutValue->visible, | |
| 266 | + 'fields' => $fields, | |
| 267 | + 'required' => $requiredFields, | |
| 268 | + 'unique' => $uniqueFields, | |
| 269 | + 'id' => $layoutValue->id, | |
| 270 | + 'fileUploadFields' => $fileUploadFields, | |
| 271 | + 'requiredFileUploadFields' => $requiredFileUploadFiles | |
| 272 | + ]; | |
| 172 | 273 | } |
| 274 | + uksort($layouts, 'strnatcasecmp'); | |
| 275 | + $response['layouts'] = $layouts; | |
| 276 | + } else { | |
| 277 | + wp_send_json_error( | |
| 278 | + 'error' === $layoutsMetaResponse->status ? $layoutsMetaResponse->message : 'Unknown', | |
| 279 | + 400 | |
| 280 | + ); | |
| 281 | + } | |
| 282 | + if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { | |
| 283 | + $response['queryModule'] = $queryParams->module; | |
| 284 | + ZohoCRMHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); | |
| 285 | + } | |
| 286 | + wp_send_json_success($response, 200); | |
| 287 | + } else { | |
| 288 | + wp_send_json_error( | |
| 289 | + __( | |
| 290 | + 'Token expired', | |
| 291 | + 'bit-form' | |
| 292 | + ), | |
| 293 | + 401 | |
| 294 | + ); | |
| 173 | 295 | } |
| 174 | - /** | |
| 175 | - * Process ajax request for refesh crm layouts | |
| 176 | - * | |
| 177 | - * @return JSON crm layout data | |
| 178 | - */ | |
| 179 | - public static function refreshLayoutsAjaxHelper() | |
| 180 | - { | |
| 181 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) { | |
| 182 | - $inputJSON = file_get_contents('php://input'); | |
| 183 | - $queryParams = json_decode($inputJSON); | |
| 184 | - if ( | |
| 185 | - empty($queryParams->module) | |
| 186 | - || empty($queryParams->tokenDetails) | |
| 187 | - || empty($queryParams->dataCenter) | |
| 188 | - || empty($queryParams->clientId) | |
| 189 | - || empty($queryParams->clientSecret) | |
| 190 | - ) { | |
| 191 | - wp_send_json_error( | |
| 192 | - __( | |
| 193 | - 'Requested parameter is empty', | |
| 194 | - 'bitforms' | |
| 195 | - ), | |
| 196 | - 400 | |
| 197 | - ); | |
| 198 | - } | |
| 199 | - $response = []; | |
| 200 | - if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 201 | - $response['tokenDetails'] = ZohoCRMHandler::_refreshAccessToken($queryParams); | |
| 202 | - } | |
| 203 | - $layoutsMetaApiEndpoint = "{$queryParams->tokenDetails->api_domain}/crm/v2/settings/layouts"; | |
| 204 | - $authorizationHeader["Authorization"] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; | |
| 205 | - $requiredParams['module'] = $queryParams->module; | |
| 206 | - $layoutsMetaResponse = HttpHelper::get($layoutsMetaApiEndpoint, $requiredParams, $authorizationHeader); | |
| 207 | - if (!is_wp_error($layoutsMetaResponse) && (empty($layoutsMetaResponse->status) || (!empty($layoutsMetaResponse->status) && $layoutsMetaResponse->status !== 'error'))) { | |
| 208 | - $retriveLayoutsData = $layoutsMetaResponse->layouts; | |
| 209 | - $layouts = []; | |
| 210 | - foreach ($retriveLayoutsData as $layoutKey => $layoutValue) { | |
| 211 | - $fields = []; | |
| 212 | - $fileUploadFields = []; | |
| 213 | - $requiredFields = []; | |
| 214 | - $requiredFileUploadFiles = []; | |
| 215 | - $uniqueFields = []; | |
| 216 | - foreach ($layoutValue->sections as $sectionKey => $sectionValue) { | |
| 217 | - foreach ($sectionValue->fields as $fieldKey => $fieldDetails) { | |
| 218 | - if (empty($fieldDetails->subform) && !empty($fieldDetails->api_name) && !empty($fieldDetails->view_type->create) && $fieldDetails->view_type->create && $fieldDetails->data_type !== 'ownerlookup') { | |
| 219 | - if ($fieldDetails->data_type === 'fileupload') { | |
| 220 | - $fileUploadFields[$fieldDetails->api_name] = (object) array( | |
| 221 | - 'display_label' => $fieldDetails->display_label, | |
| 222 | - 'length' => $fieldDetails->length, | |
| 223 | - 'visible' => $fieldDetails->visible, | |
| 224 | - 'json_type' => $fieldDetails->json_type, | |
| 225 | - 'data_type' => $fieldDetails->data_type, | |
| 226 | - 'required' => $fieldDetails->required | |
| 227 | - ); | |
| 228 | - } else { | |
| 229 | - $fields[$fieldDetails->api_name] = (object) array( | |
| 230 | - 'display_label' => $fieldDetails->display_label, | |
| 231 | - 'length' => $fieldDetails->length, | |
| 232 | - 'visible' => $fieldDetails->visible, | |
| 233 | - 'json_type' => $fieldDetails->json_type, | |
| 234 | - 'data_type' => $fieldDetails->data_type, | |
| 235 | - 'required' => $fieldDetails->required | |
| 236 | - ); | |
| 237 | - } | |
| 296 | + } | |
| 238 | 297 | |
| 239 | - if (!empty($fieldDetails->required) && $fieldDetails->required) { | |
| 240 | - if ($fieldDetails->data_type === 'fileupload') { | |
| 241 | - $requiredFileUploadFiles[] = $fieldDetails->api_name; | |
| 242 | - } else if ($fieldDetails->api_name !== 'Parent_Id') { | |
| 243 | - $requiredFields[] = $fieldDetails->api_name; | |
| 244 | - } | |
| 245 | - } | |
| 246 | - if (!empty($fieldDetails->unique) && count((array)$fieldDetails->unique)) { | |
| 247 | - $uniqueFields[] = $fieldDetails->api_name; | |
| 248 | - } | |
| 249 | - } | |
| 250 | - } | |
| 251 | - } | |
| 252 | - uksort($fields, 'strnatcasecmp'); | |
| 253 | - uksort($fileUploadFields, 'strnatcasecmp'); | |
| 254 | - usort($requiredFields, 'strnatcasecmp'); | |
| 255 | - usort($requiredFileUploadFiles, 'strnatcasecmp'); | |
| 298 | + /** | |
| 299 | + * Helps to refresh zoho crm access_token | |
| 300 | + * | |
| 301 | + * @param object $apiData Contains required data for refresh access token | |
| 302 | + * @return string json | boolean $tokenDetails API token details | |
| 303 | + */ | |
| 304 | + protected static function _refreshAccessToken($apiData) | |
| 305 | + { | |
| 306 | + if ( | |
| 307 | + empty($apiData->dataCenter) | |
| 308 | + || empty($apiData->clientId) | |
| 309 | + || empty($apiData->clientSecret) | |
| 310 | + || empty($apiData->tokenDetails) | |
| 311 | + ) { | |
| 312 | + return false; | |
| 313 | + } | |
| 314 | + $tokenDetails = $apiData->tokenDetails; | |
| 256 | 315 | |
| 257 | - $layouts[$layoutValue->name] = (object) array( | |
| 258 | - 'visible' => $layoutValue->visible, | |
| 259 | - 'fields' => $fields, | |
| 260 | - 'required' => $requiredFields, | |
| 261 | - 'unique' => $uniqueFields, | |
| 262 | - 'id' => $layoutValue->id, | |
| 263 | - 'fileUploadFields' => $fileUploadFields, | |
| 264 | - 'requiredFileUploadFields' => $requiredFileUploadFiles | |
| 265 | - ); | |
| 266 | - } | |
| 267 | - uksort($layouts, 'strnatcasecmp'); | |
| 268 | - $response["layouts"] = $layouts; | |
| 269 | - } else { | |
| 270 | - wp_send_json_error( | |
| 271 | - $layoutsMetaResponse->status === 'error' ? $layoutsMetaResponse->message : 'Unknown', | |
| 272 | - 400 | |
| 273 | - ); | |
| 274 | - } | |
| 275 | - if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { | |
| 276 | - // var_dump($queryParams->formID, $queryParams->id); | |
| 277 | - $response["queryModule"] = $queryParams->module; | |
| 278 | - ZohoCRMHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); | |
| 279 | - } | |
| 280 | - wp_send_json_success($response, 200); | |
| 281 | - } else { | |
| 282 | - wp_send_json_error( | |
| 283 | - __( | |
| 284 | - 'Token expired', | |
| 285 | - 'bitform' | |
| 286 | - ), | |
| 287 | - 401 | |
| 288 | - ); | |
| 289 | - } | |
| 316 | + $dataCenter = $apiData->dataCenter; | |
| 317 | + $apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token"; | |
| 318 | + $requestParams = [ | |
| 319 | + 'grant_type' => 'refresh_token', | |
| 320 | + 'client_id' => $apiData->clientId, | |
| 321 | + 'client_secret' => $apiData->clientSecret, | |
| 322 | + 'refresh_token' => $tokenDetails->refresh_token, | |
| 323 | + ]; | |
| 324 | + | |
| 325 | + $apiResponse = HttpHelper::post($apiEndpoint, $requestParams); | |
| 326 | + if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { | |
| 327 | + return false; | |
| 290 | 328 | } |
| 329 | + $tokenDetails->generates_on = \time(); | |
| 330 | + $tokenDetails->access_token = $apiResponse->access_token; | |
| 331 | + return $tokenDetails; | |
| 332 | + } | |
| 291 | 333 | |
| 292 | - /** | |
| 293 | - * Helps to refresh zoho crm access_token | |
| 294 | - * | |
| 295 | - * @param Array $apiData Contains required data for refresh access token | |
| 296 | - * @return JSON $tokenDetails API token details | |
| 297 | - */ | |
| 298 | - protected static function _refreshAccessToken($apiData) | |
| 299 | - { | |
| 300 | - if ( | |
| 301 | - empty($apiData->dataCenter) | |
| 302 | - || empty($apiData->clientId) | |
| 303 | - || empty($apiData->clientSecret) | |
| 304 | - || empty($apiData->tokenDetails) | |
| 305 | - ) { | |
| 306 | - return false; | |
| 307 | - } | |
| 308 | - $tokenDetails = $apiData->tokenDetails; | |
| 334 | + /** | |
| 335 | + * Save updated access_token to avoid unnecessary token generation | |
| 336 | + * | |
| 337 | + * @param integer $fromID ID of Integration related form | |
| 338 | + * @param integer $integrationID ID of Zoho crm Integration | |
| 339 | + * @param object $tokenDetails refreshed token info | |
| 340 | + * | |
| 341 | + * @return null | |
| 342 | + */ | |
| 343 | + protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) | |
| 344 | + { | |
| 345 | + if (empty($formID) || empty($integrationID)) { | |
| 346 | + return; | |
| 347 | + } | |
| 309 | 348 | |
| 310 | - $dataCenter = $apiData->dataCenter; | |
| 311 | - $apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token"; | |
| 312 | - $requestParams = array( | |
| 313 | - "grant_type" => "refresh_token", | |
| 314 | - "client_id" => $apiData->clientId, | |
| 315 | - "client_secret" => $apiData->clientSecret, | |
| 316 | - "refresh_token" => $tokenDetails->refresh_token, | |
| 317 | - ); | |
| 349 | + $integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); | |
| 350 | + $zcrmDetails = $integrationHandler->getAIntegration($integrationID); | |
| 318 | 351 | |
| 319 | - $apiResponse = HttpHelper::post($apiEndpoint, $requestParams); | |
| 320 | - if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { | |
| 321 | - return false; | |
| 322 | - } | |
| 323 | - $tokenDetails->generates_on = \time(); | |
| 324 | - $tokenDetails->access_token = $apiResponse->access_token; | |
| 325 | - return $tokenDetails; | |
| 352 | + if (is_wp_error($zcrmDetails)) { | |
| 353 | + return; | |
| 326 | 354 | } |
| 355 | + $newDetails = json_decode($zcrmDetails[0]->integration_details); | |
| 327 | 356 | |
| 328 | - /** | |
| 329 | - * Save updated access_token to avoid unnecessary token generation | |
| 330 | - * | |
| 331 | - * @param Integer $fromID ID of Integration related form | |
| 332 | - * @param Integer $integrationID ID of Zoho crm Integration | |
| 333 | - * @param Obeject $tokenDetails refreshed token info | |
| 334 | - * | |
| 335 | - * @return null | |
| 336 | - */ | |
| 337 | - protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) | |
| 338 | - { | |
| 339 | - if (empty($formID) || empty($integrationID)) { | |
| 340 | - return; | |
| 341 | - } | |
| 357 | + $newDetails->tokenDetails = $tokenDetails; | |
| 358 | + if (!empty($others['modules'])) { | |
| 359 | + $newDetails->default->modules = $others['modules']; | |
| 360 | + } | |
| 361 | + if (!empty($others['layouts']) && !empty($others['queryModule'])) { | |
| 362 | + $newDetails->default->layouts->{$others['queryModule']} = $others['layouts']; | |
| 363 | + } | |
| 342 | 364 | |
| 343 | - $integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); | |
| 344 | - $zcrmDetails = $integrationHandler->getAIntegration($integrationID); | |
| 365 | + $integrationHandler->updateIntegration($integrationID, $zcrmDetails[0]->integration_name, 'Zoho CRM', wp_json_encode($newDetails), 'form'); | |
| 366 | + } | |
| 345 | 367 | |
| 346 | - // var_dump($zcrmDetails, $formID, $integrationID, $tokenDetails, $others); | |
| 347 | - if (is_wp_error($zcrmDetails)) { | |
| 348 | - return; | |
| 368 | + /** | |
| 369 | + * Process ajax request for refresh crm users | |
| 370 | + * | |
| 371 | + * @return JSON crm users data | |
| 372 | + */ | |
| 373 | + public static function refreshUsersAjaxHelper() | |
| 374 | + { | |
| 375 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 376 | + $inputJSON = file_get_contents('php://input'); | |
| 377 | + $queryParams = json_decode($inputJSON); | |
| 378 | + if ( | |
| 379 | + empty($queryParams->tokenDetails) | |
| 380 | + || empty($queryParams->dataCenter) | |
| 381 | + || empty($queryParams->clientId) | |
| 382 | + || empty($queryParams->clientSecret) | |
| 383 | + ) { | |
| 384 | + wp_send_json_error( | |
| 385 | + __( | |
| 386 | + 'Requested parameter is empty', | |
| 387 | + 'bitformpro' | |
| 388 | + ), | |
| 389 | + 400 | |
| 390 | + ); | |
| 391 | + } | |
| 392 | + $response = []; | |
| 393 | + if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 394 | + $response['tokenDetails'] = self::_refreshAccessToken($queryParams); | |
| 395 | + } | |
| 396 | + $usersApiEndpoint = "{$queryParams->tokenDetails->api_domain}/crm/v2/users?type=ActiveConfirmedUsers"; | |
| 397 | + $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; | |
| 398 | + $retrivedUsersData = []; | |
| 399 | + $usersResponse = (object) []; | |
| 400 | + do { | |
| 401 | + $requiredParams = []; | |
| 402 | + if (!empty($usersResponse->users)) { | |
| 403 | + if (!empty($retrivedUsersData)) { | |
| 404 | + $retrivedUsersData = array_merge($retrivedUsersData, $usersResponse->users); | |
| 405 | + } else { | |
| 406 | + $retrivedUsersData = $usersResponse->users; | |
| 407 | + } | |
| 349 | 408 | } |
| 350 | - $newDetails = json_decode($zcrmDetails[0]->integration_details); | |
| 351 | - | |
| 352 | - $newDetails->tokenDetails = $tokenDetails; | |
| 353 | - if (!empty($others['modules'])) { | |
| 354 | - $newDetails->default->modules = $others['modules']; | |
| 409 | + if (!empty($usersResponse->info->more_records) && $usersResponse->info->more_records) { | |
| 410 | + $requiredParams['page'] = intval($usersResponse->info->page) + 1; | |
| 355 | 411 | } |
| 356 | - if (!empty($others['layouts']) && !empty($others['queryModule'])) { | |
| 357 | - $newDetails->default->layouts->{$others['queryModule']} = $others['layouts']; | |
| 412 | + $usersResponse = HttpHelper::get($usersApiEndpoint, $requiredParams, $authorizationHeader); | |
| 413 | + } while (null === $usersResponse || (!empty($usersResponse->info->more_records) && $usersResponse->info->more_records)); | |
| 414 | + if (empty($requiredParams) && !is_wp_error($usersResponse)) { | |
| 415 | + $retrivedUsersData = $usersResponse->users; | |
| 416 | + } | |
| 417 | + if (!is_wp_error($usersResponse) && !empty($retrivedUsersData)) { | |
| 418 | + $users = []; | |
| 419 | + foreach ($retrivedUsersData as $userKey => $userValue) { | |
| 420 | + $users[$userValue->full_name] = (object) [ | |
| 421 | + 'full_name' => $userValue->full_name, | |
| 422 | + 'id' => $userValue->id, | |
| 423 | + ]; | |
| 358 | 424 | } |
| 425 | + uksort($users, 'strnatcasecmp'); | |
| 426 | + $response['users'] = $users; | |
| 427 | + } else { | |
| 428 | + wp_send_json_error( | |
| 429 | + 'error' === $usersResponse->status ? $usersResponse->message : 'Unknown', | |
| 430 | + 400 | |
| 431 | + ); | |
| 432 | + } | |
| 433 | + if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { | |
| 434 | + self::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); | |
| 435 | + } | |
| 436 | + wp_send_json_success($response, 200); | |
| 437 | + } else { | |
| 438 | + wp_send_json_error( | |
| 439 | + __( | |
| 440 | + 'Token expired', | |
| 441 | + 'bitformpro' | |
| 442 | + ), | |
| 443 | + 401 | |
| 444 | + ); | |
| 445 | + } | |
| 446 | + } | |
| 359 | 447 | |
| 360 | - // var_dump($newDetails); | |
| 448 | + /** | |
| 449 | + * Process ajax request for refresh tags of a module | |
| 450 | + * | |
| 451 | + * @return JSON crm Tags for a module | |
| 452 | + */ | |
| 453 | + public static function refreshTagListAjaxHelper() | |
| 454 | + { | |
| 455 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 456 | + $inputJSON = file_get_contents('php://input'); | |
| 457 | + $queryParams = json_decode($inputJSON); | |
| 458 | + if ( | |
| 459 | + empty($queryParams->module) | |
| 460 | + || empty($queryParams->tokenDetails) | |
| 461 | + || empty($queryParams->dataCenter) | |
| 462 | + || empty($queryParams->clientId) | |
| 463 | + || empty($queryParams->clientSecret) | |
| 464 | + ) { | |
| 465 | + wp_send_json_error( | |
| 466 | + __( | |
| 467 | + 'Requested parameter is empty', | |
| 468 | + 'bitformpro' | |
| 469 | + ), | |
| 470 | + 400 | |
| 471 | + ); | |
| 472 | + } | |
| 473 | + $response = []; | |
| 474 | + if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 475 | + $response['tokenDetails'] = self::_refreshAccessToken($queryParams); | |
| 476 | + } | |
| 477 | + $tokenDetails = empty($response['tokenDetails']) ? $queryParams->tokenDetails : $response['tokenDetails']; | |
| 478 | + $tagApiHelper = new TagApiHelper($tokenDetails, $queryParams->module); | |
| 479 | + $tagListApiResponse = $tagApiHelper->getTagList(); | |
| 480 | + if (!is_wp_error($tagListApiResponse)) { | |
| 481 | + usort($tagListApiResponse, 'strnatcasecmp'); | |
| 482 | + $response['tags'] = $tagListApiResponse; | |
| 483 | + } else { | |
| 484 | + wp_send_json_error( | |
| 485 | + is_wp_error($tagListApiResponse) ? $tagListApiResponse->get_error_message() : (empty($tagListApiResponse) ? __('Tag is empty', 'bitformpro') : 'Unknown'), | |
| 486 | + empty($tagListApiResponse) ? 204 : 400 | |
| 487 | + ); | |
| 488 | + } | |
| 489 | + if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { | |
| 490 | + self::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); | |
| 491 | + } | |
| 492 | + wp_send_json_success($response, 200); | |
| 493 | + } else { | |
| 494 | + wp_send_json_error( | |
| 495 | + __( | |
| 496 | + 'Token expired', | |
| 497 | + 'bitformpro' | |
| 498 | + ), | |
| 499 | + 401 | |
| 500 | + ); | |
| 501 | + } | |
| 502 | + } | |
| 361 | 503 | |
| 362 | - $integrationHandler->updateIntegration($integrationID, $zcrmDetails[0]->integration_name, 'Zoho CRM', \json_encode($newDetails), 'form'); | |
| 504 | + /** | |
| 505 | + * Process ajax request to get assignment rules of a Zoho CRM module | |
| 506 | + * | |
| 507 | + * @return JSON crm assignment rules data | |
| 508 | + */ | |
| 509 | + public static function getAssignmentRulesAjaxHelper() | |
| 510 | + { | |
| 511 | + if (true || isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 512 | + $inputJSON = file_get_contents('php://input'); | |
| 513 | + $queryParams = json_decode($inputJSON); | |
| 514 | + if ( | |
| 515 | + empty($queryParams->module) | |
| 516 | + || empty($queryParams->tokenDetails) | |
| 517 | + || empty($queryParams->dataCenter) | |
| 518 | + || empty($queryParams->clientId) | |
| 519 | + || empty($queryParams->clientSecret) | |
| 520 | + ) { | |
| 521 | + wp_send_json_error( | |
| 522 | + __( | |
| 523 | + 'Requested parameter is empty', | |
| 524 | + 'bitformpro' | |
| 525 | + ), | |
| 526 | + 400 | |
| 527 | + ); | |
| 528 | + } | |
| 529 | + $response = []; | |
| 530 | + if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 531 | + $response['tokenDetails'] = self::_refreshAccessToken($queryParams); | |
| 532 | + } | |
| 533 | + $metaDataApiHelper = new MetaDataApiHelper($queryParams->tokenDetails, true); | |
| 534 | + $assignmentRulesResponse = $metaDataApiHelper->getAssignmentRules($queryParams->module); | |
| 535 | + if ( | |
| 536 | + !is_wp_error($assignmentRulesResponse) | |
| 537 | + && !empty($assignmentRulesResponse) | |
| 538 | + && empty($assignmentRulesResponse->status) | |
| 539 | + ) { | |
| 540 | + uksort($assignmentRulesResponse, 'strnatcasecmp'); | |
| 541 | + $response['assignmentRules'] = $assignmentRulesResponse; | |
| 542 | + } else { | |
| 543 | + wp_send_json_error( | |
| 544 | + !empty($assignmentRulesResponse->status) | |
| 545 | + && 'error' === $assignmentRulesResponse->status ? | |
| 546 | + $assignmentRulesResponse->message : (empty($assignmentRulesResponse) ? __('Assignment rules is empty', 'bitformpro') : 'Unknown'), | |
| 547 | + empty($assignmentRulesResponse) ? 204 : 400 | |
| 548 | + ); | |
| 549 | + } | |
| 550 | + if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { | |
| 551 | + // $response["queryModule"] = $queryParams->module; | |
| 552 | + self::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); | |
| 553 | + } | |
| 554 | + wp_send_json_success($response, 200); | |
| 555 | + } else { | |
| 556 | + wp_send_json_error( | |
| 557 | + __( | |
| 558 | + 'Token expired', | |
| 559 | + 'bitformpro' | |
| 560 | + ), | |
| 561 | + 401 | |
| 562 | + ); | |
| 363 | 563 | } |
| 564 | + } | |
| 364 | 565 | |
| 365 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 366 | - { | |
| 566 | + /** | |
| 567 | + * Process ajax request to get realted lists of a Zoho CRM module | |
| 568 | + * | |
| 569 | + * @return JSON crm layout data | |
| 570 | + */ | |
| 571 | + public static function getRelatedListsAjaxHelper() | |
| 572 | + { | |
| 573 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 574 | + $inputJSON = file_get_contents('php://input'); | |
| 575 | + $queryParams = json_decode($inputJSON); | |
| 576 | + if ( | |
| 577 | + empty($queryParams->module) | |
| 578 | + || empty($queryParams->tokenDetails) | |
| 579 | + || empty($queryParams->dataCenter) | |
| 580 | + || empty($queryParams->clientId) | |
| 581 | + || empty($queryParams->clientSecret) | |
| 582 | + ) { | |
| 583 | + wp_send_json_error( | |
| 584 | + __( | |
| 585 | + 'Requested parameter is empty', | |
| 586 | + 'bitformpro' | |
| 587 | + ), | |
| 588 | + 400 | |
| 589 | + ); | |
| 590 | + } | |
| 591 | + $response = []; | |
| 592 | + if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 593 | + $response['tokenDetails'] = self::_refreshAccessToken($queryParams); | |
| 594 | + } | |
| 595 | + $metaDataApiHelper = new MetaDataApiHelper($queryParams->tokenDetails); | |
| 596 | + $relatedListResponse = $metaDataApiHelper->getRelatedLists($queryParams->module); | |
| 597 | + if ( | |
| 598 | + !is_wp_error($relatedListResponse) | |
| 599 | + && !empty($relatedListResponse) | |
| 600 | + && empty($relatedListResponse->status) | |
| 601 | + ) { | |
| 602 | + uksort($relatedListResponse, 'strnatcasecmp'); | |
| 603 | + $response['relatedLists'] = $relatedListResponse; | |
| 604 | + } else { | |
| 605 | + wp_send_json_error( | |
| 606 | + !empty($relatedListResponse->status) | |
| 607 | + && 'error' === $relatedListResponse->status ? | |
| 608 | + $relatedListResponse->message : (empty($relatedListResponse) ? __('RelatedList is empty', 'bitformpro') : 'Unknown'), | |
| 609 | + empty($relatedListResponse) ? 204 : 400 | |
| 610 | + ); | |
| 611 | + } | |
| 612 | + if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { | |
| 613 | + // $response["queryModule"] = $queryParams->module; | |
| 614 | + self::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); | |
| 615 | + } | |
| 616 | + wp_send_json_success($response, 200); | |
| 617 | + } else { | |
| 618 | + wp_send_json_error( | |
| 619 | + __( | |
| 620 | + 'Token expired', | |
| 621 | + 'bitformpro' | |
| 622 | + ), | |
| 623 | + 401 | |
| 624 | + ); | |
| 625 | + } | |
| 626 | + } | |
| 367 | 627 | |
| 368 | - $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; | |
| 628 | + public static function addRelatedListV2($zcrmApiResponse, $formID, $entryID, $integID, $logID, $fieldValues, $integrationDetails, $recordApiHelper) | |
| 629 | + { | |
| 630 | + foreach ($integrationDetails->relatedlists as $relatedlist) { | |
| 631 | + // Related List apis.. | |
| 632 | + $relatedListModule = !empty($relatedlist->module) ? $relatedlist->module : ''; | |
| 633 | + $relatedListLayout = !empty($relatedlist->layout) ? $relatedlist->layout : ''; | |
| 634 | + $defaultDataConf = $integrationDetails->default; | |
| 635 | + if (empty($relatedListModule) || empty($relatedListLayout)) { | |
| 636 | + return new WP_Error('REQ_FIELD_EMPTY', __('module, layout are required for zoho crm relatedlist', 'bitformpro')); | |
| 637 | + } | |
| 638 | + $module = $integrationDetails->module; | |
| 639 | + $moduleSingular = \substr($module, 0, \strlen($module) - 1); | |
| 640 | + if (isset($defaultDataConf->layouts->{$relatedListModule}->{$relatedListLayout}->fields->{$module})) { | |
| 641 | + $moduleSingular = $module; | |
| 642 | + } elseif (!isset($defaultDataConf->layouts->{$relatedListModule}->{$relatedListLayout}->fields->{$moduleSingular})) { | |
| 643 | + $moduleSingular = ''; | |
| 644 | + } | |
| 645 | + $relatedListRequired = !empty($defaultDataConf->layouts->{$relatedListModule}->{$relatedListLayout}->required) ? | |
| 646 | + $defaultDataConf->layouts->{$relatedListModule}->{$relatedListLayout}->required : []; | |
| 647 | + $recordID = $zcrmApiResponse->data[0]->details->id; | |
| 648 | + $defaultDataConf->layouts->{$relatedListModule}->{$relatedListLayout}->fields->{'$se_module'} = (object) [ | |
| 649 | + 'length' => 200, | |
| 650 | + 'visible' => true, | |
| 651 | + 'json_type' => 'string', | |
| 652 | + 'data_type' => 'string', | |
| 653 | + ]; | |
| 654 | + $fieldValues['$se_module'] = $module; | |
| 655 | + $relatedlist->field_map[] = (object) | |
| 656 | + [ | |
| 657 | + 'formField' => '$se_module', | |
| 658 | + 'zohoFormField' => '$se_module' | |
| 659 | + ]; | |
| 660 | + if (isset($defaultDataConf->layouts->{$relatedListModule}->{$relatedListLayout}->fields->Parent_Id)) { | |
| 661 | + $fieldValues['Parent_Id'] = (object) ['id' => $recordID]; | |
| 662 | + $relatedlist->field_map[] = (object) | |
| 663 | + [ | |
| 664 | + 'formField' => 'Parent_Id', | |
| 665 | + 'zohoFormField' => 'Parent_Id' | |
| 666 | + ]; | |
| 667 | + } elseif (!empty($moduleSingular)) { | |
| 668 | + $fieldValues[$moduleSingular] = ['id' => $recordID]; | |
| 669 | + $relatedlist->field_map[] = (object) | |
| 670 | + [ | |
| 671 | + 'formField' => $moduleSingular, | |
| 672 | + 'zohoFormField' => $moduleSingular | |
| 673 | + ]; | |
| 674 | + } elseif ('Contacts' === $module) { | |
| 675 | + $fieldValues['Who_Id'] = (object) ['id' => $recordID]; | |
| 676 | + $relatedlist->field_map[] = (object) | |
| 677 | + [ | |
| 678 | + 'formField' => 'Who_Id', | |
| 679 | + 'zohoFormField' => 'Who_Id' | |
| 680 | + ]; | |
| 681 | + } else { | |
| 682 | + $fieldValues['What_Id'] = (object) ['id' => $recordID]; | |
| 683 | + $relatedlist->field_map[] = (object) | |
| 684 | + [ | |
| 685 | + 'formField' => 'What_Id', | |
| 686 | + 'zohoFormField' => 'What_Id' | |
| 687 | + ]; | |
| 688 | + } | |
| 369 | 689 | |
| 370 | - $tokenDetails = $integrationDetails->tokenDetails; | |
| 371 | - $module = $integrationDetails->module; | |
| 372 | - $layout = $integrationDetails->layout; | |
| 373 | - $fieldMap = $integrationDetails->field_map; | |
| 374 | - $fileMap = $integrationDetails->upload_field_map; | |
| 375 | - $actions = $integrationDetails->actions; | |
| 376 | - $defaultDataConf = $integrationDetails->default; | |
| 690 | + $zcrmRelatedlistApiResponse = $recordApiHelper->executeRecordApi( | |
| 691 | + $formID, | |
| 692 | + $entryID, | |
| 693 | + $integID, | |
| 694 | + $logID, | |
| 695 | + $defaultDataConf, | |
| 696 | + $relatedListModule, | |
| 697 | + $relatedListLayout, | |
| 698 | + $fieldValues, | |
| 699 | + $relatedlist->field_map, | |
| 700 | + $relatedlist->actions, | |
| 701 | + $relatedListRequired, | |
| 702 | + $relatedlist->upload_field_map, | |
| 703 | + true | |
| 704 | + ); | |
| 705 | + } | |
| 706 | + } | |
| 377 | 707 | |
| 378 | - if ( | |
| 379 | - empty($tokenDetails) | |
| 380 | - || empty($module) | |
| 381 | - || empty($layout) | |
| 382 | - || empty($fieldMap) | |
| 383 | - ) { | |
| 384 | - $error = new WP_Error('REQ_FIELD_EMPTY', __('module, layout, fields are required for zoho crm api', 'bitform')); | |
| 385 | - $this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error); | |
| 386 | - return $error; | |
| 387 | - } | |
| 388 | - if (empty($defaultDataConf->layouts->{$module}->{$layout}->fields) || empty($defaultDataConf->modules->{$module})) { | |
| 389 | - $error = new WP_Error('REQ_FIELD_EMPTY', __('module, layout, fields are required for zoho crm api', 'bitform')); | |
| 390 | - $this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error); | |
| 391 | - return $error; | |
| 392 | - } | |
| 393 | - if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 394 | - $requiredParams['clientId'] = $integrationDetails->clientId; | |
| 395 | - $requiredParams['clientSecret'] = $integrationDetails->clientSecret; | |
| 396 | - $requiredParams['dataCenter'] = $integrationDetails->dataCenter; | |
| 397 | - $requiredParams['tokenDetails'] = $tokenDetails; | |
| 398 | - $newTokenDetails = ZohoCRMHandler::_refreshAccessToken((object)$requiredParams); | |
| 399 | - if ($newTokenDetails) { | |
| 400 | - ZohoCRMHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails); | |
| 401 | - $tokenDetails = $newTokenDetails; | |
| 402 | - } | |
| 403 | - } | |
| 708 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 709 | + { | |
| 710 | + $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; | |
| 404 | 711 | |
| 405 | - $required = !empty($defaultDataConf->layouts->{$module}->{$layout}->required) ? | |
| 406 | - $defaultDataConf->layouts->{$module}->{$layout}->required : []; | |
| 407 | - $actions = $integrationDetails->actions; | |
| 408 | - if (class_exists('BitCode\BitFormPro\Integration\ZohoCRM\RecordApiHelper')) { | |
| 409 | - $recordApiHelper = new \BitCode\BitFormPro\Integration\ZohoCRM\RecordApiHelper($tokenDetails); | |
| 410 | - } else { | |
| 411 | - $recordApiHelper = new RecordApiHelper($tokenDetails); | |
| 412 | - } | |
| 413 | - $zcrmApiResponse = $recordApiHelper->executeRecordApi( | |
| 414 | - $this->_formID, | |
| 415 | - $entryID, | |
| 416 | - $this->_integrationID, | |
| 417 | - $logID, | |
| 418 | - $defaultDataConf, | |
| 419 | - $module, | |
| 420 | - $layout, | |
| 421 | - $fieldValues, | |
| 422 | - $fieldMap, | |
| 423 | - $actions, | |
| 424 | - $required, | |
| 425 | - $fileMap | |
| 426 | - ); | |
| 427 | - if (is_wp_error($zcrmApiResponse)) { | |
| 428 | - return $zcrmApiResponse; | |
| 429 | - } | |
| 430 | - if ( | |
| 431 | - !empty($zcrmApiResponse->data) | |
| 432 | - && !empty($zcrmApiResponse->data[0]->code) | |
| 433 | - && $zcrmApiResponse->data[0]->code === 'SUCCESS' | |
| 434 | - && count($integrationDetails->relatedlists) | |
| 435 | - ) { | |
| 436 | - $zcrmApiResponse = apply_filters('bitform_zcrm_addRelatedList', $zcrmApiResponse, $this->_formID, $entryID, $this->_integrationID, $logID, $fieldValues, $integrationDetails, $recordApiHelper); | |
| 437 | - } | |
| 438 | - return $zcrmApiResponse; | |
| 712 | + $tokenDetails = $integrationDetails->tokenDetails; | |
| 713 | + $module = $integrationDetails->module; | |
| 714 | + $layout = $integrationDetails->layout; | |
| 715 | + $fieldMap = $integrationDetails->field_map; | |
| 716 | + $fileMap = $integrationDetails->upload_field_map; | |
| 717 | + $actions = $integrationDetails->actions; | |
| 718 | + $defaultDataConf = $integrationDetails->default; | |
| 719 | + | |
| 720 | + if ( | |
| 721 | + empty($tokenDetails) | |
| 722 | + || empty($module) | |
| 723 | + || empty($layout) | |
| 724 | + || empty($fieldMap) | |
| 725 | + ) { | |
| 726 | + $error = new WP_Error('REQ_FIELD_EMPTY', __('module, layout, fields are required for zoho crm api', 'bit-form')); | |
| 727 | + $this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error); | |
| 728 | + return $error; | |
| 439 | 729 | } |
| 730 | + if (empty($defaultDataConf->layouts->{$module}->{$layout}->fields) || empty($defaultDataConf->modules->{$module})) { | |
| 731 | + $error = new WP_Error('REQ_FIELD_EMPTY', __('module, layout, fields are required for zoho crm api', 'bit-form')); | |
| 732 | + $this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error); | |
| 733 | + return $error; | |
| 734 | + } | |
| 735 | + if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) { | |
| 736 | + $requiredParams['clientId'] = $integrationDetails->clientId; | |
| 737 | + $requiredParams['clientSecret'] = $integrationDetails->clientSecret; | |
| 738 | + $requiredParams['dataCenter'] = $integrationDetails->dataCenter; | |
| 739 | + $requiredParams['tokenDetails'] = $tokenDetails; | |
| 740 | + $newTokenDetails = ZohoCRMHandler::_refreshAccessToken((object)$requiredParams); | |
| 741 | + if ($newTokenDetails) { | |
| 742 | + ZohoCRMHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails); | |
| 743 | + $tokenDetails = $newTokenDetails; | |
| 744 | + } | |
| 745 | + } | |
| 746 | + | |
| 747 | + $required = !empty($defaultDataConf->layouts->{$module}->{$layout}->required) ? | |
| 748 | + $defaultDataConf->layouts->{$module}->{$layout}->required : []; | |
| 749 | + $actions = $integrationDetails->actions; | |
| 750 | + $recordApiHelper = new RecordApiHelper($tokenDetails); | |
| 751 | + $zcrmApiResponse = $recordApiHelper->executeRecordApi( | |
| 752 | + $this->_formID, | |
| 753 | + $entryID, | |
| 754 | + $this->_integrationID, | |
| 755 | + $logID, | |
| 756 | + $defaultDataConf, | |
| 757 | + $module, | |
| 758 | + $layout, | |
| 759 | + $fieldValues, | |
| 760 | + $fieldMap, | |
| 761 | + $actions, | |
| 762 | + $required, | |
| 763 | + $fileMap | |
| 764 | + ); | |
| 765 | + if (is_wp_error($zcrmApiResponse)) { | |
| 766 | + return $zcrmApiResponse; | |
| 767 | + } | |
| 768 | + if ( | |
| 769 | + !empty($zcrmApiResponse->data) | |
| 770 | + && !empty($zcrmApiResponse->data[0]->code) | |
| 771 | + && 'SUCCESS' === $zcrmApiResponse->data[0]->code | |
| 772 | + && count($integrationDetails->relatedlists) | |
| 773 | + ) { | |
| 774 | + $zcrmApiResponse = self::addRelatedListV2($zcrmApiResponse, $this->_formID, $entryID, $this->_integrationID, $logID, $fieldValues, $integrationDetails, $recordApiHelper); | |
| 775 | + } | |
| 776 | + return $zcrmApiResponse; | |
| 777 | + } | |
| 440 | 778 | } |