You are here

function i18n_node_select_translation_submit in Internationalization 6

Same name and namespace in other branches
  1. 7 i18n_node/i18n_node.pages.inc \i18n_node_select_translation_submit()

Form submission: update / delete the translation set

File

./i18n.pages.inc, line 118
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) {
    $args = array(
      '' => $tnid,
    ) + $add;
    db_query('UPDATE {node} SET tnid = %d WHERE nid IN (' . db_placeholders($add) . ')', $args);
    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_query('UPDATE {node} SET tnid = 0 WHERE nid IN (' . db_placeholders($remove) . ')', $remove);
    drupal_set_message(format_plural(count($remove), 'Removed a node from the translation set.', 'Removed @count nodes from the translation set.'));
  }
}