diff --git a/Interfaces/AAA/Accounting_Interfaces.cpp b/Interfaces/AAA/Accounting_Interfaces.cpp index f0376c1e..dff48bf0 100644 --- a/Interfaces/AAA/Accounting_Interfaces.cpp +++ b/Interfaces/AAA/Accounting_Interfaces.cpp @@ -27,6 +27,7 @@ #include "Interfaces/AAA/Authorization.h" #include "Interfaces/Common/QtTypes.hpp" #include "Interfaces/Helpers/TokenHelper.h" +#include "Interfaces/Helpers/LanguageHelper.h" using namespace Targoman::API; using namespace Targoman::API::AAA; @@ -186,11 +187,10 @@ ORMSelectQuery intfAccountUnits::makeSelectQuery(INTFAPICALLBOOM_IMPL &APICALLBO }) .leftJoin(tblAccountUnitsI18NBase::Name) .addCol(DBExpression::VALUE(QString("COALESCE(" - "JSON_UNQUOTE(JSON_EXTRACT(%1.i18nData, '$.untName.\"%2\"'))," + + LanguageHelper::getI18NClauseForCoalesce(APICALLBOOM_PARAM, tblAccountUnitsI18NBase::Name, "untName") + "," "JSON_UNQUOTE(JSON_EXTRACT(%1.i18nData, '$.untName.default'))," - "%3.untName)") + "%2.untName)") .arg(tblAccountUnitsI18NBase::Name) - .arg(APICALLBOOM_PARAM.language()) .arg(_alias.isEmpty() ? tblAccountUnitsBase::Name : _alias) ), tblAccountUnitsBase::Fields::untName @@ -296,16 +296,16 @@ ORMSelectQuery intfAccountProducts::makeSelectQuery(INTFAPICALLBOOM_IMPL &APICAL tblAccountProductsBase::Fields::prdDesc, }) .leftJoin(tblAccountProductsI18NBase::Name) - .addCol(DBExpression::VALUE(QString("COALESCE(JSON_UNQUOTE(JSON_EXTRACT(%1.i18nData, '$.prdName.\"%2\"')), %3.prdName)") - .arg(tblAccountProductsI18NBase::Name) - .arg(APICALLBOOM_PARAM.language()) + .addCol(DBExpression::VALUE(QString("COALESCE(" + + LanguageHelper::getI18NClauseForCoalesce(APICALLBOOM_PARAM, tblAccountProductsI18NBase::Name, "prdName") + "," + "%1.prdName)") .arg(_alias.isEmpty() ? tblAccountProductsBase::Name : _alias) ), tblAccountProductsBase::Fields::prdName ) - .addCol(DBExpression::VALUE(QString("COALESCE(JSON_UNQUOTE(JSON_EXTRACT(%1.i18nData, '$.prdDesc.\"%2\"')), %3.prdDesc)") - .arg(tblAccountProductsI18NBase::Name) - .arg(APICALLBOOM_PARAM.language()) + .addCol(DBExpression::VALUE(QString("COALESCE(" + + LanguageHelper::getI18NClauseForCoalesce(APICALLBOOM_PARAM, tblAccountProductsI18NBase::Name, "prdDesc") + "," + "%1.prdDesc)") .arg(_alias.isEmpty() ? tblAccountProductsBase::Name : _alias) ), tblAccountProductsBase::Fields::prdDesc @@ -405,16 +405,16 @@ ORMSelectQuery intfAccountSaleables::makeSelectQuery(INTFAPICALLBOOM_IMPL &APICA tblAccountSaleablesBase::Fields::slbDesc, }) .leftJoin(tblAccountSaleablesI18NBase::Name) - .addCol(DBExpression::VALUE(QString("COALESCE(JSON_UNQUOTE(JSON_EXTRACT(%1.i18nData, '$.slbName.\"%2\"')), %3.slbName)") - .arg(tblAccountSaleablesI18NBase::Name) - .arg(APICALLBOOM_PARAM.language()) + .addCol(DBExpression::VALUE(QString("COALESCE(" + + LanguageHelper::getI18NClauseForCoalesce(APICALLBOOM_PARAM, tblAccountSaleablesI18NBase::Name, "slbName") + "," + "%1.slbName)") .arg(_alias.isEmpty() ? tblAccountSaleablesBase::Name : _alias) ), tblAccountSaleablesBase::Fields::slbName ) - .addCol(DBExpression::VALUE(QString("COALESCE(JSON_UNQUOTE(JSON_EXTRACT(%1.i18nData, '$.slbDesc.\"%2\"')), %3.slbDesc)") - .arg(tblAccountSaleablesI18NBase::Name) - .arg(APICALLBOOM_PARAM.language()) + .addCol(DBExpression::VALUE(QString("COALESCE(" + + LanguageHelper::getI18NClauseForCoalesce(APICALLBOOM_PARAM, tblAccountSaleablesI18NBase::Name, "slbDesc") + "," + "%1.slbDesc)") .arg(_alias.isEmpty() ? tblAccountSaleablesBase::Name : _alias) ), tblAccountSaleablesBase::Fields::slbDesc diff --git a/Interfaces/Helpers/LanguageHelper.cpp b/Interfaces/Helpers/LanguageHelper.cpp new file mode 100644 index 00000000..3cbf7f92 --- /dev/null +++ b/Interfaces/Helpers/LanguageHelper.cpp @@ -0,0 +1,56 @@ +/****************************************************************************** +# TargomanAPI: REST API for Targoman +# +# Copyright 2014-2020 by Targoman Intelligent Processing +# +# TargomanAPI is free software: you can redistribute it and/or modify +# it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# TargomanAPI is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU AFFERO GENERAL PUBLIC LICENSE for more details. +# +# You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE +# along with Targoman. If not, see . + ******************************************************************************/ +/** + * @author S. Mehran M. Ziabary + * @author Kambiz Zandi + */ + +#include "LanguageHelper.h" + +namespace Targoman::API::Helpers { + +QString LanguageHelper::getI18NClauseForCoalesce( + INTFAPICALLBOOM_IMPL &APICALLBOOM_PARAM, + const QString &_i18nTableName, + const QString &_fieldName, + const QString &_i18nDataFieldName +) { + QString FieldPath = "$"; + if (_fieldName.isEmpty() == false) + FieldPath += "." + _fieldName; + + QString Result = QString("JSON_UNQUOTE(JSON_EXTRACT(%1.%2, '%3.\"%4\"'))") + .arg(_i18nTableName) + .arg(_i18nDataFieldName) + .arg(FieldPath) + .arg(APICALLBOOM_PARAM.language()) + ; + + if (APICALLBOOM_PARAM.language().contains("-")) + Result += "," + + QString("JSON_UNQUOTE(JSON_EXTRACT(%1.%2, '%3.%4'))") + .arg(_i18nTableName) + .arg(_i18nDataFieldName) + .arg(FieldPath) + .arg(APICALLBOOM_PARAM.language().split("-").first()); + + return Result; +} + +} //namespace Targoman::API::Helpers diff --git a/Interfaces/Helpers/LanguageHelper.h b/Interfaces/Helpers/LanguageHelper.h new file mode 100644 index 00000000..03f7f53b --- /dev/null +++ b/Interfaces/Helpers/LanguageHelper.h @@ -0,0 +1,46 @@ +/****************************************************************************** +# TargomanAPI: REST API for Targoman +# +# Copyright 2014-2020 by Targoman Intelligent Processing +# +# TargomanAPI is free software: you can redistribute it and/or modify +# it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# TargomanAPI is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU AFFERO GENERAL PUBLIC LICENSE for more details. +# +# You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE +# along with Targoman. If not, see . + ******************************************************************************/ +/** + * @author S. Mehran M. Ziabary + * @author Kambiz Zandi + */ + +#ifndef TARGOMAN_API_LANGUAGEHELPER_H +#define TARGOMAN_API_LANGUAGEHELPER_H + +#include +#include "../Server/APICallBoom.h" + +namespace Targoman::API::Helpers { + +using namespace Server; + +class LanguageHelper +{ +public: + static QString getI18NClauseForCoalesce( + INTFAPICALLBOOM_IMPL &APICALLBOOM_PARAM, + const QString &_i18nTableName, + const QString &_fieldName, + const QString &_i18nDataFieldName = "i18nData"); +}; + +} //namespace Targoman::API::Helpers + +#endif // TARGOMAN_API_LANGUAGEHELPER_H diff --git a/Interfaces/Interfaces.pro b/Interfaces/Interfaces.pro index 77bc9e4c..78217cf2 100644 --- a/Interfaces/Interfaces.pro +++ b/Interfaces/Interfaces.pro @@ -45,6 +45,7 @@ HEADERS += \ API/intfModuleHelper.h \ Helpers/IteratorHelper.hpp \ Helpers/JSonHelper.h \ + Helpers/LanguageHelper.h \ Helpers/TokenHelper.h \ ORM/intfFAQ.h \ ORM/intfMigrations.h \ @@ -86,6 +87,7 @@ HEADERS += \ SOURCES += \ API/intfModuleHelper.cpp \ Helpers/JSonHelper.cpp \ + Helpers/LanguageHelper.cpp \ Helpers/TokenHelper.cpp \ ORM/intfFAQ.cpp \ ORM/intfMigrations.cpp \ diff --git a/Interfaces/ORM/intfI18N.cpp b/Interfaces/ORM/intfI18N.cpp index 89e671e5..dd3ffe9d 100644 --- a/Interfaces/ORM/intfI18N.cpp +++ b/Interfaces/ORM/intfI18N.cpp @@ -25,11 +25,14 @@ #include "Interfaces/AAA/Authorization.h" #include "Interfaces/AAA/AAADefs.hpp" #include "Interfaces/AAA/clsJWT.hpp" +#include "Interfaces/Helpers/LanguageHelper.h" using namespace Targoman::API::AAA; namespace Targoman::API::ORM { +using namespace Helpers; + intfI18N::intfI18N( const QString &_module, const QString &_schema @@ -50,12 +53,11 @@ QString intfI18N::translated( auto Data = this->makeSelectQuery(APICALLBOOM_PARAM) .addCol(tblI18N::Fields::i18nKey) .addCol(DBExpression::VALUE(QString("COALESCE(" - "JSON_UNQUOTE(JSON_EXTRACT(%1.%2, '$.\"%3\"'))," + + LanguageHelper::getI18NClauseForCoalesce(APICALLBOOM_PARAM, tblI18N::Name, "", tblI18N::Fields::i18nValue) + "," "JSON_UNQUOTE(JSON_EXTRACT(%1.%2, '$.default'))," - "'%4')") + "'%3')") .arg(tblI18N::Name) .arg(tblI18N::Fields::i18nValue) - .arg(APICALLBOOM_PARAM.language()) .arg(_key) ), "Translated") diff --git a/ModuleHelpers/MT/Interfaces/intfMTCharts.cpp b/ModuleHelpers/MT/Interfaces/intfMTCharts.cpp index de6a639f..8affe865 100644 --- a/ModuleHelpers/MT/Interfaces/intfMTCharts.cpp +++ b/ModuleHelpers/MT/Interfaces/intfMTCharts.cpp @@ -29,6 +29,7 @@ #include "Interfaces/Helpers/TokenHelper.h" #include "intfMTAccounting.h" #include "intfMTModule.h" +#include "Interfaces/Helpers/LanguageHelper.h" using namespace Targoman::API; using namespace Targoman::API::AAA; @@ -765,12 +766,11 @@ std::tuple> baseintfMTCharts::getU auto I18NSelectQuery = I18N->makeSelectQuery(APICALLBOOM_PARAM) .addCol(tblI18N::Fields::i18nKey) .addCol(DBExpression::VALUE(QString("COALESCE(" - "JSON_UNQUOTE(JSON_EXTRACT(%1.%2, '$.\"%3\"'))," + + LanguageHelper::getI18NClauseForCoalesce(APICALLBOOM_PARAM, tblI18N::Name, "", tblI18N::Fields::i18nValue) + "," "JSON_UNQUOTE(JSON_EXTRACT(%1.%2, '$.default'))" ")") .arg(tblI18N::Name) .arg(tblI18N::Fields::i18nValue) - .arg(APICALLBOOM_PARAM.language()) ), "Translated") diff --git a/Modules/Account/moduleSrc/ORM/PaymentGateways.cpp b/Modules/Account/moduleSrc/ORM/PaymentGateways.cpp index f9237023..b46f1298 100644 --- a/Modules/Account/moduleSrc/ORM/PaymentGateways.cpp +++ b/Modules/Account/moduleSrc/ORM/PaymentGateways.cpp @@ -25,6 +25,7 @@ #include "../Account.h" #include "Payment/PaymentLogic.h" #include "Interfaces/Helpers/URLHelper.h" +#include "Interfaces/Helpers/LanguageHelper.h" using namespace Targoman::API::AccountModule::Payment; using namespace Targoman::API::Helpers; @@ -79,9 +80,9 @@ ORMSelectQuery PaymentGatewayTypes::makeSelectQuery(INTFAPICALLBOOM_IMPL &APICAL Query .removeCol(tblPaymentGatewayTypes::Fields::pgtName) .leftJoin(tblPaymentGatewayTypesI18N::Name) - .addCol(DBExpression::VALUE(QString("COALESCE(JSON_UNQUOTE(JSON_EXTRACT(%1.i18nData, '$.pgtName.\"%2\"')), %3.pgtName)") - .arg(tblPaymentGatewayTypesI18N::Name) - .arg(APICALLBOOM_PARAM.language()) + .addCol(DBExpression::VALUE(QString("COALESCE(" + + LanguageHelper::getI18NClauseForCoalesce(APICALLBOOM_PARAM, tblPaymentGatewayTypesI18N::Name, "pgtName") + "," + "%1.pgtName)") .arg(_alias.isEmpty() ? tblPaymentGatewayTypes::Name : _alias) ), tblPaymentGatewayTypes::Fields::pgtName