You are here

function globallink_fieldable_panels_import in GlobalLink Connect for Drupal 7.7

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

File

globallink_fieldable_panels/globallink_fieldable_panels.inc, line 343

Code

function globallink_fieldable_panels_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_fieldable_panels_get_translated_array($target_xml);
    $fpid = $translated_arr['fpid'];
    unset($translated_arr['fpid']);
    $entity = fieldable_panels_panes_load($fpid);
    if (empty($entity)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      $globallink->sourceDeleted = TRUE;
      return;
    }
    $fields = globallink_fieldable_panels_pane_get_fields($entity->bundle);
    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 (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;
        }
        $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]->title)) {
            $entity->{$field}[$target_locale][$delta]['title'] = $t_field_arr[$delta]->title;
          }
          if (isset($t_field_arr[$delta]->alt)) {
            $entity->{$field}[$target_locale][$delta]['alt'] = $t_field_arr[$delta]->alt;
          }
        }
        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,
    );
    fieldable_panels_panes_save($entity);
  } catch (Exception $e) {
    $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;
}