View source
<?php
$GLOBALS['_lingotek_client'] = new LingotekSession();
function lingotek_download_document(&$node, $lingotek_locale, $sync_success_status = LingotekSync::STATUS_CURRENT) {
global $_lingotek_client, $_lingotek_locale;
$document_id = lingotek_lingonode($node->nid, 'document_id');
LingotekLog::trace('lingotek_download_document @doc_id (@target)', array(
'@doc_id' => $document_id,
'@target' => $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) {
LingotekLog::error("downloadDocument FAILED. Error: @error. Text: !xml.", 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);
$delta = 0;
$last_tag = NULL;
foreach ($xml as $tag => $content) {
if ($tag == $last_tag) {
$delta++;
}
else {
$delta = 0;
}
if ($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;
if (module_exists('link') && $field['type'] == 'link_field') {
$target_key = array(
'url',
'title',
);
}
if (!is_array($target_key)) {
$target_key = array(
$target_key,
);
}
$insert_array = array(
'entity_type' => 'node',
'bundle' => $node->type,
'entity_id' => $node->nid,
'revision_id' => $node->vid,
'language' => $drupal_language_code,
'delta' => $delta,
'deleted' => '0',
);
foreach ($content as $text) {
$array_key = $target_key[$index];
$db_field_name = $field['field_name'] . '_' . $array_key;
$insert_array[$db_field_name] = decode_entities($text);
if (array_key_exists($node->language, $node_field)) {
if (array_key_exists('format', $node_field[$node->language][0])) {
$format_db_field_name = $field['field_name'] . '_format';
$insert_array[$format_db_field_name] = $node_field[$node->language][0]['format'];
}
}
$index++;
}
$field_names = array(
'field_revision_' . $field['field_name'],
);
$field_names[] = 'field_data_' . $field['field_name'];
if (isset($node->workbench_moderation) && isset($node->workbench_moderation['published']) && $node->workbench_moderation['published']->vid != $node->workbench_moderation['current']->vid) {
$only_write_revisions = 1;
unset($field_names[$only_write_revisions]);
}
foreach ($field_names as $field_name) {
try {
drupal_write_record($field_name, $insert_array);
} catch (PDOException $e) {
$primary_keys = array(
'entity_type',
'entity_id',
'revision_id',
'deleted',
'delta',
'language',
);
drupal_write_record($field_name, $insert_array, $primary_keys);
}
}
}
$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);
}
}
$last_tag = $tag;
}
$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;
LingotekSync::setTargetStatus($node->nid, $lingotek_locale, $sync_success_status);
$node->lingotek_upload_override = 0;
$node->skip_status_updates = 1;
if (module_exists('workbench_moderation')) {
if (isset($node->workbench_moderation)) {
$node->workbench_moderation['updating_live_revision'] = 1;
}
}
}
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_workbench_moderation_states() {
$methods = array();
$states = workbench_moderation_states();
foreach ($states as $array) {
$methods[$array->name] = $array->name;
}
return $methods;
}
function lingotek_get_workbench_moderation_options() {
$options = array();
$options['no_moderation'] = 'Leave at the current state';
$options['increment'] = 'Increment to the next state';
$states = workbench_moderation_states();
foreach ($states as $array) {
$options[] = 'Change to ' . $array->name;
}
return $options;
}
function lingotek_get_workbench_moderation_transitions() {
$lingotek_transitions = array();
$states = lingotek_get_workbench_moderation_states();
$transitions = workbench_moderation_transitions();
foreach ($transitions as $transition) {
$trans = variable_get('lingotek_sync_wb_select_' . $transition->from_name, NULL);
if ($trans) {
$lingotek_transitions[$transition->from_name] = $trans;
unset($states[$transition->from_name]);
}
else {
if (isset($states[$transition->from_name])) {
$lingotek_transitions[$transition->from_name] = $transition->to_name;
unset($states[$transition->from_name]);
}
}
}
foreach ($states as $state) {
$lingotek_transitions[$state] = $state;
}
return $lingotek_transitions;
}
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)) {
LingotekLog::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;
}
}
LingotekLog::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();
$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;
}
$target = $api
->getTranslationTarget($translation_target->id);
$phases = $target ? $target->phases : array();
return lingotek_get_workbench_url_by_phases($node, $phases, $label, $force);
}
LingotekLog::error('lingotek_get_workbench_url - Specified language target not found');
return '';
}
function lingotek_get_workbench_url_by_phases($node, $phases, $label = FALSE, $force = FALSE) {
$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 = LingotekApi::instance()
->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("lingotek-highlight");',
'onmouseout' => 'jQuery("#node-' . $node->nid . '").removeClass("lingotek-highlight");',
),
));
}
}
}
return $l;
}
function lingotek_get_xliff($doc_id) {
global $_lingotek_client;
$xliff_text = "";
$params = array(
'documentId' => $doc_id,
);
return $_lingotek_client
->download("downloadDocumentAsXliff", $params);
}