From 683e00e851d3aa4446bc2fd43ce5b562cfcf9d9a Mon Sep 17 00:00:00 2001 From: gleemand Date: Tue, 18 Oct 2022 19:25:01 +0200 Subject: [PATCH] Added module configuration sending --- CHANGELOG.md | 3 + VERSION | 2 +- retailcrm/lib/settings/RetailcrmSettings.php | 17 ++++ retailcrm/retailcrm.php | 40 ++++++++-- retailcrm/upgrade/upgrade-3.3.2.php | 8 +- retailcrm/upgrade/upgrade-3.4.13.php | 84 ++++++++++++++++++++ 6 files changed, 142 insertions(+), 12 deletions(-) create mode 100644 retailcrm/upgrade/upgrade-3.4.13.php diff --git a/CHANGELOG.md b/CHANGELOG.md index eaded68a..6b943351 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v3.4.13 +* Добавлена передача информации о модуле в CRM при его установке + ## v3.4.12 * Исправлена ошибка получения настроек при деактивации модуля * Оптимизирован процесс генерации Icml файла каталога diff --git a/VERSION b/VERSION index 25e86328..618659f0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.4.12 +3.4.13 diff --git a/retailcrm/lib/settings/RetailcrmSettings.php b/retailcrm/lib/settings/RetailcrmSettings.php index 284a464c..7bdaf4d2 100644 --- a/retailcrm/lib/settings/RetailcrmSettings.php +++ b/retailcrm/lib/settings/RetailcrmSettings.php @@ -70,6 +70,11 @@ public function save() { if ($this->validator->validate(true)) { $this->settings->updateValueAll(); + + if (array_key_exists('apiKey', $this->settings->getChanged())) { + $this->setClientId(); + RetailCRM::updateCrmModuleState(Context::getContext()->shop->id); + } } $changed = $this->settings->getChanged(); @@ -87,6 +92,18 @@ public function save() ]; } + private function setClientId() + { + $context = Context::getContext(); + + Configuration::updateValue(RetailCRM::CLIENT_ID, hash( + 'sha256', + $context->shop->id . Configuration::get('PS_SHOP_DOMAIN') + )); + + return true; + } + private function updateConsultantCode() { $consultantCode = $this->consultantScript->getValue(); diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index d72f77cc..351a861b 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -48,7 +48,7 @@ class RetailCRM extends Module { - const VERSION = '3.4.12'; + const VERSION = '3.4.13'; const API_URL = 'RETAILCRM_ADDRESS'; const API_KEY = 'RETAILCRM_API_TOKEN'; @@ -295,7 +295,7 @@ public function hookHeader() } } - public function uninstall() + public static function updateCrmModuleState($idShop, $active = true) { $apiUrl = Configuration::get(static::API_URL); $apiKey = Configuration::get(static::API_KEY); @@ -303,8 +303,25 @@ public function uninstall() if (!empty($apiUrl) && !empty($apiKey)) { $api = new RetailcrmProxy($apiUrl, $apiKey); - $clientId = Configuration::get(static::CLIENT_ID); - $this->integrationModule($api, $clientId, false); + $clientId = Configuration::get(static::CLIENT_ID, null, null, $idShop); + self::integrationModule($api, $clientId, $active); + } + + return true; + } + + public function uninstall() + { + if (Shop::isFeatureActive()) { + $shops = Shop::getShops(); + } else { + $shops[] = Shop::getContext(); + } + + foreach ($shops as $shop) { + if (isset($shop['id_shop'])) { + self::updateCrmModuleState($shop['id_shop'], false); + } } return parent::uninstall() @@ -347,6 +364,10 @@ public function uninstall() public function enable($force_all = false) { + $context = Context::getContext(); + + self::updateCrmModuleState($context->shop->id); + return parent::enable($force_all) && $this->installTab() ; @@ -358,6 +379,10 @@ public function disable($force_all = false) return false; } + $context = Shop::getContext(); + + self::updateCrmModuleState($context->shop->id, false); + $sql = 'SELECT COUNT(`id_shop`) FROM `' . _DB_PREFIX_ . 'module_shop` WHERE `id_module` = ' . (int) $this->id; @@ -782,13 +807,14 @@ public function hookActionPaymentCCAdd($params) * * @return bool */ - private function integrationModule($apiClient, $clientId, $active = true) + public static function integrationModule($apiClient, $clientId, $active = true) { - $scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; + $context = Context::getContext(); + $logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b845ce986911-prestashop2.svg'; $integrationCode = 'prestashop'; $name = 'PrestaShop'; - $accountUrl = $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + $accountUrl = $context->link->getAdminLink('AdminModules') . '&configure=retailcrm'; $configuration = [ 'clientId' => $clientId, 'code' => $integrationCode . '-' . $clientId, diff --git a/retailcrm/upgrade/upgrade-3.3.2.php b/retailcrm/upgrade/upgrade-3.3.2.php index f88b088e..968917cb 100644 --- a/retailcrm/upgrade/upgrade-3.3.2.php +++ b/retailcrm/upgrade/upgrade-3.3.2.php @@ -70,11 +70,11 @@ function upgrade_module_3_3_2($module) } if ($isMultiStoreActive) { - $oldFile = _PS_ROOT_DIR_ . '/' . 'retailcrm_' . $shop['id_shop'] . '.xml'; - $newFile = _PS_ROOT_DIR_ . '/' . 'simla_' . $shop['id_shop'] . '.xml'; + $oldFile = _PS_ROOT_DIR_ . '/retailcrm_' . $shop['id_shop'] . '.xml'; + $newFile = _PS_ROOT_DIR_ . '/simla_' . $shop['id_shop'] . '.xml'; } else { - $oldFile = _PS_ROOT_DIR_ . '/' . 'retailcrm.xml'; - $newFile = _PS_ROOT_DIR_ . '/' . 'simla.xml'; + $oldFile = _PS_ROOT_DIR_ . '/retailcrm.xml'; + $newFile = _PS_ROOT_DIR_ . '/simla.xml'; } if (file_exists($oldFile) && !file_exists($newFile)) { diff --git a/retailcrm/upgrade/upgrade-3.4.13.php b/retailcrm/upgrade/upgrade-3.4.13.php new file mode 100644 index 00000000..a7cfa7fe --- /dev/null +++ b/retailcrm/upgrade/upgrade-3.4.13.php @@ -0,0 +1,84 @@ + + * @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL + * @license https://opensource.org/licenses/MIT The MIT License + * + * Don't forget to prefix your containers with your own identifier + * to avoid any conflicts with others containers. + */ + +if (!defined('_PS_VERSION_')) { + exit; +} + +/** + * Upgrade module to version 3.4.13 + * + * @param \RetailCRM $module + * + * @return bool + */ +function upgrade_module_3_4_13($module) +{ + if ('retailcrm' != $module->name) { + return false; + } + + if (Shop::isFeatureActive()) { + $shops = Shop::getShops(); + } else { + $shops[] = ['id_shop' => Shop::getContext()->shop->id]; + } + + foreach ($shops as $shop) { + if (isset($shop['id_shop'])) { + $idShop = (int) $shop['id_shop']; + + if ( + !Configuration::hasKey($module::CLIENT_ID, null, null, $idShop) + || empty(Configuration::get($module::CLIENT_ID, null, null, $idShop)) + ) { + Configuration::updateValue( + $module::CLIENT_ID, + hash('sha256', $idShop . Configuration::get('PS_SHOP_DOMAIN')), + false, + null, + $idShop + ); + } + + $module::updateCrmModuleState($idShop); + } + } + + return true; +}