<?php
// $Id: i18nsync.module,v 1.1.2.26 2009/06/12 13:35:12 jareyero Exp $

/**
 * @file
 * Internationalization (i18n) package. Synchronization of translations
 *
 * Keeps vocabulary terms in sync for translations.
 * This is a per-vocabulary option.
 *
 * Ref: http://drupal.org/node/115463
 *
 * Notes:
 * This module needs to run after taxonomy, i18n, translation. Check module weight.
 *
 * @ TODO Test with CCK when possible, api may have changed.
 */

/**
 * Implementation of hook_help().
 */
function i18nsync_help($path, $arg) {
  switch ($path) {
    case 'admin/help#i18nsync' :
      $output = '<p>'. t('This module synchronizes content taxonomy and fields accross translations:') .'</p>';
      $output .= '<p>'. t('First you need to select which fields should be synchronized. Then, after a node has been updated, all enabled vocabularies and fields will be synchronized as follows:') .'</p>';
      $output .= '<ul>';
      $output .= '<li>'. t('All the node fields selected for synchronization will be set to the same value for all translations.') .'</li>';
      $output .= '<li>'. t('For multilingual vocabularies, the terms for all translations will be replaced by the translations of the original node terms.') .'</li>';
      $output .= '<li>'. t('For other vocabularies, the terms will be just copied over to all the translations.') .'</li>';
      $output .= '</ul>';
      $output .= '<p><strong>'. t('Note that permissions are not checked for each node. So if someone can edit a node and it is set to synchronize, all the translations will be synchronized anyway.') .'</strong></p>';
      $output .= '<p>'. t('To enable synchronization check content type options to select which fields to synchronize for each node type.') .'</p>';
      $output .= '<p>'. t('The list of available fields for synchronization will include some standard node fields and all CCK fields. You can add more fields to the list in a configuration variable. See README.txt for how to do it.') .'</p>';
      $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@i18n">Internationalization module</a>.', array('@i18n' => 'http://drupal.org/node/133977')) .'</p>';
      return $output;
  }
}

/**
 * Implementation of hook_theme().
 */
function i18nsync_theme() {
  return array(
    'i18nsync_workflow_checkbox' => array(
      'arguments' => array('item' => NULL),
    ),
  );
}

/**
 * Implementation of hook_form_alter().
 * - Vocabulary options
 * - Content type options
 */
function i18nsync_form_alter(&$form, $form_state, $form_id) {
  // Taxonomy vocabulary form.
  switch ($form_id) {
    case 'node_type_form':
      $type = $form['#node_type']->type;
      $current = i18nsync_node_fields($type);

      $form['workflow']['i18n']['i18nsync_nodeapi'] = array(
        '#type' => 'fieldset',
        '#tree' => TRUE,
        '#title' => t('Synchronize translations'),
        '#collapsible' => TRUE,
        '#collapsed' => !count($current),
        '#description' => t('Select which fields to synchronize for all translations of this content type.')
      );
      // Each set provides title and options. We build a big checkboxes control for it to be
      // saved as an array. Special themeing for group titles.
      foreach (i18nsync_node_available_fields($type) as $group => $data) {
        $title = $data['#title'];
        if (!empty($data['#options'])) {
          foreach ($data['#options'] as $field => $name) {
            $form['workflow']['i18n']['i18nsync_nodeapi'][$field] = array(
              '#group_title' => $title,
              '#title' => $name,
              '#type' => 'checkbox',
              '#default_value' => in_array($field, $current),
              '#theme' => 'i18nsync_workflow_checkbox',
            );
            $title = '';
          }
        }
      }
      break;
    case 'node_delete_confirm':
      // Intercept form submission so we can handle uploads, replace callback
      $form['#submit'] = array_merge(array('i18nsync_node_delete_submit'), $form['#submit']);
      break;    
    case 'node_admin_content':
      if (!empty($form['operation']) && $form['operation']['#value'] == 'delete') {
        $form['#submit'] = array_merge(array('i18nsync_node_delete_submit'), $form['#submit']);
      }
      break;
  }
}

/**
 * Submit callback for 
 * - node delete confirm
 * - node multiple delete confirm
 */
function i18nsync_node_delete_submit($form, $form_state) {
  if ($form_state['values']['confirm']) {
    if (!empty($form_state['values']['nid'])) {
      // Single node
      i18nsync_node_delete_prepare($form_state['values']['nid']);
    }
    elseif (!empty($form_state['values']['nodes'])) {
      // Multiple nodes
      foreach ($form_state['values']['nodes'] as $nid => $value) {
        i18nsync_node_delete_prepare($nid);
      }      
    }
  }
  // Then it will go through normal form submission 
}

