You are here

globallink_taxonomy.inc in GlobalLink Connect for Drupal 7.7

File

globallink_taxonomy/globallink_taxonomy.inc
View source
<?php

/**
 * Sends taxonomies for translation.
 *
 * @param array $lids
 *   The array of taxonomy LIDs.
 * @param string $pd4
 *   The project director details.
 * @param string $submission_name
 *   The name of the submission.
 * @param string $due_date
 *   When the translation is due.
 * @param string $project_code
 *   The project's registered code.
 * @param string $source_locale
 *   The locale of the content being translated.
 * @param array $target_locale_arr
 *   Array of desired locales to translate into.
 * @param array $submission_details
 *   Associative array of details about the submission.
 *
 * @return object
 *   GlobalLink object that represents active translation.
 */
function globallink_taxonomy_send_for_translations($lids, $pd4, $submission_name, $due_date, $project_code, $source_locale, $target_locale_arr, $submission_details, $submission_priority) {
  module_load_include('inc', 'globallink', 'gl_ws/gl_ws_send_translations');
  module_load_include('inc', 'globallink', 'globallink');
  $submitter = $submission_details['submitter'];
  $globallink_arr = array();
  $override_localize = variable_get('globallink_override_localize', 0);
  $vocabs = taxonomy_get_vocabularies();
  foreach ($lids as $lid) {
    $rows = globallink_taxonomy_get_sent_rows_by_bid($lid);
    $target_arr = $target_locale_arr;
    $tax_info = globallink_taxonomy_get_info($lid);
    $title = $tax_info->name;
    foreach ($rows as $row) {
      if (array_search($row->target_lang_code, $target_locale_arr)) {
        unset($target_arr[$row->target_lang_code]);
        watchdog(GLOBALLINK_MODULE, 'Skipping Taxonomy Id - %id for locales %locale', array(
          '%id' => $lid,
          '%locale' => $row->target_lang_code,
        ), WATCHDOG_DEBUG);
      }
    }
    if (empty($target_arr)) {
      continue;
    }
    $term = taxonomy_term_load($lid);
    $path = drupal_lookup_path('alias', 'taxonomy/term/' . $lid, globallink_get_drupal_locale_code($source_locale));
    if (empty($term->name)) {
      continue;
    }
    $vocab = '';
    if (isset($vocabs[$term->vid])) {
      $vocab = $vocabs[$term->vid];
    }
    else {
      continue;
    }
    $entity_version = system_get_info('module', 'entity_translation');
    if ($entity_version['version'] == '7.x-1.0-beta5') {
      if ($vocab->i18n_mode == I18N_MODE_LOCALIZE) {
        if ($override_localize == 1) {
          $xml = globallink_get_taxonomy_xml_for_I18N_MODE_ENTITY_TRANSLATE($term, NULL);
        }
        else {
          $xml = globallink_get_taxonomy_xml_for_I18N_MODE_LOCALIZE($term, $path);
        }
      }
      else {
        if ($vocab->i18n_mode == I18N_MODE_TRANSLATE) {
          $xml = globallink_get_taxonomy_xml_for_I18N_MODE_ENTITY_TRANSLATE($term, NULL);
        }
        else {
          continue;
        }
      }
    }
    else {
      if ($vocab->i18n_mode == I18N_MODE_LOCALIZE) {
        $xml = globallink_get_taxonomy_xml_for_I18N_MODE_LOCALIZE($term, $path);
      }
      else {
        if ($vocab->i18n_mode == I18N_MODE_ENTITY_TRANSLATION) {
          $xml = globallink_get_taxonomy_xml_for_I18N_MODE_ENTITY_TRANSLATE($term, NULL);
        }
        else {
          if ($vocab->i18n_mode == I18N_MODE_TRANSLATE) {
            $xml = globallink_get_taxonomy_xml_for_I18N_MODE_ENTITY_TRANSLATE($term, NULL);
          }
          else {
            continue;
          }
        }
      }
    }
    $name = 'Taxonomy_' . $lid . '.xml';
    $globallink = new GlobalLink();
    $globallink->type = GLOBALLINK_ENTITY_TYPE_TAXONOMY;
    $globallink->metadata = GLOBALLINK_ENTITY_TYPE_TAXONOMY;
    $globallink->title = $title;
    $globallink->sourceLocale = $source_locale;
    $globallink->targetLocale = $target_arr;
    $globallink->sourceXML = $xml;
    $globallink->sourceFileName = $name;
    $globallink->submissionName = $submission_name;
    $globallink->submissionPriority = $submission_priority;
    $globallink->dueDate = $due_date;
    $globallink->otherObjectId = $lid;
    $globallink->submissionInstructions = $submission_details['instructions'] . "\nSubmitter: " . $submitter;
    $globallink_arr[GLOBALLINK_ENTITY_TYPE_TAXONOMY][] = $globallink;
  }
  return $globallink_arr;
}
function globallink_get_taxonomy_xml_for_I18N_MODE_ENTITY_TRANSLATE($term, $path) {
  $dom = new DOMDocument('1.0', 'UTF-8');
  $dom->formatOutput = TRUE;
  $root = $dom
    ->createElement("content");
  $dom
    ->appendChild($root);
  $id = $dom
    ->createAttribute('bid');
  $id->value = $term->tid;
  $root
    ->appendChild($id);
  if (!empty($path)) {
    globallink_insert_child_element($dom, $root, "taxonomy", $path, array(
      'name' => 'path',
      'label' => 'Path',
    ));
  }
  $language = $term->language;
  $fields = get_taxonomy_fields($term->vocabulary_machine_name);
  $configured_fields = get_configured_fields_for_taxonomy($term->vocabulary_machine_name);
  foreach ($fields as $field) {
    if (array_key_exists($field, $configured_fields) && $configured_fields[$field]->translatable == 1) {
      if (!empty($term->{$field})) {
        $field_values = $term->{$field};
        if (is_array($field_values) && !empty($field_values[$language])) {
          foreach ($field_values[$language] as $delta => $values) {
            globallink_insert_child_element($dom, $root, 'field', $values['value'], array(
              'field' => $field,
              'delta' => $delta,
              'langcode' => $language,
              'label' => $configured_fields[$field]->field_label,
            ));
          }
        }
        elseif (!empty($field_values) && is_string($field_values)) {

          // Test this without field translation
          globallink_insert_child_element($dom, $root, 'field', $field_values, array(
            'field' => $field,
            'label' => $configured_fields[$field]->field_label,
          ));
        }
      }
    }
  }
  if (array_key_exists('metatags', $configured_fields)) {
    if (isset($term->metatags) && isset($term->metatags[$language])) {
      $mt = $term->metatags[$language];
      if (isset($mt['title']) && isset($mt['title']['value'])) {
        globallink_insert_child_element($dom, $root, 'metatag', $mt['title']['value'], array(
          'field' => 'title',
          'delta' => '0',
          'langcode' => $language,
          'label' => 'MetaTag Title',
        ));
      }
      if (isset($mt['description']) && isset($mt['description']['value'])) {
        globallink_insert_child_element($dom, $root, 'metatag', $mt['description']['value'], array(
          'field' => 'description',
          'delta' => '0',
          'langcode' => $language,
          'label' => 'MetaTag Description',
        ));
      }
      if (isset($mt['abstract']) && isset($mt['abstract']['value'])) {
        globallink_insert_child_element($dom, $root, 'metatag', $mt['abstract']['value'], array(
          'field' => 'abstract',
          'delta' => '0',
          'langcode' => $language,
          'label' => 'MetaTag Abstract',
        ));
      }
      if (isset($mt['keywords']) && isset($mt['keywords']['value'])) {
        globallink_insert_child_element($dom, $root, 'metatag', $mt['keywords']['value'], array(
          'field' => 'keywords',
          'delta' => '0',
          'langcode' => $language,
          'label' => 'MetaTag Keywords',
        ));
      }
    }
  }
  $xml = $dom
    ->saveXML();
  return $xml;
}

