You are here

function globallink_file_entity_import in GlobalLink Connect for Drupal 7.7

Updates the file entity with translation.

Parameters

array $globallink_arr: Array of GlobalLink objects.

1 call to globallink_file_entity_import()
globallink_background_import in ./globallink_background_jobs.inc
Imports the documents

File

globallink_file_entity/globallink_file_entity.inc, line 194

Code

function globallink_file_entity_import(&$globallink) {
  module_load_include('inc', 'globallink', 'globallink');
  $target_xml = $globallink->targetXML;
  try {
    if (empty($target_xml)) {
      throw new Exception("Target XML is missing.");
    }
    $target_locale = globallink_get_drupal_locale_code($globallink->targetLocale);
    $source_locale = globallink_get_drupal_locale_code($globallink->sourceLocale);
    $translated_arr = globallink_file_entity_get_translated_array($target_xml);
    $fid = $translated_arr['fid'];
    unset($translated_arr['fid']);
    $entity_arr = entity_load('file', FALSE, array(
      'fid' => $fid,
    ));
    $key = key($entity_arr);
    $file_entity = $entity_arr[$key];
    if (empty($file_entity)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      $globallink->sourceDeleted = TRUE;
      return;
    }
    if (module_exists('metatag')) {
      if (isset($translated_arr['metatag'])) {
        $lang = entity_language('file', $file_entity);
        $target_metatag_arr = $translated_arr['metatag'];
        if (!empty($file_entity->metatags[$lang])) {
          $metatags_names = array_keys($file_entity->metatags[$lang]);
          $n_metatag =& $file_entity->metatags;
          foreach ($metatags_names as $name) {
            if (isset($target_metatag_arr[$name]) && isset($target_metatag_arr[$name][0])) {
              $gl_obj = $target_metatag_arr[$name]['0'];
              if (is_object($gl_obj)) {
                $translated_content = $gl_obj->translatedContent;
              }
              else {
                $translated_content = $gl_obj;
              }
              $n_metatag[$target_locale][$name] = array(
                'value' => $translated_content,
              );
            }
          }
        }
      }
    }
    $fields = globallink_file_entity_get_fields($file_entity->type);
    foreach ($fields as $field) {
      $field_def = field_read_field($field);
      $t_field_lang = LANGUAGE_NONE;

      //If no translations present copy the source data.
      if (empty($translated_arr[$field])) {
        if (isset($file_entity->{$field}[$source_locale])) {
          $file_entity->{$field}[$target_locale] = $file_entity->{$field}[$source_locale];
        }
        else {
          $file_entity->{$field}[$target_locale] = $file_entity->{$field}[$t_field_lang];
        }
        continue;
      }
      if (empty($file_entity->{$field}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
        if (isset($file_entity->{$field}[$t_field_lang])) {
          $file_entity->{$field}[$target_locale] = $file_entity->{$field}[$t_field_lang];
        }
        else {
          $file_entity->{$field}[$target_locale] = $file_entity->{$field}[$source_locale];
        }
      }
      if (key($translated_arr[$field]) !== LANGUAGE_NONE) {
        $t_field_lang = key($translated_arr[$field]);
      }
      $t_field_arr = $translated_arr[$field][$t_field_lang];
      foreach ($file_entity->{$field}[$target_locale] as $delta => $fp_field) {
        if (empty($t_field_arr[$delta])) {
          continue;
        }
        if (isset($t_field_arr[$delta]->translatedContent)) {
          $translation = $t_field_arr[$delta]->translatedContent;
        }
        if ($field_def['type'] == 'link_field') {
          $file_entity->{$field}[$target_locale][$delta]['title'] = $translation;
        }
        elseif ($field_def['type'] == 'image') {
          if (isset($t_field_arr[$delta]->alt)) {
            $file_entity->{$field}[$target_locale][$delta]['alt'] = $t_field_arr[$delta]->alt;
          }
          if (isset($t_field_arr[$delta]->title)) {
            $file_entity->{$field}[$target_locale][$delta]['title'] = $t_field_arr[$delta]->title;
          }
        }
        else {
          $file_entity->{$field}[$target_locale][$delta]['value'] = $translation;
        }
      }
    }
    $file_entity->revision = 1;
    $file_entity->translations->data[$target_locale] = array(
      'language' => $target_locale,
      'source' => $source_locale,
      'uid' => $file_entity->uid,
      'status' => variable_get('globallink_publish_node', 0),
      'translate' => 0,
    );
    $file_entity->translations->hook[$target_locale] = array(
      'hook' => 'insert',
      'date' => NULL,
    );
    file_save($file_entity);
  } catch (SoapFault $se) {
    $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
    watchdog(GLOBALLINK_MODULE, 'Exception - %function - File[%file], Line[%line], Code[%code], Message[%message]', array(
      '%function' => __FUNCTION__,
      '%file' => $e
        ->getFile(),
      '%line' => $e
        ->getLine(),
      '%code' => $e
        ->getCode(),
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }
  if ($globallink->status != GLOBALLINK_STATUS_TRANSLATION_ERROR) {
    $globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
  }
  return;
}