You are here

gl_ws_receive_translations.inc in GlobalLink Connect for Drupal 7.7

File

gl_ws/gl_ws_receive_translations.inc
View source
<?php

/**
 * @file
 * File contains Web Service functions for retreiving translations.
 */
require_once 'glc/GLExchange.php';
require_once 'glc/model/PDConfig.inc.php';
require_once 'glc/model/Submission.inc.php';
require_once 'glc/model/Document.inc.php';

/**
 *
 */
function get_cancelled_targets_by_documents($pd_obj, $document_tickets) {
  if (globallink_debug_enabled()) {
    watchdog(GLOBALLINK_MODULE, 'Connecting to PD - %pd', array(
      '%pd' => print_r($pd_obj, TRUE),
    ), WATCHDOG_DEBUG);
  }
  $cancelled_arr = array();
  $connectorConfig = new PDConfig();
  $connectorConfig->url = $pd_obj->url;
  $connectorConfig->username = $pd_obj->username;
  $connectorConfig->password = $pd_obj->password;
  $connectorConfig->userAgent = $pd_obj->userAgent;
  $glExchange = new GLExchange($connectorConfig);
  $targets_arr = $glExchange
    ->getCancelledTargetsByDocuments($document_tickets, $pd_obj->maxTargetCount);
  if (is_null($targets_arr) || empty($targets_arr)) {
    return $cancelled_arr;
  }
  if (count($targets_arr) > 0) {
    foreach ($targets_arr as $target) {
      $tgt_locale = str_replace('-', '_', $target->targetLocale);
      if (isset($cancelled_arr[$tgt_locale])) {
        $cancelled_arr[$tgt_locale][] = $target->documentTicket;
      }
      else {
        $cancelled_arr[$tgt_locale] = array();
        $cancelled_arr[$tgt_locale][] = $target->documentTicket;
      }
    }
  }
  return $cancelled_arr;
}

/**
 *
 */
