You are here

function message_admin_text_copy_batch in Message 7

Batch callback. Copy the message text fields from one language to others.

Parameters

$origin_lang: The origin language.

$dest_langs: List of destination languages.

$override: Whether to override existing values.

1 string reference to 'message_admin_text_copy_batch'
message_admin_text_copy_submit in includes/message.admin.inc
Submit callback for the copy texts form.

File

./message.module, line 1066
API functions to manipulate messages.

Code

function message_admin_text_copy_batch($origin_lang, $dest_langs, $override, &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['max'] = db_select('message_type')
      ->countQuery()
      ->execute()
      ->fetchField();
  }
  $result = db_select('message_type', 'mt')
    ->fields('mt', array(
    'id',
  ))
    ->condition('id', $context['sandbox']['current_id'], '>')
    ->orderBy('id')
    ->range(0, 50)
    ->execute();
  foreach ($result as $row) {
    $context['sandbox']['progress']++;
    $context['sandbox']['current_id'] = $row->id;
    $entity = entity_load_single('message_type', $row->id);

    // Ignore the current message type if it has no value in its origin
    // language.
    if (empty($entity->message_text[$origin_lang])) {
      continue;
    }
    foreach ($entity->message_text[$origin_lang] as $delta => $message_text) {

      // Copy the message text values from the origin language to the
      // destination languages.
      foreach ($dest_langs as $dest_lang) {
        foreach (message_get_text_fields() as $field_name) {

          // Check if there's already a value in the destination language. If so,
          // skip it unless override is checked.
          if (!$override && !empty($entity->{$field_name}[$dest_lang][$delta])) {
            continue;
          }

          // Copy the origin language text.
          $entity->{$field_name}[$dest_lang][$delta] = $message_text;
        }
      }
    }

    // Save the message type with the updated texts.
    message_type_save($entity);
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}