function tmgmt_smartling_download_file in TMGMT Translator Smartling 8.3
Same name and namespace in other branches
- 8.4 tmgmt_smartling.module \tmgmt_smartling_download_file()
- 8 tmgmt_smartling.module \tmgmt_smartling_download_file()
- 8.2 tmgmt_smartling.module \tmgmt_smartling_download_file()
8 calls to tmgmt_smartling_download_file()
- DownloadFlowTest::testDownloadFailFlow in tests/
src/ Kernel/ DownloadFlowTest.php - Download fail flow.
- DownloadFlowTest::testDownloadFailFlowImportFailed in tests/
src/ Kernel/ DownloadFlowTest.php - Download fail flow: import failed.
- DownloadFlowTest::testDownloadFailFlowValidationFailed in tests/
src/ Kernel/ DownloadFlowTest.php - Download fail flow: validation failed.
- DownloadFlowTest::testDownloadSuccessFullFlow in tests/
src/ Kernel/ DownloadFlowTest.php - Download success full flow.
- DownloadFlowTest::testDownloadSuccessPartialFlowImportSkipped in tests/
src/ Kernel/ DownloadFlowTest.php - Download success partial flow: import skipped.
File
- ./
tmgmt_smartling.module, line 34 - Contains
Code
function tmgmt_smartling_download_file(JobInterface $job) {
$api_wrapper = $job
->getTranslatorPlugin()
->getApiWrapper($job
->getTranslator()
->getSettings());
$api_wrapper
->createAuditLogRecord($job, NULL, Drupal::currentUser(), CreateRecordParameters::ACTION_TYPE_DOWNLOAD);
try {
$smartling_api = $api_wrapper
->getApi('file');
$retrieval_type = $job
->getTranslator()
->getSetting('retrieval_type');
$filename = $job
->getTranslatorPlugin()
->getFileName($job);
$download_parameters = new DownloadFileParameters();
$download_parameters
->set('retrievalType', $retrieval_type);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$xml = $smartling_api
->downloadFile($filename, $job
->getRemoteTargetLanguage(), $download_parameters);
} catch (\Exception $e) {
Drupal::logger('tmgmt_smartling')
->error($e
->getMessage());
$api_wrapper
->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
"message" => t('File @name (job id = @job_id) wasn\'t downloaded. Please see logs for more info.', [
'@name' => $job
->getTranslatorPlugin()
->getFileName($job),
'@job_id' => $job
->id(),
])
->render(),
"type" => "error",
]);
return FALSE;
}
$path = $job
->getSetting('scheme') . '://tmgmt_smartling_translations/' . $job
->getTranslatorPlugin()
->getFileName($job);
$dirname = dirname($path);
if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && ($file = file_save_data($xml, $path, FILE_EXISTS_REPLACE))) {
$plugin = \Drupal::service('plugin.manager.tmgmt_file.format')
->createInstance($extension);
if ($plugin) {
// Validate the file on job.
if (!$plugin
->validateImport($file
->getFileUri(), $job)) {
$job
->addMessage('Failed to validate file @file. Import for job @job_id aborted.', [
'@file' => $file
->getFileUri(),
'@job_id' => $job
->id(),
], 'error');
\Drupal::logger('tmgmt_smartling')
->error('Failed to validate file @file. Import for job @job_id aborted.', [
'@file' => $file
->getFileUri(),
'@job_id' => $job
->id(),
]);
$api_wrapper
->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
"message" => t('Translation for "@file" (job id = @job_id) was successfully downloaded but validation failed. See logs for more info.', [
'@file' => $file
->getFileUri(),
'@job_id' => $job
->id(),
])
->render(),
"type" => "error",
]);
return FALSE;
}
else {
try {
// Find job items with related entities which doesn't have
// target translation.
$job_items = $job
->getItems();
$force_import = NULL;
$entity_type_manager = \Drupal::entityTypeManager();
foreach ($job_items as $job_item) {
// Load target translation. Save job item id if it
// doesn't have target translation.
try {
$entity = $entity_type_manager
->getStorage($job_item
->getItemType())
->load($job_item
->getItemId());
// Entity can be removed by the moment we load it from the storage.
if ($entity instanceof ContentEntityInterface) {
$entity
->getTranslation($job
->getTargetLangcode());
}
} catch (Exception $e) {
// No translation found.
$force_import[$job_item
->id()] = $job_item;
}
}
// Compare old and new hashes in order to decide should we apply
// translation or not. In other words we do not want to apply
// downloaded translation if it's the same as it was.
$old_hash = $job
->get('job_file_content_hash')
->getValue();
$old_hash = !empty($old_hash[0]['value']) ? $old_hash[0]['value'] : '';
$hash = md5($xml);
if ($old_hash !== $hash || $force_import) {
$job
->set('job_file_content_hash', $hash);
$job
->save();
// Validation successful, start import.
foreach ($plugin
->import($file
->getFileUri(), $job) as $key => $value) {
if (isset($job_items[$key])) {
if ($old_hash !== $hash || isset($force_import[$key])) {
// Set active state for the job item in order to be able
// force translation. It allows to override existing
// translations that might be in an "Accepted" state.
// @see JobItem::addTranslatedData() method.
$job_items[$key]
->setState(JobItemInterface::STATE_ACTIVE);
// Import translation.
$job_items[$key]
->addTranslatedData($value, [], NULL);
}
}
}
$job
->addMessage('Successfully imported file.');
\Drupal::logger('tmgmt_smartling')
->info('Translation for "@filename" was successfully downloaded and imported.', [
'@filename' => $filename,
]);
$api_wrapper
->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
"message" => t('Translation for "@filename" (job id = @job_id) was successfully downloaded and imported.', [
'@filename' => $file
->getFileUri(),
'@job_id' => $job
->id(),
])
->render(),
"type" => "status",
]);
}
else {
$job
->addMessage('Import of downloaded file was skipped: downloaded and existing translations are equal.');
\Drupal::logger('tmgmt_smartling')
->warning('Translation for "@filename" was successfully downloaded but import was skipped: downloaded and existing translations are equal.', [
'@filename' => $filename,
]);
$api_wrapper
->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
"message" => t('Translation for "@filename" (job id = @job_id) was successfully downloaded but import was skipped: downloaded and existing translations are equal.', [
'@filename' => $file
->getFileUri(),
'@job_id' => $job
->id(),
])
->render(),
"type" => "warning",
]);
}
} catch (Exception $e) {
$job
->addMessage('File import failed with the following message: @message', [
'@message' => $e
->getMessage(),
], 'error');
\Drupal::logger('tmgmt_smartling')
->error('File import failed with the following message: @message', [
'@message' => $e
->getMessage(),
]);
$api_wrapper
->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
"message" => t('Translation for "@filename" (job id = @job_id) was successfully downloaded but import failed. See logs for more info.', [
'@filename' => $file
->getFileUri(),
'@job_id' => $job
->id(),
])
->render(),
"type" => "error",
]);
return FALSE;
}
}
}
}
return TRUE;
}