Commit a0abee1b authored by Aster's avatar Aster Committed by waltermazza
Browse files

General Bug Fixes and Improvements

parent 8994033a
......@@ -6,11 +6,49 @@ Translation management.
Insert in config/component-amos
```php
'view' => [
'class' => 'arter\amos\core\components\AmosView',
'class' => 'elitedivision\amos\core\components\AmosView',
'theme' => [
'pathMap' => [
'@vendor/lajax/yii2-translate-manager/views/language/' => '@vendor/arter/amos-translation/src/views/translatemanager/'
'@vendor/lajax/yii2-translate-manager/views/language/' => '@vendor/elitedivision/amos-translation/src/views/translatemanager/'
],
],
],
```
### Configurable properties
***defaultTranslationLanguage** - string
Default language to translate if the record translate is not present
```php
'translation' => [
'class' => 'elitedivision\amos\translation\AmosTranslation',
'defaultTranslationLanguage' => 'en-GB',
],
```
***secureCookie** - boolean
Added configuration to enable to send cookie without security
***enableCookieFor2LevelDomain** - boolean
Added configuration to set cookie for the second level domain
***byPassPermissionInlineTranslation** - boolean
If the value is true it disable all the permission on the record translation
***enableLabelTranslationField** - boolean, default = false
If set to true it enables the display of the translatable fields in the forms
***templateTranslationField** - string, default = '{translation}'
Template of translation field in the form, near the label
***templateTranslationAltField** - string, default = '{altTranslation}'
Template of translation alt field in the form, near the $templateTranslationField
***translationLabelField** - string, default = 'strtoupper(substr(\Yii::$app->language, 0, 2));'
This string will be parsed by the "eval()" function instead of $tempalteTransaltionField, by default {translation}
***translationLabelAltField** - string, default = '\Yii::t("amostranslation", "Testo traducibile, la visualizzazione attuale è in");'
This string will be parsed by the "eval()" function instead of $templateTranslationAltField, by default {altTranslation}
***labelTranslationField** - string, default = ' (<span class="label_translation am am-translate" title="{altTranslation} {translation}"> - {translation}</span>)'
This string is the html code that will be used in the labels to represent a translatable field
{
"name": "arter/amos-translation",
"name": "elitedivision/amos-translation",
"description": "AMOS Translation",
"keywords": ["amos", "yii2", "translation"],
"homepage": "http://www.arter.it/",
"homepage": "http://www.elitedivision.it/",
"type": "component",
"license": "BSD-3-Clause",
"support": {
"issues": "http://www.example.com/",
"forum": "http://www.example.com/forum/",
"wiki": "http://www.example.com/wiki/",
"source": "https://github.com/arter/amos-workflow"
"source": "https://github.com/elitedivision/amos-workflow"
},
"minimum-stability": "dev",
"require": {
"php": ">=5.5.0",
"arter/amos-core":">=1.9.50",
"arter/amos-dashboard": ">=1.8.15",
"arter/amos-workflow": "*",
"elitedivision/amos-core":">=1.9.50",
"elitedivision/amos-dashboard": ">=1.8.15",
"elitedivision/amos-workflow": "*",
"lajax/yii2-translate-manager": "1.*"
},
"extra": {
......@@ -27,7 +27,7 @@
},
"autoload": {
"psr-4": {
"arter\\amos\\translation\\": "src"
"elitedivision\\amos\\translation\\": "src"
}
}
}
<?php
/**
* Copyright 2020 Art-ER S. Cons. P.A.
* EROI - Emilia Romagna Open Innovation is based on:
* https://www.open2.0.regione.lombardia.it
* Lombardia Informatica S.p.A.
* OPEN 2.0
*
* @see http://example.com Developers'community
* @license GPLv3
* @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
*
* @package arter\amos\translation
* @package elitedivision\amos\translation
* @category CategoryName
* @author Elite Division S.r.l.
* @author Lombardia Informatica S.p.A.
*/
namespace arter\amos\translation;
namespace elitedivision\amos\translation;
use arter\amos\core\module\Module;
use arter\amos\core\module\AmosModule;
use arter\amos\core\module\ModuleInterface;
use arter\amos\core\record\Record;
use elitedivision\amos\core\module\Module;
use elitedivision\amos\core\module\AmosModule;
use elitedivision\amos\core\module\ModuleInterface;
use elitedivision\amos\core\record\Record;
use yii\web\Application;
use yii\base\BootstrapInterface;
use Yii;
......@@ -28,7 +27,7 @@ use yii\helpers\FileHelper;
/**
* Class AmosWorkflow
* @package arter\amos\workflow
* @package elitedivision\amos\workflow
*/
class AmosTranslation extends AmosModule implements BootstrapInterface
{
......@@ -44,7 +43,7 @@ class AmosTranslation extends AmosModule implements BootstrapInterface
public $newFileMode = 0666;
public $newDirMode = 0777;
public $languageField = 'language';
public $controllerNamespace = 'arter\amos\translation\controllers';
public $controllerNamespace = 'elitedivision\amos\translation\controllers';
/**
* It set the language source, if is set every saved record is copied in the translation language default
......@@ -117,7 +116,7 @@ class AmosTranslation extends AmosModule implements BootstrapInterface
* ```php
* 'modules' => [
* 'translation' => [
* 'class' => 'arter\amos\translation\AmosTranslation',
* 'class' => 'elitedivision\amos\translation\AmosTranslation',
* 'translationBootstrap' => [
* 'configuration' => [
* 'translationLabels' => [
......@@ -132,12 +131,12 @@ class AmosTranslation extends AmosModule implements BootstrapInterface
* ],
* ],
* 'translationContents' => [
* 'classBehavior' => \arter\amos\translation\behaviors\TranslateableBehavior::className(),
* 'classBehavior' => \elitedivision\amos\translation\behaviors\TranslateableBehavior::className(),
* 'models' => [
* [
* 'namespace' => 'backend\modules\id_plugin\models\NameModel',
* //'connection' => 'db', //if not set it use 'db'
* //'classBehavior' => null,//if not set it use default classBehavior 'arter\amos\translation\behaviors\TranslateableBehavior'
* //'classBehavior' => null,//if not set it use default classBehavior 'elitedivision\amos\translation\behaviors\TranslateableBehavior'
* //'enableWorkflow' => false,//if not set it use default configuration of the plugin
* //'workflow' => 'AmosTranslationWorkflow',
* //'view' => '@backend/modules/module/controller/view,
......@@ -165,8 +164,8 @@ class AmosTranslation extends AmosModule implements BootstrapInterface
* ```php
* 'bootstrap' => [
* 'translation',
* 'arter\amos\translation\bootstrap\EventActiveRecordBootstrap',//fot the translation of the records
* 'arter\amos\translation\bootstrap\EventViewBootstrap',//for the widget in the view file
* 'elitedivision\amos\translation\bootstrap\EventActiveRecordBootstrap',//fot the translation of the records
* 'elitedivision\amos\translation\bootstrap\EventViewBootstrap',//for the widget in the view file
* ],
* ```
*/
......@@ -178,7 +177,7 @@ class AmosTranslation extends AmosModule implements BootstrapInterface
public $eventViewBlackListModels = [];
public $eventViewWhiteListParts = ['_form'];
public $enableWidgetView = true;
public $widgetViewFile = '@vendor/arter/amos-translation/src/views/default/language_status.php';
public $widgetViewFile = '@vendor/elitedivision/amos-translation/src/views/default/language_status.php';
public $actionLanguage = '/translation/default/language';
public $numberGridViewField = 3;
public $enableRTE = true;
......@@ -188,7 +187,7 @@ class AmosTranslation extends AmosModule implements BootstrapInterface
* Model base class will be generated
* @var string
*/
public $modelBaseClass = 'arter\\amos\\core\\record\\Record';
public $modelBaseClass = 'elitedivision\\amos\\core\\record\\Record';
public $modelNs = "backend\\models\\translations";
public $modelGenerateRelation = 'all';
......@@ -267,7 +266,7 @@ class AmosTranslation extends AmosModule implements BootstrapInterface
{
parent::init();
\Yii::setAlias('@arter/amos/'.static::getModuleName().'/controllers', __DIR__.'/controllers');
\Yii::setAlias('@elitedivision/amos/'.static::getModuleName().'/controllers', __DIR__.'/controllers');
// initialize the module with the configuration loaded from config.php
Yii::configure($this, require(__DIR__.DIRECTORY_SEPARATOR.self::$CONFIG_FOLDER.DIRECTORY_SEPARATOR.'config.php'));
$this->configTranslationLabelsByModules();
......@@ -310,12 +309,12 @@ class AmosTranslation extends AmosModule implements BootstrapInterface
'@app',
'@backend',
'@frontend',
'@vendor/arter/',
'@vendor/elitedivision/',
]);
$configuration['scanRootParentDirectory'] = (isset($this->module_translation_labels_options['scanRootParentDirectory'])
? $this->module_translation_labels_options['scanRootParentDirectory'] : true);
$configuration['layout'] = (isset($this->module_translation_labels_options['layout']) ? $this->module_translation_labels_options['layout']
: '@vendor/arter/amos-layout/src/views/layouts/main');
: '@vendor/elitedivision/amos-layout/src/views/layouts/main');
$configuration['allowedIPs'] = (isset($this->module_translation_labels_options['allowedIPs']) ? $this->module_translation_labels_options['allowedIPs']
: ['*']);
$configuration['roles'] = (isset($this->module_translation_labels_options['roles']) ? $this->module_translation_labels_options['roles']
......
......@@ -5,5 +5,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
- Added configuration to enable to send cookie without security
- Added configuration to set cookie for the second level domain
### Added
- Automatic configuration of the fields of the db reading from an attribute of the AmosModule valued in the plugins installed on the platform
- Added default language to translate when the record in the translation request is empty
- Added rollback for the source record if the language to translate is not the same of the default language
- Handled the exception in case of missing model translations
- Added params to disable translation user permission on the translation record
- Added params enableLabelTranslationField, if set to true it enables the display of the translatable fields in the forms
- Added params templateTranslationField, template of translation field in the form, near the label
- Added params templateTranslationAltField, template of translation alt field in the form, near the $templateTranslationField
- Added params translationLabelField, this string will be parsed by the "eval()" function instead of $tempalteTransaltionField, by default {translation}
- Added params translationLabelAltField, this string will be parsed by the "eval()" function instead of $templateTranslationAltField, by default {altTranslation}
- Added params labelTranslationField, this string is the html code that will be used in the labels to represent a translatable field
\ No newline at end of file
<?php
/**
* Copyright 2020 Art-ER S. Cons. P.A.
* EROI - Emilia Romagna Open Innovation is based on:
* https://www.open2.0.regione.lombardia.it
* Lombardia Informatica S.p.A.
* OPEN 2.0
*
* @see http://example.com Developers'community
* @license GPLv3
* @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
*
* @package arter\amos\chat
* @package elitedivision\amos\chat
* @category CategoryName
* @author Elite Division S.r.l.
* @author Lombardia Informatica S.p.A.
*/
namespace arter\amos\translation\assets;
namespace elitedivision\amos\translation\assets;
use yii\web\AssetBundle;
/**
* Class AmosChatAsset
* @package arter\amos\chat\assets
* @package elitedivision\amos\chat\assets
*/
class AmosTranslationAsset extends AssetBundle
{
/**
* @var string
*/
public $sourcePath = '@vendor/arter/amos-translation/src/assets/web';
public $sourcePath = '@vendor/elitedivision/amos-translation/src/assets/web';
/**
* @var array
......@@ -44,6 +43,6 @@ class AmosTranslationAsset extends AssetBundle
* @var array
*/
public $depends = [
'arter\amos\core\views\assets\AmosCoreAsset',
'elitedivision\amos\core\views\assets\AmosCoreAsset',
];
}
\ No newline at end of file
<?php
/**
* Copyright 2020 Art-ER S. Cons. P.A.
* EROI - Emilia Romagna Open Innovation is based on:
* https://www.open2.0.regione.lombardia.it
* Lombardia Informatica S.p.A.
* OPEN 2.0
*
* @see http://example.com Developers'community
* @license GPLv3
* @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
*
* @package arter\amos\translation
* @package elitedivision\amos\translation
* @category CategoryName
* @author Elite Division S.r.l.
* @author Lombardia Informatica S.p.A.
*/
namespace arter\amos\translation\behaviors;
namespace elitedivision\amos\translation\behaviors;
use Yii;
use yii\base\Behavior;
......@@ -21,12 +20,19 @@ use yii\base\Event;
use yii\db\ActiveRecord;
use yii\helpers\FileHelper;
use yii\helpers\StringHelper;
use arter\amos\core\record\CachedActiveQuery;
use elitedivision\amos\core\record\CachedActiveQuery;
class TranslateableBehavior extends Behavior
{
const DELETE_ONLY_LANGUAGE_SELECTED = 0;
const DELETE_ALL = 1;
const MAP_LANGUAGE = [
'it' => 'it-IT',
'en' => 'en-GB',
'fr' => 'fr-FR',
'es' => 'es-ES',
'de' => 'de-DE'
];
/**
* @var string the name of the translations relation
......@@ -212,35 +218,45 @@ class TranslateableBehavior extends Behavior
*/
public function afterInit($event)
{
$this->loadTranslation(\Yii::$app->language);
$language = self::getMappedLanguage(\Yii::$app->language);
$this->loadTranslation($language);
}
/**
*
*
*/
public function translateOriginalValues()
{
$module = \Yii::$app->getModule('translation');
$blackListAttributes = array_merge($this->blackListAttributes, $this->systemBlackListAttributes);
$language = $this->getLanguage();
foreach ($this->getTranslation($language)->attributes as $key => $attribute) {
$originalAttributes = $this->owner->attributes;
$beta_language = false;
if(\Yii::$app instanceof \yii\web\Application)
{
$beta_language = \Yii::$app->user->can('CONTENT_TRANSLATOR') ? true : false;
}
$allLanguage = $module->getAvailableLanguages($beta_language);
if (!empty($attribute) && array_key_exists($key, $originalAttributes) && in_array($key,
$this->translationAttributes) && !in_array($key, $blackListAttributes)) {
$this->owner->$key = $attribute;
} else if (!empty($module->defaultTranslationLanguage) && empty($attribute) && array_key_exists($key,
$originalAttributes) && in_array($key, $this->translationAttributes) && !in_array($key,
$blackListAttributes) && (empty($module->defaultLanguage) || (strcasecmp($module->defaultLanguage,
$language) != 0))) {
$defaultTranslationAttributes = $this->getTranslation($module->defaultTranslationLanguage);
if (!empty($defaultTranslationAttributes) && !empty($defaultTranslationAttributes->$key)) {
$this->owner->$key = $defaultTranslationAttributes->$key;
$beta_language = false;
if (\Yii::$app instanceof \yii\web\Application) {
$beta_language = \Yii::$app->user->can('CONTENT_TRANSLATOR') ? true : false;
}
$allLanguage = $module->getAvailableLanguages($beta_language);
if (array_key_exists($language, $allLanguage)) {
$language = self::getMappedLanguage(\Yii::$app->language);
foreach ($this->getTranslation($language)->attributes as $key => $attribute) {
$originalAttributes = $this->owner->attributes;
// print_r(!empty($attribute));print_r("<br>");print_r(array_key_exists($key, $originalAttributes));print_r("<br>");print_r(in_array($key,
// $this->translationAttributes));print_r("<br>");print_r(!in_array($key, $blackListAttributes));die;
if (!empty($attribute) && array_key_exists($key, $originalAttributes) && in_array($key,
$this->translationAttributes) && !in_array($key, $blackListAttributes)) {
$this->owner->$key = $attribute;
} else if (!empty($module->defaultTranslationLanguage) && empty($attribute) && array_key_exists($key,
$originalAttributes) && in_array($key, $this->translationAttributes) && !in_array($key,
$blackListAttributes) && (empty($module->defaultLanguage) || (strcasecmp($module->defaultLanguage,
$language) != 0))) {
$defaultTranslationAttributes = $this->getTranslation($module->defaultTranslationLanguage);
if (!empty($defaultTranslationAttributes) && !empty($defaultTranslationAttributes->$key)) {
$this->owner->$key = $defaultTranslationAttributes->$key;
}
}
}
}
......@@ -269,10 +285,11 @@ class TranslateableBehavior extends Behavior
*/
public function setLanguage($value)
{
$value = strtolower($value);
$value = self::getMappedLanguage(strtolower($value));
if (!isset($this->_models[$value])) {
$this->_models[$value] = $this->loadTranslation($value);
}
$this->_language = $value;
}
......@@ -289,6 +306,9 @@ class TranslateableBehavior extends Behavior
$this->_language = $this->defaultLanguage;
}
}
if (!empty($this->_language)) {
$this->_language = self::getMappedLanguage($this->_language);
}
return $this->_language;
}
......@@ -299,16 +319,17 @@ class TranslateableBehavior extends Behavior
*/
public function saveTranslation($save = true)
{
$delete = $this->owner->deleted_by;
$languages = [];
$module = \Yii::$app->getModule('translation');
$result = true;
$languages = \arter\amos\translation\models\TranslationConf::getStaticAllActiveLanguages($module->byPassPermissionInlineTranslation)->asArray()->all();
$defaultLanguage = (!empty($this->defaultLanguage) ? $this->defaultLanguage : null);
$platformLanguage = self::getMappedLanguage(\Yii::$app->language);
$delete = $this->owner->deleted_by;
$languages = [];
$module = \Yii::$app->getModule('translation');
$result = true;
$languages = \elitedivision\amos\translation\models\TranslationConf::getStaticAllActiveLanguages($module->byPassPermissionInlineTranslation)->asArray()->all();
$defaultLanguage = (!empty($this->defaultLanguage) ? $this->defaultLanguage : null);
if ($delete) {
if ($this->afterDeleteSource == self::DELETE_ONLY_LANGUAGE_SELECTED) {
$languages = [];
$languages[]['language_id'] = \Yii::$app->language;
$languages[]['language_id'] = $platformLanguage;
}
}
$classOwner = StringHelper::basename(get_class($this->owner));
......@@ -383,7 +404,7 @@ class TranslateableBehavior extends Behavior
$result = $result && $ret;
} else {
$this->rollbackSource($model, $dirty, $defaultLanguage);
if (\Yii::$app->language == $language) {
if ($platformLanguage == $language) {
// pr($language,'language');
try {
$model->setIsNewRecord(true);
......@@ -427,7 +448,8 @@ class TranslateableBehavior extends Behavior
*/
private function rollbackSource($model, $dirty, $defaultLanguage = null)
{
if (\Yii::$app->language != $defaultLanguage || $defaultLanguage == null) {
$language = self::getMappedLanguage(\Yii::$app->language);
if ($language != $defaultLanguage || $defaultLanguage == null) {
if (!empty($dirty) && !is_null($this->owner)) {
$module = \Yii::$app->getModule('translation');
$classOwner = get_class($this->owner);
......@@ -569,6 +591,7 @@ class TranslateableBehavior extends Behavior
$translation = $class::find()->andWhere(
[$this->languageField => $language, key($relation->link) => $this->owner->getPrimarykey()]
);
if ($module->cached) {
$translation = CachedActiveQuery::instance($translation);
$translation->cache($module->cacheDuration, $module->queryCache);
......@@ -643,4 +666,17 @@ class TranslateableBehavior extends Behavior
}
}
}
/**
*
* @param string $language
* @return string
*/
public static function getMappedLanguage($language)
{
if (method_exists('elitedivision\amos\core\i18n\MessageSource', 'getMappedLanguage')) {
return \elitedivision\amos\core\i18n\MessageSource::getMappedLanguage($language);
}
return $language;
}
}
\ No newline at end of file
<?php
/**
* Copyright 2020 Art-ER S. Cons. P.A.
* EROI - Emilia Romagna Open Innovation is based on:
* https://www.open2.0.regione.lombardia.it
* Lombardia Informatica S.p.A.
* OPEN 2.0
*
* @see http://example.com Developers'community
* @license GPLv3
* @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
*
* @package arter\amos\translation
* @package elitedivision\amos\translation
* @category CategoryName
* @author Elite Division S.r.l.
* @author Lombardia Informatica S.p.A.
*/
namespace arter\amos\translation\bootstrap;
namespace elitedivision\amos\translation\bootstrap;
use arter\amos\translation\AmosTranslation;
use arter\amos\translation\models\LanguageTranslateUserFields;
use elitedivision\amos\translation\AmosTranslation;
use elitedivision\amos\translation\models\LanguageTranslateUserFields;
use Yii;
use yii\base\Event;
use yii\db\ActiveRecord;
use yii\helpers\FileHelper;
/**
* Class EventActiveRecordBootstrap
* @package arter\amos\translation\bootstrap
* @package elitedivision\amos\translation\bootstrap
*/
class EventActiveRecordBootstrap extends \yii\db\ActiveRecord
{
......@@ -89,10 +89,29 @@ class EventActiveRecordBootstrap extends \yii\db\ActiveRecord
{
$processed = [];
$translationModule = \Yii::$app->getModule('translation');
$path = \Yii::getAlias('@'.str_replace('\\', '/', $translationModule->modelNs)).'/'."{$translationModule->fileNameDbConfFields}".'.php';
if(!is_writable($path)) {
throw new \Exception("La Directory {$path} non esiste o non è scrivibile, bisogna correggere manualmente il problema");
$dirPath = \Yii::getAlias('@'.str_replace('\\', '/', $translationModule->modelNs));
$path = $dirPath.'/'."{$translationModule->fileNameDbConfFields}".'.php';
if (!is_dir($dirPath)) {
FileHelper::createDirectory($dirPath);
} else if (!is_writable($dirPath)) {
$ok = chmod($dirPath, 0775);
if (!$ok) {
throw new \Exception("La Directory {$dirPath} non esiste o non è scrivibile, bisogna correggere manualmente il problema");
}
}
if (!file_exists($path)) {
$handle = fopen($path, 'w');
fwrite($handle, "<?php return [];");
fclose($handle);
}
if (!is_writable($path)) {
$ok2 = chmod($path, 0775);
if (!$ok2) {
throw new \Exception("Il File {$path} non esiste o non è scrivibile, bisogna correggere manualmente il problema");
}
}
try {
......
<?php
/**
* Copyright 2020 Art-ER S. Cons. P.A.
* EROI - Emilia Romagna Open Innovation is based on:
* https://www.open2.0.regione.lombardia.it
* Lombardia Informatica S.p.A.
* OPEN 2.0
*
* @see http://example.com Developers'community
* @license GPLv3
* @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
*
* @package arter\amos\translation
* @package elitedivision\amos\translation
* @category CategoryName
* @author Elite Division S.r.l.
* @author Lombardia Informatica S.p.A.
*/
namespace arter\amos\translation\bootstrap;
namespace elitedivision\amos\translation\bootstrap;
use Yii;
use yii\base\Application;
use yii\helpers\FileHelper;
use arter\amos\translation\models\TranslationConf;
use arter\amos\translation\AmosTranslation;
use elitedivision\amos\translation\models\TranslationConf;
use elitedivision\amos\translation\AmosTranslation;
/**
* Class TranslationBootstrap: <br />
......@@ -55,7 +54,7 @@ class EventViewBootstrap extends \yii\web\View
$modelsEnabled = [];
$blackListModels = array_merge([
'arter\amos\translation\bootstrap\EventViewBootstrap',
'elitedivision\amos\translation\bootstrap\EventViewBootstrap',
], $moduleTranslation->eventViewBlackListModels);
$blackListViews = array_merge([
......
<?php
/**
* Copyright 2020 Art-ER S. Cons. P.A.
* EROI - Emilia Romagna Open Innovation is based on:
* https://www.open2.0.regione.lombardia.it
* Lombardia Informatica S.p.A.
* OPEN 2.0
*
* @see http://example.com Developers'community
* @license GPLv3
* @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
*
* @package arter\amos\translation
* @package elitedivision\amos\translation
* @category CategoryName
* @author Elite Division S.r.l.
* @author Lombardia Informatica S.p.A.
*/
namespace arter\amos\translation\components;