You are here

function i18n_node_select_translation_submit in Internationalization 7

Same name and namespace in other branches
  1. 6 i18n.pages.inc \i18n_node_select_translation_submit()

Form submission: update / delete the translation set

File

i18n_node/i18n_node.pages.inc, line 186
User page callbacks for the translation module.

Code

function i18n_node_select_translation_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : NULL;
  $node = $form_state['values']['node'];
  $translations = $node->tnid ? translation_node_get_translations($node->tnid) : array(
    $node->language => $node,
  );
  foreach ($translations as $trans) {
    $current[$trans->language] = $trans->nid;
  }
  $update = array(
    $node->language => $node->nid,
  ) + array_filter($form_state['values']['translations']['nid']);

  // Compute the difference to see which are the new translations and which ones to remove
  $new = array_diff_assoc($update, $current);
  $remove = array_diff_assoc($current, $update);

  // The tricky part: If the existing source is not in the new set, we need to create a new tnid
  if ($node->tnid && in_array($node->tnid, $update)) {
    $tnid = $node->tnid;
    $add = $new;
  }
  else {

    // Create new tnid, which is the source node
    $tnid = $node->nid;
    $add = $update;
  }

  // Now update values for all nodes
  if ($add) {
    db_update('node')
      ->fields(array(
      'tnid' => $tnid,
    ))
      ->condition('nid', $add)
      ->execute();
    entity_get_controller('node')
      ->resetCache($add);
    if (count($new)) {
      drupal_set_message(format_plural(count($new), 'Added a node to the translation set.', 'Added @count nodes to the translation set.'));
    }
  }
  if ($remove) {
    db_update('node')
      ->fields(array(
      'tnid' => 0,
    ))
      ->condition('nid', $remove)
      ->execute();
    entity_get_controller('node')
      ->resetCache($remove);
    drupal_set_message(format_plural(count($remove), 'Removed a node from the translation set.', 'Removed @count nodes from the translation set.'));
  }
}