You are here

function globallink_import_node in GlobalLink Connect for Drupal 7.7

Updates node.

Parameters

object $globallink: A GlobalLink object.

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

3 calls to globallink_import_node()
globallink_background_import in ./globallink_background_jobs.inc
Imports the documents
globallink_upload_translation_all_form_submit in ./globallink_workbench_all_active_submissions.inc
globallink_upload_translation_node_form_submit in ./globallink_workbench_all_submissions.inc
Submit handler of XML file upload for direct translation

File

./globallink_node.inc, line 121

Code

function globallink_import_node(&$globallink, $t_arr = NULL) {
  module_load_include('inc', GLOBALLINK_ENTITY_TYPE_NODE, 'node.pages');
  module_load_include('inc', 'globallink', 'globallink');
  $success = FALSE;
  try {
    $default_language = language_default();
    $default_language_code = $default_language->language;
    $target_locale_code = globallink_get_drupal_locale_code($globallink->targetLocale);
    $publish_node = variable_get('globallink_publish_node', 0);
    if ($t_arr != NULL) {
      $translated_arr = $t_arr;
    }
    else {
      $translated_arr = globallink_get_translated_array($globallink->targetXML, $target_locale_code);
    }
    $target_nid = $translated_arr['nid'];
    $target_vid = $translated_arr['vid'];
    $target_title = '';
    if (isset($translated_arr['title'])) {
      $target_title = $translated_arr['title'];
    }
    $globallink->nid = $target_nid;
    $globallink->vid = $target_vid;
    $node = node_load($target_nid, $target_vid);
    if (!$node || is_null($node) || !is_object($node)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      return;
    }

    // Check if a translation set is already present.
    $tnid = $node->nid;
    if (!empty($node->tnid) && $node->nid != $node->tnid) {
      $tnid = $node->tnid;
    }
    $node_arr = translation_node_get_translations($tnid);
    $update_flag = FALSE;
    if (sizeof($node_arr) > 0) {
      foreach ($node_arr as $_tnode) {
        if ($_tnode->language == $target_locale_code) {
          $update_flag = TRUE;
          break;
        }
      }
    }
    if ($update_flag) {
      foreach ($node_arr as $_tnode) {
        if ($node->nid == $_tnode->nid) {
          continue;
        }
        if ($_tnode->language != $target_locale_code) {
          continue;
        }

        // Create a node and save.
        $target_node = node_load($_tnode->nid);
        $new_node = unserialize(serialize($node));
        unset($new_node->nid);
        unset($new_node->vid);
        $new_node->nid = $_tnode->nid;
        $new_node->vid = 0;
        $new_node->tnid = $node->tnid;
        $new_node->language = $_tnode->language;
        $new_node->revision = 1;

        // Fix for menu link issue
        // node_object_prepare($new_node);
        $nodepath = drupal_lookup_path('alias', 'node/' . $node->nid);

        // Get the path object of the tnode, so that we can get the pid.
        $path = path_load('node/' . $_tnode->nid);
        if ($nodepath === FALSE) {

          // Just leave empty for now.
        }
        else {
          if ($path) {
            if (!empty($translated_arr['path'])) {
              $new_node->path = array(
                'pid' => $path['pid'],
                'alias' => $translated_arr['path'],
                'pathauto' => 0,
              );
            }
            else {
              $new_node->path = array(
                'pid' => $path['pid'],
                'alias' => $nodepath,
                'pathauto' => 0,
              );
            }
          }
          else {

            // Update the path object with the alias.
            $new_node->path = array(
              'alias' => $nodepath,
              'pathauto' => 0,
            );
            if (!empty($translated_arr['path'])) {
              $new_node->path = array(
                'alias' => $translated_arr['path'],
                'pathauto' => 0,
              );
            }
            else {
              $new_node->path = array(
                'alias' => $nodepath,
                'pathauto' => 0,
              );
            }
          }
        }
        if ($publish_node == 0 || $publish_node == 1) {
          $new_node->status = $publish_node;
        }
        else {
          $new_node->status = $node->status;
        }
        if (module_exists('revisioning')) {
          $new_node->is_pending = FALSE;
          $new_node->revision_moderation = FALSE;
        }

        // Update title with translated content.
        if ($target_title != '') {
          $new_node->title = $target_title;
        }
        $new_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 =& $new_node->metatags[$new_node->language];
              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[$name] = array(
                    'value' => $translated_content,
                  );
                }
              }
            }
          }
        }
        if (module_exists('field_collection') && isset($translated_arr['field_collection'])) {

          // Unset the field collection as it will be created later.
          $t_fc_arr = $translated_arr['field_collection'];
          $fcs = array_keys($t_fc_arr);
          foreach ($fcs as $fc) {
            if (isset($new_node->{$fc})) {
              unset($new_node->{$fc});
            }
          }
        }

        // Workbench Moderation Support.
        $enabled = globallink_content_type_workbench_enabled($new_node->type);
        if ($enabled) {
          $current_value = variable_get('globallink_moderation_' . $new_node->type, FALSE);
          if ($current_value) {
            $new_node->workbench_moderation['current']->from_state = $current_value;
            $new_node->workbench_moderation['current']->state = $current_value;
            $new_node->workbench_moderation_state_current = $current_value;
            $new_node->workbench_moderation_state_new = $current_value;
            if ($current_value != workbench_moderation_state_published()) {
              $new_node->workbench_moderation['current']->published = 0;
              unset($new_node->workbench_moderation['published']);
            }
          }
        }
        globallink_update_non_translatable_field($target_node, $new_node);
        $success = globallink_save_translated_node_with_fields($node, $new_node, $translated_arr);

        // Exclude Title Module Support.
        if ($success && module_exists('exclude_node_title')) {
          $exclude = _exclude_node_title($node->nid);
          $exclude_list = variable_get('exclude_node_title_nid_list', array());
          $is_excluded = array_search($new_node->nid, $exclude_list);
          if ($exclude && $is_excluded === FALSE) {
            $exclude_list[] = $new_node->nid;
            variable_set('exclude_node_title_nid_list', $exclude_list);
          }
          elseif ($exclude === FALSE && $is_excluded) {
            unset($exclude_list[$is_excluded]);
            variable_set('exclude_node_title_nid_list', $exclude_list);
          }
        }
        if ($success) {
          globallink_update_node_tnid($node->nid, $tnid);
          $globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
        }
        else {
          $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
        }
      }
    }
    else {

      // Create a node and save.
      $new_node = unserialize(serialize($node));
      unset($new_node->nid);
      unset($new_node->vid);
      node_object_prepare($new_node);

      // IF translating from default to any other language e.g. en to fr.
      if ($default_language_code == $node->language) {

        // Set the tnid from the source node.
        $new_node->tnid = $node->nid;
      }
      else {

        // Translating from non default to non default
        // Set the tnid from the default node.
        $new_node->tnid = $tnid;
      }
      $nodepath = drupal_lookup_path('alias', 'node/' . $node->nid);
      if ($nodepath === FALSE) {

        // Just leave empty for now.
      }
      else {
        if (!empty($translated_arr['path'])) {
          $new_node->path = array(
            'alias' => $translated_arr['path'],
          );
        }
        else {
          $new_node->path = array(
            'alias' => $nodepath,
          );
        }
      }
      if ($publish_node == 0 || $publish_node == 1) {
        $new_node->status = $publish_node;
      }
      else {
        $new_node->status = $node->status;
      }
      if (module_exists('revisioning')) {
        $new_node->is_pending = FALSE;
        $new_node->revision_moderation = FALSE;
      }
      $lang = globallink_get_drupal_locale_code($globallink->targetLocale);
      if ($lang == '') {
        throw new Exception('Locale Mapping not found.');
      }
      $new_node->language = $lang;

      // Update title with translated content.
      if ($target_title != '') {
        $new_node->title = $target_title;
      }
      $new_node->tpt_skip = TRUE;
      if (module_exists('metatag')) {
        if (isset($translated_arr['metatag'])) {
          $target_metatag_arr = $translated_arr['metatag'];
          $metatags_names = array_keys($node->metatags[$node->language]);
          $n_metatag =& $new_node->metatags[$new_node->language];
          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[$name] = array(
                'value' => $translated_content,
              );
            }
          }
        }
      }
      if (module_exists('field_collection') && isset($translated_arr['field_collection'])) {

        // Unset the field collection as it will be created later.
        $t_fc_arr = $translated_arr['field_collection'];
        $fcs = array_keys($t_fc_arr);
        foreach ($fcs as $fc) {
          if (isset($new_node->{$fc})) {
            unset($new_node->{$fc});
          }
        }
      }

      // Workbench Moderation Support.
      $enabled = globallink_content_type_workbench_enabled($new_node->type);
      if ($enabled) {
        $current_value = variable_get('globallink_moderation_' . $new_node->type, FALSE);
        if ($current_value) {
          $new_node->workbench_moderation['current']->from_state = $current_value;
          $new_node->workbench_moderation['current']->state = $current_value;
          $new_node->workbench_moderation_state_current = $current_value;
          $new_node->workbench_moderation_state_new = $current_value;
          if ($current_value != workbench_moderation_state_published()) {
            $new_node->workbench_moderation['current']->published = 0;
            unset($new_node->workbench_moderation['published']);
          }
        }
      }

      // Pathauto Module Support.
      if ($nodepath) {
        if (module_exists('pathauto')) {
          $new_node->path['pathauto'] = 0;
        }
      }
      $success = globallink_save_translated_node_with_fields($node, $new_node, $translated_arr);
      if ($success) {

        // Exclude Title Module Support.
        if (module_exists('exclude_node_title')) {
          $exclude = _exclude_node_title($node->nid);
          if ($exclude) {
            $exclude_list = variable_get('exclude_node_title_nid_list', array());
            $exclude_list[] = $new_node->nid;
            variable_set('exclude_node_title_nid_list', $exclude_list);
          }
        }
        globallink_update_node_tnid($node->nid, $tnid);
        $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;
}