/**
* @param $target NotificationTarget object
* @param $user_infos array
* @param $event
* @param $options array
*
* @return id of the template in templates_by_languages / false if computation failed
**/
function getTemplateByLanguage(NotificationTarget $target, $user_infos = array(), $event, $options = array())
{
$lang = array();
$language = $user_infos['language'];
if (isset($user_infos['additionnaloption'])) {
$additionnaloption = $user_infos['additionnaloption'];
} else {
$additionnaloption = array();
}
$tid = $language;
$tid .= serialize($additionnaloption);
$tid = sha1($tid);
if (!isset($this->templates_by_languages[$tid])) {
//Switch to the desired language
$bak_dropdowntranslations = $_SESSION['glpi_dropdowntranslations'];
$_SESSION['glpi_dropdowntranslations'] = DropdownTranslation::getAvailableTranslations($language);
Session::loadLanguage($language);
$bak_language = $_SESSION["glpilanguage"];
$_SESSION["glpilanguage"] = $language;
//If event is raised by a plugin, load it in order to get the language file available
if ($plug = isPluginItemType(get_class($target->obj))) {
Plugin::loadLang(strtolower($plug['plugin']), $language);
}
//Get template's language data for in this language
$options['additionnaloption'] = $additionnaloption;
$data =& $target->getForTemplate($event, $options);
$footer_string = Html::entity_decode_deep(sprintf(__('Automatically generated by GLPI %s'), GLPI_VERSION));
$add_header = Html::entity_decode_deep($target->getContentHeader());
$add_footer = Html::entity_decode_deep($target->getContentFooter());
//Restore default language
$_SESSION["glpilanguage"] = $bak_language;
Session::loadLanguage();
$_SESSION['glpi_dropdowntranslations'] = $bak_dropdowntranslations;
if ($plug = isPluginItemType(get_class($target->obj))) {
Plugin::loadLang(strtolower($plug['plugin']));
}
if ($template_datas = $this->getByLanguage($language)) {
//Template processing
// Decode html chars to have clean text
$template_datas['content_text'] = Html::entity_decode_deep($template_datas['content_text']);
$save_data = $data;
$data = Html::entity_decode_deep($data);
$template_datas['subject'] = Html::entity_decode_deep($template_datas['subject']);
$this->signature = Html::entity_decode_deep($this->signature);
$lang['subject'] = $target->getSubjectPrefix($event) . self::process($template_datas['subject'], $data);
$lang['content_html'] = '';
//If no html content, then send only in text
if (!empty($template_datas['content_html'])) {
// Encode in HTML all chars
$data_html = Html::entities_deep($data);
$data_html = Html::nl2br_deep($data_html);
// Restore HTML tags
if (count($target->html_tags)) {
foreach ($target->html_tags as $tag) {
if (isset($save_data[$tag])) {
$data_html[$tag] = $save_data[$tag];
}
}
}
$signature_html = Html::entities_deep($this->signature);
$signature_html = Html::nl2br_deep($signature_html);
$template_datas['content_html'] = self::process($template_datas['content_html'], $data_html);
$lang['content_html'] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>" . "<html>\n <head>\n <META http-equiv='Content-Type' content='text/html; charset=utf-8'>\n <title>" . Html::entities_deep($lang['subject']) . "</title>\n <style type='text/css'>\n " . $this->fields['css'] . "\n </style>\n </head>\n <body>\n" . (!empty($add_header) ? $add_header . "\n<br><br>" : '') . $template_datas['content_html'] . "<br><br>-- \n<br>" . $signature_html . "<br>{$footer_string}" . "<br><br>\n" . (!empty($add_footer) ? $add_footer . "\n<br><br>" : '') . "\n</body></html>";
}
$lang['content_text'] = (!empty($add_header) ? $add_header . "\n\n" : '') . Html::clean(self::process($template_datas['content_text'], $data) . "\n\n-- \n" . $this->signature . "\n" . Html::entity_decode_deep(sprintf(__('Automatically generated by GLPI %s'), GLPI_VERSION))) . "\n\n" . $add_footer;
$this->templates_by_languages[$tid] = $lang;
}
}
if (isset($this->templates_by_languages[$tid])) {
return $tid;
}
return false;
}