Skip to content

Commit

Permalink
Fix completename translated value
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Oct 27, 2023
1 parent 7be6221 commit 992a338
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 57 deletions.
118 changes: 61 additions & 57 deletions src/DropdownTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,84 +279,84 @@ public function generateCompletename($input, $add = true)
{
/** @var \DBmysql $DB */
global $DB;
// Force completename translated : used for the first translation
$_SESSION['glpi_dropdowntranslations'][$input['itemtype']]['completename'] = 'completename';

//If there's already a completename for this language, get it's ID, otherwise 0
if (!is_a($input['itemtype'], CommonTreeDropdown::class, true)) {
return; // `completename` is used only for tree dropdowns
}

//If there's already a completename for this language, get it's ID, otherwise 0
$completenames_id = self::getTranslationID(
$input['items_id'],
$input['itemtype'],
'completename',
$input['language']
);
$item = new $input['itemtype']();
//Completename is used only for tree dropdowns !
if (
$item instanceof CommonTreeDropdown
&& isset($input['language'])
) {
$item->getFromDB($input['items_id']);
$foreignKey = $item->getForeignKeyField();
$item->getFromDB($input['items_id']);
$foreignKey = $item->getForeignKeyField();

//Regenerate completename : look for item's ancestors
$completename = "";
$completename = "";

//Get ancestors as an array

if ($item->fields[$foreignKey] != 0) {
if ($item->fields[$foreignKey] != 0) {
// Get translated complename from parent
$parent = new $input['itemtype']();
if ($parent->getFromDB($item->fields[$foreignKey])) {
$completename = self::getTranslatedValue(
$item->fields[$foreignKey],
$parent->getID(),
$input['itemtype'],
'completename',
$input['language']
$input['language'],
$parent->fields['completename']
);
}

if ($completename != '') {
// Append complename separator
$completename .= " > ";
}
$completename .= self::getTranslatedValue(
$item->getID(),
$input['itemtype'],
'name',
$input['language']
);
}

//Add or update completename for this language
$translation = new self();
$tmp = [];
$tmp['items_id'] = $input['items_id'];
$tmp['itemtype'] = $input['itemtype'];
$tmp['field'] = 'completename';
$tmp['value'] = addslashes($completename);
$tmp['language'] = $input['language'];
$tmp['_no_completename'] = true;
if ($completenames_id) {
$tmp['id'] = $completenames_id;
if ($completename === $item->fields['completename']) {
$translation->delete(['id' => $completenames_id]);
} else {
$translation->update($tmp);
}
// Append translated name of item
$completename .= self::getTranslatedValue(
$item->getID(),
$input['itemtype'],
'name',
$input['language'],
$item->fields['name']
);

// Add or update completename for this language
$translation = new self();
$tmp = [];
$tmp['items_id'] = $input['items_id'];
$tmp['itemtype'] = $input['itemtype'];
$tmp['field'] = 'completename';
$tmp['value'] = addslashes($completename);
$tmp['language'] = $input['language'];
$tmp['_no_completename'] = true;
if ($completenames_id) {
$tmp['id'] = $completenames_id;
if ($completename === $item->fields['completename']) {
$translation->delete(['id' => $completenames_id]);
} else {
if ($completename != $item->fields['completename']) {
$translation->add($tmp);
}
$translation->update($tmp);
}
} else {
if ($completename != $item->fields['completename']) {
$translation->add($tmp);
}
}

$iterator = $DB->request([
'SELECT' => ['id'],
'FROM' => $item->getTable(),
'WHERE' => [
$foreignKey => $item->getID()
]
]);
$iterator = $DB->request([
'SELECT' => ['id'],
'FROM' => $item->getTable(),
'WHERE' => [
$foreignKey => $item->getID()
]
]);

foreach ($iterator as $tmp) {
$input2 = $input;
$input2['items_id'] = $tmp['id'];
$this->generateCompletename($input2, $add);
}
foreach ($iterator as $tmp) {
$input2 = $input;
$input2['items_id'] = $tmp['id'];
$this->generateCompletename($input2, $add);
}
}

Expand Down Expand Up @@ -674,12 +674,16 @@ public static function getTranslatedValue($ID, $itemtype, $field = 'name', $lang
$language = $_SESSION['glpilanguage'];
}

$translated_fields = $language === $_SESSION['glpilanguage']
? $_SESSION['glpi_dropdowntranslations']
: DropdownTranslation::getAvailableTranslations($language);

//If dropdown translation is globally off, or if this itemtype cannot be translated,
//then original value should be returned
$item = new $itemtype();
if (
!$ID
|| !Session::haveTranslations($itemtype, $field)
|| !isset($translated_fields[$itemtype][$field])
) {
return $value;
}
Expand Down
105 changes: 105 additions & 0 deletions tests/functional/DropdownTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2023 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace tests\units;

use DbTestCase;
use Generator;
use Glpi\Socket;
use Glpi\Toolbox\Sanitizer;
use Session;
use State;

class DropdownTranslation extends DbTestCase
{
/**
* @return void
* @see https://github.com/glpi-project/glpi/issues/15801
*/
public function testCompleteNameGeneration()
{
global $CFG_GLPI;
$CFG_GLPI['translate_dropdowns'] = 1;

$this->login();
$itilcategory = new \ITILCategory();
$this->integer($itilcategory->add([
'name' => 'test',
]))->isGreaterThan(0);

$this->string(\DropdownTranslation::getTranslatedValue($itilcategory->getID(), 'ITILCategory', 'completename'))
->isEqualTo('');

$trans = new \DropdownTranslation();
$this->integer($trans->add([
'language' => 'fr_FR',
'itemtype' => 'ITILCategory',
'items_id' => $itilcategory->getID(),
'field' => 'name',
'value' => 'test_FR',
]))->isGreaterThan(0);
$_SESSION['glpilanguage'] = 'fr_FR';
$_SESSION['glpi_dropdowntranslations'] = \DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]);

$this->string(\DropdownTranslation::getTranslatedValue($itilcategory->getID(), 'ITILCategory', 'name'))
->isEqualTo('test_FR');
$this->string(\DropdownTranslation::getTranslatedValue($itilcategory->getID(), 'ITILCategory', 'completename'))
->isEqualTo('test_FR');

$_SESSION['glpilanguage'] = 'en_GB';
$_SESSION['glpi_dropdowntranslations'] = \DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]);

$this->string(\DropdownTranslation::getTranslatedValue($itilcategory->getID(), 'ITILCategory', 'completename'))
->isEqualTo('');

$this->integer($trans->add([
'language' => 'fr_FR',
'itemtype' => 'ITILCategory',
'items_id' => $itilcategory->getID(),
'field' => 'comment',
'value' => 'comment_FR',
]))->isGreaterThan(0);

$_SESSION['glpilanguage'] = 'fr_FR';

$_SESSION['glpi_dropdowntranslations'] = \DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]);
$this->string(\DropdownTranslation::getTranslatedValue($itilcategory->getID(), 'ITILCategory', 'name'))
->isEqualTo('test_FR');
$this->string(\DropdownTranslation::getTranslatedValue($itilcategory->getID(), 'ITILCategory', 'completename'))
->isEqualTo('test_FR');
$this->string(\DropdownTranslation::getTranslatedValue($itilcategory->getID(), 'ITILCategory', 'comment'))
->isEqualTo('comment_FR');
}
}

0 comments on commit 992a338

Please sign in to comment.