class-zcrm-contact.php
9 months ago
class-zcrm-modules.php
9 months ago
class-zcrm-product.php
9 months ago
class-zcrm-salesorder.php
9 months ago
index.php
1 year ago
class-zcrm-modules.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class CMBIRD_ZCRM_Modules { |
| 8 | |
| 9 | /** |
| 10 | * create API Get call to Zoho CRM to get all custom fields |
| 11 | * |
| 12 | * @param module $module - module name |
| 13 | * @return array | \WP_Error |
| 14 | */ |
| 15 | public function cmbird_get_module( $module ) { |
| 16 | $zoho_crm_url = get_option( 'cmbird_zoho_crm_url' ); |
| 17 | $url = $zoho_crm_url . 'crm/v7/settings/fields?module=' . $module; |
| 18 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 19 | $json = $execute_curl_call_handle->execute_curl_call_get( $url ); |
| 20 | if ( is_wp_error( $json ) ) { |
| 21 | return $json; |
| 22 | } else { |
| 23 | // Parse the response. |
| 24 | $parsed_fields = array(); |
| 25 | |
| 26 | foreach ( $json->fields as $field ) { |
| 27 | $parsed_fields[] = array( |
| 28 | 'id' => $field->id, |
| 29 | 'apiName' => $field->api_name, |
| 30 | 'visible' => (bool) $field->visible, |
| 31 | 'fieldLabel' => $field->field_label, |
| 32 | 'displayLabel' => $field->display_label, |
| 33 | 'customField' => (bool) $field->custom_field, |
| 34 | 'dataType' => $field->data_type, |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | return $parsed_fields; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * create API Get call to Zoho CRM to get a record of a module |
| 44 | * |
| 45 | * @param module $module - module name |
| 46 | * @param record_id $record_id - record id |
| 47 | * @return array | \WP_Error |
| 48 | */ |
| 49 | public function cmbird_get_record( $module, $record_id ) { |
| 50 | // add delay of 0.5 seconds to avoid rate limiting. |
| 51 | usleep( 500000 ); // 0.5 seconds. |
| 52 | if ( empty( $record_id ) ) { |
| 53 | return new \WP_Error( 'cmbird_no_record_id', __( 'No record ID provided.', 'cmbird' ) ); |
| 54 | } |
| 55 | $zoho_crm_url = get_option( 'cmbird_zoho_crm_url' ); |
| 56 | $url = $zoho_crm_url . 'crm/v7/' . $module . '/' . $record_id; |
| 57 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 58 | $json = $execute_curl_call_handle->execute_curl_call_get( $url ); |
| 59 | if ( is_wp_error( $json ) ) { |
| 60 | return $json; |
| 61 | } else { |
| 62 | return $json; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * create API Get call to Zoho CRM to create a record of a module |
| 68 | * |
| 69 | * @param string $module - module name |
| 70 | * @param array $data - data to be created |
| 71 | * @return array | \WP_Error |
| 72 | */ |
| 73 | public function cmbird_create_record( $module, $data ) { |
| 74 | // add delay of 0.5 seconds to avoid rate limiting. |
| 75 | usleep( 500000 ); // 0.5 seconds. |
| 76 | $zoho_crm_url = get_option( 'cmbird_zoho_crm_url' ); |
| 77 | $url = $zoho_crm_url . 'crm/v7/' . $module; |
| 78 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 79 | // convert the data to JSON format. |
| 80 | if ( is_array( $data ) ) { |
| 81 | $data = json_encode( $data ); |
| 82 | } |
| 83 | if ( ! $data ) { |
| 84 | return new \WP_Error( 'cmbird_no_data', __( 'No data provided for creating record.', 'cmbird' ) ); |
| 85 | } |
| 86 | $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data ); |
| 87 | if ( ! isset( $json->data[0]->code ) || 'SUCCESS' !== $json->data[0]->code ) { |
| 88 | return new \WP_Error( '500', __( $json->data[0]->message, 'cmbird' ), array( 'status' => 500 ) ); |
| 89 | } |
| 90 | return $json; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * create API Get call to Zoho CRM to update a record of a module |
| 95 | * |
| 96 | * @param string $module - module name |
| 97 | * @param string $record_id - record id |
| 98 | * @param array $data - data to be updated |
| 99 | * @return array | \WP_Error |
| 100 | */ |
| 101 | public function cmbird_update_record( $module, $record_id, $data ) { |
| 102 | // add delay of 0.5 seconds to avoid rate limiting. |
| 103 | usleep( 500000 ); // 0.5 seconds. |
| 104 | if ( empty( $record_id ) ) { |
| 105 | return new \WP_Error( 'cmbird_no_record_id', __( 'No record ID provided.', 'cmbird' ) ); |
| 106 | } |
| 107 | if ( empty( $data ) ) { |
| 108 | return new \WP_Error( 'cmbird_no_data', __( 'No data provided for updating record.', 'cmbird' ) ); |
| 109 | } |
| 110 | $zoho_crm_url = get_option( 'cmbird_zoho_crm_url' ); |
| 111 | $url = $zoho_crm_url . 'crm/v7/' . $module . '/' . $record_id; |
| 112 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 113 | // convert the data to JSON format. |
| 114 | if ( is_array( $data ) ) { |
| 115 | $data = json_encode( $data ); |
| 116 | } |
| 117 | $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data ); |
| 118 | // response has data in the format of {"data":[{"code":"SUCCESS","details":{}}],"info":{"per_page":200,"count":1,"page":1}}. |
| 119 | // if code is not SUCCESS, return error. |
| 120 | if ( ! isset( $json->data[0]->code ) || 'SUCCESS' !== $json->data[0]->code ) { |
| 121 | return new \WP_Error( '500', __( $json->data[0]->message, 'cmbird' ), array( 'status' => 500 ) ); |
| 122 | } |
| 123 | return $json; |
| 124 | } |
| 125 | } |
| 126 |