Added showing validation results when opening the settings page

This commit is contained in:
max-baranikov 2022-08-03 14:13:47 +03:00 committed by GitHub
parent c60adf5370
commit 4098be6a13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 102 additions and 90 deletions

View File

@ -1,3 +1,7 @@
## v3.4.11
* Добавлено отображение ошибок валидации при открытии страницы настроек
* Добавлено сохранение данных заказа в таблицу "Работа с заказами" при обратной синхронизации
## v3.4.10
* Исправлена ошибка при ручной выгрузке заказов

View File

@ -1 +1 @@
3.4.10
3.4.11

View File

@ -49,9 +49,14 @@ class RetailcrmSettingsController extends RetailcrmAdminPostAbstractController
protected function getHandler()
{
$validator = new RetailcrmSettingsValidator(new RetailcrmSettingsItems(), $this->module->reference);
$validator->validate();
return [
'success' => true,
'settings' => RetailcrmSettingsHelper::getSettings(),
'warnings' => $validator->getWarnings(),
'errors' => $validator->getErrors(),
];
}
}

View File

@ -379,38 +379,6 @@ class RetailcrmTools
return $phonesArray;
}
/**
* Validate crm address
*
* @param $address
*
* @return bool
*/
public static function validateCrmAddress($address)
{
if (preg_match("/https:\/\/(.*).(retailcrm.(pro|ru|es)|simla.com)/", $address)) {
return Validate::isGenericName($address);
}
return false;
}
/**
* Validate crm api key
*
* @param $apiKey
*
* @return bool
*/
public static function validateCrmApiKey($apiKey)
{
if (32 === mb_strlen($apiKey)) {
return Validate::isGenericName($apiKey);
}
return false;
}
public static function getDate($file)
{
if (file_exists($file)) {

View File

@ -68,7 +68,7 @@ class RetailcrmSettings
*/
public function save()
{
if ($this->validator->validate()) {
if ($this->validator->validate(true)) {
$this->settings->updateValueAll();
}

View File

@ -80,87 +80,122 @@ class RetailcrmSettingsValidator
/**
* Settings form validator
*/
public function validate()
public function validate($validateFromRequestOnly = false)
{
// check url and apiKey
$urlAndApiKeyValidated = true;
if ($this->settings->issetValue('url') && !RetailcrmTools::validateCrmAddress($this->settings->getValue('url'))) {
$this->addError('errors.url');
$urlAndApiKeyValidated = false;
}
if ($this->settings->issetValue('apiKey') && !RetailcrmTools::validateCrmApiKey($this->settings->getValue('apiKey'))) {
$this->addError('errors.key');
$urlAndApiKeyValidated = false;
}
if ($urlAndApiKeyValidated && ($this->settings->issetValue('url') || $this->settings->issetValue('apiKey'))) {
$this->validateApiCredentials(
$this->settings->getValueWithStored('url'),
$this->settings->getValueWithStored('apiKey')
);
if (!$validateFromRequestOnly || $this->settings->issetValue('url') || $this->settings->issetValue('apiKey')) {
if ($this->validateCrmAddress($this->settings->getValueWithStored('url'))
&& $this->validateCrmApiKey($this->settings->getValueWithStored('apiKey'))
) {
$this->validateApiCredentials(
$this->settings->getValueWithStored('url'),
$this->settings->getValueWithStored('apiKey')
);
}
}
// check abandoned carts status
if ($this->settings->issetValue('status') || $this->settings->issetValue('synchronizedCartStatus')) {
if (!$validateFromRequestOnly || $this->settings->issetValue('status') || $this->settings->issetValue('synchronizedCartStatus')) {
if (!$this->validateCartStatus(
$this->settings->getValueWithStored('status'),
$this->settings->getValueWithStored('synchronizedCartStatus')
)
) {
$this->addError('errors.carts'); // todo check if it works
$this->addError('synchronizedCartStatus', 'errors.carts');
}
}
// check mapping statuses
if ($this->settings->issetValue('status')) {
if (!$this->validateMappingOneToOne($this->settings->getValue('status'))) {
$this->addError('errors.status');
if (!$validateFromRequestOnly || $this->settings->issetValue('status')) {
if (!$this->validateMappingOneToOne($this->settings->getValueWithStored('status'))) {
$this->addError('status', 'errors.status');
}
}
// check mapping delivery
if ($this->settings->issetValue('delivery')) {
if (!$this->validateMappingOneToOne($this->settings->getValue('delivery'))) {
$this->addError('errors.delivery');
if (!$validateFromRequestOnly || $this->settings->issetValue('delivery')) {
if (!$this->validateMappingOneToOne($this->settings->getValueWithStored('delivery'))) {
$this->addError('delivery', 'errors.delivery');
}
}
// check mapping payment
if ($this->settings->issetValue('payment')) {
if (!$this->validateMappingOneToOne($this->settings->getValue('payment'))) {
$this->addError('errors.payment');
if (!$validateFromRequestOnly || $this->settings->issetValue('payment')) {
if (!$this->validateMappingOneToOne($this->settings->getValueWithStored('payment'))) {
$this->addError('payment', 'errors.payment');
}
}
// check collector identifier
if ($this->settings->issetValue('collectorActive') || $this->settings->issetValue('collectorKey')) {
if (!$validateFromRequestOnly || $this->settings->issetValue('collectorActive') || $this->settings->issetValue('collectorKey')) {
if (!$this->validateCollector(
$this->settings->getValueWithStored('collectorActive'),
$this->settings->getValueWithStored('collectorKey')
)) {
$this->addError('errors.collector');
$this->addError('collectorKey', 'errors.collector');
}
}
$errorTabs = $this->validateStoredSettings(); // todo maybe refactor
if (!array_key_exists('url', $this->getErrors()) && !array_key_exists('apiKey', $this->getErrors())) {
$errorTabs = $this->validateStoredSettings($validateFromRequestOnly); // todo maybe refactor
if (in_array('delivery', $errorTabs)) {
$this->addWarning('warnings.delivery');
}
if (in_array('status', $errorTabs)) {
$this->addWarning('warnings.status');
}
if (in_array('payment', $errorTabs)) {
$this->addWarning('warnings.payment');
}
if (in_array('deliveryDefault', $errorTabs) || in_array('paymentDefault', $errorTabs)) {
$this->addWarning('warnings.default');
if (in_array('delivery', $errorTabs)) {
$this->addWarning('delivery', 'warnings.delivery');
}
if (in_array('status', $errorTabs)) {
$this->addWarning('status', 'warnings.status');
}
if (in_array('payment', $errorTabs)) {
$this->addWarning('payment', 'warnings.payment');
}
if (in_array('deliveryDefault', $errorTabs) || in_array('paymentDefault', $errorTabs)) {
$this->addWarning('deliveryDefault', 'warnings.default');
}
}
return $this->getSuccess();
}
/**
* Validate crm address
*
* @param $address
*
* @return bool
*/
private function validateCrmAddress($address)
{
if (preg_match("/https:\/\/(.*).(retailcrm.(pro|ru|es)|simla.com)/", $address)) {
if (Validate::isGenericName($address)) {
return true;
}
}
$this->addError('url', 'errors.url');
return false;
}
/**
* Validate crm api key
*
* @param $apiKey
*
* @return bool
*/
private function validateCrmApiKey($apiKey)
{
if (32 === mb_strlen($apiKey)) {
if (Validate::isGenericName($apiKey)) {
return true;
}
}
$this->addError('apiKey', 'errors.key');
return false;
}
/**
* Cart status must be present and must be unique to cartsIds only
*
@ -210,11 +245,11 @@ class RetailcrmSettingsValidator
return true;
}
public function validateStoredSettings() // todo also uses in settings template to show errors on page load
public function validateStoredSettings($validateFromRequestOnly)
{
$tabsWithWarnings = [];
$tabsNamesAndCheckApiMethods = [
'delivery' => 'getApiDeliveryTypes', // todo check and replace with new functions
'delivery' => 'getApiDeliveryTypes',
'status' => 'getApiStatuses',
'payment' => 'getApiPaymentTypes',
'deliveryDefault' => null,
@ -222,11 +257,11 @@ class RetailcrmSettingsValidator
];
foreach ($tabsNamesAndCheckApiMethods as $tabName => $checkApiMethod) {
if (!$this->settings->issetValue($tabName)) { // todo remove
if ($validateFromRequestOnly && !$this->settings->issetValue($tabName)) {
continue;
}
$storedValues = $this->settings->getValueWithStored($tabName); // todo get encoded value from Tools::
$storedValues = $this->settings->getValueWithStored($tabName);
if (false === $storedValues || null === $storedValues) {
continue;
@ -296,10 +331,10 @@ class RetailcrmSettingsValidator
}
}
$this->addError('errors.version');
$this->addError('url', 'errors.version');
}
} else {
$this->addError('errors.connect');
$this->addError('apiKey', 'errors.connect');
}
return false;
@ -331,7 +366,7 @@ class RetailcrmSettingsValidator
return true;
}
$this->addError('errors.access');
$this->addError('apiKey', 'errors.access');
return false;
}
@ -345,7 +380,7 @@ class RetailcrmSettingsValidator
return true;
}
$this->addError('errors.scopes');
$this->addError('apiKey', 'errors.scopes');
return false;
}
@ -355,13 +390,13 @@ class RetailcrmSettingsValidator
return !$collectorActive || '' !== $collectorKey;
}
private function addError($message)
private function addError($field, $message)
{
$this->errors[] = $message;
$this->errors[$field][] = $message;
}
private function addWarning($message)
private function addWarning($field, $message)
{
$this->warnings[] = $message;
$this->warnings[$field][] = $message;
}
}

View File

@ -48,7 +48,7 @@ require_once dirname(__FILE__) . '/bootstrap.php';
class RetailCRM extends Module
{
const VERSION = '3.4.10';
const VERSION = '3.4.11';
const API_URL = 'RETAILCRM_ADDRESS';
const API_KEY = 'RETAILCRM_API_TOKEN';

File diff suppressed because one or more lines are too long