/**
 * Prepare node for deletion, work out synchronization issues
 */
function i18nsync_node_delete_prepare($nid) {
  $node = node_load($nid);
  // Delete file associations when files are shared with existing translations 
  // so they are not removed by upload module
  if (!empty($node->tnid) && module_exists('upload')) {
    $result = db_query('SELECT u.* FROM {upload} u WHERE u.nid = %d AND u.fid IN (SELECT t.fid FROM {upload} t WHERE t.fid = u.fid AND t.nid <> u.nid)', $nid);
    while ($up = db_fetch_object($result)) {
      db_query("DELETE FROM {upload} WHERE fid = %d AND vid = %d", $up->fid, $up->vid);
    }
  }
}

/**
 * Theming function for workflow checkboxes.
 */
function theme_i18nsync_workflow_checkbox($element) {
  $output = $element['#group_title'] ? '<div class="description">'. $element['#group_title'] .'</div>' : '';
  $output .= theme('checkbox', $element);
  return $output;
}

/**
 * Implementation of hook_nodeapi().
 */
function i18nsync_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $i18nsync; // This variable will be true when a sync operation is in progress.

  // Only for nodes that have language and belong to a translation set.
  if (translation_supported_type($node->type) && !empty($node->language) && !$i18nsync) {
    switch ($op) {
      case 'load':
        // Add instance count for cck fields so we can use the information later, see hook_file_references()
        if (!empty($node->tnid) && ($sync_fields = i18nsync_node_fields($node->type)) && ($content_fields = _i18nsync_cck_fields($node->type))) {
          if ($translations = _i18nsync_node_translations($node, TRUE)) {
            $count = count($translations);
            foreach ($sync_fields as $field) {
              if (isset($content_fields[$field]) && !empty($node->$field) && is_array($node->$field)) {
                // The node field should be an array with one or more fields
                // Reminder: Use brackets for $node->{$field}[$key] as $node->$field[$key] won't work
                foreach (array_keys($node->$field) as $key) {
                  if (is_array($node->{$field}[$key])) {
                    $node->{$field}[$key]['i18nsync'] = $count;
                  }
                }
              }
            }
          }
        }
        break;
      case 'prepare translation':
        // We copy over all the fields to be synchronized.
        if ($fields = i18nsync_node_fields($node->type)) {
          i18nsync_prepare_translation($node, $node->translation_source, $fields);
        }
        break;

      case 'insert':
        // When creating a translation, there are some aditional steps, different from update
        if (!empty($node->translation_source)) {
          // Set tnid that is not set by translation module
          $node->tnid = $node->translation_source->tnid ? $node->translation_source->tnid : $node->translation_source->nid;
          // If we have files, we need to save the files that have been inherited
          if (!empty($node->files)) {
            foreach ($node->files as $fid => $file) {
              $file = (object)$file;
              if (empty($file->remove) && !isset($_SESSION['upload_files'][$fid])) {
                db_query("INSERT INTO {upload} (fid, nid, vid, list, description, weight) VALUES (%d, %d, %d, %d, '%s', %d)", $file->fid, $node->nid, $node->vid, $file->list, $file->description, $file->weight);
              }
            }
          }
        }

      case 'update':
        // Intentional no break.
        // Let's go with field synchronization.
        if (!empty($node->tnid) && ($fields = i18nsync_node_fields($node->type)) && ($translations = _i18nsync_node_translations($node, TRUE))) {
          $i18nsync = TRUE;
          foreach ($translations as $trnode) {
            if ($node->nid != $trnode->nid) {
              i18nsync_node_translation($node, $trnode, $fields, $op);
            }
          }
          $i18nsync = FALSE;
          drupal_set_message(format_plural(count($translations) - 1, 'One node translation has been synchronized.', 'All @count node translations have been synchronized.'));
        }
        break;
    }
  }
}

/**
 * Prepare node translation. Copy over sincronizable fields.
 */
function i18nsync_prepare_translation(&$node, $source, $field_list) {
  foreach ($field_list as $field) {
    if (empty($source->$field)) continue;
    switch ($field) {
      case 'taxonomy':
        i18nsync_taxonomyfield($node, $source);
        break;

      default:
        $node->$field = $source->$field;
        break;
    }
  }
}

/**
 * Synchronizes fields for node translation.
 *
 * There's some specific handling for known fields like:
 * - files, for file attachments.
 * - iid (CCK node attachments, translations for them will be handled too).
 *
 * All the rest of the fields will be just copied over.
 * The 'revision' field will have the special effect of creating a revision too for the translation.
 *
 * @param $node
 *   Source node being edited.
 * @param $translation
 *   Node translation to synchronize, just needs nid property.
 * @param $fields
 *   List of fields to synchronize.
 * @param $op
 *   Node operation (insert|update).
 */
