View source
<?php
function lingotek_field_language_data_cleanup_batch_create($front = FALSE) {
$operations = array();
$entity_type = 'node';
$source_language = lingotek_get_source_language();
$translated_types = lingotek_translatable_node_types();
$query1 = new EntityFieldQuery();
$nodes1 = $query1
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', $translated_types)
->propertyCondition('language', 'und', '=')
->execute();
if (isset($nodes1[$entity_type])) {
foreach ($nodes1[$entity_type] as $node1) {
$operations[] = array(
'lingotek_node_source_language_cleanup_batch_worker',
array(
$node1->nid,
$source_language,
),
);
$operations[] = array(
'lingotek_field_language_data_cleanup_batch_worker',
array(
$node1->nid,
),
);
}
}
$query2 = new EntityFieldQuery();
$nodes2 = $query2
->entityCondition('entity_type', $entity_type)
->propertyCondition('language', 'und', '<>')
->execute();
if (isset($nodes2[$entity_type])) {
foreach ($nodes2[$entity_type] as $node2) {
$operations[] = array(
'lingotek_field_language_data_cleanup_batch_worker',
array(
$node2->nid,
),
);
}
}
if (count($operations) > 0) {
$batch = array(
'title' => t('Lingotek Field Language Updater'),
'operations' => $operations,
'finished' => 'lingotek_field_language_data_cleanup_batch_finished',
'file' => 'lingotek.batch.inc',
);
batch_set($batch);
if ($front) {
batch_process('<front>');
}
}
}
function lingotek_sync_batch_create($upload = true, $download = true, $download_locales = array(), $upload_et = false, $download_incomplete = false) {
$download_commands = $download ? lingotek_get_sync_download_batch_elements($download_locales, $download_incomplete) : array();
$upload_commands = $upload ? lingotek_get_sync_upload_batch_elements($upload_et) : array();
$operations = array();
$operations[] = array(
'lingotek_mt_sync_set_status',
array(
'set',
),
);
$operations = array_merge($operations, $download_commands, $upload_commands);
$operations[] = array(
'lingotek_mt_sync_set_status',
array(
'clear',
),
);
$redirect = isset($_GET['redirect_url']) && strlen($_GET['redirect_url']) ? $_GET['redirect_url'] : LINGOTEK_BASE_URL;
if (count($operations) > 0) {
$batch = array(
'title' => t('Syncing Content Translations with Lingotek'),
'operations' => $operations,
'file' => 'lingotek.batch.inc',
);
batch_set($batch);
batch_process($redirect);
}
else {
$options = strpos($redirect, '//') !== FALSE ? array(
'external' => TRUE,
) : array();
drupal_goto($redirect, $options);
}
}
function lingotek_sync_batch_create_old() {
$download_commands = lingotek_get_sync_download_batch_elements();
$upload_commands = lingotek_get_sync_upload_batch_elements();
$operations = array();
$operations[] = array(
'lingotek_mt_sync_set_status',
array(
'set',
),
);
$operations = array_merge($operations, $download_commands, $upload_commands);
$operations[] = array(
'lingotek_mt_sync_set_status',
array(
'clear',
),
);
$redirect = isset($_GET['redirect_url']) && strlen($_GET['redirect_url']) ? $_GET['redirect_url'] : LINGOTEK_BASE_URL;
if (count($operations) > 0) {
$batch = array(
'title' => t('Syncing Content Translations with Lingotek'),
'operations' => $operations,
'file' => drupal_get_path('module', 'lingotek') . '/lib/Drupal/batch/lingotek.mt.batch.sync.inc',
);
batch_set($batch);
batch_process($redirect);
}
else {
$options = strpos($redirect, '//') !== FALSE ? array(
'external' => TRUE,
) : array();
drupal_goto($redirect, $options);
}
}
function lingotek_get_sync_upload_batch_elements() {
$operations = array();
$query = db_select('lingotek', 'l')
->fields('l');
$query
->condition('lingokey', 'node_sync_status');
$query
->condition('lingovalue', LINGOTEK_NODE_SYNC_STATUS_EDITED);
$result = $query
->execute();
while ($record = $result
->fetchAssoc()) {
$operations[] = array(
'lingotek_mt_sync_upload_node',
array(
$record['nid'],
),
);
}
return $operations;
}
function lingotek_get_sync_download_batch_elements() {
$operations = array();
$target_locales = lingotek_get_target_locales();
foreach ($target_locales as $lingotek_locale) {
$key = 'target_sync_status_' . $lingotek_locale;
$query = db_select('lingotek', 'l')
->fields('l');
$query
->condition('lingokey', $key);
$query
->condition('lingovalue', LINGOTEK_TARGET_SYNC_STATUS_PENDING);
$result = $query
->execute();
while ($record = $result
->fetchAssoc()) {
$operations[] = array(
'lingotek_mt_sync_download_node_target',
array(
$record['nid'],
$lingotek_locale,
),
);
}
}
return $operations;
}
function lingotek_batch_identify_content($front = FALSE) {
$result = db_query('SELECT DISTINCT nid FROM {lingotek}');
$existing_nids = $result
->fetchCol();
$operations = array();
foreach (lingotek_get_content_types(TRUE) as $type) {
$nodes = node_load_multiple(array(), array(
'type' => $type,
));
foreach ($nodes as $node) {
if (!in_array($node->nid, $existing_nids)) {
$operations[] = array(
'lingotek_set_node_sync_status',
array(
$node->nid,
LINGOTEK_NODE_SYNC_STATUS_EDITED,
),
);
}
}
}
$batch = array(
'title' => t('Identifying Translatable Content'),
'operations' => $operations,
'finished' => 'lingotek_batch_identify_content_finished',
);
batch_set($batch);
if ($front) {
batch_process('<front>');
}
}
function lingotek_bulk_mt_upload_batch_create() {
$operations = array();
$bundles = lingotek_translatable_node_types();
$query = db_select('lingotek', 'l')
->fields('l');
$query
->condition('lingokey', 'node_sync_status');
$query
->condition('lingovalue', LINGOTEK_NODE_SYNC_STATUS_EDITED);
$result = $query
->execute();
while ($record = $result
->fetchAssoc()) {
$operations[] = array(
'lingotek_machine_translate_node',
array(
$record['nid'],
),
);
}
$batch = array(
'title' => t('Sending Content to Lingotek for Translation.'),
'operations' => $operations,
'finished' => 'lingotek_bulk_mt_upload_batch_finished',
'file' => 'lingotek.batch.inc',
);
batch_set($batch);
batch_process('<front>');
}
function lingotek_bulk_mt_download_batch_create($lingotek_locale) {
$operations = array();
if ($lingotek_locale) {
$drupal_language_code = Lingotek::convertLingotek2Drupal($lingotek_locale);
$query = db_select('lingotek', 'l')
->fields('l');
$query
->condition('lingokey', 'node_sync_status');
$query
->condition('lingovalue', LINGOTEK_TARGET_SYNC_STATUS_PENDING);
$result = $query
->execute();
while ($record = $result
->fetchAssoc()) {
$operations[] = array(
'lingotek_download_node_machine_translation',
array(
$record['nid'],
$drupal_language_code,
),
);
}
$batch = array(
'title' => t('Downloading Lingotek Translated Content'),
'operations' => $operations,
'finished' => 'lingotek_bulk_mt_download_batch_finished',
'file' => 'lingotek.batch.inc',
);
batch_set($batch);
batch_process('<front>');
}
}
function lingotek_dashboard_mt_upload() {
lingotek_bulk_mt_upload_batch_create();
}
function lingotek_dashboard_mt_download() {
if (isset($_GET['code'])) {
lingotek_bulk_mt_download_batch_create($_GET['code']);
}
}
function lingotek_download_node_machine_translation($nid, $language_code, &$context) {
$node = node_load($nid);
lingotek_download_document($node, $language_code);
$context['results'][] = 'Download Translated Node Content: ' . $nid;
}
function lingotek_bulk_mt_download_batch_finished($success, $results, $operations) {
$messages = array();
$result_count = count($results);
if ($success) {
$messages[] = t('The process finished successfully.');
$messages[] = format_plural($result_count, 'One translated node has been downloaded.', '@count translated nodes have been downloaded.');
}
else {
$messages[] = t('The process finished with an errors. Some content may have been missed.');
$messages[] = format_plural($result_count, 'One translated node has been downloaded.', '@count translated nodes have been downloaded.');
}
$_SESSION['bulk_mt_batch_download_results'] = $messages;
drupal_goto(LINGOTEK_BASE_URL . '/mt-content-downloaded');
}
function lingotek_bulk_mt_download_batch_complete() {
$form = array();
$messages = $_SESSION['bulk_mt_batch_download_results'];
$form['lingotek_message_1'] = array(
'#markup' => '<h3>Your translated content has been downloaded.</h3>',
);
$form['lingotek_message_2'] = array(
'#markup' => '<div> </div>',
);
$form['lingotek_message_3'] = array(
'#markup' => '<div><strong>Details:</strong></div>',
);
$form['lingotek_message_4'] = array(
'#markup' => '<blockquote>',
);
$cnt = 1;
foreach ($messages as $message) {
$form['lingotek_message_details_' . $cnt] = array(
'#markup' => '<div>' . $message . '</div>',
);
$cnt++;
}
$form['lingotek_message_5'] = array(
'#markup' => '</blockquote>',
);
$form['lingotek_button_spacer'] = array(
'#markup' => '<div> </div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Continue'),
);
return $form;
}
function lingotek_bulk_mt_download_batch_complete_submit($form, $form_state) {
drupal_goto(LINGOTEK_BASE_URL);
}
function lingotek_mt_sync_upload_node($nid, &$context) {
watchdog('ltk_upload_node', 'node: @node_id', array(
'@node_id' => $nid,
), WATCHDOG_DEBUG);
$api = LingotekApi::instance();
$node = node_load($nid);
if ($existing_document = lingotek_lingonode($node->nid, 'document_id')) {
$api
->updateContentDocument($node);
}
else {
$api
->addContentDocument($node, TRUE);
}
$context['results'][] = 'Upload Node: ' . $nid;
}
function lingotek_mt_sync_download_node_target($nid, $lingotek_locale, &$context) {
watchdog('ltk_download_target', 'node: @node_id (@language)', array(
'@node_id' => $nid,
'@language' => $lingotek_locale,
), WATCHDOG_DEBUG);
$node = node_load($nid);
lingotek_download_document($node, $lingotek_locale);
$context['results'][] = t('Download Target Translation: Node #@nid (@langcode)', array(
'@nid' => $nid,
'@langcode' => $lingotek_locale,
));
}
function lingotek_mt_sync_set_status($status, &$context) {
switch ($status) {
case 'set':
$_SESSION['lingotek_sync_in_progress'] = 'syncing';
break;
case 'clear':
if (isset($_SESSION['lingotek_sync_in_progress'])) {
unset($_SESSION['lingotek_sync_in_progress']);
}
break;
}
}
function lingotek_machine_translate_node($nid, &$context) {
watchdog('ltk_upload', 'nide: @node_id', array(
'@node_id' => $nid,
), WATCHDOG_DEBUG);
$api = LingotekApi::instance();
$node = node_load($nid);
if ($existing_document = lingotek_lingonode($node->nid, 'document_id')) {
$api
->updateContentDocument($node);
}
else {
$api
->addContentDocument($node, TRUE);
}
$context['results'][] = 'Upload Node: ' . $nid;
}
function lingotek_bulk_mt_upload_batch_finished($success, $results, $operations) {
$messages = array();
$result_count = count($results);
if ($success) {
$messages[] = t('The process finished successfully.');
$messages[] = format_plural($result_count, 'One node has been queued for translation.', '@count nodes have been queued for translation.');
}
else {
$messages[] = t('The process finished with an errors. Some content may have been missed.');
$messages[] = format_plural($result_count, 'One node queued for translation.', '@count nodes queued for translation.');
}
$_SESSION['bulk_mt_batch_results'] = $messages;
drupal_goto(LINGOTEK_BASE_URL . '/mt-content-queued');
}
function lingotek_bulk_mt_upload_batch_complete() {
$form = array();
$messages = $_SESSION['bulk_mt_batch_results'];
$form['lingotek_message_1'] = array(
'#markup' => '<h3>Your content has been queued for translation.</h3>',
);
$form['lingotek_message_2'] = array(
'#markup' => '<div> </div>',
);
$form['lingotek_message_3'] = array(
'#markup' => '<div><strong>Details:</strong></div>',
);
$form['lingotek_message_4'] = array(
'#markup' => '<blockquote>',
);
$cnt = 1;
foreach ($messages as $message) {
$form['lingotek_message_details_' . $cnt] = array(
'#markup' => '<div>' . $message . '</div>',
);
$cnt++;
}
$form['lingotek_message_5'] = array(
'#markup' => '</blockquote>',
);
$form['lingotek_button_spacer'] = array(
'#markup' => '<div> </div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Continue'),
);
return $form;
}
function lingotek_bulk_mt_upload_batch_complete_submit($form, $form_state) {
drupal_goto(LINGOTEK_BASE_URL);
}
function lingotek_node_source_language_cleanup_batch_worker($nid, $source_language) {
$loaded_node = node_load($nid);
$loaded_node->language = $source_language;
$loaded_node->lingotek_upload_override = 0;
node_save($loaded_node);
}
function lingotek_field_language_data_cleanup_batch_worker($nid, &$context) {
$process_node = node_load($nid);
if ($process_node->nid) {
lingotek_field_language_data_cleanup_update_node($process_node->nid);
$context['message'] = t('Preparing translatable content: @node_title', array(
'@node_title' => $process_node->title,
));
}
$context['finished'] = 1;
}
function lingotek_field_language_data_cleanup_update_node($nid) {
$edited = FALSE;
$node = node_load($nid, NULL, TRUE);
if ($node->nid) {
if ($node->language != 'und') {
$translatable_fields = lingotek_translatable_node_fields();
foreach ($translatable_fields as $field_name) {
if (!empty($node->{$field_name}['und']) && empty($node->{$field_name}[$node->language])) {
$node->{$field_name}[$node->language] = $node->{$field_name}['und'];
$edited = TRUE;
}
}
}
if ($edited) {
$node->lingotek_upload_override = 0;
node_save($node);
}
}
else {
watchdog('lingotek', 'Attempted to update field data for a non-existent node: @node_id', array(
'@node_id' => $node_id,
), WATCHDOG_ERROR);
}
return $edited;
}
function lingotek_field_language_data_cleanup_batch_finished($success, $results, $operations) {
}