View source
<?php
use Drupal\tmgmt\MessageInterface;
use Drupal\tmgmt\Entity\Job;
function globallink_theme() {
return [
'globallink_comments' => [
'variables' => [
'comments' => NULL,
],
'template' => 'globallink_comments',
],
];
}
function globallink_cron() {
$globallink_translators = \Drupal::entityTypeManager()
->getStorage('tmgmt_translator')
->loadByProperties([
'plugin' => 'globallink',
]);
try {
foreach ($globallink_translators as $translator) {
if (empty($translator
->getSettings()['pd_projectid'])) {
$message = t('GlobalLink is missing connection credentials. Please fix it on TMGMT GlobalLink plugin page.');
\Drupal::logger('globallink')
->warning($message);
\Drupal::messenger()
->addWarning($message);
return;
}
$translator_plugin = $translator
->getPlugin();
$completed_translations = $translator_plugin
->getCompletedTranslations($translator);
if (!empty($completed_translations)) {
foreach ($completed_translations as $completed_translation) {
$query = \Drupal::entityQuery('tmgmt_job')
->condition('uuid', $completed_translation->clientIdentifier);
$job_ids = $query
->execute();
if (!empty($job_ids)) {
$job = Job::load(array_shift($job_ids));
$translator_plugin
->retrieveTranslation($completed_translation->ticket, $job);
}
}
}
}
} catch (Exception $ex) {
$message = t('GlobalLink: Unexpected error, ' . $ex
->getMessage());
\Drupal::logger('globallink')
->warning($message);
}
}
function globallink_tmgmt_message_insert(MessageInterface $entity) {
$langcode = \Drupal::languageManager()
->getDefaultLanguage()
->getId();
$job = $entity
->getJob();
if (!$job
->hasTranslator()) {
return;
}
$translator = $job
->getTranslator();
$settings = $translator
->getSettings();
if ($translator
->get('plugin') == 'globallink') {
if (!$entity
->get('variables')
->isEmpty() && !empty($settings['pd_notify_emails']) && !empty($settings['pd_notify_level'])) {
if (in_array($entity
->getType(), $settings['pd_notify_level'])) {
$emails = explode(' ', $settings['pd_notify_emails']);
$mail_manager = \Drupal::service('plugin.manager.mail');
$mail_manager
->mail('globallink', 'log', $emails, $langcode, [
'message' => $entity,
]);
}
}
}
}
function globallink_mail($key, &$message, $params) {
$tmgmt_message = $params['message'];
$langcode = $message['langcode'];
switch ($key) {
case 'log':
$message['subject'] = t('Translations.com: new message from @site_name', [
'@site_name' => \Drupal::config('system.site')
->get('name'),
], [
'langcode' => $langcode,
]);
$message['body'][] = t('New log message from translations.com service: @message.', [
'@message' => $tmgmt_message
->getMessage(),
]);
break;
}
}