You are here

function globallink_commerce_import in GlobalLink Connect for Drupal 7.7

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

File

globallink_commerce/globallink_commerce.inc, line 426

Code

function globallink_commerce_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_commerce_get_translated_array($target_xml);
    $pid = $translated_arr['pid'];
    unset($translated_arr['pid']);
    $product = commerce_product_load($pid);
    if (empty($product)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      $globallink->sourceDeleted = TRUE;
      return;
    }
    if (module_exists('metatag')) {
      if (isset($translated_arr['metatag'])) {
        $lang = entity_language('commerce_product', $product);
        $target_metatag_arr = $translated_arr['metatag'];
        if (!empty($product->metatags[$lang])) {
          $metatags_names = array_keys($product->metatags[$lang]);
          $n_metatag =& $product->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_commerce_get_fields($product->type);
    foreach ($fields as $field) {
      $field_def = field_read_field($field);
      $t_field_lang = LANGUAGE_NONE;
      if ($field_def['type'] == 'image') {
        if (isset($product->{$field}[$source_locale])) {
          $product->{$field}[$target_locale] = $product->{$field}[$source_locale];
          $delta = key($product->{$field}[$target_locale]);
          $obj = $translated_arr[$field][$source_locale][$delta];
          if (is_object($obj)) {
            if (isset($obj->title)) {
              $product->{$field}[$target_locale][$delta]['title'] = $obj->title;
            }
            if (isset($obj->alt)) {
              $product->{$field}[$target_locale][$delta]['alt'] = $obj->alt;
            }
          }
        }
      }
      else {
        if (empty($translated_arr[$field])) {
          if (isset($product->{$field}[$source_locale])) {
            $product->{$field}[$target_locale] = $product->{$field}[$source_locale];
          }
          continue;
        }
        if (key($translated_arr[$field]) !== LANGUAGE_NONE) {
          $t_field_lang = key($translated_arr[$field]);
        }
        if (empty($product->{$field}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
          $product->{$field}[$target_locale] = $product->{$field}[$t_field_lang];
        }
        $t_field_arr = $translated_arr[$field][$source_locale];
        foreach ($product->{$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') {
            $product->{$field}[$target_locale][$delta]['title'] = $translation;
          }
          else {
            $product->{$field}[$target_locale][$delta]['value'] = $translation;
          }
        }
      }
    }
    $product->revision = 1;
    $product->translations->data[$target_locale] = array(
      'language' => $target_locale,
      // Target language
      'source' => $source_locale,
      // Source language
      'uid' => $product->uid,
      'status' => variable_get('globallink_publish_node', 0),
      // publish translation
      'translate' => 0,
      // Translation out of date
      'created' => $product->created,
    );
    $product->translations->hook[$target_locale] = array(
      'hook' => 'insert',
      'date' => NULL,
    );
    $result = commerce_product_save($product);
    if ($result != FALSE) {
      if (module_exists('field_collection') && isset($translated_arr['field_collection'])) {
        globallink_commerce_save_field_collections($product, $translated_arr, $target_locale);
      }
    }
  } 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;
}