You are here

globallink_interface.inc in GlobalLink Connect for Drupal 7.7

File

globallink_interface/globallink_interface.inc
View source
<?php

/**
 * Sends interfaces for translation.
 *
 * @param array $lids
 *   The array of interface 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_interface_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();
  foreach ($lids as $lid) {
    $rows = globallink_interface_get_sent_rows_by_lid($lid);
    $target_arr = $target_locale_arr;
    $interface_info = globallink_load_source_data($lid);
    $title = $interface_info[0]->source;
    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 Interface Id - %id for locales %locale', array(
          '%id' => $lid,
          '%locale' => $row->target_lang_code,
        ), WATCHDOG_DEBUG);
      }
    }
    if (empty($target_arr)) {
      continue;
    }
    $source_arr = globallink_get_source($lid);
    $xml = globallink_interface_get_xml($lid, $source_arr[$lid]);
    $name = 'Interface_' . $lid . '.xml';
    $globallink = new GlobalLink();
    $globallink->type = GLOBALLINK_ENTITY_TYPE_INTERFACE;
    $globallink->metadata = GLOBALLINK_ENTITY_TYPE_INTERFACE;
    $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_INTERFACE][] = $globallink;
  }
  return $globallink_arr;
}

/**
 * Gets XML data from specific interface.
 *
 * @param string $lid
 *   The interface LID.
 * @param array $source_arr
 *   The source array.
 *
 * @return array
 *   Associative array of block XML data.
 */
function globallink_interface_get_xml($lid, $source_arr) {
  $dom = new DOMDocument('1.0', 'UTF-8');
  $dom->formatOutput = TRUE;
  $root = $dom
    ->createElement('content');
  $dom
    ->appendChild($root);
  $id = $dom
    ->createAttribute('lid');
  $id->value = $lid;
  $root
    ->appendChild($id);
  globallink_insert_child_element($dom, $root, GLOBALLINK_ENTITY_TYPE_INTERFACE, $source_arr['source'], array(
    'name' => 'source',
    'lid' => $lid,
    'location' => $source_arr['location'],
  ));
  $xml = $dom
    ->saveXML();
  return $xml;
}

/**
 * Gets interface filters.
 *
 * @return array
 *   Associative array of interface filters.
 */
function globallink_interface_get_translate_filter_query() {
  $filter = array();
  if (isset($_SESSION['globallink_interface_filter'])) {
    foreach ($_SESSION['globallink_interface_filter'] as $key => $value) {
      if ($key == 'string' && $value != '') {
        $filter[$key] = $value;
      }
      else {
        if ($key == 'group' || $key == 'language' || $key == 'translation') {
          if ($value != '' && $value != 'all') {
            $filter[$key] = $value;
          }
        }
      }
    }
  }
  return $filter;
}

/**
 * Gets sent interface rows by LID.
 *
 * @param string $lid
 *   The interface LID.
 *
 * @return array
 *   Array of interface rows.
 */
function globallink_interface_get_sent_rows_by_lid($lid) {
  $result = db_select('globallink_document', 'gd')
    ->fields('gd')
    ->condition('entity_type', GLOBALLINK_ENTITY_TYPE_INTERFACE, '=')
    ->condition('object_id', $lid, '=')
    ->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 interface translation status.
 *
 * @param string $lid
 *   The interface LID.
 * @param string $tgt_locale
 *   The target locale.
 * @param string $title
 *   The translation title.
 *
 * @return string
 *   Status message.
 */
function globallink_interface_check_delete($lid) {
  $interface = globallink_load_source_data($lid);
  if (!$interface || is_null($interface)) {
    return TRUE;
  }
  return FALSE;
}
function globallink_interface_import(&$globallink) {
  module_load_include('inc', 'globallink', 'globallink');
  $target_xml = $globallink->targetXML;
  if (!isset($target_xml)) {
    $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
    return;
  }
  $language = globallink_get_drupal_locale_code($globallink->targetLocale);
  $translated_arr = globallink_interface_get_translated_items($target_xml);
  $lid = $translated_arr['lid'];
  foreach ($translated_arr as $attribute => $translations) {
    try {
      if ($attribute == 'lid') {
        continue;
      }
      $interface = '';
      if ($attribute == 'source') {
        $interface = globallink_load_source_data($translations['lid']);
        if ($interface == '') {
          throw new Exception('Source string not found for interface id ' . $lid . ' and field name ' . $attribute);
        }
      }
      $textgroup = 'default';
      $context = '';
      if (!empty($interface[0]->textgroup)) {
        $textgroup = $interface[0]->textgroup;
      }
      if (!empty($interface[0]->context)) {
        $context = $interface[0]->context;
      }
      $report =& drupal_static(__FUNCTION__, array(
        'additions' => 0,
        'updates' => 0,
        'deletes' => 0,
        'skips' => 0,
      ));
      _locale_import_one_string_db($report, $language, $context, $interface[0]->source, $translations['translation'], $textgroup, $translations['location'], LOCALE_IMPORT_OVERWRITE);
    } 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 interfaces from XML data.
 *
 * @param object $xml
 *   XML representation of interfaces.
 *
 * @return array
 *   Array of interfaces.
 */
function globallink_interface_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');
  $lid = '';
  foreach ($contents as $content) {
    if (!is_null($content->attributes)) {
      foreach ($content->attributes as $attr_name => $attr_node) {
        if ($attr_name == 'lid') {
          $lid = $attr_node->value;
        }
      }
    }
  }
  if ($lid == '') {
    return array();
  }
  $interface_arr = array();
  $interface_arr['lid'] = $lid;
  $interfaces = $dom
    ->getElementsByTagName(GLOBALLINK_ENTITY_TYPE_INTERFACE);
  foreach ($interfaces as $interface) {
    if (!is_null($interface->attributes)) {
      $i_arr = array();
      foreach ($interface->attributes as $attr_name => $attr_node) {
        $i_arr[$attr_name] = $attr_node->value;
      }
      $i_arr['translation'] = $interface->nodeValue;
      $interface_arr[$i_arr['name']] = $i_arr;
    }
  }
  return $interface_arr;
}
function globallink_interface_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_INTERFACE, '=');
  $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;
}

Functions

Namesort descending Description
globallink_interface_check_delete Gets interface translation status.
globallink_interface_get_active_submission_by_id
globallink_interface_get_sent_rows_by_lid Gets sent interface rows by LID.
globallink_interface_get_translated_items Gets translated interfaces from XML data.
globallink_interface_get_translate_filter_query Gets interface filters.
globallink_interface_get_xml Gets XML data from specific interface.
globallink_interface_import
globallink_interface_send_for_translations Sends interfaces for translation.