You are here

function globallink_entity_update_entity in GlobalLink Connect for Drupal 7.7

Same name and namespace in other branches
  1. 7.6 globallink_entity/globallink_entity.inc \globallink_entity_update_entity()

Updates entity.

Parameters

object $globallink: GlobalLink object.

array $t_arr: The translated array. Defaults to NULL.

2 calls to globallink_entity_update_entity()
globallink_background_import in ./globallink_background_jobs.inc
Imports the documents
globallink_upload_translation_all_form_submit in ./globallink_workbench_all_active_submissions.inc

File

globallink_entity/globallink_entity.inc, line 130

Code

function globallink_entity_update_entity($globallink, $t_arr = NULL) {
  module_load_include('inc', 'globallink', 'globallink');
  $target_nid = '';
  $target_vid = '';
  try {
    if (isset($globallink->targetLocale)) {
      $target_lang = globallink_get_drupal_locale_code($globallink->targetLocale);
    }
    if ($t_arr != NULL) {
      $translated_arr = $t_arr;
    }
    else {
      $translated_arr = globallink_entity_get_translated_array($globallink->targetXML, $target_lang);
    }
    if (isset($translated_arr['nid'])) {
      $target_nid = $translated_arr['nid'];
    }
    if (isset($translated_arr['vid'])) {
      $target_vid = $translated_arr['vid'];
    }
    $globallink->nid = $target_nid;
    $globallink->vid = $target_vid;
    $node = node_load($target_nid, $target_vid);
    if (isset($translated_arr['title'])) {
      $node->title = $translated_arr['title'];
    }
    if (isset($node->title_original) && $node->title != $node->title_original) {
      $node->title = $node->title_original;
    }
    if (!$node || is_null($node) || !is_object($node)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      return;
    }
    $nodepath = drupal_lookup_path('alias', 'node/' . $target_nid);
    if ($nodepath) {
      if (!empty($translated_arr['path'])) {
        $path = array(
          'alias' => $translated_arr['path'],
          'source' => 'node/' . $target_nid,
          'language' => $target_lang,
        );
      }
      else {
        $path = array(
          'alias' => $nodepath,
          'source' => 'node/' . $target_nid,
          'language' => $target_lang,
        );
      }
      if (module_exists('pathauto')) {
        $path['pathauto'] = 0;
        $node->path['pathauto'] = 0;
      }
      path_save($path);
    }
    $node->revision = 1;
    if (module_exists('revisioning')) {
      $node->is_pending = FALSE;
      $node->revision_moderation = FALSE;
    }

    // Node presave hook skip
    $node->tpt_skip = TRUE;
    if (module_exists('metatag')) {
      if (isset($translated_arr['metatag'])) {
        $target_metatag_arr = $translated_arr['metatag'];
        if (!empty($node->metatags[$node->language])) {
          $metatags_names = array_keys($node->metatags[$node->language]);
          $n_metatag =& $node->metatags;
          foreach ($metatags_names as $name) {
            $n_metatag[$target_lang][$name] = $node->metatags[$node->language][$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_lang][$name] = array(
                'value' => $translated_content,
              );
            }
          }
        }
      }
    }
    $success = globallink_entity_save_translated_entity_with_fields($node, $translated_arr, $target_lang);
    if ($success) {

      // todo Pathauto Module Support
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
    }
    else {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
    }
  } 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);
  }
  return;
}