function i18nsync_node_translation($node, $translation, $fields, $op) {
  // Load full node, we need all data here.
  $translation = node_load($translation->nid, NULL, TRUE);

  // Collect info on any CCK fields.
  $content_fields = _i18nsync_cck_fields($node->type);

  foreach ($fields as $field) {
    // Check for CCK fields first.
    if (isset($content_fields[$field]) && isset($node->$field)) {
      switch ($content_fields[$field]['type']) {
        // TODO take type specific actions.
        case 'filefield':
        case 'imagefield':
          break;

        case 'nodereference':
          i18nsync_node_translation_nodereference_field($node, $translation, $field);
          break;

        default:
          // For fields that don't need special handling.
          $translation->$field = $node->$field;
      }
      // Skip over the regular handling.
      continue;
    }
    else {
      switch ($field) {
        case 'taxonomy': // Do nothing it has already been syncd.
          i18nsync_taxonomyfield($translation, $node);
          break;

        case 'parent': // Book outlines, translating parent page if exists.
        case 'iid': // Attached image nodes.
          i18nsync_node_translation_attached_node($node, $translation, $field);
          break;

        case 'files':
          // Sync existing attached files.
          foreach ((array)$node->files as $fid => $file) {
            if (isset($translation->files[$fid])) {
              $translation->files[$fid]->list = is_array($file) ? $file['list'] : $file->list;
            }
            else {
              // New file. Create new revision of file for the translation.
              $translation->files[$fid] = $file;
              // If it's a new node revision it will just be created, but if it's not
              // we have to update the table directly. The revision field was before this one in the list.
              if (!isset($translation->revision) || !$translation->revision) {
                db_query("INSERT INTO {upload} (fid, nid, vid, list, description) VALUES (%d, %d, %d, %d, '%s')", $file->fid, $translation->nid, $translation->vid, $file->list, $file->description);
              }
            }
          }
          // Drop removed files.
          foreach ((array)$translation->files as $fid => $file) {
            if (!isset($node->files[$fid])) {
              $translation->files[$fid]->remove = TRUE;
            }
          }
          break;

        default:
          // For fields that don't need special handling.
          if (isset($node->$field)) {
            $translation->$field = $node->$field;
          }
      }
    }
  }
  node_save($translation);
}

/**
 * Synchronize taxonomy.
 *
 * Translate translatable terms, just copy over the rest.
 */
function i18nsync_taxonomyfield(&$node, $source) {
  if (module_exists('i18ntaxonomy')) {
    $node->taxonomy = i18ntaxonomy_translate_terms($source->taxonomy, $node->language);
  }
  else {
    // If not multilingual taxonomy enabled, just copy over.
    $node->taxonomy = $source->taxonomy;
  }
}

/**
 * Node attachments (CCK) that may have translation.
 */
function i18nsync_node_translation_attached_node(&$node, &$translation, $field) {
  if ($attached = node_load($node->$field)) {
    $translation->$field = i18nsync_node_translation_reference_field($attached, $node->$field, $translation->language);
  }
}

/**
 * Translating a nodereference field (cck).
 */
function i18nsync_node_translation_nodereference_field(&$node, &$translation, $field) {
  $translated_references = array();
  foreach ($node->$field as $reference) {
    if ($reference_node = node_load($reference['nid'])) {
      $translated_references[] = array(
        'nid' => i18nsync_node_translation_reference_field($reference_node, $reference['nid'], $translation->language)
      );
    }
  }
  $translation->$field = $translated_references;
}

/**
 * Helper function to which translates reference field. We try to use translations for reference, otherwise fallback.
 * Example:
 *   English A references English B and English C.
 *   English A and B are translated to German A and B, but English C is not.
 *   The syncronization from English A to German A would it German B and English C.
 */
function i18nsync_node_translation_reference_field(&$reference_node, $default_value, $langcode) {
  if (isset($reference_node->tnid) && translation_supported_type($reference_node->type)) {
    // This content type has translations, find the one.
    if (($reference_trans = translation_node_get_translations($reference_node->tnid)) && isset($reference_trans[$langcode])) {
      return $reference_trans[$langcode]->nid;
    }
    else {
      // No requested language found, just copy the field.
      return $default_value;
    }
  }
  else {
    // Content type without language, just copy the field.
    return $default_value;
  }
}

