You are here

function globallink_get_translated_array in GlobalLink Connect for Drupal 7.7

Same name and namespace in other branches
  1. 7.5 globallink_node.inc \globallink_get_translated_array()
  2. 7.6 globallink_node.inc \globallink_get_translated_array()

Gets translated array from XML input.

Parameters

string $xml: A string of raw XML.

Return value

array The array of translated content.

1 call to globallink_get_translated_array()
globallink_import_node in ./globallink_node.inc
Updates node.

File

./globallink_node.inc, line 517

Code

function globallink_get_translated_array($xml, $target_locale) {
  if (is_null($xml) || !is_string($xml) || $xml == '') {
    return array();
  }
  $dom = new DomDocument();
  $dom->preserveWhiteSpace = FALSE;
  $dom
    ->loadXML($xml);
  $arr = array();
  $contents = $dom
    ->getElementsByTagName('content');
  foreach ($contents as $content) {
    if (is_null($content->attributes)) {
      continue;
    }
    foreach ($content->attributes as $attr_name => $attr_node) {
      switch ($attr_name) {
        case 'rid':
          $arr['rid'] = $attr_node->value;
          break;
        case 'nid':
          $arr['nid'] = $attr_node->value;
          break;
        case 'vid':
          $arr['vid'] = $attr_node->value;
          break;
      }
    }
  }
  $titles = $dom
    ->getElementsByTagName('title');
  foreach ($titles as $title) {
    $arr['title'] = $title->nodeValue;
  }
  $paths = $dom
    ->getElementsByTagName('path');
  foreach ($paths as $path) {
    $arr['path'] = $path->nodeValue;
  }
  $field_image = $dom
    ->getElementsByTagName('field_image');
  foreach ($field_image as $attr) {
    $field_image_obj = new GLFieldImage();
    if (is_null($attr->attributes)) {
      continue;
    }
    foreach ($attr->attributes as $attr_name => $attr_node) {
      switch ($attr_name) {
        case 'type':
          if ($attr_node->value == 'title') {
            $field_image_obj->title = $attr->nodeValue;
          }
          if ($attr_node->value == 'alt') {
            $field_image_obj->alt = $attr->nodeValue;
          }
          continue 2;
        case 'delta':
          $field_image_obj->delta = $attr_node->value;
          continue 2;
        case 'field_name':
          $field_image_obj->field_name = $attr_node->value;
          continue 2;
        case 'subfield':
          $field_image_obj->subfield = $attr_node->value;
          $field_image_obj->{$field_image_obj->subfield} = $attr->nodeValue;
          $field_image_obj->type = 'file_entity';
          continue 2;
        case 'langcode':
        case 'label':
        case 'subfield_label':
          $field_image_obj->{$attr_name} = $attr_node->value;
          continue 2;
      }
    }
    if (is_null($field_image_obj->delta)) {
      $field_image_obj->delta = '0';
    }
    if (isset($field_image_obj->field_name)) {
      if (!isset($arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta])) {
        $arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta] = new GLFieldImage();
      }
      if (isset($field_image_obj->title)) {
        $arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta]->title = $field_image_obj->title;
      }
      if (isset($field_image_obj->alt)) {
        $arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta]->alt = $field_image_obj->alt;
      }
    }
    else {
      $arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta] = $field_image_obj;
    }
  }
  $fields = $dom
    ->getElementsByTagName('field');
  foreach ($fields as $field) {
    $field_obj = new GLField();
    $field_obj->type = 'field';
    $field_obj->translatedContent = $field->nodeValue;
    if (is_null($field->attributes)) {
      continue;
    }
    foreach ($field->attributes as $attr_name => $attr_node) {
      switch ($attr_name) {
        case 'entity_type':
          $field_obj->entityType = $attr_node->value;
          continue 2;
        case 'content_type':
          $field_obj->contentType = $attr_node->value;
          continue 2;
        case 'parent_fc':
          $field_obj->parentFCName = $attr_node->value;
          continue 2;
        case 'bundle':
          $field_obj->bundle = $attr_node->value;
          continue 2;
        case 'entity_id':
          $field_obj->entityId = $attr_node->value;
          continue 2;
        case 'field_name':
          $field_obj->fieldName = $attr_node->value;
          continue 2;
        case 'label':
          $field_obj->fieldLabel = $attr_node->value;
          continue 2;
        case 'delta':
          $field_obj->delta = $attr_node->value;
          continue 2;
        case 'format':
          $field_obj->format = $attr_node->value;
          continue 2;
        case 'langcode':
          $field_obj->langcode = $attr_node->value;
          continue 2;
      }
    }
    if (is_null($field_obj->entityId)) {
      $field_obj->entityId = '0';
    }
    if (is_null($field_obj->bundle)) {
      $field_obj->bundle = $field_obj->fieldName;
    }
    if (is_null($field_obj->langcode)) {
      $field_obj->langcode = LANGUAGE_NONE;
    }
    if (is_null($field_obj->delta)) {
      $field_obj->delta = '0';
    }
    if ($field_obj->entityType == GLOBALLINK_ENTITY_TYPE_NODE) {
      $arr[$field_obj->fieldName][$field_obj->langcode][$field_obj->delta] = $field_obj;
    }
    else {
      $arr['field_collection'][$field_obj->parentFCName][$field_obj->bundle][$field_obj->entityId][$field_obj->fieldName][$field_obj->langcode][$field_obj->delta] = $field_obj;
    }
  }
  $metatags = $dom
    ->getElementsByTagName('metatag');
  foreach ($metatags as $metatag) {
    $metatag_obj = new GLField();
    $metatag_obj->type = 'metatag';
    $metatag_obj->translatedContent = $metatag->nodeValue;
    if (is_null($metatag->attributes)) {
      continue;
    }
    foreach ($metatag->attributes as $attr_name => $attr_node) {
      switch ($attr_name) {
        case 'entity_type':
          $metatag_obj->entityType = $attr_node->value;
          continue 2;
        case 'content_type':
          $field_obj->contentType = $attr_node->value;
          continue 2;
        case 'bundle':
          $field_obj->bundle = $attr_node->value;
          continue 2;
        case 'entity_id':
          $metatag_obj->entityId = $attr_node->value;
          continue 2;
        case 'name':
          $metatag_obj->fieldName = $attr_node->value;
          continue 2;
        case 'label':
          $metatag_obj->fieldLabel = $attr_node->value;
          continue 2;
      }
    }
    if (is_null($metatag_obj->entityId)) {
      $metatag_obj->entityId = '0';
    }
    if (is_null($metatag_obj->bundle)) {
      $metatag_obj->bundle = $metatag_obj->fieldName;
    }
    $arr['metatag'][$metatag_obj->bundle][$metatag_obj->entityId] = $metatag_obj;
  }
  return $arr;
}