You are here

function globallink_eck_import in GlobalLink Connect for Drupal 7.7

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

File

globallink_custom_entity/globallink_custom_entity.inc, line 109

Code

function globallink_eck_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_eck_get_translated_array($target_xml, $target_locale);
    $enid = $translated_arr['eid'];

    // get the entity type here.
    $entity_type = $translated_arr['entity_type'];
    $entity_bundle = $translated_arr['entity_bundle'];
    unset($translated_arr['eid']);
    $entity = entity_load_single($entity_type, $enid);
    if (empty($entity)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      $globallink->sourceDeleted = TRUE;
      return;
    }
    if (module_exists('metatag')) {
      if (isset($translated_arr['metatag'])) {
        $lang = entity_language($entity_type, $entity);
        $target_metatag_arr = $translated_arr['metatag'];
        if (!empty($entity->metatags[$lang])) {
          $metatags_names = array_keys($entity->metatags[$lang]);
          $n_metatag =& $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_eck_get_fields($entity_bundle, $entity_type);
    foreach ($fields as $field) {
      $field_def = field_read_field($field);
      $t_field_lang = LANGUAGE_NONE;
      if ($field_def['type'] == 'image') {
        $entity->{$field}[$target_locale] = $entity->{$field}[$source_locale];
        $delta = key($entity->{$field}[$target_locale]);
        if (!isset($translated_arr[$field])) {
          continue;
        }
        $key = key($translated_arr[$field]);
        $obj = $translated_arr[$field][$key][$delta];
        if (is_object($obj)) {
          if (isset($obj->title)) {
            $entity->{$field}[$target_locale][$delta]['title'] = $obj->title;
          }
          if (isset($obj->alt)) {
            $entity->{$field}[$target_locale][$delta]['alt'] = $obj->alt;
          }
        }
      }
      else {
        if (empty($translated_arr[$field])) {
          if (isset($entity->{$field}[$source_locale])) {
            $entity->{$field}[$target_locale] = $entity->{$field}[$source_locale];
          }
          continue;
        }
        if (key($translated_arr[$field]) !== LANGUAGE_NONE) {
          $t_field_lang = key($translated_arr[$field]);
        }
        if (empty($entity->{$field}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
          $entity->{$field}[$target_locale] = $entity->{$field}[$source_locale];
        }
        $t_field_arr = $translated_arr[$field][$t_field_lang];
        foreach ($entity->{$field}[$target_locale] as $delta => $fp_field) {
          if (empty($t_field_arr[$delta])) {
            continue;
          }
          $translation = $t_field_arr[$delta]->translatedContent;
          if ($field_def['type'] == 'link_field') {
            $entity->{$field}[$target_locale][$delta]['title'] = $translation;
          }
          else {
            $entity->{$field}[$target_locale][$delta]['value'] = $translation;
          }
        }
      }
    }
    $entity->revision = 1;
    $entity->translations->data[$target_locale] = array(
      'language' => $target_locale,
      // Target language
      'source' => $source_locale,
      // Source language
      'status' => variable_get('globallink_publish_node', 0),
      // publish translation
      'translate' => 0,
    );
    $entity->translations->hook[$target_locale] = array(
      'hook' => 'insert',
      'date' => NULL,
    );
    $result = entity_save($entity_type, $entity);
    if ($result != FALSE) {

      /*if (module_exists('field_collection') && isset($translated_arr['field_collection'])) {
          globallink_beans_save_field_collections($entity, $translated_arr, $target_locale);
        }*/
    }
  } 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;
}