diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php index 85f2c4f..036d25d 100644 --- a/admin/controller/module/retailcrm.php +++ b/admin/controller/module/retailcrm.php @@ -84,10 +84,7 @@ class ControllerModuleRetailcrm extends Controller !empty($this->data['saved_settings']['retailcrm_apikey']) ) { - $this->retailcrm = new ApiHelper( - $this->data['saved_settings']['retailcrm_url'], - $this->data['saved_settings']['retailcrm_apikey'] - ); + $this->retailcrm = new ApiHelper($this->data['saved_settings']); $this->data['delivery'] = $this->model_retailcrm_references ->getDeliveryTypes(); diff --git a/admin/model/retailcrm/order.php b/admin/model/retailcrm/order.php index 61265c9..dc74271 100644 --- a/admin/model/retailcrm/order.php +++ b/admin/model/retailcrm/order.php @@ -5,10 +5,7 @@ class ModelRetailcrmOrder extends Model { { $this->load->model('setting/setting'); $settings = $this->model_setting_setting->getSetting('retailcrm'); - $settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST); - - $status = $this->model_setting_setting->getSetting('retailcrm'); - $order['order_status'] = $status['retailcrm_status'][$order['order_status_id']]; + $order['order_status'] = $settings['retailcrm_status'][$order['order_status_id']]; if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { require_once DIR_SYSTEM . 'library/retailcrm.php'; diff --git a/admin/model/retailcrm/references.php b/admin/model/retailcrm/references.php index 7d2e24b..8ac2218 100644 --- a/admin/model/retailcrm/references.php +++ b/admin/model/retailcrm/references.php @@ -1,7 +1,10 @@ retailcrm->deliveryTypesList(); - } catch (CurlException $e) { - $this->data['retailcrm_error'][] = $e->getMessage(); - $this->log->addError('RestApi::deliveryTypesList::Curl:' . $e->getMessage()); - } catch (InvalidJsonException $e) { - $this->data['retailcrm_error'][] = $e->getMessage(); - $this->log->addError('RestApi::deliveryTypesList::JSON:' . $e->getMessage()); + $this->load->model('setting/setting'); + $settings = $this->model_setting_setting->getSetting('retailcrm'); + + if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { + $this->retailcrm = new ApiHelper($settings); + + try { + return $this->retailcrm->api->deliveryTypesList(); + } catch (CurlException $e) { + $this->data['retailcrm_error'][] = $e->getMessage(); + $this->log->addError('RestApi::deliveryTypesList::Curl:' . $e->getMessage()); + } catch (InvalidJsonException $e) { + $this->data['retailcrm_error'][] = $e->getMessage(); + $this->log->addError('RestApi::deliveryTypesList::JSON:' . $e->getMessage()); + } + } else { + return array(); } } protected function getApiOrderStatuses() { - try { - return $this->retailcrm->orderStatusesList(); - } catch (CurlException $e) { - $this->data['retailcrm_error'][] = $e->getMessage(); - $this->log->addError('RestApi::orderStatusesList::Curl:' . $e->getMessage()); - } catch (InvalidJsonException $e) { - $this->data['retailcrm_error'][] = $e->getMessage(); - $this->log->addError('RestApi::orderStatusesList::JSON:' . $e->getMessage()); + $this->load->model('setting/setting'); + $settings = $this->model_setting_setting->getSetting('retailcrm'); + + if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { + $this->retailcrm = new ApiHelper($settings); + + try { + return $this->retailcrm->api->statusesList(); + } catch (CurlException $e) { + $this->data['retailcrm_error'][] = $e->getMessage(); + $this->log->addError('RestApi::orderStatusesList::Curl:' . $e->getMessage()); + } catch (InvalidJsonException $e) { + $this->data['retailcrm_error'][] = $e->getMessage(); + $this->log->addError('RestApi::orderStatusesList::JSON:' . $e->getMessage()); + } + } else { + return array(); } } protected function getApiPaymentTypes() { - try { - return $this->retailcrm->paymentTypesList(); - } catch (CurlException $e) { - $this->data['retailcrm_error'][] = $e->getMessage(); - $this->log->addError('RestApi::paymentTypesList::Curl:' . $e->getMessage()); - } catch (InvalidJsonException $e) { - $this->data['retailcrm_error'][] = $e->getMessage(); - $this->log->addError('RestApi::paymentTypesList::JSON:' . $e->getMessage()); + $this->load->model('setting/setting'); + $settings = $this->model_setting_setting->getSetting('retailcrm'); + + if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { + $this->retailcrm = new ApiHelper($settings); + + try { + return $this->retailcrm->api->paymentTypesList(); + } catch (CurlException $e) { + $this->data['retailcrm_error'][] = $e->getMessage(); + $this->log->addError('RestApi::paymentTypesList::Curl:' . $e->getMessage()); + } catch (InvalidJsonException $e) { + $this->data['retailcrm_error'][] = $e->getMessage(); + $this->log->addError('RestApi::paymentTypesList::JSON:' . $e->getMessage()); + } + } else { + return array(); } } } diff --git a/system/library/retailcrm.php b/system/library/retailcrm.php index abb6b36..20340ce 100644 --- a/system/library/retailcrm.php +++ b/system/library/retailcrm.php @@ -3,15 +3,14 @@ class ApiHelper { private $fileDate; - protected $api, $log, $settings; + protected $log, $settings; + public $api; public function __construct($settings) { $this->settings = $settings; - $this->domain = $settings['domain']; - $this->lastRun = $settings['retailcrm_history']; - $this->api = new Client( + $this->api = new ApiClient( $settings['retailcrm_url'], $settings['retailcrm_apikey'] ); @@ -30,10 +29,10 @@ class ApiHelper try { $customers = $this->api->customers($data['telephone'], $data['email'], $data['lastname'], 200, 0); } catch (CurlException $e) { - $this->log->addError('['.$this->domain.'] RestApi::customers:' . $e->getMessage()); - $this->log->addError('['.$this->domain.'] RestApi::customers:' . json_encode($data)); + $this->log->write('RestApi::customers:' . $e->getMessage()); + $this->log->write('RestApi::customers:' . json_encode($data)); } catch (InvalidJsonException $e) { - $this->log->addError('['.$this->domain.'] RestApi::customers::Curl:' . $e->getMessage()); + $this->log->write('RestApi::customers::Curl:' . $e->getMessage()); } @@ -62,11 +61,11 @@ class ApiHelper $this->customer = $this->api->customersEdit($customer); } catch (CurlException $e) { $this->customer = $e->getMessage(); - $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage()); - $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order)); + $this->log->write('RestApi::orderCreate:' . $e->getMessage()); + $this->log->write('RestApi::orderCreate:' . json_encode($order)); } catch (InvalidJsonException $e) { $this->customer = $e->getMessage(); - $this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage()); + $this->log->write('RestApi::orderCreate::Curl:' . $e->getMessage()); } } @@ -130,10 +129,10 @@ class ApiHelper try { $this->api->ordersEdit($order); } catch (CurlException $e) { - $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage()); - $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order)); + $this->log->write('RestApi::orderCreate:' . $e->getMessage()); + $this->log->write('RestApi::orderCreate:' . json_encode($order)); } catch (InvalidJsonException $e) { - $this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage()); + $this->log->write('RestApi::orderCreate::Curl:' . $e->getMessage()); } } @@ -145,12 +144,12 @@ class ApiHelper $orders = $this->api->ordersHistory($this->getDate()); $this->saveDate($this->api->getGeneratedAt()->format('Y-m-d H:i:s')); } catch (CurlException $e) { - $this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . $e->getMessage()); - $this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . json_encode($orders)); + $this->log->write('RestApi::orderHistory:' . $e->getMessage()); + $this->log->write('RestApi::orderHistory:' . json_encode($orders)); return false; } catch (InvalidJsonException $e) { - $this->log->addError('['.$this->domain.'] RestApi::orderHistory::Curl:' . $e->getMessage()); + $this->log->write('RestApi::orderHistory::Curl:' . $e->getMessage()); return false; } @@ -163,12 +162,12 @@ class ApiHelper try { return $this->api->orderFixExternalIds($data); } catch (CurlException $e) { - $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage()); - $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($data)); + $this->log->write('RestApi::orderFixExternalIds:' . $e->getMessage()); + $this->log->write('RestApi::orderFixExternalIds:' . json_encode($data)); return false; } catch (InvalidJsonException $e) { - $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage()); + $this->log->write('RestApi::orderFixExternalIds::Curl:' . $e->getMessage()); return false; } @@ -179,12 +178,12 @@ class ApiHelper try { return $this->api->customerFixExternalIds($data); } catch (CurlException $e) { - $this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . $e->getMessage()); - $this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . json_encode($data)); + $this->log->write('RestApi::customerFixExternalIds:' . $e->getMessage()); + $this->log->write('RestApi::customerFixExternalIds:' . json_encode($data)); return false; } catch (InvalidJsonException $e) { - $this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds::Curl:' . $e->getMessage()); + $this->log->write('RestApi::customerFixExternalIds::Curl:' . $e->getMessage()); return false; } @@ -195,10 +194,10 @@ class ApiHelper try { return $this->api->orderGet($order_id); } catch (CurlException $e) { - $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage()); + $this->log->write('RestApi::orderFixExternalIds:' . $e->getMessage()); return false; } catch (InvalidJsonException $e) { - $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage()); + $this->log->write('RestApi::orderFixExternalIds::Curl:' . $e->getMessage()); return false; }