\n";
$this->showFormButtons($options);
return true;
}
/**
* Display a dropdown with fields that can be translated for an itemtype
*
* @param CommonDBTM $item a Dropdown item
* @param string $language language to look for translations (default '')
* @param string $value field which must be selected by default (default '')
*
* @return integer the dropdown's random identifier
**/
static function dropdownFields(CommonDBTM $item, $language = '', $value = '') {
global $DB;
$options = [];
foreach (Search::getOptions(get_class($item)) as $id => $field) {
//Can only translate name, and fields whose datatype is text or string
if (isset ($field['field'])
&& ($field['field'] == 'name')
&& ($field['table'] == getTableForItemType(get_class($item)))
|| (isset($field['datatype'])
&& in_array($field['datatype'], ['text', 'string']))) {
$options[$field['field']] = $field['name'];
}
}
$used = [];
if (!empty($options)) {
$iterator = $DB->request([
'SELECT' => 'field',
'FROM' => self::getTable(),
'WHERE' => [
'itemtype' => $item->getType(),
'items_id' => $item->getID(),
'language' => $language
]
]);
if (count($iterator) > 0) {
while ($data = $iterator->next()) {
$used[$data['field']] = $data['field'];
}
}
}
//$used = array();
return Dropdown::showFromArray('field', $options, ['value' => $value,
'used' => $used]);
}
/**
* Get translated value for a field in a particular language
*
* @param integer $ID dropdown item's id
* @param string $itemtype dropdown itemtype
* @param string $field the field to look for (default 'name')
* @param string $language get translation for this language
* @param string $value default value for the field (default '')
*
* @return string the translated value of the value in the default language
**/
static function getTranslatedValue($ID, $itemtype, $field = 'name', $language = '', $value = '') {
global $DB;
if ($language == '') {
$language = $_SESSION['glpilanguage'];
}
//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)) {
return $value;
}
//ID > 0 : dropdown item might be translated !
if ($ID > 0) {
//There's at least one translation for this itemtype
if (self::hasItemtypeATranslation($itemtype)) {
$iterator = $DB->request([
'SELECT' => ['value'],
'FROM' => self::getTable(),
'WHERE' => [
'itemtype' => $itemtype,
'items_id' => $ID,
'field' => $field,
'language' => $language
]
]);
//The field is already translated in this language
if (count($iterator)) {
$current = $iterator->next();
return $current['value'];
}
}
//Get the value coming from the dropdown table
$iterator = $DB->request([
'SELECT' => $field,
'FROM' => getTableForItemType($itemtype),
'WHERE' => ['id' => $ID]
]);
if (count($iterator)) {
$current = $iterator->next();
return $current[$field];
}
}
return "";
}
/**
* Get the id of a translated string
*
* @param integer $ID item id
* @param string $itemtype item type
* @param string $field the field for which the translation is needed
* @param string $language the target language
*
* @return integer the row id or 0 if not translation found
**/
static function getTranslationID($ID, $itemtype, $field, $language) {
global $DB;
$iterator = $DB->request([
'SELECT' => ['id'],
'FROM' => self::getTable(),
'WHERE' => [
'itemtype' => $itemtype,
'items_id' => $ID,
'language' => $language,
'field' => $field
]
]);
if (count($iterator)) {
$current = $iterator->next();
return $current['id'];
}
return 0;
}
/**
* Check if an item can be translated
* It be translated if translation if globally on and item is an instance of CommonDropdown
* or CommonTreeDropdown and if translation is enabled for this class
*
* @param CommonGLPI $item the item to check
*
* @return boolean true if item can be translated, false otherwise
**/
static function canBeTranslated(CommonGLPI $item) {
return (self::isDropdownTranslationActive()
&& (($item instanceof CommonDropdown)
&& $item->maybeTranslated()));
}
/**
* Is dropdown item translation functionnality active
*
* @return true if active, false if not
**/
static function isDropdownTranslationActive() {
global $CFG_GLPI;
return $CFG_GLPI['translate_dropdowns'];
}
/**
* Get a translation for a value
*
* @param string $itemtype itemtype
* @param string $field field to query
* @param string $value value to translate
*
* @return string the value translated if a translation is available, or the same value if not
**/
static function getTranslationByName($itemtype, $field, $value) {
global $DB;
$iterator = $DB->request([
'SELECT' => ['id'],
'FROM' => getTableForItemType($itemtype),
'WHERE' => [
$field => Toolbox::addslashes_deep($value)
]
]);
if (count($iterator) > 0) {
$current = $iterator->next();
return self::getTranslatedValue($current['id'], $itemtype, $field,
$_SESSION['glpilanguage'], $value);
}
return $value;
}
/**
* Get translations for an item
*
* @param string $itemtype itemtype
* @param integer $items_id item ID
* @param string $field the field for which the translation is needed
*
* @return string the value translated if a translation is available, or the same value if not
**/
static function getTranslationsForAnItem($itemtype, $items_id, $field) {
global $DB;
$iterator = $DB->request([
'FROM' => self::getTable(),
'WHERE' => [
'itemtype' => $itemtype,
'items_id' => $items_id,
'field' => $field
]
]);
$data = [];
while ($tmp = $iterator->next()) {
$data[$tmp['id']] = $tmp;
}
return $data;
}
/**
* Regenerate all completename translations for an item
*
* @param string $itemtype itemtype
* @param integer $items_id item ID
*
* @return string the value translated if a translation is available, or the same value if not
**/
static function regenerateAllCompletenameTranslationsFor($itemtype, $items_id) {
foreach (self::getTranslationsForAnItem($itemtype, $items_id, 'completename') as $data) {
$dt = new DropdownTranslation();
$dt->generateCompletename($data, false);
}
}
/**
* Check if there's at least one translation for this itemtype
*
* @param string $itemtype itemtype to check
*
* @return boolean true if there's at least one translation, otherwise false
**/
static function hasItemtypeATranslation($itemtype) {
return countElementsInTable(self::getTable(), ['itemtype'=> $itemtype ]);
}
/**
* Get available translations for a language
*
* @param string $language language
*
* @return array of table / field translated item
**/
static function getAvailableTranslations($language) {
global $DB;
$tab = [];
if (self::isDropdownTranslationActive()) {
$iterator = $DB->request([
'SELECT' => [
'itemtype',
'field'
],
'DISTINCT' => true,
'FROM' => self::getTable(),
'WHERE' => ['language' => $language]
]);
while ($data = $iterator->next()) {
$tab[$data['itemtype']][$data['field']] = $data['field'];
}
}
return $tab;
}
}