function _localize_fields_copy_i18n_field_translations in Localize Fields 7
Batch callback.
Parameters
boolean $to_foreign: TRUE: copy from localize_fields to i18n_field.
array &$batch_context:
1 string reference to '_localize_fields_copy_i18n_field_translations'
File
- ./
localize_fields.admin.inc, line 148 - Drupal Localize Fields module
Code
function _localize_fields_copy_i18n_field_translations($to_foreign, &$batch_context) {
// In drush mode $batch_context is a descendant class of ArrayObject - not an array.
// So do not do stuff like: $batch_context['sandbox'] = array(...).
$drush = drupal_is_cli();
$iteration_limit = 100;
$time_limit_percent = 50;
// Use db.locales_target.l10n_status column if it exists.
$column_l10n_status = module_exists('features_translations');
try {
$delim = LocalizeFields::CONTEXT_DELIMITER;
$location = 'localize_fields.module';
// First call.
if (empty($batch_context['sandbox'])) {
$batch_context['finished'] = 0;
$batch_context['sandbox']['iteration'] = 1;
$batch_context['sandbox']['progress'] = 0;
$batch_context['sandbox']['max'] = 0;
$batch_context['sandbox']['sources'] = array();
$batch_context['results']['sources_total'] = $batch_context['results']['sources_completed'] = $batch_context['results']['translations'] = 0;
// We'll need that parameter value when reporting in the 'finished' function.
$batch_context['results']['to_foreign'] = $to_foreign;
// Find sources having any translation.
/*
* -- i18n_field -> localize_fields
* SELECT DISTINCT src.lid
* FROM locales_source AS src
* JOIN locales_target as trg ON (trg.lid = src.lid)
* WHERE src.textgroup = 'field'
* AND trg.translation != ''
* ORDER BY src.lid
*
* -- localize_fields -> i18n_field
* SELECT DISTINCT src.lid
* FROM locales_source AS src
* JOIN locales_target as trg ON (trg.lid = src.lid)
* WHERE src.textgroup = 'default'
* AND (src.context LIKE ('field:%') OR src.context LIKE ('field_instance:%'))
* AND trg.translation != ''
* ORDER BY src.lid
*/
$query = db_select('locales_source', 'src')
->distinct();
$query
->leftJoin('locales_target', 'trg', 'trg.lid = src.lid');
$query
->fields('src', array(
'lid',
'context',
'source',
));
if (!$to_foreign) {
$query
->condition('src.textgroup', 'field');
}
else {
$query
->condition('src.textgroup', 'default')
->condition(db_or()
->condition('src.context', 'field' . $delim . '%', 'LIKE')
->condition('src.context', 'field_instance' . $delim . '%', 'LIKE'));
}
$query
->condition('trg.translation', '', '!=')
->orderBy('src.lid');
$sources = $query
->execute()
->fetchAll();
if (!$sources) {
$batch_context['finished'] = 1;
$batch_context['results'][] = $drush ? dt('No non-empty translations to copy.') : t('No non-empty translations to copy.');
return;
}
$batch_context['sandbox']['sources'] =& $sources;
$batch_context['sandbox']['max'] = $batch_context['results']['sources_total'] = count($sources);
}
elseif (empty($batch_context['sandbox']['sources'])) {
$batch_context['finished'] = 1;
$batch_context['results'][] = $drush ? dt('Should already be finished.') : t('Should already be finished.');
return;
}
else {
$batch_context['sandbox']['iteration'] += 1;
if ($batch_context['sandbox']['iteration'] > $iteration_limit) {
$replacers = array(
'!n' => $iteration_limit,
);
$batch_context['results'][] = $drush ? dt('Batch processing stopped because reached !n iterations limit.', $replacers) : t('Batch processing stopped because reached !n iterations limit.', $replacers);
$batch_context['finished'] = 1;
return;
}
$sources =& $batch_context['sandbox']['sources'];
}
$time_limit = 0;
if ($max_duration = ini_get('max_execution_time')) {
$time_limit = REQUEST_TIME + floor($max_duration * $time_limit_percent / 100);
}
while ((!$time_limit || time() < $time_limit) && $sources) {
$source = array_shift($sources);
// If the target module translates a label having such context.
if ($context = _localize_fields_i18n_field_context($source->context, $source->source, $to_foreign)) {
$copy_targets = db_select('locales_target', 'trg')
->fields('trg', array(
'language',
'translation',
))
->condition('lid', $source->lid)
->execute()
->fetchAllKeyed();
// If there already exists a source having the 'new' context, we copy translation to that.
$matching_source_id = db_select('locales_source', 'src')
->fields('src', array(
'lid',
))
->condition('source', $source->source)
->condition('context', $context)
->execute()
->fetchField();
if ($matching_source_id) {
foreach ($copy_targets as $language => $translation) {
db_merge('locales_target')
->key(array(
'lid' => $matching_source_id,
'language' => $language,
))
->fields(array(
'translation' => $translation,
))
->execute();
$batch_context['results']['translations'] += 1;
}
}
else {
// We do not create new sources for i18n_field, because i18n_field
// discovers all field label sources upon enabling anyway.
if (!$to_foreign) {
$new_source_id = db_insert('locales_source')
->fields(array(
'location' => $location,
'source' => $source->source,
'context' => $context,
))
->execute();
foreach ($copy_targets as $language => $translation) {
$fields = array(
'lid' => $new_source_id,
'translation' => $translation,
'language' => $language,
);
if ($column_l10n_status) {
$fields['l10n_status'] = 1;
}
db_insert('locales_target')
->fields($fields)
->execute();
$batch_context['results']['translations'] += 1;
}
unset($fields);
}
}
}
$batch_context['sandbox']['progress'] += 1;
$batch_context['results']['sources_completed'] += 1;
}
if ($batch_context['sandbox']['progress'] < $batch_context['sandbox']['max']) {
$batch_context['finished'] = $batch_context['sandbox']['progress'] / $batch_context['sandbox']['max'];
}
else {
$batch_context['finished'] = 1;
}
} catch (Exception $xc) {
// Stop batch processing.
$batch_context['finished'] = 1;
// Tell ..._finished.
$batch_context['results']['failed'] = TRUE;
// Log.
if ($drush) {
$ms = !$to_foreign ? dt('Failed copying translations from i18n_field to localize_fields') : dt('Failed copying translations from localize_fields to i18n_field');
drush_log($ms . '.', 'failed');
}
else {
$ms = !$to_foreign ? t('Failed copying translations from i18n_field to localize_fields') : t('Failed copying translations from localize_fields to i18n_field');
drupal_set_message($ms . '.', 'error');
}
if (module_exists('inspect') && user_access('inspect log')) {
inspect_trace($xc, array(
'category' => 'localize_fields',
'message' => $ms,
'severity' => WATCHDOG_ERROR,
));
}
else {
watchdog('localize_fields', __CLASS__ . '::' . __FUNCTION__ . ': ' . $ms . ', error: @error.', array(
'@error' => $xc
->getMessage(),
), WATCHDOG_ERROR);
}
}
}