From df9523f5b3e3b6da82a7892ec78c154ef33b72bc Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Thu, 23 Jul 2015 13:14:23 +0300 Subject: [PATCH] custom dir, minor fixes --- admin/controller/module/retailcrm.php | 146 ++++++++++++-------------- admin/model/retailcrm/custom/.gitkeep | 0 admin/model/retailcrm/history.php | 6 +- admin/model/retailcrm/references.php | 69 +++++++++++- system/library/retailcrm.php | 16 +-- 5 files changed, 144 insertions(+), 93 deletions(-) create mode 100644 admin/model/retailcrm/custom/.gitkeep diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php index 7fe3838..85f2c4f 100644 --- a/admin/controller/module/retailcrm.php +++ b/admin/controller/module/retailcrm.php @@ -2,12 +2,14 @@ require_once DIR_SYSTEM . 'library/retailcrm.php'; -class ControllerModuleRetailcrm extends Controller { +class ControllerModuleRetailcrm extends Controller +{ private $error = array(); protected $log, $statuses, $payments, $deliveryTypes, $retailcrm; public $children, $template; - public function install() { + public function install() + { $this->load->model('setting/setting'); $this->model_setting_setting->editSetting( 'retailcrm', @@ -15,7 +17,8 @@ class ControllerModuleRetailcrm extends Controller { ); } - public function uninstall() { + public function uninstall() + { $this->load->model('setting/setting'); $this->model_setting_setting->editSetting( 'retailcrm', @@ -23,7 +26,8 @@ class ControllerModuleRetailcrm extends Controller { ); } - public function index() { + public function index() + { $this->load->model('setting/setting'); $this->load->model('setting/extension'); @@ -32,10 +36,22 @@ class ControllerModuleRetailcrm extends Controller { $this->document->setTitle($this->language->get('heading_title')); $this->document->addStyle('/admin/view/stylesheet/retailcrm.css'); - if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { - $this->model_setting_setting->editSetting('retailcrm', $this->request->post); + if ( + $this->request->server['REQUEST_METHOD'] == 'POST' + && + $this->validate() + ) { + $this->model_setting_setting + ->editSetting('retailcrm', $this->request->post); + $this->session->data['success'] = $this->language->get('text_success'); - $this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')); + + $this->redirect( + $this->url->link( + 'extension/module', + 'token=' . $this->session->data['token'], 'SSL' + ) + ); } $text_strings = array( @@ -59,7 +75,8 @@ class ControllerModuleRetailcrm extends Controller { } $this->data['retailcrm_errors'] = array(); - $this->data['saved_settings'] = $this->model_setting_setting->getSetting('retailcrm'); + $this->data['saved_settings'] = $this->model_setting_setting + ->getSetting('retailcrm'); if ( !empty($this->data['saved_settings']['retailcrm_url']) @@ -72,65 +89,12 @@ class ControllerModuleRetailcrm extends Controller { $this->data['saved_settings']['retailcrm_apikey'] ); - /* - * Delivery - */ - $this->deliveryTypes = array(); - - try { - $this->deliveryTypes = $this->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->data['delivery'] = array( - 'opencart' => $this->model_retailcrm_tools->getOpercartDeliveryMethods(), - 'retailcrm' => $this->deliveryTypes - ); - - /* - * Statuses - */ - $this->statuses = array(); - - try { - $this->statuses = $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->data['statuses'] = array( - 'opencart' => $this->model_retailcrm_tools->getOpercartOrderStatuses(), - 'retailcrm' => $this->statuses - ); - - /* - * Payment - */ - $this->payments = array(); - - try { - $this->payments = $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->data['payments'] = array( - 'opencart' => $this->model_retailcrm_tools->getOpercartPaymentTypes(), - 'retailcrm' => $this->payments - ); + $this->data['delivery'] = $this->model_retailcrm_references + ->getDeliveryTypes(); + $this->data['statuses'] = $this->model_retailcrm_references + ->getOrderStatuses(); + $this->data['payments'] = $this->model_retailcrm_references + ->getPaymentTypes(); } @@ -156,25 +120,40 @@ class ControllerModuleRetailcrm extends Controller { $this->data['breadcrumbs'][] = array( 'text' => $this->language->get('text_home'), - 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'), + 'href' => $this->url->link( + 'common/home', + 'token=' . $this->session->data['token'], 'SSL' + ), 'separator' => false ); $this->data['breadcrumbs'][] = array( 'text' => $this->language->get('text_module'), - 'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'), + 'href' => $this->url->link( + 'extension/module', + 'token=' . $this->session->data['token'], 'SSL' + ), 'separator' => ' :: ' ); $this->data['breadcrumbs'][] = array( 'text' => $this->language->get('heading_title'), - 'href' => $this->url->link('module/retailcrm', 'token=' . $this->session->data['token'], 'SSL'), + 'href' => $this->url->link( + 'module/retailcrm', + 'token=' . $this->session->data['token'], 'SSL' + ), 'separator' => ' :: ' ); - $this->data['action'] = $this->url->link('module/retailcrm', 'token=' . $this->session->data['token'], 'SSL'); + $this->data['action'] = $this->url->link( + 'module/retailcrm', + 'token=' . $this->session->data['token'], 'SSL' + ); - $this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'); + $this->data['cancel'] = $this->url->link( + 'extension/module', + 'token=' . $this->session->data['token'], 'SSL' + ); $this->data['modules'] = array(); @@ -200,17 +179,28 @@ class ControllerModuleRetailcrm extends Controller { public function history() { - $this->load->model('retailcrm/history'); - $this->model_retailcrm_history->request(); + if (file_exists(DIR_APPLICATION . 'model/retailcrm/custom/history')) { + $this->load->model('retailcrm/custom/history'); + $this->model_retailcrm_custom_history->request(); + } else { + $this->load->model('retailcrm/history'); + $this->model_retailcrm_history->request(); + } } public function icml() { - $this->load->model('retailcrm/icml'); - $this->model_retailcrm_icml->generateICML(); + if (file_exists(DIR_APPLICATION . 'model/retailcrm/custom/icml')) { + $this->load->model('retailcrm/custom/icml'); + $this->model_retailcrm_custom_icml->generateICML(); + } else { + $this->load->model('retailcrm/icml'); + $this->model_retailcrm_icml->generateICML(); + } } - private function validate() { + private function validate() + { if (!$this->user->hasPermission('modify', 'module/retailcrm')) { $this->error['warning'] = $this->language->get('error_permission'); } @@ -221,6 +211,4 @@ class ControllerModuleRetailcrm extends Controller { return FALSE; } } - - } diff --git a/admin/model/retailcrm/custom/.gitkeep b/admin/model/retailcrm/custom/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index 062089a..918eedb 100644 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -37,11 +37,11 @@ class ModelRetailcrmHistory extends Model foreach ($orders as $order) { - if (!isset($order['deleted']) || !$order['deleted']) { + if (empty($order['deleted'])) { $data = array(); - $customer_id = (isset($order['customer']['externalId']) && $order['customer']['externalId'] != 0) + $customer_id = (!empty($order['customer']['externalId'])) ? $order['customer']['externalId'] : '' ; @@ -303,4 +303,4 @@ class ModelRetailcrmHistory extends Model ); } } -} \ No newline at end of file +} diff --git a/admin/model/retailcrm/references.php b/admin/model/retailcrm/references.php index b77cc80..7d2e24b 100644 --- a/admin/model/retailcrm/references.php +++ b/admin/model/retailcrm/references.php @@ -3,7 +3,31 @@ class ModelRetailcrmReferences extends Model { - public function getOpercartDeliveryMethods() + public function getDeliveryTypes() + { + return array( + 'opencart' => $this->getOpercartDeliveryTypes(), + 'retailcrm' => $this->getApiDeliveryTypes() + ); + } + + public function getOrderStatuses() + { + return array( + 'opencart' => $this->getOpercartOrderStatuses(), + 'retailcrm' => $this->getApiOrderStatuses() + ); + } + + public function getPaymentTypes() + { + return array( + 'opencart' => $this->getOpercartPaymentTypes(), + 'retailcrm' => $this->getApiPaymentTypes() + ); + } + + protected function getOpercartDeliveryTypes() { $deliveryMethods = array(); $files = glob(DIR_APPLICATION . 'controller/shipping/*.php'); @@ -25,7 +49,7 @@ class ModelRetailcrmReferences extends Model return $deliveryMethods; } - public function getOpercartOrderStatuses() + protected function getOpercartOrderStatuses() { $this->load->model('localisation/order_status'); @@ -33,7 +57,7 @@ class ModelRetailcrmReferences extends Model ->getOrderStatuses(array()); } - public function getOpercartPaymentTypes() + protected function getOpercartPaymentTypes() { $paymentTypes = array(); $files = glob(DIR_APPLICATION . 'controller/payment/*.php'); @@ -54,4 +78,43 @@ class ModelRetailcrmReferences extends Model return $paymentTypes; } + + protected function getApiDeliveryTypes() + { + try { + return $this->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()); + } + } + + 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()); + } + } + + 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()); + } + } } diff --git a/system/library/retailcrm.php b/system/library/retailcrm.php index d550b01..abb6b36 100644 --- a/system/library/retailcrm.php +++ b/system/library/retailcrm.php @@ -2,7 +2,7 @@ class ApiHelper { - private $dir, $fileDate; + private $fileDate; protected $api, $log, $settings; public function __construct($settings) { @@ -59,7 +59,7 @@ class ApiHelper )); try { - $this->customer = $this->api->customerEdit($customer); + $this->customer = $this->api->customersEdit($customer); } catch (CurlException $e) { $this->customer = $e->getMessage(); $this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage()); @@ -128,7 +128,7 @@ class ApiHelper } try { - $this->api->orderEdit($order); + $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)); @@ -196,8 +196,6 @@ class ApiHelper return $this->api->orderGet($order_id); } catch (CurlException $e) { $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage()); - $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($order)); - return false; } catch (InvalidJsonException $e) { $this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage()); @@ -390,6 +388,7 @@ class ApiResponse implements ArrayAccess * Allow to access for the property throw class method * * @param string $name + * @param array $arguments * @return mixed */ public function __call($name, $arguments) @@ -479,7 +478,7 @@ class ApiClient * * @param string $url * @param string $apiKey - * @param string $siteCode + * @param string $site * @return mixed */ public function __construct($url, $apiKey, $site = null) @@ -515,8 +514,9 @@ class ApiClient /** * Edit a order * - * @param array $order - * @param string $site (default: null) + * @param array $order + * @param string $by + * @param string $site (default: null) * @return ApiResponse */ public function ordersEdit(array $order, $by = 'externalId', $site = null)