/**
 * Gets XML data from specific taxonomy.
 *
 * @param object $term
 *   The taxonomy term.
 * @param string $path
 *   The taxonomy path.
 *
 * @return array
 *   Associative array of block XML data.
 */
function globallink_get_taxonomy_xml_for_I18N_MODE_LOCALIZE($term, $path) {
  $dom = new DOMDocument('1.0', 'UTF-8');
  $dom->formatOutput = TRUE;
  $root = $dom
    ->createElement("content");
  $dom
    ->appendChild($root);
  $id = $dom
    ->createAttribute('bid');
  $id->value = $term->tid;
  $root
    ->appendChild($id);
  if (!empty($term->name)) {
    globallink_insert_child_element($dom, $root, "taxonomy", $term->name, array(
      'name' => 'name',
      'label' => 'Name',
    ));
  }
  if (!empty($term->description)) {
    globallink_insert_child_element($dom, $root, "taxonomy", $term->description, array(
      'name' => 'description',
      'label' => 'Description',
    ));
  }
  if (!empty($path)) {
    globallink_insert_child_element($dom, $root, "taxonomy", $path, array(
      'name' => 'path',
      'label' => 'Path',
    ));
  }
  $xml = $dom
    ->saveXML();
  return $xml;
}

/**
 * Gets taxonomy strings.
 *
 * @param string $language
 *   The taxonomy language.  Defaults to NULL.
 * @param string $group
 *   The node group.  Defaults to 'taxonomy.'
 *
 * @return array
 *   Array of taxonomy strings
 */
