\n";
$this->showFormButtons($options);
return true;
}
/**
* Get a translation for a value
*
* @param KnowbaseItem $item item to translate
* @param string $field field to return (default 'name')
*
* @return string the field translated if a translation is available, or the original field if not
**/
static function getTranslatedValue(KnowbaseItem $item, $field = "name") {
$obj = new self;
$found = $obj->find([
'knowbaseitems_id' => $item->getID(),
'language' => $_SESSION['glpilanguage']
]);
if ((count($found) > 0)
&& in_array($field, ['name', 'answer'])) {
$first = array_shift($found);
return $first[$field];
}
return $item->fields[$field];
}
/**
* Is kb item translation functionnality active
*
* @return true if active, false if not
**/
static function isKbTranslationActive() {
global $CFG_GLPI;
return $CFG_GLPI['translate_kb'];
}
/**
* 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 item the item to check
*
* @return true if item can be translated, false otherwise
**/
static function canBeTranslated(CommonGLPI $item) {
return (self::isKbTranslationActive()
&& $item instanceof KnowbaseItem);
}
/**
* Return the number of translations for an item
*
* @param KnowbaseItem $item
*
* @return integer the number of translations for this item
**/
static function getNumberOfTranslationsForItem($item) {
return countElementsInTable(getTableForItemType(__CLASS__),
['knowbaseitems_id' => $item->getID()]);
}
/**
* Get already translated languages for item
*
* @param item
*
* @return array of already translated languages
**/
static function getAlreadyTranslatedForItem($item) {
global $DB;
$tab = [];
$iterator = $DB->request([
'FROM' => getTableForItemType(__CLASS__),
'WHERE' => ['knowbaseitems_id' => $item->getID()]
]);
while ($data = $iterator->next()) {
$tab[$data['language']] = $data['language'];
}
return $tab;
}
function pre_updateInDB() {
$revision = new KnowbaseItem_Revision();
$translation = new KnowbaseItemTranslation();
$translation->getFromDB($this->getID());
$revision->createNewTranslated($translation);
}
/**
* Reverts item translation contents to specified revision
*
* @param integer $revid Revision ID
*
* @return boolean
*/
public function revertTo($revid) {
$revision = new KnowbaseItem_Revision();
$revision->getFromDB($revid);
$values = [
'id' => $this->getID(),
'name' => $revision->fields['name'],
'answer' => $revision->fields['answer']
];
if ($this->update($values)) {
Event::log($this->getID(), "knowbaseitemtranslation", 5, "tools",
//TRANS: %1$s is the user login, %2$s the revision number
sprintf(__('%1$s reverts item translation to revision %2$s'), $_SESSION["glpiname"], $revision));
return true;
} else {
return false;
}
}
}