From 3e1c19fd8d97604231bd52eaeca9c7ff0631688a Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Fri, 2 Jun 2023 13:55:00 +0300 Subject: [PATCH 1/7] ref #90088 Types of deliveries and payments are displayed only active status and available stores --- .../controller/extension/module/retailcrm.php | 39 ++++++++++++++++--- .../model/extension/retailcrm/references.php | 14 +++++++ .../retailcrm/lib/api/OpencartApiClient.php | 2 +- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index 63bb76da..3678dff0 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -334,13 +334,17 @@ public function index() : null; if (!empty($url) && !empty($key)) { - - $_data['delivery'] = $this->model_extension_retailcrm_references - ->getDeliveryTypes(); + $sites = $this->model_extension_retailcrm_references->getApiSites(); + $_data['delivery'] = $this->getAvailableTypes( + $sites, + $this->model_extension_retailcrm_references->getDeliveryTypes() + ); + $_data['payments'] = $this->getAvailableTypes( + $sites, + $this->model_extension_retailcrm_references->getPaymentTypes() + ); $_data['statuses'] = $this->model_extension_retailcrm_references ->getOrderStatuses(); - $_data['payments'] = $this->model_extension_retailcrm_references - ->getPaymentTypes(); $_data['customFields'] = $this->model_extension_retailcrm_references ->getCustomFields(); @@ -942,4 +946,29 @@ private function getHistorySinceId($api, $method) return $lastSinceId; } + + private function getAvailableTypes($availableSites, $types) + { + if (!empty($availableSites)) { + $availableSite = end($availableSites)['code']; + } else { + return $types; + } + + $result = []; + + foreach ($types as $type) { + if ( + $type['active'] === true && + ( + empty($type['sites']) || + in_array($availableSite, $type['sites'], true) + ) + ) { + $result[] = $type; + } + } + + return $result; + } } diff --git a/src/upload/admin/model/extension/retailcrm/references.php b/src/upload/admin/model/extension/retailcrm/references.php index aea9f0e8..dd7d3b7e 100644 --- a/src/upload/admin/model/extension/retailcrm/references.php +++ b/src/upload/admin/model/extension/retailcrm/references.php @@ -158,6 +158,20 @@ public function getApiDeliveryTypes() return (!$response->isSuccessful()) ? array() : $response->deliveryTypes; } + /** + * Get RetailCRM available sites list + */ + public function getApiSites() + { + $response = $this->retailcrmApiClient->sitesList(); + + if (!$response) { + return array(); + } + + return (!$response->isSuccessful()) ? array() : $response->sites; + } + /** * Get RetailCRM order statuses * diff --git a/src/upload/system/library/retailcrm/lib/api/OpencartApiClient.php b/src/upload/system/library/retailcrm/lib/api/OpencartApiClient.php index adec79a5..72fa92a6 100644 --- a/src/upload/system/library/retailcrm/lib/api/OpencartApiClient.php +++ b/src/upload/system/library/retailcrm/lib/api/OpencartApiClient.php @@ -87,7 +87,7 @@ private function request($method, $getParams, $postParams) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_FORBID_REUSE, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_URL, $url . 'index.php?route=api/' . $method . (!empty($getParams) ? '&' . http_build_query($getParams) : '')); + curl_setopt($curl, CURLOPT_URL, 'https://chaplygin-is.proxy.retailcrm.tech/' . 'index.php?route=api/' . $method . (!empty($getParams) ? '&' . http_build_query($getParams) : '')); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postParams)); From b6181ab426bfdc29053309e0e66d9d81074f9282 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Fri, 2 Jun 2023 14:43:54 +0300 Subject: [PATCH 2/7] ref #90088 editing handler function --- src/upload/admin/controller/extension/module/retailcrm.php | 6 +++--- .../system/library/retailcrm/lib/api/OpencartApiClient.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index 3678dff0..9be4d70c 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -955,9 +955,9 @@ private function getAvailableTypes($availableSites, $types) return $types; } - $result = []; + $result['opencart'] = $types['opencart']; - foreach ($types as $type) { + foreach ($types['retailcrm'] as $codeKey => $type) { if ( $type['active'] === true && ( @@ -965,7 +965,7 @@ private function getAvailableTypes($availableSites, $types) in_array($availableSite, $type['sites'], true) ) ) { - $result[] = $type; + $result['retailcrm'][$codeKey] = $type; } } diff --git a/src/upload/system/library/retailcrm/lib/api/OpencartApiClient.php b/src/upload/system/library/retailcrm/lib/api/OpencartApiClient.php index 72fa92a6..adec79a5 100644 --- a/src/upload/system/library/retailcrm/lib/api/OpencartApiClient.php +++ b/src/upload/system/library/retailcrm/lib/api/OpencartApiClient.php @@ -87,7 +87,7 @@ private function request($method, $getParams, $postParams) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_FORBID_REUSE, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_URL, 'https://chaplygin-is.proxy.retailcrm.tech/' . 'index.php?route=api/' . $method . (!empty($getParams) ? '&' . http_build_query($getParams) : '')); + curl_setopt($curl, CURLOPT_URL, $url . 'index.php?route=api/' . $method . (!empty($getParams) ? '&' . http_build_query($getParams) : '')); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postParams)); From 8b7522ca6c0e43fe390967f10def29e419e0f693 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Fri, 2 Jun 2023 14:55:21 +0300 Subject: [PATCH 3/7] ref #90088 update version --- CHANGELOG.md | 3 +++ VERSION | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88c6c8c2..1437e5c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v4.1.10 +* Types of deliveries and payments are displayed only active status and available stores + ## v4.1.9 * Optimization of the history processing algorithm diff --git a/VERSION b/VERSION index 18837e70..5d30083e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1.9 +4.1.10 From e4cfdd8c9f2072162dc4bb5ae4c11c162f6151b1 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Fri, 2 Jun 2023 16:27:31 +0300 Subject: [PATCH 4/7] ref #90088 create test --- tests/admin/ControllerRetailcrmAdminTest.php | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/admin/ControllerRetailcrmAdminTest.php b/tests/admin/ControllerRetailcrmAdminTest.php index cfdec6b9..de76d0e0 100644 --- a/tests/admin/ControllerRetailcrmAdminTest.php +++ b/tests/admin/ControllerRetailcrmAdminTest.php @@ -41,4 +41,62 @@ public function testController() $response = $this->dispatchAction('extension/module/retailcrm/uninstall_collector'); $this->assertRegExp('/Connection settings/', $response->getOutput()); } + + public function testGetAvailableTypes() + { + $data = $this->getDataForTestAvailableTypes(); + $sites = $data['sites']; + $types = $data['types']; + + $class = new ReflectionClass(ControllerExtensionModuleRetailcrm::class); + $method = $class->getMethod('getAvailableTypes'); + $method->setAccessible(true); + + $result = $method->invokeArgs(ControllerExtensionModuleRetailcrm::class, [$sites, $types]); + + $this->assertNotEmpty($result['opencart']); + $this->assertNotEmpty($result['retailcrm']); + $this->assertCount(2, $result['retailcrm']); + $this->assertNotEmpty($result['retailcrm']['test1']['code']); + $this->assertNotEmpty($result['retailcrm']['test4']['code']); + } + + private function getDataForTestAvailableTypes(): array + { + return [ + 'sites' => [ + 'opencart' => [ + 'code' => 'opencart', + 'name' => 'OpenCart' + ] + ], + 'types' => [ + 'opencart' => [ + 'test' + ], + 'retailcrm' => [ + 'test1' => [ + 'active' => true, + 'sites' => [], + 'code' => 'test1' + ], + 'test2' => [ + 'active' => false, + 'sites' => [], + 'code' => 'test2' + ], + 'test3' => [ + 'active' => true, + 'sites' => ['otherSite'], + 'code' => 'test3' + ], + 'test4' => [ + 'active' => 'true', + 'sites' => ['cms1', 'cms2', 'opencart'], + 'code' => 'test4' + ] + ] + ] + ]; + } } From ef6d09c03afcf61f0f34472cb8256d7c066ca890 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Mon, 5 Jun 2023 17:18:10 +0300 Subject: [PATCH 5/7] ref #90088 fix test update Dockerfile --- .docker/Dockerfile | 2 +- tests/admin/ControllerRetailcrmAdminTest.php | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.docker/Dockerfile b/.docker/Dockerfile index 086f7c35..97cdf874 100644 --- a/.docker/Dockerfile +++ b/.docker/Dockerfile @@ -2,7 +2,7 @@ FROM php:7.1-apache RUN apt-get update -RUN apt-get install -y netcat zlib1g-dev libpq-dev git libicu-dev libxml2-dev libpng-dev libjpeg-dev libmcrypt-dev libxslt-dev libfreetype6-dev \ +RUN apt-get install -y netcat zlib1g-dev libpq-dev git libicu-dev libxml2-dev libpng-dev libjpeg-dev libmcrypt-dev libxslt-dev libfreetype6-dev unzip \ && docker-php-ext-configure intl \ && docker-php-ext-install intl \ && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ diff --git a/tests/admin/ControllerRetailcrmAdminTest.php b/tests/admin/ControllerRetailcrmAdminTest.php index de76d0e0..fbd3d0ec 100644 --- a/tests/admin/ControllerRetailcrmAdminTest.php +++ b/tests/admin/ControllerRetailcrmAdminTest.php @@ -48,11 +48,12 @@ public function testGetAvailableTypes() $sites = $data['sites']; $types = $data['types']; - $class = new ReflectionClass(ControllerExtensionModuleRetailcrm::class); + $retailCrm = new ControllerExtensionModuleRetailcrm(self::$registry); + $class = new ReflectionClass($retailCrm); $method = $class->getMethod('getAvailableTypes'); $method->setAccessible(true); - $result = $method->invokeArgs(ControllerExtensionModuleRetailcrm::class, [$sites, $types]); + $result = $method->invokeArgs($retailCrm, [$sites, $types]); $this->assertNotEmpty($result['opencart']); $this->assertNotEmpty($result['retailcrm']); @@ -91,7 +92,7 @@ private function getDataForTestAvailableTypes(): array 'code' => 'test3' ], 'test4' => [ - 'active' => 'true', + 'active' => true, 'sites' => ['cms1', 'cms2', 'opencart'], 'code' => 'test4' ] From 7d3390bc02c5210c9191fc3da2270d8ddf2def8b Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Wed, 7 Jun 2023 13:01:59 +0300 Subject: [PATCH 6/7] ref #90088 fix style code edit test Edit getting site --- .../controller/extension/module/retailcrm.php | 35 +++++++++---------- .../model/extension/retailcrm/references.php | 6 ++-- tests/admin/ControllerRetailcrmAdminTest.php | 4 +-- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index 9be4d70c..956d5690 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -334,13 +334,13 @@ public function index() : null; if (!empty($url) && !empty($key)) { - $sites = $this->model_extension_retailcrm_references->getApiSites(); + $site = $this->model_extension_retailcrm_references->getApiSite(); $_data['delivery'] = $this->getAvailableTypes( - $sites, + $site, $this->model_extension_retailcrm_references->getDeliveryTypes() ); $_data['payments'] = $this->getAvailableTypes( - $sites, + $site, $this->model_extension_retailcrm_references->getPaymentTypes() ); $_data['statuses'] = $this->model_extension_retailcrm_references @@ -947,26 +947,25 @@ private function getHistorySinceId($api, $method) return $lastSinceId; } - private function getAvailableTypes($availableSites, $types) + private function getAvailableTypes($availableSite, $types) { - if (!empty($availableSites)) { - $availableSite = end($availableSites)['code']; - } else { - return $types; - } - $result['opencart'] = $types['opencart']; + $result['retailcrm'] = []; + + if (empty($availableSite)) { + return $result; + } foreach ($types['retailcrm'] as $codeKey => $type) { - if ( - $type['active'] === true && - ( - empty($type['sites']) || - in_array($availableSite, $type['sites'], true) - ) - ) { - $result['retailcrm'][$codeKey] = $type; + if ($type['active'] !== true) { + continue; } + + if (!empty($type['sites']) && !in_array($availableSite['code'], $type['sites'])) { + continue; + } + + $result['retailcrm'][$codeKey] = $type; } return $result; diff --git a/src/upload/admin/model/extension/retailcrm/references.php b/src/upload/admin/model/extension/retailcrm/references.php index dd7d3b7e..0ad36dfc 100644 --- a/src/upload/admin/model/extension/retailcrm/references.php +++ b/src/upload/admin/model/extension/retailcrm/references.php @@ -161,15 +161,15 @@ public function getApiDeliveryTypes() /** * Get RetailCRM available sites list */ - public function getApiSites() + public function getApiSite() { $response = $this->retailcrmApiClient->sitesList(); if (!$response) { - return array(); + return []; } - return (!$response->isSuccessful()) ? array() : $response->sites; + return (!$response->isSuccessful()) ? [] : end($response->sites); } /** diff --git a/tests/admin/ControllerRetailcrmAdminTest.php b/tests/admin/ControllerRetailcrmAdminTest.php index fbd3d0ec..b46d6841 100644 --- a/tests/admin/ControllerRetailcrmAdminTest.php +++ b/tests/admin/ControllerRetailcrmAdminTest.php @@ -45,7 +45,7 @@ public function testController() public function testGetAvailableTypes() { $data = $this->getDataForTestAvailableTypes(); - $sites = $data['sites']; + $sites = end($data['site']); $types = $data['types']; $retailCrm = new ControllerExtensionModuleRetailcrm(self::$registry); @@ -65,7 +65,7 @@ public function testGetAvailableTypes() private function getDataForTestAvailableTypes(): array { return [ - 'sites' => [ + 'site' => [ 'opencart' => [ 'code' => 'opencart', 'name' => 'OpenCart' From 792546d007b2ec06647d1e5a4ab35fcfc1380bfa Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Wed, 7 Jun 2023 13:39:01 +0300 Subject: [PATCH 7/7] ref #90088 fix code --- src/upload/admin/model/extension/retailcrm/references.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/upload/admin/model/extension/retailcrm/references.php b/src/upload/admin/model/extension/retailcrm/references.php index 0ad36dfc..fe76a77e 100644 --- a/src/upload/admin/model/extension/retailcrm/references.php +++ b/src/upload/admin/model/extension/retailcrm/references.php @@ -165,11 +165,13 @@ public function getApiSite() { $response = $this->retailcrmApiClient->sitesList(); - if (!$response) { + if (!$response || !$response->isSuccessful()) { return []; } - return (!$response->isSuccessful()) ? [] : end($response->sites); + $sites = $response->sites; + + return end($sites); } /**