public static function importXML(\SimpleXMLElement $xml, $overwriteConflicts = false, $frontendLanguages = null, $backendLanguages = null, $userId = null, $date = null)
{
$overwriteConflicts = (bool) $overwriteConflicts;
$statistics = array('total' => 0, 'imported' => 0);
// set defaults if necessary
// we can't simply use these right away, because this function is also calls by the installer,
// which does not have Backend-functions
if ($frontendLanguages === null) {
$frontendLanguages = array_keys(BL::getWorkingLanguages());
}
if ($backendLanguages === null) {
$backendLanguages = array_keys(BL::getInterfaceLanguages());
}
if ($userId === null) {
$userId = BackendAuthentication::getUser()->getUserId();
}
if ($date === null) {
$date = BackendModel::getUTCDate();
}
// get database instance
$db = BackendModel::getContainer()->get('database');
// possible values
$possibleApplications = array('Frontend', 'Backend');
$possibleModules = (array) $db->getColumn('SELECT m.name FROM modules AS m');
// types
$typesShort = (array) $db->getEnumValues('locale', 'type');
foreach ($typesShort as $type) {
$possibleTypes[$type] = self::getTypeName($type);
}
// install English translations anyhow, they're fallback
$possibleLanguages = array('Frontend' => array_unique(array_merge(array('en'), $frontendLanguages)), 'Backend' => array_unique(array_merge(array('en'), $backendLanguages)));
// current locale items (used to check for conflicts)
$currentLocale = (array) $db->getColumn('SELECT CONCAT(application, module, type, language, name)
FROM locale');
// applications
foreach ($xml as $application => $modules) {
// application does not exist
if (!in_array($application, $possibleApplications)) {
continue;
}
// modules
foreach ($modules as $module => $items) {
// module does not exist
if (!in_array($module, $possibleModules)) {
continue;
}
// items
foreach ($items as $item) {
// attributes
$attributes = $item->attributes();
$type = \SpoonFilter::getValue($attributes['type'], $possibleTypes, '');
$name = ucfirst(\SpoonFilter::getValue($attributes['name'], null, ''));
// missing attributes
if ($type == '' || $name == '') {
continue;
}
// real type (shortened)
$type = array_search($type, $possibleTypes);
// translations
foreach ($item->translation as $translation) {
// statistics
++$statistics['total'];
// attributes
$attributes = $translation->attributes();
$language = \SpoonFilter::getValue($attributes['language'], $possibleLanguages[$application], '');
// language does not exist
if ($language == '') {
continue;
}
// the actual translation
$translation = (string) $translation;
// locale item
$locale['user_id'] = $userId;
$locale['language'] = $language;
$locale['application'] = $application;
$locale['module'] = $module;
$locale['type'] = $type;
$locale['name'] = $name;
$locale['value'] = $translation;
$locale['edited_on'] = $date;
// check if translation does not yet exist, or if the translation can be overridden
if (!in_array($application . $module . $type . $language . $name, $currentLocale) || $overwriteConflicts) {
$db->execute('INSERT INTO locale (user_id, language, application, module, type, name, value, edited_on)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE user_id = ?, value = ?, edited_on = ?', array($locale['user_id'], $locale['language'], $locale['application'], $locale['module'], $locale['type'], $locale['name'], $locale['value'], $locale['edited_on'], $locale['user_id'], $locale['value'], $locale['edited_on']));
// statistics
++$statistics['imported'];
}
}
}
}
}
// rebuild cache
foreach ($possibleApplications as $application) {
foreach ($possibleLanguages[$application] as $language) {
self::buildCache($language, $application);
}
}
return $statistics;
}