View source
<?php
$GLOBALS['_lingotek_client'] = new LingotekSession();
function lingotek_download_document(&$node, $lingotek_locale) {
global $_lingotek_client, $_lingotek_locale;
$document_id = lingotek_lingonode($node->nid, 'document_id');
lingotek_trace('lingotek_download_document', array(
'document_id' => $document_id,
"language" => $lingotek_locale,
));
$use_source = lingotek_lingonode($node->nid, 'use_source');
if ($use_source === FALSE) {
$use_source = variable_get('lingotek_use_source', TRUE);
}
if ($use_source == '1') {
$use_source = 'TRUE';
}
$drupal_language_code = Lingotek::convertLingotek2Drupal($lingotek_locale);
$params = array(
'documentId' => $document_id,
'targetLanguage' => $lingotek_locale,
'useSource' => $use_source,
);
$text = $_lingotek_client
->download("downloadDocument", $params);
try {
$xml = new SimpleXMLElement($text);
} catch (Exception $e) {
lingotek_error("downloadDocument FAILED", array(
'xml' => $text,
'error' => $e
->getMessage(),
));
return;
}
$url_alias_translation = lingotek_variable_get(lingotek_lingonode($node->nid, 'url_alias_translation'), 'lingotek_url_alias_translation', 1);
foreach ($xml as $tag => $content) {
if ($tag == 'menu_title') {
}
elseif ($tag == 'url_alias' && $url_alias_translation == 1) {
$target = check_plain($content);
$conditions = array(
'source' => 'node/' . $node->nid,
);
if ($node->language != LANGUAGE_NONE) {
$conditions['language'] = $node->language;
}
$path = path_load($conditions);
if ($path !== FALSE) {
$conditions['language'] = $drupal_language_code;
if ($path['alias'] != $target) {
$original = path_load($conditions);
$conditions['alias'] = $target;
if ($original === FALSE) {
path_save($conditions);
}
else {
path_delete($original);
path_save($conditions);
}
}
}
}
else {
$drupal_field_name = $tag;
$target_key = 'value';
$subfield_parts = explode('__', $tag);
if (count($subfield_parts) == 2) {
$drupal_field_name = $subfield_parts[0];
$target_key = $subfield_parts[1];
}
$field = field_info_field($drupal_field_name);
if (isset($field) && array_key_exists('lingotek_translatable', $field) && $field['lingotek_translatable'] == 1) {
$node_field =& $node->{$drupal_field_name};
$index = 0;
foreach ($content as $text) {
$node_field[$drupal_language_code][$index][$target_key] = decode_entities(lingotek_xml_decode($text));
if (array_key_exists($node->language, $node_field)) {
if (array_key_exists('format', $node_field[$node->language][0])) {
$node_field[$drupal_language_code][$index]['format'] = $node_field[$node->language][0]['format'];
}
}
$index++;
}
}
$url_alias_translation = lingotek_variable_get(lingotek_lingonode($node->nid, 'url_alias_translation'), 'lingotek_url_alias_translation', 1);
if ($tag == 'title_field' && $url_alias_translation == 2 && module_exists('pathauto') && $node->language != LANGUAGE_NONE) {
module_load_include('inc', 'pathauto');
$uri = entity_uri('node', $node);
pathauto_create_alias('node', 'update', $uri['path'], array(
'node' => clone $node,
), $node->type, $drupal_language_code);
}
}
}
$node->path = path_load(array(
'source' => 'node/' . $node->nid,
'language' => $node->language,
));
$node->path['alias'] = isset($node->path['alias']) ? $node->path['alias'] : '';
$node->path['pathauto'] = 0;
lingotek_set_target_sync_status($node->nid, $lingotek_locale, LINGOTEK_TARGET_SYNC_STATUS_CURRENT);
$node->lingotek_upload_override = 0;
$node->skip_status_updates = 1;
node_save($node);
}
function lingotek_save_segment($source_text, $target_text, $target_language, $doc_id) {
$param = array(
"sourceText" => $source_text,
"targetText" => $target_text,
"targetLanguage" => $target_language,
"documentId" => $doc_id,
"overwrite" => 0,
);
$save_segment = LingotekApi::instance()
->request("saveSegment", $param);
return $save_segment->results == "success";
}
function lingotek_add_phase_template($translation_target_id, $phase_template_id) {
$params = array(
'translationTargetId' => $translation_target_id,
'phaseTemplateId' => $phase_template_id,
);
LingotekApi::instance()
->request("applyPhaseTemplate", $params);
}
function lingotek_add_project($name) {
$output = LingotekApi::instance()
->request('addProject', array(
'projectName' => $name,
));
if ($output->results == "success") {
variable_set('lingotek_project', $output->id);
return $output->id;
}
}
function lingotek_add_vault($name) {
$output = LingotekApi::instance()
->request('addTMVault', array(
'tmVaultName' => $name,
));
if ($output->results == "success") {
variable_set('lingotek_vault', $output->id);
return $output->id;
}
}
function lingotek_add_vault_to_project() {
$vault_id = variable_get('lingotek_vault', '');
$project_id = variable_get('lingotek_project', '');
if ($vault_id != '' && $project_id != '') {
$param = array(
'project_id' => $project_id,
'index_id' => $vault_id,
);
LingotekApi::instance()
->request('addProjectTMVault', $param);
}
}
function lingotek_analyze_project() {
LingotekApi::instance()
->request("analyzeProject", array(
'projectId' => variable_get('lingotek_project', -1),
));
}
function lingotek_get_url_alias_translations() {
$methods = array();
$methods[0] = t("Don't translate");
$methods[1] = t("Translate the URL alias");
$methods[2] = t("Use the translated page title");
return $methods;
}
function lingotek_get_cms_key() {
global $_lingotek_client;
$output = LingotekApi::instance()
->request("getCMSKey");
if ($output->results == "success") {
variable_del('lingotek_password');
return $output->cms;
}
else {
return "";
}
}
function lingotek_get_communities() {
$options = array();
if (!$_lingotek_client
->canLogIn()) {
return $options;
}
$list_communities = LingotekApi::instance()
->request("listCommunities", array());
if ($list_communities->results == "success") {
foreach ($list_communities->communities as $community) {
$options[$community->id] = $community->name;
}
}
return $options;
}
function lingotek_get_document_targets($document_id, $flush_cache = FALSE) {
global $_lingotek_client;
$targets =& drupal_static(__FUNCTION__);
if (isset($targets[$document_id])) {
return $targets[$document_id];
}
$results = array();
$cache_id = 'lingotek_targets_' . $document_id;
$cache = cache_get($cache_id);
if (lingotek_do_cache() && !$flush_cache && !empty($cache->data)) {
lingotek_trace("lingotek_get_document_targets USING CACHE", array(
'document_id' => $document_id,
'flushCache' => $flush_cache,
));
$results = $cache->data;
}
else {
$output = LingotekApi::instance()
->getDocument($document_id);
if ($output) {
foreach ($output->translationTargets as $target) {
$results[$target->language] = $target;
}
}
lingotek_trace("lingotek_get_document_targets GENERATING NEW CACHE DATA getDocument", array(
'document_id' => $document_id,
'flushCache' => $flush_cache,
));
$targets[$document_id] = $results;
if (!empty($results)) {
cache_set($cache_id, $results, 'cache', time() + 900);
}
}
return $results;
}
function lingotek_get_phase_name($phase_id) {
$phases =& drupal_static(__FUNCTION__);
$phase_name = '';
if (!empty($phases[$phase_id])) {
$phase_name = $phases[$phase_id]->name;
}
else {
$params = array(
'phaseId' => $phase_id,
);
$output = LingotekApi::instance()
->request('getPhase', $params);
if ($output->results == 'success') {
$phases[$phase_id] = $output;
$phase_name = $output->name;
}
}
return $phase_name;
}
function lingotek_get_sync_methods() {
$methods = array();
$methods[0] = t("Never");
$methods[1] = t("Always");
$methods[100] = t("100%");
return $methods;
}
function lingotek_get_translation_target($translation_target_id) {
$params = array(
'translationTargetId' => $translation_target_id,
);
$output = LingotekApi::instance()
->request("getTranslationTarget", $params);
if ($output->results == "success") {
return $output;
}
}
function lingotek_get_workbench_url($node, $lingotek_locale, $label = FALSE, $force = FALSE) {
if ($lingotek_locale === FALSE) {
return "";
}
$api = LingotekApi::instance();
$link = array();
$document_id = lingotek_lingonode($node->nid, 'document_id');
$targets = lingotek_get_document_targets($document_id, TRUE);
if (count($targets) == 0) {
return '';
}
foreach ($targets as $lang => $translation_target) {
if ($lang != $lingotek_locale) {
continue;
}
lingotek_trace('lingotek_get_workbench_url TARGETS', array(
'lang' => $lang,
'translation_target' => $translation_target,
));
$target = $api
->getTranslationTarget($translation_target->id);
$phases = $target ? $target->phases : array();
$phase_id = -1;
$which_phase = 0;
foreach ($phases as $phase) {
if (!$phase->isMarkedComplete || $force) {
$phase_id = $phase->id;
break;
}
$which_phase++;
}
if (!empty($phases) && $phase_id == -1) {
$last_phase = end($phases);
$phase_id = $last_phase->id;
}
$l = '';
if ($phase_id != -1) {
$document_id = lingotek_lingonode($node->nid, 'document_id');
if ($document_id && ($workbench_url = $api
->getWorkbenchLink($document_id, $phase_id))) {
if ($label === FALSE) {
$l = $workbench_url;
}
else {
$path = $workbench_url;
if ($label === TRUE) {
$label = lingotek_get_phase_name($phase_id);
}
$l = l($label, '', array(
'attributes' => array(
'onclick' => 'window.open(\'' . $path . '\'); return false;',
'onmouseover' => 'jQuery("#node-' . $node->nid . '").addClass("highlight");',
'onmouseout' => 'jQuery("#node-' . $node->nid . '").removeClass("highlight");',
),
));
}
}
}
$link[$lang] = array(
'which_phase' => $which_phase,
'link' => $l,
);
}
return $link[$lingotek_locale]['link'];
}
function lingotek_get_xliff($doc_id) {
global $_lingotek_client;
$xliff_text = "";
$params = array(
'documentId' => $doc_id,
);
return $_lingotek_client
->download("downloadDocumentAsXliff", $params);
}