Skip to content

Commit

Permalink
Added module configuration sending
Browse files Browse the repository at this point in the history
  • Loading branch information
gleemand authored Oct 18, 2022
1 parent 383ac58 commit 683e00e
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v3.4.13
* Добавлена передача информации о модуле в CRM при его установке

## v3.4.12
* Исправлена ошибка получения настроек при деактивации модуля
* Оптимизирован процесс генерации Icml файла каталога
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.12
3.4.13
17 changes: 17 additions & 0 deletions retailcrm/lib/settings/RetailcrmSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
40 changes: 33 additions & 7 deletions retailcrm/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -295,16 +295,33 @@ 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);

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()
Expand Down Expand Up @@ -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()
;
Expand All @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions retailcrm/upgrade/upgrade-3.3.2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
84 changes: 84 additions & 0 deletions retailcrm/upgrade/upgrade-3.4.13.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* MIT License
*
* Copyright (c) 2021 DIGITAL RETAIL TECHNOLOGIES SL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author DIGITAL RETAIL TECHNOLOGIES SL <[email protected]>
* @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;
}

0 comments on commit 683e00e

Please sign in to comment.