View source
<?php
$GLOBALS['_lingotek_client'] = new LingotekSession();
function lingotek_get_translated_node($node, $drupal_language_code) {
$tset = translation_node_get_translations($node->nid);
if ($node->tnid == 0) {
$node->tnid = $node->nid;
db_update('{node}')
->fields(array(
'tnid' => $node->nid,
))
->condition('nid', $node->nid)
->execute();
}
if (isset($tset[$drupal_language_code])) {
$localized_node = node_load($tset[$drupal_language_code]->nid);
}
else {
$localized_node = new stdClass();
$localized_node->type = $node->type;
node_object_prepare($localized_node);
if (!empty($node->lingotek)) {
$localized_node->lingotek = $node->lingotek;
}
$localized_node->title = $node->title . ' (' . $drupal_language_code . ')';
$localized_node->tnid = $node->tnid;
$localized_node->language = $drupal_language_code;
$localized_node->uid = $node->uid;
$localized_node->name = $node->name;
$localized_node->comment = $node->comment;
$localized_node->promote = $node->promote;
$localized_node->sticky = $node->sticky;
$localized_node->status = $node->status;
$localized_node->create_lingotek_document = FALSE;
$source_fields = field_info_instances('node', $node->type);
foreach (array_keys($source_fields) as $key) {
$copied_field = $node->{$key};
if (!empty($copied_field[$node->language])) {
$copied_field[$localized_node->language] = $copied_field[$node->language];
unset($copied_field[$node->language]);
}
$localized_node->{$key} = $copied_field;
}
$lingotek_fields = variable_get('lingotek_enabled_fields');
foreach ($lingotek_fields['node'][$localized_node->type] as $field_name) {
$field = $node->{$field_name};
$f = array();
if (isset($field[$node->language])) {
foreach ($field[$node->language] as $key => $value) {
if (isset($value['format'])) {
$f[$drupal_language_code][$key]['format'] = $value['format'];
}
}
}
$localized_node->{$field_name} = $f;
}
node_save($localized_node);
lingotek_keystore('node', $localized_node->nid, 'node_sync_status', 'TARGET');
$parent_profile = lingotek_keystore('node', $node->nid, 'profile');
if ($parent_profile !== FALSE) {
lingotek_keystore('node', $localized_node->nid, 'profile', $parent_profile);
}
}
return $localized_node;
}
function lingotek_process_entity_xml($xml, $entity, $entity_type, $drupal_language_code, $node_based_translation = FALSE, $url_alias_translation = 0) {
list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
if ($entity_type == 'node') {
$node_based_translation = lingotek_uses_node_translation($entity) ? TRUE : FALSE;
}
$source_entity = $entity_type == 'node' && $node_based_translation ? lingotek_get_source_node($entity) : $entity;
if ($vid === NULL || $entity_type == 'comment') {
$vid = $id;
}
$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/' . $id,
);
if ($entity->language != LANGUAGE_NONE) {
$conditions['language'] = $entity->language;
}
$path = path_load($conditions);
if ($path !== FALSE) {
$conditions['language'] = $drupal_language_code;
if ($path['alias'] != $target || $entity->language != $drupal_language_code) {
$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;
$field = field_info_field($drupal_field_name);
$source_field_data = field_get_items($entity_type, $source_entity, $drupal_field_name);
$translatable_field = !empty($field['translatable']);
$field_language = $node_based_translation && !$translatable_field ? LANGUAGE_NONE : $drupal_language_code;
$target_key = 'value';
$subfield_parts = explode('__', $tag);
if (count($subfield_parts) == 2) {
$drupal_field_name = $subfield_parts[0];
$target_key = $subfield_parts[1];
}
if (isset($field)) {
$curr_field_data =& $entity->{$drupal_field_name};
$index = 0;
if (module_exists('link') && $field['type'] == 'link_field') {
$target_key = array(
'title',
'url',
);
}
if (!is_array($target_key)) {
$target_key = array(
$target_key,
);
}
$insert_array = array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'entity_id' => $id,
'revision_id' => $vid,
'language' => $field_language,
'delta' => $delta,
'deleted' => '0',
);
$field_names = array(
'field_revision_' . $field['field_name'],
'field_data_' . $field['field_name'],
);
if (module_exists('field_collection') && $field['type'] == 'field_collection') {
$default = language_default();
$default_language = $default->language;
if (isset($curr_field_data[LANGUAGE_NONE][$delta]['value'])) {
$field_collection_id = $curr_field_data[LANGUAGE_NONE][$delta]['value'];
}
elseif (isset($curr_field_data[$default_language][$delta]['value'])) {
$field_collection_id = $curr_field_data[$language_default][$delta]['value'];
}
else {
if (!$node_based_translation) {
continue;
}
$field_collection_item = entity_create('field_collection_item', array(
'field_name' => $field['field_name'],
));
$field_collection_item
->setHostEntity($entity_type, $entity);
$field_collection_item
->save();
$field_collection_id = $field_collection_item->item_id;
}
$field_collection_item = lingotek_entity_load_single('field_collection_item', $field_collection_id);
if (!$field_collection_item) {
continue;
}
$field_collection_item->type = $field['field_name'];
$field_collection_item->language = $entity->language;
$field_collection_item->nid = $field_collection_item->item_id;
$field_collection_item->vid = $field_collection_item->revision_id;
lingotek_process_entity_xml($content, $field_collection_item, 'field_collection_item', $drupal_language_code, $node_based_translation);
$last_tag = $tag;
continue;
}
else {
foreach ($content as $text) {
if ($entity_type == 'field_collection_item' && !$node_based_translation && $field['translatable'] != 1) {
$field['translatable'] = 1;
field_update_field($field);
}
$is_link = in_array('url', $target_key);
$without_title = count($content) == 1;
if ($is_link && $without_title) {
$array_key = $target_key[1];
}
else {
$array_key = $target_key[$index];
}
$db_field_name = $field['field_name'] . '_' . $array_key;
$insert_array[$db_field_name] = lingotek_unfilter_placeholders(decode_entities($text));
if ($db_field_name == 'title_field_value' && strlen($insert_array[$db_field_name]) > 254) {
$insert_array[$db_field_name] = substr($insert_array[$db_field_name], 0, 254);
$language_list = language_list('language');
$langcode = $insert_array['language'];
LingotekLog::info('The @lang (@langcode) title was truncated, since the translation exceeded the maximum of 255 characters.', array(
'@lang' => $language_list[$langcode]->name,
'@langcode' => Lingotek::convertDrupal2Lingotek($langcode),
));
}
$field_format = NULL;
if (!empty($source_field_data[0]['format'])) {
$field_format = $source_field_data[0]['format'];
}
elseif (!empty($curr_field_data[$entity->language][0]['format'])) {
$field_format = $curr_field_data[$entity->language][0]['format'];
}
if (!empty($field_format)) {
$format_db_field_name = $field['field_name'] . '_format';
$insert_array[$format_db_field_name] = $field_format;
}
$index++;
}
}
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);
}
}
}
cache_clear_all('field:' . $entity_type . ':' . $id, 'cache_field');
if ($tag == 'title_field' && $url_alias_translation == 2 && module_exists('pathauto') && $entity->language != LANGUAGE_NONE) {
module_load_include('inc', 'pathauto');
$uri = entity_uri('node', $entity);
$entity_unchanged = entity_load_unchanged('node', $id);
pathauto_create_alias('node', 'update', $uri['path'], array(
'node' => clone $entity_unchanged,
), $entity->type, $drupal_language_code);
}
}
$last_tag = $tag;
}
}
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)) {
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 (!empty($output->translationTargets)) {
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($document_id, $lingotek_locale, $label = FALSE, $force = FALSE) {
if ($lingotek_locale === FALSE) {
return "";
}
$api = LingotekApi::instance();
$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($document_id, $phases, $label, $force);
}
LingotekLog::error('lingotek_get_workbench_url - Specified language target not found', $document_id);
return '';
}
function lingotek_get_workbench_url_by_phases($document_id, $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) {
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);
}
list($nid, $entity_type) = LingotekSync::getEntityIdFromDocId($document_id);
$l = l($label, '', array(
'attributes' => array(
'onclick' => 'window.open(\'' . $path . '\'); return false;',
'onmouseover' => 'jQuery("#node-' . $nid . '").addClass("lingotek-highlight");',
'onmouseout' => 'jQuery("#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
->downloadTriggered("downloadDocumentAsXliff", $params);
}