You are here

function lingotek_process_entity_xml in Lingotek Translation 7.4

Same name and namespace in other branches
  1. 7.7 lingotek.remote.inc \lingotek_process_entity_xml()
  2. 7.5 lingotek.remote.inc \lingotek_process_entity_xml()
  3. 7.6 lingotek.remote.inc \lingotek_process_entity_xml()
1 call to lingotek_process_entity_xml()
lingotek_download_document in ./lingotek.api.inc

File

./lingotek.api.inc, line 151

Code

function lingotek_process_entity_xml($xml, $node, $entity_type, $drupal_language_code, $url_alias_translation = 0) {
  $delta = 0;
  $last_tag = NULL;
  foreach ($xml as $tag => $content) {
    if ($tag == $last_tag) {
      $delta++;
    }
    else {
      $delta = 0;
    }
    if ($tag == 'url_alias' && $url_alias_translation == 1) {
      $target = check_plain($content);

      //URL Alias related to the page:
      $conditions = array(
        'source' => 'node/' . $node->nid,
      );
      if ($node->language != LANGUAGE_NONE) {
        $conditions['language'] = $node->language;
      }
      $path = path_load($conditions);
      if ($path !== FALSE) {
        $conditions['language'] = $drupal_language_code;
        if ($path['alias'] != $target) {
          $original = path_load($conditions);
          $conditions['alias'] = $target;
          if ($original === FALSE) {
            path_save($conditions);
          }
          else {
            path_delete($original);
            path_save($conditions);
          }
        }
      }
    }
    else {

      //this part of the xml is a field
      $drupal_field_name = $tag;
      $target_key = 'value';
      $subfield_parts = explode('__', $tag);
      if (count($subfield_parts) == 2) {
        $drupal_field_name = $subfield_parts[0];
        $target_key = $subfield_parts[1];
      }
      $field = field_info_field($drupal_field_name);
      if (isset($field)) {
        $node_field =& $node->{$drupal_field_name};
        $index = 0;
        if (module_exists('link') && $field['type'] == 'link_field') {
          $target_key = array(
            'url',
            'title',
          );
        }
        if (!is_array($target_key)) {
          $target_key = array(
            $target_key,
          );
        }
        $insert_array = array(
          'entity_type' => $entity_type,
          'bundle' => $node->type,
          'entity_id' => $node->nid,
          'revision_id' => $node->vid,
          'language' => $drupal_language_code,
          'delta' => $delta,
          'deleted' => '0',
        );
        $field_names = array(
          'field_revision_' . $field['field_name'],
          'field_data_' . $field['field_name'],
        );
        if (module_exists('field_collection') && $field['type'] == 'field_collection') {
          $default = language_default();
          $default_language = $default->language;
          $field_collection_id = isset($node_field['und'][$delta]['value']) ? $node_field['und'][$delta]['value'] : $node_field[$default_language][$delta]['value'];
          $field_collection_item = lingotek_entity_load_single('field_collection_item', $field_collection_id);
          $field_collection_item->type = $field['field_name'];
          $field_collection_item->language = $node->language;
          $field_collection_item->nid = $field_collection_item->item_id;
          $field_collection_item->vid = $field_collection_item->revision_id;
          lingotek_process_entity_xml($content, $field_collection_item, 'field_collection_item', $drupal_language_code);
          $db_field_name = $field['field_name'] . '_value';
          $db_revision_field_name = $field['field_name'] . '_revision_id';
          $insert_array['language'] = $drupal_language_code;
          $lang = isset($node_field[$node->language][$delta]['value']) ? $node->language : 'und';
          $insert_array[$db_field_name] = $node_field[$lang][$delta]['value'];
          $insert_array[$db_revision_field_name] = $node_field[$lang][$delta]['revision_id'];
        }
        else {
          foreach ($content as $text) {
            if ($field['translatable'] != 1 && $entity_type == 'field_collection_item') {
              $field['translatable'] = 1;
              field_update_field($field);
            }
            $array_key = $target_key[$index];
            $db_field_name = $field['field_name'] . '_' . $array_key;
            $insert_array[$db_field_name] = lingotek_unfilter_placeholders(decode_entities($text));

            //$node_field[$drupal_language_code][$delta][$array_key] = decode_entities($text);
            if (isset($node->language) && array_key_exists($node->language, $node_field)) {
              if (array_key_exists('format', $node_field[$node->language][0])) {

                //$node_field[$drupal_language_code][$index]['format'] = $node_field[$node->language][0]['format'];
                $format_db_field_name = $field['field_name'] . '_format';
                $insert_array[$format_db_field_name] = $node_field[$node->language][0]['format'];
              }
            }
            $index++;
          }

          // put fields into both field_data_ and field_revision_ tables
          // if published version (in node table) is as current as most current revision (in node_revision table)
          if (isset($node->workbench_moderation) && isset($node->workbench_moderation['published']) && $node->workbench_moderation['published']->vid != $node->workbench_moderation['current']->vid) {
            $only_write_revisions = 1;
            unset($field_names[$only_write_revisions]);
          }
        }
        foreach ($field_names as $field_name) {

          // using drupal_write_record to avoid node_save - node_save overwrites publications unless called on both revised and published versions of the node (i.e. workbench_moderation)
          try {
            drupal_write_record($field_name, $insert_array);
          } catch (PDOException $e) {
            $primary_keys = array(
              'entity_type',
              'entity_id',
              'revision_id',
              'deleted',
              'delta',
              'language',
            );
            drupal_write_record($field_name, $insert_array, $primary_keys);
          }
        }

        /* removed conversion of field collection leaves from und to the default language
            * in favor of using load and presave hooks to sync und and default-language translations
           if ($entity_type == 'field_collection_item') {
             $field_names = array('field_revision_' . $field['field_name'], 'field_data_' . $field['field_name']);
             foreach ($field_names as $table) {
               db_update($table)
                   ->fields(array(
                     'language' => $node->language,
                   ))
                   ->condition('entity_id', $node->nid)
                   ->condition('language', 'und')
                   ->execute();
             }
           }
            *
            */
      }
      cache_clear_all('field:' . $entity_type . ':' . $node->nid, 'cache_field');

      //Set URL alias
      if ($tag == 'title_field' && $url_alias_translation == 2 && module_exists('pathauto') && $node->language != LANGUAGE_NONE) {
        module_load_include('inc', 'pathauto');
        $uri = entity_uri('node', $node);
        pathauto_create_alias('node', 'update', $uri['path'], array(
          'node' => clone $node,
        ), $node->type, $drupal_language_code);
      }
    }
    $last_tag = $tag;
  }
}