You are here

globallink_block.inc in GlobalLink Connect for Drupal 7.7

File

globallink_block/globallink_block.inc
View source
<?php

/**
 * Sends blocks for translation.
 *
 * @param array $bids
 *   The array of block IDs.
 * @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_block_send_blocks_for_translations($bids, $pd4, $submission_name, $due_date, $project_code, $source_locale, $target_locale_arr, $submission_details, $submission_priority) {
  module_load_include('inc', 'globallink', 'globallink');
  $strings = globallink_block_get_strings(globallink_get_drupal_locale_code($source_locale), 'blocks');
  $submitter = $submission_details['submitter'];
  $globallink_arr = array();
  foreach ($bids as $bid) {
    $rows = globallink_block_get_sent_tpt_rows_by_bid($bid);
    $target_arr = $target_locale_arr;
    $block_info = globallink_block_get_info($bid);
    $title = $block_info->title;
    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 Block Id - %id for locales %locale', array(
          '%id' => $bid,
          '%locale' => $row->target_lang_code,
        ), WATCHDOG_DEBUG);
      }
    }
    if (empty($target_arr)) {
      continue;
    }
    $title_string_arr = isset($strings['blocks:block:' . $bid . ':title']) ? $strings['blocks:block:' . $bid . ':title'] : FALSE;
    $body_string_arr = isset($strings['blocks:block:' . $bid . ':body']) ? $strings['blocks:block:' . $bid . ':body'] : FALSE;
    if (!$body_string_arr) {
      continue;
    }
    $xml = globallink_block_get_xml($bid, $title_string_arr, $body_string_arr);
    $name = 'Block_' . $bid . '.xml';
    $globallink = new GlobalLink();
    $globallink->type = GLOBALLINK_ENTITY_TYPE_BLOCK;
    $globallink->metadata = GLOBALLINK_ENTITY_TYPE_BLOCK;
    $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 = $bid;
    $globallink->submissionInstructions = $submission_details['instructions'] . "\nSubmitter: " . $submitter;
    $globallink_arr[GLOBALLINK_ENTITY_TYPE_BLOCK][] = $globallink;
  }
  return $globallink_arr;
}

/**
 * Gets XML data from specific block.
 *
 * @param string $bid
 *   The block ID.
 * @param array $title_string_arr
 *   Array of block's title strings.
 * @param array $body_string_arr
 *   Array of block's body strings.
 *
 * @return array
 *   Associative array of block XML data.
 */
function globallink_block_get_xml($bid, $title_string_arr, $body_string_arr) {
  $dom = new DOMDocument('1.0', 'UTF-8');
  $dom->formatOutput = TRUE;
  $root = $dom
    ->createElement('content');
  $dom
    ->appendChild($root);
  $id = $dom
    ->createAttribute('bid');
  $id->value = $bid;
  $root
    ->appendChild($id);
  $custom_block = block_custom_block_get($bid);
  if ($title_string_arr) {
    globallink_insert_child_element($dom, $root, GLOBALLINK_ENTITY_TYPE_BLOCK, $title_string_arr['source'], array(
      'name' => 'title',
      'lid' => $title_string_arr['lid'],
      'location' => $title_string_arr['location'],
      'context' => $title_string_arr['context'],
    ));
  }
  if ($body_string_arr) {
    $format = isset($custom_block['format']) ? $custom_block['format'] : '';
    globallink_insert_child_element($dom, $root, GLOBALLINK_ENTITY_TYPE_BLOCK, $body_string_arr['source'], array(
      'name' => 'body',
      'lid' => $body_string_arr['lid'],
      'location' => $body_string_arr['location'],
      'context' => $body_string_arr['context'],
      'format' => $format,
    ));
  }
  $xml = $dom
    ->saveXML();
  return $xml;
}

/**
 * Retrieves strings for all blocks.
 *
 * @param string $language
 *   The blocks' $language. Defaults to NULL.
 * @param string $group
 *   The Drupal content type.  Defaults to 'blocks.'
 *
 * @return array
 *   Array of strings for blocks.
 */
