View source
<?php
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}