function globallink_taxonomy_get_strings($language = NULL, $group = GLOBALLINK_ENTITY_TYPE_TAXONOMY) {
  if (isset($language)) {
    $result = db_query('SELECT s.lid, s.source, s.context, s.location, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.textgroup = :textgroup ORDER BY t.plid, t.plural', array(
      ':language' => $language,
      ':textgroup' => $group,
    ));
  }
  else {
    $result = db_query('SELECT s.lid, s.source, s.context, s.location, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.textgroup = :textgroup ORDER BY t.plid, t.plural', array(
      ':textgroup' => $group,
    ));
  }
  $strings = array();
  foreach ($result as $child) {
    $string = array(
      'lid' => $child->lid,
      'location' => $child->location,
      'source' => $child->source,
      'context' => $child->context,
    );
    $strings[$child->location] = $string;
  }
  return $strings;
}

/**
 * Gets taxonomy translation status.
 *
 * @param string $bid
 *   The taxonomy object ID.
 * @param string $tgt_locale
 *   The target locale.
 * @param string $title
 *   The translation title.
 *
 * @return string
 *   Status message.
 */
function globallink_taxonomy_check_delete($bid) {
  $s_taxonomy = taxonomy_term_load($bid);
  if (!$s_taxonomy || is_null($s_taxonomy)) {
    return TRUE;
  }
  return FALSE;
}
function globallink_taxonomy_import(&$globallink) {
  module_load_include('inc', 'globallink', 'globallink');
  $vocabs = taxonomy_get_vocabularies();
  $override_localize = variable_get('globallink_override_localize', 0);
  $entity_version = system_get_info('module', 'entity_translation');
  $target_xml = $globallink->targetXML;
  if (!isset($target_xml)) {
    $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
    return;
  }
  $target_locale = globallink_get_drupal_locale_code($globallink->targetLocale);
  $translated_arr = globallink_taxonomy_get_translated_items($target_xml);
  try {
    $bid = $translated_arr['bid'];
    unset($translated_arr['bid']);
    $term = taxonomy_term_load($bid);
    if (empty($term)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      watchdog(GLOBALLINK_MODULE, 'Source Term not found for %id', array(
        '%id' => $bid,
      ), WATCHDOG_ERROR);
      return;
    }
    if (isset($vocabs[$term->vid])) {
      $vocab = $vocabs[$term->vid];
    }
    else {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      watchdog(GLOBALLINK_MODULE, 'Source Vocabulary not found for %id', array(
        '%id' => $term->vid,
      ), WATCHDOG_ERROR);
      return;
    }
    if ($vocab->i18n_mode == I18N_MODE_LOCALIZE) {
      if ($override_localize == 1 && $entity_version['version'] == '7.x-1.0-beta5') {

        // Use i18n fields translation
        $source_locale = $term->language;
        $fields = get_configured_fields_for_taxonomy($vocab->machine_name);
        $field_names = array_keys($fields);
        foreach ($field_names as $fieldName) {
          if ($fieldName == 'metatags') {
            if (empty($translated_arr[$fieldName])) {
              if (isset($term->{$fieldName}) && isset($term->{$fieldName}[$source_locale])) {
                $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$source_locale];
              }
              continue;
            }
            foreach ($translated_arr['metatags'] as $metatag) {
              if (key($metatag) !== LANGUAGE_NONE) {
                $t_field_lang = key($metatag);
              }
              if (empty($term->{$fieldName}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
                $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$t_field_lang];
              }
              $t_field_arr = $metatag[$source_locale];
              $mt_name = $t_field_arr->fieldName;
              if (empty($t_field_arr->translatedContent)) {
                continue;
              }
              if (empty($term->{$fieldName}[$target_locale][$mt_name])) {
                continue;
              }
              $translation = $t_field_arr->translatedContent;
              $term->{$fieldName}[$target_locale][$mt_name]['value'] = $translation;
            }
            continue;
          }
          $t_field_lang = LANGUAGE_NONE;
          if (empty($translated_arr[$fieldName])) {
            if (isset($term->{$fieldName}) && isset($term->{$fieldName}[$source_locale])) {
              $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$source_locale];
            }
            continue;
          }
          if (key($translated_arr[$fieldName]) !== LANGUAGE_NONE) {
            $t_field_lang = key($translated_arr[$fieldName]);
          }
          if (empty($term->{$fieldName}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
            $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$t_field_lang];
          }
          $t_field_arr = $translated_arr[$fieldName][$source_locale];
          foreach ($term->{$fieldName}[$target_locale] as $delta => $fp_field) {
            if (empty($t_field_arr[$delta])) {
              continue;
            }
            $translation = $t_field_arr[$delta]->translatedContent;
            $term->{$fieldName}[$target_locale][$delta]['value'] = $translation;
          }
        }
        if (!isset($term->translations->data[$target_locale])) {
          $term->translations->data[$target_locale] = array(
            'language' => $target_locale,
            // Target language
            'source' => $term->language,
            // Source language
            'uid' => $term->translations->data[$term->language]['uid'],
            'status' => variable_get('globallink_publish_node', 0),
            // publish translation
            'created' => $term->translations->data[$term->language]['created'],
          );
          $term->translations->hook[$target_locale] = array(
            'hook' => 'insert',
            'date' => NULL,
          );
        }
        else {
          $term->translations->data[$target_locale]['status'] = variable_get('globallink_publish_node', 0);

          // publish translation
          $term->translations->hook[$target_locale] = array(
            'hook' => 'update',
            'date' => NULL,
          );
        }
      }
      else {
        unset($term->tid);
        $term->language = $target_locale;
        $new_term = taxonomy_term_save($term);
        if ($new_term != 1) {
          $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
          throw new Exception('Unable to save new term for ' . $bid . ' where language is ' . $target_locale);
        }

        // Use strings for name and description
        foreach ($translated_arr as $attribute => $translations) {
          if ($attribute == 'name') {
            $term->name = $translations['translation'];
          }
          elseif ($attribute == 'description') {
            $term->description = $translations['translation'];
          }
          elseif ($attribute == 'path') {
            $term->path['pathauto'] = FALSE;
            $uri = taxonomy_term_uri($term);
            $new_path = path_load($uri['path']);
            $new_path['alias'] = $translations['translation'];
            path_save($new_path);
          }
        }
      }
    }
    else {
      if ($entity_version['version'] != '7.x-1.0-beta5' && $vocab->i18n_mode == I18N_MODE_ENTITY_TRANSLATION || $entity_version['version'] == '7.x-1.0-beta5' && $vocab->i18n_mode == I18N_MODE_TRANSLATE) {

        // Use i18n fields translation
        $source_locale = $term->language;
        $fields = get_configured_fields_for_taxonomy($vocab->machine_name);
        $field_names = array_keys($fields);
        foreach ($field_names as $fieldName) {
          if ($fieldName == 'metatags') {
            if (empty($translated_arr[$fieldName])) {
              if (isset($term->{$fieldName}) && isset($term->{$fieldName}[$source_locale])) {
                $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$source_locale];
              }
              continue;
            }
            foreach ($translated_arr['metatags'] as $metatag) {
              if (key($metatag) !== LANGUAGE_NONE) {
                $t_field_lang = key($metatag);
              }
              if (empty($term->{$fieldName}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
                $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$t_field_lang];
              }
              $t_field_arr = $metatag[$source_locale];
              $mt_name = $t_field_arr->fieldName;
              if (empty($t_field_arr->translatedContent)) {
                continue;
              }
              if (empty($term->{$fieldName}[$target_locale][$mt_name])) {
                continue;
              }
              $translation = $t_field_arr->translatedContent;
              $term->{$fieldName}[$target_locale][$mt_name]['value'] = $translation;
            }
            continue;
          }
          $t_field_lang = LANGUAGE_NONE;
          if (empty($translated_arr[$fieldName])) {
            if (isset($term->{$fieldName}) && isset($term->{$fieldName}[$source_locale])) {
              $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$source_locale];
            }
            continue;
          }
          if (key($translated_arr[$fieldName]) !== LANGUAGE_NONE) {
            $t_field_lang = key($translated_arr[$fieldName]);
          }
          if (empty($term->{$fieldName}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
            $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$t_field_lang];
          }
          $t_field_arr = $translated_arr[$fieldName][$source_locale];
          foreach ($term->{$fieldName}[$target_locale] as $delta => $fp_field) {
            if (empty($t_field_arr[$delta])) {
              continue;
            }
            $translation = $t_field_arr[$delta]->translatedContent;
            $term->{$fieldName}[$target_locale][$delta]['value'] = $translation;
          }
        }
        if (!isset($term->translations->data[$target_locale])) {
          $term->translations->data[$target_locale] = array(
            'language' => $target_locale,
            // Target language
            'source' => $term->language,
            // Source language
            'uid' => $term->translations->data[$term->language]['uid'],
            'status' => variable_get('transperfect_publish_node', 0),
            // publish translation
            'created' => $term->translations->data[$term->language]['created'],
          );
          $term->translations->hook[$target_locale] = array(
            'hook' => 'insert',
            'date' => NULL,
          );
        }
        else {
          $term->translations->data[$target_locale]['status'] = variable_get('transperfect_publish_node', 0);

          // publish translation
          $term->translations->hook[$target_locale] = array(
            'hook' => 'update',
            'date' => NULL,
          );
        }
      }
      else {
        if ($vocab->i18n_mode == I18N_MODE_TRANSLATE && $entity_version['version'] != '7.x-1.0-beta5') {
          $vocabulary = $term->vocabulary_machine_name;
          $translation_set = !empty($term->i18n_tsid) ? i18n_translation_set_load($term->i18n_tsid) : NULL;
          if ($translation_set == NULL) {
            $translation_set = i18n_translation_set_create('taxonomy_term', $vocabulary)
              ->add_item($term);
          }
          $tsid = $translation_set->tsid;
          $source_locale = $term->language;
          $term->i18n_tsid = $tsid;
          taxonomy_term_save($term);
          $set = 0;

          //Added the condition here for loading existing term, so that it gets updated and not recreated.
          $trans_set = $translation_set
            ->load_translations();
          $keys = array_keys($trans_set);
          foreach ($keys as $key) {
            if ($key == $target_locale) {
              $term = taxonomy_term_load($trans_set[$key]->tid);
              $set = 1;
            }
          }
          if ($set != 1) {
            unset($term->tid);
            $term->language = $target_locale;
            $term->i18n_tsid = $tsid;
            $new_term = taxonomy_term_save($term);
            if ($new_term != 1) {
              $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
              throw new Exception('Unable to save new term for ' . $bid . ' where language is ' . $target_locale);
            }
          }

          // Use i18n fields translation
          $fields = get_configured_fields_for_taxonomy($vocab->machine_name);
          $field_names = array_keys($fields);
          $term->i18n_tsid = $tsid;
          foreach ($field_names as $fieldName) {
            if ($fieldName == 'metatags') {
              if (empty($translated_arr[$fieldName])) {
                if (isset($term->{$fieldName}) && isset($term->{$fieldName}[$source_locale])) {
                  $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$source_locale];
                }
                continue;
              }
              foreach ($translated_arr['metatags'] as $metatag) {
                if (key($metatag) !== LANGUAGE_NONE) {
                  $t_field_lang = key($metatag);
                }
                if (empty($term->{$fieldName}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
                  $term->{$fieldName}[$target_locale] = $term->{$fieldName}[$t_field_lang];
                }
                $t_field_arr = $metatag[$source_locale];
                $mt_name = $t_field_arr->fieldName;
                if (empty($t_field_arr->translatedContent)) {
                  continue;
                }
                if (empty($term->{$fieldName}[$target_locale][$mt_name])) {
                  continue;
                }
                $translation = $t_field_arr->translatedContent;
                $term->{$fieldName}[$target_locale][$mt_name]['value'] = $translation;
              }
              continue;
            }
            $t_field_lang = LANGUAGE_NONE;
            $t_field_arr = $translated_arr[$fieldName][$source_locale];
            foreach ($term->{$fieldName}[$source_locale] as $delta => $fp_field) {
              if (empty($t_field_arr[$delta])) {
                continue;
              }
              $translation = $t_field_arr[$delta]->translatedContent;
              $term->{$fieldName}[$source_locale][$delta]['value'] = $translation;
              $term->{$fieldName}[$target_locale][$delta]['value'] = $translation;
            }
          }
        }
        else {
          return;
        }
      }
    }
    taxonomy_term_save($term);
    $globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
  } 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;
}