/**
 * Returns list of fields to synchronize for a given content type.
 *
 * @param $type
 *   Node type.
 */
function i18nsync_node_fields($type) {
  return variable_get('i18nsync_nodeapi_'. $type, array());
}

/**
 * Returns list of available fields for given content type.
 * 
 * There are two hidden variables (without UI) that can be used to add fields
 * with the form array('field' => 'Field name')
 *  - i18nsync_fields_node
 *  - i18nsync_fields_node_$type;
 * 
 * Fields can also be changed using hook_i18nsync_fields_alter($fields, $type)
 * 
 * @param $type
 *   Node type.
 */
function i18nsync_node_available_fields($type) {
  static $cache;

  if (!isset($cache[$type])) {
    // Default node fields.
    $fields['node']['#title'] = t('Standard node fields.');
    $options = variable_get('i18nsync_fields_node', array());
    $options += array(
      'name' => t('Author'),
      'status' => t('Status'),
      'promote' => t('Promote'),
      'moderate' => t('Moderate'),
      'sticky' => t('Sticky'),
      'revision' => t('Revision (Create also new revision for translations)'),
      'parent' => t('Book outline (with the translated parent)'),
      'taxonomy' => t('Taxonomy terms'),
    );
    if (module_exists('comment')) {
      $options['comment'] = t('Comment settings');
    }
    if (module_exists('upload')) {
      $options['files'] = t('File attachments');
    }
  
    // If no type defined yet, that's it.
    $fields['node']['#options'] = $options;
    
    if (!$type) {
      return $fields;
    }
  
    // Get variable for this node type.
    $fields += variable_get("i18nsync_fields_node_$type", array());
  
    // Image and image attach.
    /** No way to synchronize images with the current image module. See 
    if (module_exists('image') && $type == 'image') {
      $image['images'] = t('Image files');
    }
    */
    if (module_exists('image_attach') && variable_get('image_attach_'. $type, 0)) {
      $image['iid'] = t('Attached image nodes');
    }
    if (!empty($image)) {
      $fields['image']['#title'] = t('Image module');
      $fields['image']['#options'] = $image;    
    }
    // Event fields.
    if (variable_get('event_nodeapi_'. $type, 'never') != 'never') {
      $fields['event']['#title'] = t('Event fields');
      $fields['event']['#options'] = array(
        'event_start' => t('Event start'),
        'event_end' => t('Event end'),
        'timezone' => t('Timezone')
      );
    }
    // Location module
    if (module_exists('location')) {
      $options['locations'] = t('Location settings');
    }
    // Get CCK fields.
    if (($contentfields = _i18nsync_cck_fields($type))) {
      // Get context information.
      $info = module_invoke('content', 'fields', NULL, $type);
      $fields['cck']['#title'] = t('CCK fields');
      foreach ($contentfields as $name => $data) {
        $fields['cck']['#options'][$data['field_name']] = $data['widget']['label'];
      }
    }
    
    // Give a chance to modules to change/remove/add their own fields
    drupal_alter('i18nsync_fields', $fields, $type);

    $cache[$type] = $fields;
  }
  return $cache[$type];
}

/**
 * Helper function to get list of cck fields
 */
function _i18nsync_cck_fields($type) {
  if (($content = module_invoke('content', 'types', $type)) && !empty($content['fields'])) {
    return $content['fields'];   
  }
}

/**
 * Get node translations if any, optionally excluding this node
 * 
 * Translations will be stored in the node itself so we have them cached
 */
function _i18nsync_node_translations($node, $exclude = FALSE) {
  // Maybe translations are already here
  if (!empty($node->tnid) && ($translations = translation_node_get_translations($node->tnid))) {
    if ($exclude && $node->language) {
      unset($translations[$node->language]);
    }
    return $translations;
  }  
}

/**
 * Implementation of hook_file_references()
 * 
 * Inform CCK's filefield that we have other nodes using that file so it won't be deleted
 */
function i18nsync_file_references($file) {
  // We have marked the field previously on nodeapi load
  return !empty($file->i18nsync);
}

/*
 * Sample CCK field definition for Drupal 5.
'field_text' =>
    array
      'field_name' => string 'field_text' (length=10)
      'type' => string 'text' (length=4)
      'required' => string '0' (length=1)
      'multiple' => string '1' (length=1)
      'db_storage' => string '0' (length=1)
      'text_processing' => string '0' (length=1)
      'max_length' => string '' (length=0)
      'allowed_values' => string '' (length=0)
      'allowed_values_php' => string '' (length=0)
      'widget' =>
        array
          ...
      'type_name' => string 'test' (length=4)
 */
