You are here

function lingotek_entity_save in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.5 lingotek.module \lingotek_entity_save()
  2. 7.6 lingotek.module \lingotek_entity_save()
3 calls to lingotek_entity_save()
lingotek_entity_update in ./lingotek.module
Implements hook_entity_update().
lingotek_get_node_settings_form_submit in ./lingotek.module
lingotek_rules_entity_change_profile in ./lingotek.rules.inc

File

./lingotek.module, line 1067

Code

function lingotek_entity_save($entity, $entity_type) {
  if (!variable_get('lingotek_login_id')) {

    // Lingotek is installed but has not been setup yet
    // (could use function lingotek_is_config_missing() but did not want
    // to make that many checks on every entity create/update)
    return $entity;
  }
  $managed_entity_types = array_keys(lingotek_managed_entity_types());
  if (!in_array($entity_type, $managed_entity_types)) {

    // nothing to do here
    return $entity;
  }
  if (!isset($entity->lingotek)) {

    // load default lingotek settings when alternate node creation paths are employed (e.g., commons)
    lingotek_entity_load(array(
      $entity,
    ), $entity_type);
  }
  if (isset($entity->lingotek_upload_override)) {
    return $entity;
  }
  list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
  $has_been_uploaded = !empty($entity->lingotek['document_id']);
  $remove = lingotek_get_profile_fields(!$has_been_uploaded, TRUE);
  db_delete('lingotek_entity_metadata')
    ->condition('entity_type', $entity_type)
    ->condition('entity_id', $id)
    ->condition('entity_key', $remove, 'IN')
    ->execute();
  lingotek_cache_clear($entity_type, $id);

  // Always store the entity's profile, in case the profile of the same content
  // type changes in the future to something else.
  if ($entity_type == 'menu_link') {
    $is_source_menu_link = lingotek_is_menu_link_source($id);

    // Don't put target menu links in the lingotek entity metadata table
    if (!$is_source_menu_link) {
      return;
    }
  }
  if (!isset($entity->lingotek['profile'])) {
    drupal_set_message(t('Unable to load Lingotek profile for @entity_type. Make sure user has Lingotek permission when manipulating entities.', array(
      '@entity_type' => $entity_type,
    )), 'error');
    return;
  }
  lingotek_keystore($entity_type, $id, 'profile', $entity->lingotek['profile']);

  // If the hash changed, handle upload status and target statuses
  $entity_changed = lingotek_entity_changed($entity_type, $entity, $id);
  if ($entity_changed) {
    $target_statuses = LingotekSync::getAllTargetStatusForEntity($entity_type, $id);
    if ($entity->lingotek['profile'] == LingotekSync::PROFILE_DISABLED) {
      if ($has_been_uploaded) {
        LingotekSync::setUploadStatus($entity_type, $id, LingotekSync::STATUS_EDITED);
      }
      LingotekSync::setAllTargetStatus($entity_type, $id, LingotekSync::STATUS_UNTRACKED);
    }
    else {
      LingotekSync::setUploadStatus($entity_type, $id, LingotekSync::STATUS_NONE);
    }
  }
  $existing_targets = LingotekSync::getAllTargetStatusForEntity($entity_type, $id);
  if (count($existing_targets) == 0) {

    // Get the source language and available target languages.
    $source = isset($entity->language) ? $entity->language : language_default()->language;
    $source = Lingotek::convertDrupal2Lingotek($source);
    $target_locales = Lingotek::getLanguagesWithoutSource($source);
    foreach (array_keys($target_locales) as $locale) {
      lingotek_keystore($entity_type, $id, 'target_sync_status_' . $locale, LingotekSync::STATUS_NONE);
    }
  }

  //This reloads the lingotek values to ensure that the proper defaults and hierarchy are used.

  //We clear out the lingotek values first, then wrap in an array, call the lingotek load function, and then unwrap from array.
  unset($entity->lingotek);
  $entitys = array(
    $id => $entity,
  );
  $types = array(
    $bundle,
  );
  lingotek_entity_load($entitys, $entity_type);
  $entity = $entitys[$id];
  lingotek_entity_upload_triggered($entity, $entity_type, $entity_changed);
  return $entity;
}