/**
 * Gets translated taxonomies from XML data.
 *
 * @param object $xml
 *   XML representation of taxonomies.
 *
 * @return array
 *   Array of taxonomies.
 */
function globallink_taxonomy_get_translated_items($xml) {
  if (is_null($xml) || !is_string($xml) || $xml == '') {
    return array();
  }
  $dom = new DomDocument();
  $dom->preserveWhiteSpace = FALSE;
  $dom
    ->loadXML($xml);
  $contents = $dom
    ->getElementsByTagName('content');
  $bid = '';
  foreach ($contents as $content) {
    if (!is_null($content->attributes)) {
      foreach ($content->attributes as $attr_name => $attr_node) {
        if ($attr_name == 'bid') {
          $bid = $attr_node->value;
        }
      }
    }
  }
  if ($bid == '') {
    return array();
  }
  $taxonomy_arr = array();
  $taxonomy_arr['bid'] = $bid;
  $taxonomies = $dom
    ->getElementsByTagName(GLOBALLINK_ENTITY_TYPE_TAXONOMY);
  foreach ($taxonomies as $taxonomy) {
    if (!is_null($taxonomy->attributes)) {
      $b_arr = array();
      foreach ($taxonomy->attributes as $attr_name => $attr_node) {
        $b_arr[$attr_name] = $attr_node->value;
      }
      $b_arr['translation'] = $taxonomy->nodeValue;
      $taxonomy_arr[$b_arr['name']] = $b_arr;
    }
  }
  $fields = $dom
    ->getElementsByTagName('field');
  foreach ($fields as $field) {
    $field_object = new GLField();
    $field_object->type = 'field';
    $field_object->translatedContent = $field->nodeValue;
    if (!is_null($field->attributes)) {
      foreach ($field->attributes as $attr_name => $attr_node) {
        switch ($attr_name) {
          case 'field':
            $field_object->fieldName = $attr_node->value;
            continue 2;
          case 'delta':
            $field_object->delta = $attr_node->value;
            continue 2;
          case 'langcode':
            $field_object->langcode = $attr_node->value;
            continue 2;
        }
      }
      if (is_null($field_object->langcode)) {
        $field_object->langcode = LANGUAGE_NONE;
      }
      if (is_null($field_object->delta)) {
        $field_object->delta = '0';
      }
      $taxonomy_arr[$field_object->fieldName][$field_object->langcode][$field_object->delta] = $field_object;
    }
  }
  $metatags = $dom
    ->getElementsByTagName('metatag');
  foreach ($metatags as $metatag) {
    $metatag_object = new GLField();
    $metatag_object->type = 'metatag';
    $metatag_object->translatedContent = $metatag->nodeValue;
    if (!is_null($metatag->attributes)) {
      foreach ($metatag->attributes as $attr_name => $attr_node) {
        switch ($attr_name) {
          case 'field':
            $metatag_object->fieldName = $attr_node->value;
            continue 2;
          case 'delta':
            $metatag_object->delta = $attr_node->value;
            continue 2;
          case 'langcode':
            $metatag_object->langcode = $attr_node->value;
            continue 2;
          case 'label':
            $metatag_object->fieldLabel = $attr_node->value;
            continue 2;
        }
      }
      if (is_null($metatag_object->delta)) {
        $metatag_object->entityId = '0';
      }
      if (is_null($metatag_object->langcode)) {
        $metatag_object->langcode = LANGUAGE_NONE;
      }
    }
    $taxonomy_arr['metatags'][$metatag_object->fieldName][$metatag_object->langcode] = $metatag_object;
  }
  return $taxonomy_arr;
}