function check_if_submission_cancelled_or_deleted($pd_obj, $submission_ticket) {
  if (globallink_debug_enabled()) {
    watchdog(GLOBALLINK_MODULE, 'Connecting to PD - %pd', array(
      '%pd' => print_r($pd_obj, TRUE),
    ), WATCHDOG_DEBUG);
  }
  $connectorConfig = new PDConfig();
  $connectorConfig->url = $pd_obj->url;
  $connectorConfig->username = $pd_obj->username;
  $connectorConfig->password = $pd_obj->password;
  $connectorConfig->userAgent = $pd_obj->userAgent;
  $glExchange = new GLExchange($connectorConfig);
  $submission_status = $glExchange
    ->getSubmissionStatus($submission_ticket);
  if (is_null($submission_status) || empty($submission_status)) {
    return TRUE;
  }
  else {
    if ($submission_status->name == 'CANCELLED') {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 *
 */
function globallink_get_ready_translations_for_documents($pd_obj, $document_tickets) {
  if (globallink_debug_enabled()) {
    watchdog(GLOBALLINK_MODULE, 'Connecting to PD - %pd', array(
      '%pd' => print_r($pd_obj, TRUE),
    ), WATCHDOG_DEBUG);
  }
  $connectorConfig = new PDConfig();
  $connectorConfig->url = $pd_obj->url;
  $connectorConfig->username = $pd_obj->username;
  $connectorConfig->password = $pd_obj->password;
  $connectorConfig->userAgent = $pd_obj->userAgent;
  $glExchange = new GLExchange($connectorConfig);
  $targets = $glExchange
    ->getCompletedTargetsByDocument($document_tickets, $pd_obj->maxTargetCount);
  $globallink_arr = array();
  if (isset($targets) && count($targets) > 0) {
    if (globallink_debug_enabled()) {
      watchdog(GLOBALLINK_MODULE, 'Targets Returned from GlobalLink Project Director - %targets', array(
        '%targets' => print_r($targets, TRUE),
      ), WATCHDOG_DEBUG);
    }
    $count = 1;
    if (is_array($targets)) {
      foreach ($targets as $target) {
        if (!is_null($target->ticket) && $target->ticket != '') {
          $m_arr = $target->metadata;
          $source_type = '';
          $type_id = '';
          if (is_array($m_arr)) {
            foreach ($m_arr as $m_key => $md) {
              if ($m_key == 'type') {
                $source_type = $md;
              }
              else {
                $type_id = $md;
              }
            }
          }
          $globallink = new GlobalLink();
          $globallink->submissionTicket = $target->submissionTicket;
          $globallink->submissionName = $target->submissionName;
          $globallink->documentTicket = $target->documentTicket;
          $globallink->sourceFileName = $target->documentName;
          $globallink->targetTicket = $target->ticket;
          $globallink->sourceLocale = str_replace('-', '_', $target->sourceLocale);
          $globallink->targetLocale = str_replace('-', '_', $target->targetLocale);
          $globallink->type = $source_type;
          if ($source_type != GLOBALLINK_ENTITY_TYPE_NODE && $source_type != GLOBALLINK_ENTITY_TYPE_ENTITY) {
            $globallink->otherObjectId = $type_id;
          }
          else {
            $globallink->nid = $type_id;
          }
          $globallink_arr[$count] = $globallink;
          $count++;
        }
      }
    }
  }
  return $globallink_arr;
}

/**
 *
 */
function globallink_get_ready_translations_for_submission($pd_obj, $submission_tickets) {
  if (globallink_debug_enabled()) {
    watchdog(GLOBALLINK_MODULE, 'Connecting to PD - %pd', array(
      '%pd' => print_r($pd_obj, TRUE),
    ), WATCHDOG_DEBUG);
  }
  $connectorConfig = new PDConfig();
  $connectorConfig->url = $pd_obj->url;
  $connectorConfig->username = $pd_obj->username;
  $connectorConfig->password = $pd_obj->password;
  $connectorConfig->userAgent = $pd_obj->userAgent;
  $glExchange = new GLExchange($connectorConfig);
  $targets = $glExchange
    ->getCompletedTargetsBySubmission($submission_tickets, $pd_obj->maxTargetCount);
  $globallink_arr = array();
  if (isset($targets) && count($targets) > 0) {
    if (globallink_debug_enabled()) {
      watchdog(GLOBALLINK_MODULE, 'Targets Returned from GlobalLink Project Director - %targets', array(
        '%targets' => print_r($targets, TRUE),
      ), WATCHDOG_DEBUG);
    }
    $count = 1;
    if (is_array($targets)) {
      foreach ($targets as $target) {
        if (!is_null($target->ticket) && $target->ticket != '') {
          $m_arr = $target->metadata;
          $source_type = '';
          $type_id = '';
          if (is_array($m_arr)) {
            foreach ($m_arr as $m_key => $md) {
              if ($m_key == 'type') {
                $source_type = $md->value;
              }
              else {
                $type_id = $md->value;
              }
            }
          }
          $globallink = new GlobalLink();
          $globallink->submissionTicket = $target->submissionTicket;
          $globallink->submissionName = $target->submissionName;
          $globallink->documentTicket = $target->documentTicket;
          $globallink->sourceFileName = $target->documentName;
          $globallink->targetTicket = $target->ticket;
          $globallink->sourceLocale = str_replace('-', '_', $target->sourceLocale);
          $globallink->targetLocale = str_replace('-', '_', $target->targetLocale);
          $globallink->type = $source_type;
          if ($source_type != GLOBALLINK_ENTITY_TYPE_NODE && $source_type != GLOBALLINK_ENTITY_TYPE_ENTITY) {
            $globallink->otherObjectId = $type_id;
          }
          else {
            $globallink->nid = $type_id;
          }
          $globallink_arr[$count] = $globallink;
          $count++;
        }
      }
    }
  }
  return $globallink_arr;
}

/**
 *
 */
function globallink_download_target_resource($pd_obj, $target_ticket) {
  if (globallink_debug_enabled()) {
    watchdog(GLOBALLINK_MODULE, 'Connecting to PD - %pd', array(
      '%pd' => print_r($pd_obj, TRUE),
    ), WATCHDOG_DEBUG);
    watchdog(GLOBALLINK_MODULE, 'Downloading Target File for Target Ticket - %ticket', array(
      '%ticket' => $target_ticket,
    ), WATCHDOG_DEBUG);
  }
  $connectorConfig = new PDConfig();
  $connectorConfig->url = $pd_obj->url;
  $connectorConfig->username = $pd_obj->username;
  $connectorConfig->password = $pd_obj->password;
  $connectorConfig->userAgent = $pd_obj->userAgent;
  $glExchange = new GLExchange($connectorConfig);
  return $glExchange
    ->downloadTarget($target_ticket);
}

/**
 *
 */
function globallink_send_download_confirmation($target_ticket, $pd_obj) {
  if (globallink_debug_enabled()) {
    watchdog(GLOBALLINK_MODULE, 'Connecting to PD - %pd', array(
      '%pd' => print_r($pd_obj, TRUE),
    ), WATCHDOG_DEBUG);
    watchdog(GLOBALLINK_MODULE, 'Sending Download Confirmation for Target Ticket - %ticket', array(
      '%ticket' => $target_ticket,
    ), WATCHDOG_DEBUG);
  }
  $connectorConfig = new PDConfig();
  $connectorConfig->url = $pd_obj->url;
  $connectorConfig->username = $pd_obj->username;
  $connectorConfig->password = $pd_obj->password;
  $connectorConfig->userAgent = $pd_obj->userAgent;
  $glExchange = new GLExchange($connectorConfig);
  return $glExchange
    ->sendDownloadConfirmation($target_ticket);
}