You are here

function lingotek_save_field_record in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.6 lingotek.remote.inc \lingotek_save_field_record()
1 call to lingotek_save_field_record()
lingotek_process_entity_xml in ./lingotek.remote.inc

File

./lingotek.remote.inc, line 439

Code

function lingotek_save_field_record($fname, $params) {

  // using drupal_write_record to avoid node_save - node_save overwrites publications unless called on both revised and published versions of the node (i.e. workbench_moderation)
  // UPDATE: This could perhaps be rewritten now that workbench_moderation is supported through the rules module
  $entity_type = $params['entity_type'];
  $entity_id = $params['entity_id'];
  $langcode = $params['language'];
  $lingotek_locale = Lingotek::convertDrupal2Lingotek($langcode);
  try {
    LingotekSync::deleteLastSyncError($entity_type, $entity_id);
    drupal_write_record($fname, $params);
  } catch (PDOException $e) {

    /**
     *Error logging became necessary for this try-catch when writing to the database
     *because the drupal_write_record() was failing and we had no way of knowing
     *why it was happening or even that is was happening because of the try-catch.
     *
     *The following code logs the error with the approriate detail and also displays
     *a drupal message is the error is that the data was too long to be inserted
     *into a specific column so that end users can know what happened and know why
     *a particular translation is not working
     *
     *@author t.murphy
     *
     */
    $state_error = $e->errorInfo[0];
    $driver_error = $e->errorInfo[1];
    $error_message = $e->errorInfo[2];
    if ($state_error !== '23000' && $driver_error !== '1062') {
      $log_message = t('The following error occurred while writing to the database: SQLSTATE error @sqlError (Driver-specific error code @driverError): @errorMessage', array(
        '@sqlError' => $state_error,
        '@driverError' => $driver_error,
        '@errorMessage' => $error_message,
      ));
      LingotekLog::error($log_message, $e);
    }
    if ($state_error === '22001') {
      $str_args = array();
      foreach ($e->args as $key => $value) {
        array_push($str_args, (string) $value);
      }
      $message = t('Entity @entity_id had the translation @errorDetail', array(
        '@entity_id' => $str_args[3],
        '@errorDetail' => $e->errorInfo[2],
      ));
      drupal_set_message($message, 'error');
      LingotekSync::setTargetStatus($entity_type, $entity_id, $lingotek_locale, LingotekSync::STATUS_ERROR);
      LingotekSync::setLastSyncError($entity_type, $entity_id, $error_message);
    }
    $primary_keys = array(
      'entity_type',
      'entity_id',
      'revision_id',
      'deleted',
      'delta',
      'language',
    );
    drupal_write_record($fname, $params, $primary_keys);
  }
}