You are here

function globallink_beans_import in GlobalLink Connect for Drupal 7.7

Gets number of translated beans.

Parameters

string $pd4: The project director details.

array $globallink_arr: Array of GlobalLink objects.

Return value

int The number of translated beans.

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

File

globallink_beans/globallink_beans.inc, line 411

Code

function globallink_beans_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_beans_get_translated_array($target_xml);
    $bnid = $translated_arr['object_id'];
    unset($translated_arr['object_id']);
    $entity_arr = entity_load('bean', array(
      $bnid,
    ));
    $key = key($entity_arr);
    $entity = $entity_arr[$key];
    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('bean', $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_bean_get_fields($entity->type);
    foreach ($fields as $field) {
      $field_def = field_read_field($field);
      $t_field_lang = LANGUAGE_NONE;
      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 ($field_def['type'] == 'image') {
        if (!isset($entity->{$field}[$target_locale])) {
          $entity->{$field}[$target_locale] = $entity->{$field}[$source_locale];
        }
      }
      if (empty($entity->{$field}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
        $entity->{$field}[$target_locale] = $entity->{$field}[$t_field_lang];
      }
      $t_field_arr = $translated_arr[$field][$source_locale];
      foreach ($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') {
          $entity->{$field}[$target_locale][$delta]['title'] = $translation;
        }
        elseif ($field_def['type'] == 'image') {
          if (isset($t_field_arr[$delta]->alt)) {
            $entity->{$field}[$target_locale][$delta]['alt'] = $t_field_arr[$delta]->alt;
          }
          if (isset($t_field_arr[$delta]->title)) {
            $entity->{$field}[$target_locale][$delta]['title'] = $t_field_arr[$delta]->title;
          }
        }
        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
      'uid' => $entity->uid,
      'status' => variable_get('globallink_publish_node', 0),
      // publish translation
      'translate' => 0,
      // Translation out of date
      'created' => $entity->created,
    );
    $entity->translations->hook[$target_locale] = array(
      'hook' => 'insert',
      'date' => NULL,
    );
    $result = bean_save($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;
}