/**
 * Gets active taxonomy submission rows by object ID.
 *
 * @param string $bid
 *   The taxonomy object ID.
 *
 * @return
 *   Associative array of taxonomy active submission rows.  FALSE if the array is empty.
 */
function globallink_taxonomy_get_sent_rows_by_bid($tid) {
  $result = db_select('globallink_document', 'gd')
    ->fields('gd')
    ->condition('entity_type', GLOBALLINK_ENTITY_TYPE_TAXONOMY, '=')
    ->condition('object_id', $tid, '=')
    ->condition('target_status', array(
    GLOBALLINK_STATUS_TRANSLATION_SENT,
    GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
    GLOBALLINK_STATUS_TRANSLATION_ERROR,
  ), 'IN')
    ->execute();
  $rows = array();
  foreach ($result as $row) {
    $rows[] = $row;
  }
  return $rows;
}

/**
 * Gets taxonomy vocabulary list.
 *
 * @return array
 *   Array of taxonomy vocabulary.
 */
function globallink_taxonomy_get_vocabulary_list() {
  $v_arr = array(
    '[any]' => 'Any',
  );
  $arr = array();
  $vocabs = taxonomy_vocabulary_load_multiple(FALSE);
  foreach ($vocabs as $vocab) {
    if ($vocab->i18n_mode == I18N_MODE_LOCALIZE || $vocab->i18n_mode == I18N_MODE_TRANSLATE || $vocab->i18n_mode == I18N_MODE_ENTITY_TRANSLATION) {
      $arr[$vocab->vid] = $vocab->name;
    }
  }
  asort($arr);
  return $v_arr + $arr;
}
function get_configured_fields_for_taxonomy($taxonomy_type) {
  $result = db_select('globallink_field_config', 'tfg')
    ->fields('tfg')
    ->condition('content_type', 'tax:' . $taxonomy_type, '=')
    ->condition('entity_type', 'taxonomy_term', '=')
    ->execute();
  $arr = array();
  foreach ($result as $row) {
    $arr[$row->field_name] = $row;
  }
  return $arr;
}
function get_taxonomy_fields($taxonomy_type) {
  $fields = array();
  $info = entity_get_info('taxonomy_term');
  foreach ($info['bundles'] as $bundle_name => $bundle_info) {
    if ($bundle_name == $taxonomy_type) {
      foreach (field_info_instances('taxonomy_term', $bundle_name) as $field) {
        $fields[] = $field['field_name'];
      }
    }
  }
  return $fields;
}
function globallink_get_all_translatable_taxonomy_types_and_names() {
  $override_localize = variable_get('globallink_override_localize', 0);
  $entity_version = system_get_info('module', 'entity_translation');
  $entity_info = entity_get_info('taxonomy_term');
  $arr = array();
  if (isset($entity_info) && isset($entity_info['bundles'])) {
    $bundles = $entity_info['bundles'];
    foreach ($bundles as $key => $bundle) {
      $vocab = taxonomy_vocabulary_machine_name_load($key);
      if ($entity_version['version'] == '7.x-1.0-beta5') {
        if ($vocab->i18n_mode == I18N_MODE_TRANSLATE || $vocab->i18n_mode == I18N_MODE_LOCALIZE && $override_localize == 1) {
          $arr['tax:' . $key] = $bundle['label'];
        }
      }
      else {
        if ($vocab->i18n_mode == I18N_MODE_TRANSLATE || $vocab->i18n_mode == I18N_MODE_ENTITY_TRANSLATION) {
          $arr['tax:' . $key] = $bundle['label'];
        }
      }
    }
    asort($arr);
  }
  return $arr;
}
function globallink_taxonomy_get_info($tid) {
  $query = db_select('taxonomy_term_data', 'ttd')
    ->fields('ttd', array(
    'name',
  ))
    ->distinct()
    ->condition('tid', $tid, '=');
  $results = $query
    ->execute();
  $arr = new stdClass();
  foreach ($results as $result) {
    $arr = $result;
  }
  return $arr;
}
function globallink_taxonomy_get_active_submission_by_id($id) {
  $query = db_select('globallink_document', 'gd');
  $query
    ->join('globallink_submission', 'gs', 'gd.submission_rid = gs.rid');
  $query
    ->condition('gd.object_id', $id, '=');
  $query
    ->condition('gd.entity_type', GLOBALLINK_ENTITY_TYPE_TAXONOMY, '=');
  $or = db_or();
  $or
    ->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_SENT, '=');
  $or
    ->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_COMPLETED, '=');
  $or
    ->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_ERROR, '=');
  $query
    ->condition($or);
  $query
    ->fields('gd');
  $query
    ->fields('gs', array(
    'submission',
    'source_lang_code',
    'source_lang_name',
    'sub_target_lang_code',
    'sub_target_lang_name',
  ));
  $results = $query
    ->execute()
    ->fetchAll();
  return $results;
}