function globallink_block_get_strings($language = NULL, $group = 'blocks') {
  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 block translation status.
 *
 * @param string $bid
 *   The block ID.
 * @param string $tgt_locale
 *   The target locale.
 * @param string $title
 *   The translation title.
 *
 * @return string
 *   Status message.
 */
function globallink_block_check_delete($bid) {
  $s_block = block_custom_block_get($bid);
  if (!$s_block || is_null($s_block)) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Gets number of translated blocks.
 *
 * @param string $pd4
 *   The project director details.
 * @param array $globallink_arr
 *   Array of GlobalLink objects.
 *
 * @return int
 *   The number of translated blocks.
 */
function globallink_block_import(&$globallink) {
  module_load_include('inc', 'globallink', 'globallink');
  $strings = globallink_block_get_strings(NULL, 'blocks');
  $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_block_get_translated_items($target_xml);
  $bid = $translated_arr['bid'];
  foreach ($translated_arr as $attribute => $translations) {
    try {
      if ($attribute == 'bid') {
        continue;
      }
      $source = '';
      switch ($attribute) {
        case 'title':
          $title_string_arr = isset($strings['blocks:block:' . $bid . ':title']) ? $strings['blocks:block:' . $bid . ':title'] : FALSE;
          if (!$title_string_arr) {
            continue;
          }
          $source = $title_string_arr['source'];
          break;
        case 'body':
          $body_string_arr = isset($strings['blocks:block:' . $bid . ':body']) ? $strings['blocks:block:' . $bid . ':body'] : FALSE;
          if (!$body_string_arr) {
            throw new Exception('Source string not found for block id ' . $bid . ' and field name ' . $attribute);
          }
          $source = $body_string_arr['source'];
          break;
      }
      $report =& drupal_static(__FUNCTION__, array(
        'additions' => 0,
        'updates' => 0,
        'deletes' => 0,
        'skips' => 0,
      ));
      _locale_import_one_string_db($report, $language, $translations['context'], $source, $translations['translation'], 'blocks', $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 blocks.
 *
 * @param object $xml
 *   XML representation of blocks.
 *
 * @return array
 *   Array of blocks.
 */
function globallink_block_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();
  }
  $block_arr = array();
  $block_arr['bid'] = $bid;
  $blocks = $dom
    ->getElementsByTagName(GLOBALLINK_ENTITY_TYPE_BLOCK);
  foreach ($blocks as $block) {
    if (!is_null($block->attributes)) {
      $b_arr = array();
      foreach ($block->attributes as $attr_name => $attr_node) {
        $b_arr[$attr_name] = $attr_node->value;
      }
      $b_arr['translation'] = $block->nodeValue;
      $block_arr[$b_arr['name']] = $b_arr;
    }
  }
  return $block_arr;
}

/**
 * Gets sent block rows by ID.
 *
 * @param string $bid
 *   The block ID.
 *
 * @return array
 *   Array of block rows.
 */
function globallink_block_get_sent_tpt_rows_by_bid($bid) {
  $result = db_select('globallink_document', 'gd')
    ->fields('gd')
    ->condition('entity_type', GLOBALLINK_ENTITY_TYPE_BLOCK, '=')
    ->condition('object_id', $bid, '=')
    ->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;
}

/**
 * Loads block data.
 *
 * @param string $bid
 *   The block ID.
 *
 * @return array
 *   Array of block data.
 */
function globallink_block_load_data($bid) {
  $query = db_select('block_custom', 'bc')
    ->fields('bc')
    ->condition('bid', $bid, '=');
  $results = $query
    ->execute();
  $rows = array();
  foreach ($results as $item) {
    $rows[] = $item;
  }
  return $rows;
}
function globallink_block_get_info($bid) {
  $results = db_select(GLOBALLINK_ENTITY_TYPE_BLOCK, 'bk')
    ->fields('bk')
    ->condition('delta', $bid, '=')
    ->distinct()
    ->execute();
  $arr = new stdClass();
  foreach ($results as $result) {
    $arr = $result;
  }
  return $arr;
}
function globallink_block_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_BLOCK, '=');
  $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_block_check_delete Gets block translation status.
globallink_block_get_active_submission_by_id
globallink_block_get_info
globallink_block_get_sent_tpt_rows_by_bid Gets sent block rows by ID.
globallink_block_get_strings Retrieves strings for all blocks.
globallink_block_get_translated_items Gets translated blocks.
globallink_block_get_xml Gets XML data from specific block.
globallink_block_import Gets number of translated blocks.
globallink_block_load_data Loads block data.
globallink_block_send_blocks_for_translations Sends blocks for translation.