You are here

function lingotek_entity_save in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.module \lingotek_entity_save()
  2. 7.5 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 1038

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_not_been_uploaded = !isset($entity->lingotek['document_id']) || empty($entity->lingotek['document_id']);
  $remove = lingotek_get_profile_fields($has_not_been_uploaded, TRUE);
  db_delete('lingotek_entity_metadata')
    ->condition('entity_type', $entity_type)
    ->condition('entity_id', $id)
    ->condition('entity_key', $remove, 'IN')
    ->execute();

  // Set the default value for whether the entity's content actually changed.
  $entity_changed = TRUE;
  if ($entity->lingotek['profile'] == LingotekSync::PROFILE_CUSTOM) {
    $query = db_insert('lingotek_entity_metadata')
      ->fields(array(
      'entity_type',
      'entity_id',
      'entity_key',
      'value',
    ));
    foreach ($remove as $key) {
      if (isset($entity->lingotek[$key])) {
        $query
          ->values(array(
          $entity_type,
          $id,
          $key,
          $entity->lingotek[$key],
        ));
      }
    }
    $query
      ->execute();

    // check for a workflow change
    if (!empty($entity->lingotek['workflow_id']) && !empty($entity->lingotek['document_id'])) {
      $curr_workflow = $entity->lingotek['workflow_id'];
      $document_id = $entity->lingotek['document_id'];
      if (!isset($entity->original->lingotek['workflow_id']) || $entity->original->lingotek['workflow_id'] != $curr_workflow) {
        $api = LingotekApi::instance();
        $prefill_checked = isset($entity->lingotek['prefill_phases_checkbox']) ? $entity->lingotek['prefill_phases_checkbox'] : NULL;
        $prefill_phase = $prefill_checked ? $entity->lingotek['prefill_phase_select'] : NULL;
        $result = $api
          ->changeWorkflow(array(
          $document_id,
        ), $curr_workflow, $prefill_phase);
        if ($result === TRUE) {
          LingotekSync::setAllTargetStatus($entity_type, $id, LingotekSync::STATUS_PENDING);
          lingotek_keystore($entity_type, $id, 'workflow_id', $curr_workflow);
        }
        else {
          LingotekLog::error('Failed to change workflow for @et #@id: @error', array(
            '@et' => $entity_type,
            '@id' => $id,
            '@error' => $result,
          ));
        }
      }
    }
  }
  else {

    // Specific Profile
    $defaults = lingotek_load_profile_defaults($entity_type);
    $profile_overridden = !isset($defaults[$bundle]['profile']) || $defaults[$bundle]['profile'] != $entity->lingotek['profile'];
    if ($profile_overridden) {
      lingotek_keystore($entity_type, $id, 'profile', $entity->lingotek['profile']);
    }
    else {
      lingotek_keystore_delete($entity_type, $id, 'profile');
    }

    // If the hash changed, handle target statuses
    $entity_changed = lingotek_entity_changed($entity_type, $entity, $id);
    if ($entity_changed) {
      if ($entity->lingotek['profile'] == LingotekSync::PROFILE_DISABLED) {
        LingotekSync::setAllTargetStatus($entity_type, $id, LingotekSync::STATUS_UNTRACKED);
      }
      else {

        // if this is a node-based translation target and target localization is enabled, set to TARGET_EDITED
        // TODO:
        // if this is a source node and target localization is enabled, set all targets to TARGET_LOCALIZE
        // TODO:
        if ($entity->lingotek['upload_status'] != LingotekSync::STATUS_TARGET) {
          LingotekSync::setAllTargetStatus($entity_type, $id, LingotekSync::STATUS_EDITED);
        }
      }
    }
  }

  //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;
}