You are here

function lingotek_entity_translation_save_status in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.util.inc \lingotek_entity_translation_save_status()
  2. 7.4 lingotek.util.inc \lingotek_entity_translation_save_status()
  3. 7.5 lingotek.util.inc \lingotek_entity_translation_save_status()

Save a translation to the Entity Translation table with status and additional parameters

4 calls to lingotek_entity_translation_save_status()
lingotek_cleanup_notify_entity_translation in ./lingotek.util.inc
Report all Lingotek translations to the Entity Translation module
lingotek_entity_download in ./lingotek.module
lingotek_publish_form_submit in ./lingotek.page.inc
Submit handler for the lingotek_publish_form form. Update the entity_translation module publishing fields
lingotek_update_7408 in ./lingotek.install
Creates an upgrade path for existing translated content to be inserted into entity_translation module table if necessary

File

./lingotek.util.inc, line 1236
Utility functions.

Code

function lingotek_entity_translation_save_status($entity_type, $entity, $language_codes, $addtl_params = NULL) {
  global $user;
  $languages_updated = array();
  $updates = 0;
  if (!entity_translation_enabled($entity_type)) {
    return array(
      $languages_updated,
      $updates,
    );
  }
  if ($entity_type == 'node' && !entity_translation_node_supported_type($entity->type)) {
    return array(
      $languages_updated,
      $updates,
    );
  }

  // Lock reading for writing to the entity translation table
  // since many ET entries may be written concurrently
  for ($i = 0; $i < 10; $i++) {
    $acquired = lock_acquire('lingotek_entity_translation');
    if ($acquired) {
      break;
    }
    sleep($i + 1);
  }
  if (!$acquired) {
    LingotekLog::error("Failed to obtain lock to write Entity Translation status.");
    return array(
      $languages_updated,
      $updates,
    );
  }
  list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
  $source_langcode = lingotek_entity_langcode($entity_type, $entity);
  if ($vid === NULL) {
    $vid = $id;
  }
  $vid = $vid !== NULL ? $vid : $id;
  $uid = !empty($entity->uid) ? $entity->uid : $user->uid;
  $translate = isset($entity->translate) ? $entity->translate : 0;
  $changed = !empty($addtl_params['changed']) ? $addtl_params['changed'] : time();
  $created = !empty($addtl_params['created']) ? $addtl_params['created'] : time();
  foreach ($language_codes as $langcode) {
    if ($langcode !== 0) {
      $et_params = array(
        'entity_type' => $entity_type,
        'entity_id' => $id,
        'revision_id' => $vid,
        'language' => $langcode,
        'uid' => $uid,
        'translate' => $translate,
        'changed' => $changed,
      );
      if ($langcode != $source_langcode) {
        $et_params['source'] = $source_langcode;
      }
      if (isset($addtl_params['status_request'])) {
        $et_params['status'] = $addtl_params['status_request'];
      }
      else {

        // Check for a current status for the given langcode.
        if (isset($entity->translations->data[$langcode]['status'])) {
          $et_params['status'] = $entity->translations->data[$langcode]['status'];
        }
        elseif (isset($entity->translations->data[$entity->language])) {
          $et_params['status'] = $entity->translations->data[$entity->language]['status'];
        }
        else {

          // Set to published as a default.
          $et_params['status'] = 1;

          // published
        }
      }

      // If Entity Translation has been upgraded to work with revisions
      // NOTE: for this, for now, we will not be incrementing the revision ID
      // of each field of a node every time any translation changes.  So,
      // if Entity Translation is upgraded to support revisions, then we will
      // write the extra columns to the entity-translation table; however,
      // we will not (for now) increment everything in every table whenever
      // a translation is downloaded.  We will simply overwrite the most
      // current revision in Drupal, leaving rollback of translation changes
      // to the Lingotek TMS.
      // If this changes in the future, we should check for the variable
      // 'entity_translation_revision_enabled' to know whether the site
      // administrator wants revisions enabled for ET.
      try {
        db_merge('entity_translation')
          ->key(array(
          'entity_type' => $entity_type,
          'entity_id' => $id,
          'language' => $langcode,
        ))
          ->insertFields(array(
          'created' => $created,
        ))
          ->fields($et_params)
          ->execute();
        db_merge('entity_translation_revision')
          ->key(array(
          'entity_type' => $entity_type,
          'entity_id' => $id,
          'revision_id' => $vid,
          'language' => $langcode,
        ))
          ->insertFields(array(
          'created' => $created,
        ))
          ->fields($et_params)
          ->execute();

        /*
         // THIS WOULD BE THE CORRECT WAY TO RECORD ET CHANGES, IF IT WORKED.
         // (currently, it appears to just find the current published revision)
         $entity = lingotek_entity_load_single($entity_type, $id);
         $handler = entity_translation_get_handler($entity_type, $entity);
         $handler->initTranslations();
         $handler->saveTranslations();
        *
        */
      } catch (PDOException $e) {

        // Entity Translation must not be upgraded yet.
        unset($et_params['revision_id']);
        db_merge('entity_translation')
          ->key(array(
          'entity_type' => $entity_type,
          'entity_id' => $id,
          'language' => $langcode,
        ))
          ->insertFields(array(
          'created' => $created,
        ))
          ->fields($et_params)
          ->execute();
      }
      $languages_updated[$langcode] = lingotek_language_name($langcode);
      $updates++;
    }
  }
  lock_release('lingotek_entity_translation');
  return array(
    $languages_updated,
    $updates,
  );
}