View source
<?php
function globallink_send_for_translations($nids, $pd4, $submission_name, $due_date, $project_code, $source_locale, $target_locale_arr, $submission_details, $submission_priority, $parents = NULL) {
module_load_include('inc', 'globallink', 'globallink');
$globallink_arr = array();
$node_check = variable_get('globallink_implementation_type', 0);
$submitter = $submission_details['submitter'];
foreach ($nids as $nid) {
list($nid, $vid) = explode('-', $nid, 2);
$rows = globallink_get_sent_rows_by_nid($nid);
$target_arr = $target_locale_arr;
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 Node Id - %id for locales %locale', array(
'%id' => $nid,
'%locale' => $row->target_lang_code,
), WATCHDOG_DEBUG);
}
}
if (empty($target_arr)) {
continue;
}
$node = node_load($nid, $vid);
if ($node_check == 1) {
foreach ($target_arr as $key => $target_locale) {
if (!globallink_translate_node_for_language($node, globallink_get_drupal_locale_code($target_locale))) {
unset($target_arr[$key]);
}
}
}
if (empty($target_arr)) {
watchdog(GLOBALLINK_MODULE, 'No target languages. Skipping nid - %nid', array(
'%nid' => $nid,
), WATCHDOG_WARNING);
continue;
}
$drupal_target_arr = array();
foreach ($target_arr as $target_locale) {
array_push($drupal_target_arr, globallink_get_drupal_locale_code($target_locale));
}
$tnid = NULL;
$tvid = NULL;
$name = '.xml';
$xml = globallink_get_xml($node, $drupal_target_arr, $tnid, $tvid, $name);
if (!$xml) {
watchdog(GLOBALLINK_MODULE, 'Cannot create XML. Skipping nid - %nid', array(
'%nid' => $nid,
), WATCHDOG_WARNING);
continue;
}
$globallink = new GlobalLink();
$globallink->nid = $node->nid;
$globallink->vid = $node->vid;
$globallink->title = $node->title;
$globallink->type = $node->type;
$globallink->metadata = GLOBALLINK_ENTITY_TYPE_NODE;
$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->submissionInstructions = $submission_details['instructions'] . "\nSubmitter: " . $submitter;
$globallink_arr[GLOBALLINK_ENTITY_TYPE_NODE][] = $globallink;
}
return $globallink_arr;
}
function globallink_import_node(&$globallink, $t_arr = NULL) {
module_load_include('inc', GLOBALLINK_ENTITY_TYPE_NODE, 'node.pages');
module_load_include('inc', 'globallink', 'globallink');
$success = FALSE;
try {
$default_language = language_default();
$default_language_code = $default_language->language;
$target_locale_code = globallink_get_drupal_locale_code($globallink->targetLocale);
$publish_node = variable_get('globallink_publish_node', 0);
if ($t_arr != NULL) {
$translated_arr = $t_arr;
}
else {
$translated_arr = globallink_get_translated_array($globallink->targetXML, $target_locale_code);
}
$target_nid = $translated_arr['nid'];
$target_vid = $translated_arr['vid'];
$target_title = '';
if (isset($translated_arr['title'])) {
$target_title = $translated_arr['title'];
}
$globallink->nid = $target_nid;
$globallink->vid = $target_vid;
$node = node_load($target_nid, $target_vid);
if (!$node || is_null($node) || !is_object($node)) {
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
return;
}
$tnid = $node->nid;
if (!empty($node->tnid) && $node->nid != $node->tnid) {
$tnid = $node->tnid;
}
$node_arr = translation_node_get_translations($tnid);
$update_flag = FALSE;
if (sizeof($node_arr) > 0) {
foreach ($node_arr as $_tnode) {
if ($_tnode->language == $target_locale_code) {
$update_flag = TRUE;
break;
}
}
}
if ($update_flag) {
foreach ($node_arr as $_tnode) {
if ($node->nid == $_tnode->nid) {
continue;
}
if ($_tnode->language != $target_locale_code) {
continue;
}
$target_node = node_load($_tnode->nid);
$new_node = unserialize(serialize($node));
unset($new_node->nid);
unset($new_node->vid);
$new_node->nid = $_tnode->nid;
$new_node->vid = 0;
$new_node->tnid = $node->tnid;
$new_node->language = $_tnode->language;
$new_node->revision = 1;
$nodepath = drupal_lookup_path('alias', 'node/' . $node->nid);
$path = path_load('node/' . $_tnode->nid);
if ($nodepath === FALSE) {
}
else {
if ($path) {
if (!empty($translated_arr['path'])) {
$new_node->path = array(
'pid' => $path['pid'],
'alias' => $translated_arr['path'],
'pathauto' => 0,
);
}
else {
$new_node->path = array(
'pid' => $path['pid'],
'alias' => $nodepath,
'pathauto' => 0,
);
}
}
else {
$new_node->path = array(
'alias' => $nodepath,
'pathauto' => 0,
);
if (!empty($translated_arr['path'])) {
$new_node->path = array(
'alias' => $translated_arr['path'],
'pathauto' => 0,
);
}
else {
$new_node->path = array(
'alias' => $nodepath,
'pathauto' => 0,
);
}
}
}
if ($publish_node == 0 || $publish_node == 1) {
$new_node->status = $publish_node;
}
else {
$new_node->status = $node->status;
}
if (module_exists('revisioning')) {
$new_node->is_pending = FALSE;
$new_node->revision_moderation = FALSE;
}
if ($target_title != '') {
$new_node->title = $target_title;
}
$new_node->tpt_skip = TRUE;
if (module_exists('metatag')) {
if (isset($translated_arr['metatag'])) {
$target_metatag_arr = $translated_arr['metatag'];
if (!empty($node->metatags[$node->language])) {
$metatags_names = array_keys($node->metatags[$node->language]);
$n_metatag =& $new_node->metatags[$new_node->language];
foreach ($metatags_names as $name) {
if (isset($target_metatag_arr[$name]) && isset($target_metatag_arr[$name][0])) {
$gl_obj = $target_metatag_arr[$name]['0'];
if (is_object($gl_obj)) {
$translated_content = $gl_obj->translatedContent;
}
else {
$translated_content = $gl_obj;
}
$n_metatag[$name] = array(
'value' => $translated_content,
);
}
}
}
}
}
if (module_exists('field_collection') && isset($translated_arr['field_collection'])) {
$t_fc_arr = $translated_arr['field_collection'];
$fcs = array_keys($t_fc_arr);
foreach ($fcs as $fc) {
if (isset($new_node->{$fc})) {
unset($new_node->{$fc});
}
}
}
$enabled = globallink_content_type_workbench_enabled($new_node->type);
if ($enabled) {
$current_value = variable_get('globallink_moderation_' . $new_node->type, FALSE);
if ($current_value) {
$new_node->workbench_moderation['current']->from_state = $current_value;
$new_node->workbench_moderation['current']->state = $current_value;
$new_node->workbench_moderation_state_current = $current_value;
$new_node->workbench_moderation_state_new = $current_value;
if ($current_value != workbench_moderation_state_published()) {
$new_node->workbench_moderation['current']->published = 0;
unset($new_node->workbench_moderation['published']);
}
}
}
globallink_update_non_translatable_field($target_node, $new_node);
$success = globallink_save_translated_node_with_fields($node, $new_node, $translated_arr);
if ($success && module_exists('exclude_node_title')) {
$exclude = _exclude_node_title($node->nid);
$exclude_list = variable_get('exclude_node_title_nid_list', array());
$is_excluded = array_search($new_node->nid, $exclude_list);
if ($exclude && $is_excluded === FALSE) {
$exclude_list[] = $new_node->nid;
variable_set('exclude_node_title_nid_list', $exclude_list);
}
elseif ($exclude === FALSE && $is_excluded) {
unset($exclude_list[$is_excluded]);
variable_set('exclude_node_title_nid_list', $exclude_list);
}
}
if ($success) {
globallink_update_node_tnid($node->nid, $tnid);
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
}
else {
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
}
}
}
else {
$new_node = unserialize(serialize($node));
unset($new_node->nid);
unset($new_node->vid);
node_object_prepare($new_node);
if ($default_language_code == $node->language) {
$new_node->tnid = $node->nid;
}
else {
$new_node->tnid = $tnid;
}
$nodepath = drupal_lookup_path('alias', 'node/' . $node->nid);
if ($nodepath === FALSE) {
}
else {
if (!empty($translated_arr['path'])) {
$new_node->path = array(
'alias' => $translated_arr['path'],
);
}
else {
$new_node->path = array(
'alias' => $nodepath,
);
}
}
if ($publish_node == 0 || $publish_node == 1) {
$new_node->status = $publish_node;
}
else {
$new_node->status = $node->status;
}
if (module_exists('revisioning')) {
$new_node->is_pending = FALSE;
$new_node->revision_moderation = FALSE;
}
$lang = globallink_get_drupal_locale_code($globallink->targetLocale);
if ($lang == '') {
throw new Exception('Locale Mapping not found.');
}
$new_node->language = $lang;
if ($target_title != '') {
$new_node->title = $target_title;
}
$new_node->tpt_skip = TRUE;
if (module_exists('metatag')) {
if (isset($translated_arr['metatag'])) {
$target_metatag_arr = $translated_arr['metatag'];
$metatags_names = array_keys($node->metatags[$node->language]);
$n_metatag =& $new_node->metatags[$new_node->language];
foreach ($metatags_names as $name) {
if (isset($target_metatag_arr[$name]) && isset($target_metatag_arr[$name][0])) {
$gl_obj = $target_metatag_arr[$name]['0'];
if (is_object($gl_obj)) {
$translated_content = $gl_obj->translatedContent;
}
else {
$translated_content = $gl_obj;
}
$n_metatag[$name] = array(
'value' => $translated_content,
);
}
}
}
}
if (module_exists('field_collection') && isset($translated_arr['field_collection'])) {
$t_fc_arr = $translated_arr['field_collection'];
$fcs = array_keys($t_fc_arr);
foreach ($fcs as $fc) {
if (isset($new_node->{$fc})) {
unset($new_node->{$fc});
}
}
}
$enabled = globallink_content_type_workbench_enabled($new_node->type);
if ($enabled) {
$current_value = variable_get('globallink_moderation_' . $new_node->type, FALSE);
if ($current_value) {
$new_node->workbench_moderation['current']->from_state = $current_value;
$new_node->workbench_moderation['current']->state = $current_value;
$new_node->workbench_moderation_state_current = $current_value;
$new_node->workbench_moderation_state_new = $current_value;
if ($current_value != workbench_moderation_state_published()) {
$new_node->workbench_moderation['current']->published = 0;
unset($new_node->workbench_moderation['published']);
}
}
}
if ($nodepath) {
if (module_exists('pathauto')) {
$new_node->path['pathauto'] = 0;
}
}
$success = globallink_save_translated_node_with_fields($node, $new_node, $translated_arr);
if ($success) {
if (module_exists('exclude_node_title')) {
$exclude = _exclude_node_title($node->nid);
if ($exclude) {
$exclude_list = variable_get('exclude_node_title_nid_list', array());
$exclude_list[] = $new_node->nid;
variable_set('exclude_node_title_nid_list', $exclude_list);
}
}
globallink_update_node_tnid($node->nid, $tnid);
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
}
else {
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
}
}
} 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);
}
return;
}
function globallink_get_translated_array($xml, $target_locale) {
if (is_null($xml) || !is_string($xml) || $xml == '') {
return array();
}
$dom = new DomDocument();
$dom->preserveWhiteSpace = FALSE;
$dom
->loadXML($xml);
$arr = array();
$contents = $dom
->getElementsByTagName('content');
foreach ($contents as $content) {
if (is_null($content->attributes)) {
continue;
}
foreach ($content->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'rid':
$arr['rid'] = $attr_node->value;
break;
case 'nid':
$arr['nid'] = $attr_node->value;
break;
case 'vid':
$arr['vid'] = $attr_node->value;
break;
}
}
}
$titles = $dom
->getElementsByTagName('title');
foreach ($titles as $title) {
$arr['title'] = $title->nodeValue;
}
$paths = $dom
->getElementsByTagName('path');
foreach ($paths as $path) {
$arr['path'] = $path->nodeValue;
}
$field_image = $dom
->getElementsByTagName('field_image');
foreach ($field_image as $attr) {
$field_image_obj = new GLFieldImage();
if (is_null($attr->attributes)) {
continue;
}
foreach ($attr->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'type':
if ($attr_node->value == 'title') {
$field_image_obj->title = $attr->nodeValue;
}
if ($attr_node->value == 'alt') {
$field_image_obj->alt = $attr->nodeValue;
}
continue 2;
case 'delta':
$field_image_obj->delta = $attr_node->value;
continue 2;
case 'field_name':
$field_image_obj->field_name = $attr_node->value;
continue 2;
case 'subfield':
$field_image_obj->subfield = $attr_node->value;
$field_image_obj->{$field_image_obj->subfield} = $attr->nodeValue;
$field_image_obj->type = 'file_entity';
continue 2;
case 'langcode':
case 'label':
case 'subfield_label':
$field_image_obj->{$attr_name} = $attr_node->value;
continue 2;
}
}
if (is_null($field_image_obj->delta)) {
$field_image_obj->delta = '0';
}
if (isset($field_image_obj->field_name)) {
if (!isset($arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta])) {
$arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta] = new GLFieldImage();
}
if (isset($field_image_obj->title)) {
$arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta]->title = $field_image_obj->title;
}
if (isset($field_image_obj->alt)) {
$arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta]->alt = $field_image_obj->alt;
}
}
else {
$arr[$field_image_obj->field_name][$target_locale][$field_image_obj->delta] = $field_image_obj;
}
}
$fields = $dom
->getElementsByTagName('field');
foreach ($fields as $field) {
$field_obj = new GLField();
$field_obj->type = 'field';
$field_obj->translatedContent = $field->nodeValue;
if (is_null($field->attributes)) {
continue;
}
foreach ($field->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'entity_type':
$field_obj->entityType = $attr_node->value;
continue 2;
case 'content_type':
$field_obj->contentType = $attr_node->value;
continue 2;
case 'parent_fc':
$field_obj->parentFCName = $attr_node->value;
continue 2;
case 'bundle':
$field_obj->bundle = $attr_node->value;
continue 2;
case 'entity_id':
$field_obj->entityId = $attr_node->value;
continue 2;
case 'field_name':
$field_obj->fieldName = $attr_node->value;
continue 2;
case 'label':
$field_obj->fieldLabel = $attr_node->value;
continue 2;
case 'delta':
$field_obj->delta = $attr_node->value;
continue 2;
case 'format':
$field_obj->format = $attr_node->value;
continue 2;
case 'langcode':
$field_obj->langcode = $attr_node->value;
continue 2;
}
}
if (is_null($field_obj->entityId)) {
$field_obj->entityId = '0';
}
if (is_null($field_obj->bundle)) {
$field_obj->bundle = $field_obj->fieldName;
}
if (is_null($field_obj->langcode)) {
$field_obj->langcode = LANGUAGE_NONE;
}
if (is_null($field_obj->delta)) {
$field_obj->delta = '0';
}
if ($field_obj->entityType == GLOBALLINK_ENTITY_TYPE_NODE) {
$arr[$field_obj->fieldName][$field_obj->langcode][$field_obj->delta] = $field_obj;
}
else {
$arr['field_collection'][$field_obj->parentFCName][$field_obj->bundle][$field_obj->entityId][$field_obj->fieldName][$field_obj->langcode][$field_obj->delta] = $field_obj;
}
}
$metatags = $dom
->getElementsByTagName('metatag');
foreach ($metatags as $metatag) {
$metatag_obj = new GLField();
$metatag_obj->type = 'metatag';
$metatag_obj->translatedContent = $metatag->nodeValue;
if (is_null($metatag->attributes)) {
continue;
}
foreach ($metatag->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'entity_type':
$metatag_obj->entityType = $attr_node->value;
continue 2;
case 'content_type':
$field_obj->contentType = $attr_node->value;
continue 2;
case 'bundle':
$field_obj->bundle = $attr_node->value;
continue 2;
case 'entity_id':
$metatag_obj->entityId = $attr_node->value;
continue 2;
case 'name':
$metatag_obj->fieldName = $attr_node->value;
continue 2;
case 'label':
$metatag_obj->fieldLabel = $attr_node->value;
continue 2;
}
}
if (is_null($metatag_obj->entityId)) {
$metatag_obj->entityId = '0';
}
if (is_null($metatag_obj->bundle)) {
$metatag_obj->bundle = $metatag_obj->fieldName;
}
$arr['metatag'][$metatag_obj->bundle][$metatag_obj->entityId] = $metatag_obj;
}
return $arr;
}
function globallink_get_active_submission_by_nid($nid) {
$query = db_select('globallink_document', 'gd');
$query
->join('globallink_submission', 'gs', 'gd.submission_rid = gs.rid');
$query
->condition('gd.object_id', $nid, '=');
$query
->condition('gd.entity_type', GLOBALLINK_ENTITY_TYPE_NODE, '=');
$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;
}
function globallink_node_save(&$node) {
try {
node_save($node);
return TRUE;
} catch (Exception $e) {
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);
return FALSE;
}
}
function globallink_save_translated_node_with_fields($node, &$tnode, $t_arr) {
$field_arr = field_info_instances(GLOBALLINK_ENTITY_TYPE_NODE, $node->type);
$keys = array_keys($field_arr);
$non_translate_field_arr = globallink_get_non_translatable_config_fields($node->type);
foreach ($keys as $field) {
$items = field_get_items(GLOBALLINK_ENTITY_TYPE_NODE, $node, $field);
if (!$items || array_key_exists($field, $non_translate_field_arr)) {
continue;
}
$field_def = field_read_field($field);
switch ($field_def['type']) {
case 'list_boolean':
case 'file':
case 'taxonomy_term_reference':
case 'field_collection':
continue 2;
default:
if (!isset($t_arr[$field])) {
continue 2;
}
$format = '';
$t_field_lang = LANGUAGE_NONE;
$t_target_lang = LANGUAGE_NONE;
if (key($t_arr[$field]) !== LANGUAGE_NONE) {
$t_field_lang = key($t_arr[$field]);
$t_target_lang = $tnode->language;
}
$t_field_arr = $t_arr[$field][$t_field_lang];
$body_summary = '';
if ($field_def['type'] == 'text_with_summary' && isset($t_arr[$field . '@summary'])) {
$body_summary = $t_arr[$field . '@summary'][$t_field_lang];
}
$arr = $tnode->{$field};
if ($field_def['type'] !== 'image') {
if (empty($arr[$t_target_lang]) && $t_target_lang != LANGUAGE_NONE) {
$arr[$t_target_lang] = $arr[$t_field_lang];
}
}
$updated = FALSE;
if ($field_def['type'] == 'image') {
$source_lang = field_language('node', $node, $field);
if (!isset($arr[$t_target_lang])) {
$arr[$t_target_lang] = $arr[$source_lang];
}
}
foreach ($arr[$t_target_lang] as $delta => $n_field) {
if (isset($t_field_arr[$delta])) {
$f_arr = array();
$gl_obj = $t_field_arr[$delta];
if ($field_def['type'] == 'image') {
if (is_object($gl_obj)) {
if (isset($gl_obj->title)) {
$arr[$t_target_lang][$delta]['title'] = $gl_obj->title;
}
if (isset($gl_obj->alt)) {
$arr[$t_target_lang][$delta]['alt'] = $gl_obj->alt;
}
}
}
else {
if (is_object($gl_obj)) {
$translated_content = $gl_obj->translatedContent;
}
else {
$translated_content = $gl_obj;
}
if (isset($n_field['format'])) {
$format = $n_field['format'];
}
if ($format != '') {
$f_arr['format'] = $format;
}
if ($field_def['type'] == 'link_field') {
$n_field['title'] = $translated_content;
$arr[$t_target_lang][$delta] = $n_field;
}
else {
$f_arr['value'] = $translated_content;
if ($field_def['type'] == 'text_with_summary' && !empty($body_summary)) {
$f_arr['summary'] = $body_summary[$delta]->translatedContent;
}
$arr[$t_target_lang][$delta] = $f_arr;
}
}
$updated = TRUE;
}
}
if ($updated) {
$tnode->{$field} = $arr;
}
}
}
$is_hook_enabled = variable_get('globallink_implementation_type', 0);
if ($is_hook_enabled == 1) {
globallink_update_node_hook($node, $tnode);
}
$success = globallink_node_save($tnode);
if ($success) {
if (module_exists('field_collection') && isset($t_arr['field_collection'])) {
globallink_save_field_collections($node, $tnode, $t_arr, $t_target_lang);
}
}
return $success;
}
function globallink_save_field_collections($node, $tnode, $t_arr, $target_locale) {
$field_arr = field_info_instances(GLOBALLINK_ENTITY_TYPE_NODE, $node->type);
$keys = array_keys($field_arr);
foreach ($keys as $field) {
$items = field_get_items(GLOBALLINK_ENTITY_TYPE_NODE, $node, $field);
if ($items) {
$field_def = field_read_field($field);
if ($field_def['type'] == 'field_collection') {
if (isset($t_arr['field_collection'][$field])) {
foreach ($items as $entity_id_arr) {
if (isset($entity_id_arr['value'])) {
$fc_entity_id = $entity_id_arr['value'];
globallink_save_field_collections_recursively(GLOBALLINK_ENTITY_TYPE_NODE, $tnode, $fc_entity_id, $t_arr['field_collection'][$field], $target_locale);
}
}
}
}
}
}
}
function globallink_save_field_collections_recursively($entity_type, $host_entity, $fc_entity_id, $translated_fc_arr, $target_locale) {
$field_collection_item_entity = entity_load_unchanged('field_collection_item', $fc_entity_id);
if (!$field_collection_item_entity) {
return;
}
$field_collection_name = $field_collection_item_entity->field_name;
$field_collection_item_array = get_object_vars($field_collection_item_entity);
$arr = array_keys($field_collection_item_array);
$new_field_collection_arr = array(
'field_name' => $field_collection_name,
);
foreach ($arr as $key) {
$fc_field_def = field_read_field($key);
if (!$fc_field_def || empty($fc_field_def) || !isset($fc_field_def['type'])) {
continue;
}
switch ($fc_field_def['type']) {
case 'list_boolean':
case 'file':
case 'taxonomy_term_reference':
continue 2;
case 'field_collection':
if (isset($field_collection_item_array[$key]) && isset($field_collection_item_array[$key][LANGUAGE_NONE])) {
$field_data_arr = $field_collection_item_array[$key][LANGUAGE_NONE];
$new_field_collection_arr[$key][$target_locale] = $field_data_arr;
}
break;
default:
$lang = key($field_collection_item_array[$key]);
if (!isset($field_collection_item_array[$key]) || !isset($field_collection_item_array[$key][$lang])) {
break;
}
$field_data_arr = $field_collection_item_array[$key][$lang];
foreach ($field_data_arr as $delta => $field_data) {
$trans_lang = key($translated_fc_arr[$field_collection_name][$fc_entity_id][$key]);
if (isset($translated_fc_arr[$field_collection_name][$fc_entity_id][$key][$trans_lang][$delta])) {
$gl_obj = $translated_fc_arr[$field_collection_name][$fc_entity_id][$key][$trans_lang][$delta];
if (is_object($gl_obj)) {
$translated_content = $gl_obj->translatedContent;
}
else {
$translated_content = $gl_obj;
}
if (isset($field_data['format'])) {
$format = $field_data['format'];
$new_field_collection_arr[$key][$target_locale][$delta] = array(
'value' => $translated_content,
'format' => $format,
);
}
else {
$new_field_collection_arr[$key][$target_locale][$delta] = array(
'value' => $translated_content,
);
}
}
else {
$new_field_collection_arr[$key][$target_locale][$delta] = $field_data;
}
}
}
}
if ($entity_type != GLOBALLINK_ENTITY_TYPE_NODE) {
$host_entity->tpt_skip = TRUE;
}
$field_collection_item = entity_create('field_collection_item', $new_field_collection_arr);
$field_collection_item
->setHostEntity($entity_type, $host_entity, $target_locale);
$field_collection_item
->save(TRUE);
field_attach_presave($entity_type, $host_entity);
field_attach_update($entity_type, $host_entity);
foreach ($arr as $key) {
$fc_field_def = field_read_field($key);
if ($fc_field_def && !empty($fc_field_def) && isset($fc_field_def['type'])) {
if ($fc_field_def['type'] == 'field_collection') {
$items = field_get_items('field_collection_item', $field_collection_item_entity, $key);
if ($items) {
foreach ($items as $entity_id_arr) {
if (isset($entity_id_arr['value'])) {
$fc_entity_id = $entity_id_arr['value'];
globallink_save_field_collections_recursively('field_collection_item', $field_collection_item, $fc_entity_id, $translated_fc_arr, $target_locale);
}
}
}
}
}
}
}
function globallink_get_xml($node, $target_arr, $tnid = NULL, $tvid = NULL, &$name = '', $for_display = FALSE) {
if (is_null($node)) {
return GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
}
elseif (!$node) {
return GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
}
if ($node && is_object($node)) {
if ($node->language != 'en') {
$name = 'Node_' . $node->nid . '_Non_English' . $name;
}
else {
$name = globallink_format_file_name($node->title) . '_' . $node->nid . $name;
}
$xml = globallink_generate_xml_document($node, $target_arr, $tnid, $tvid, $for_display);
return $xml;
}
return GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
}
function globallink_generate_xml_document($node, $target_arr, $tnid = NULL, $tvid = NULL, $for_display = FALSE) {
$is_hook_enabled = variable_get('globallink_implementation_type', 0);
try {
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = TRUE;
$root = $dom
->createElement('content');
$nid = $dom
->createAttribute('nid');
if ($tnid != NULL) {
$nid->value = $tnid;
}
else {
$nid->value = $node->nid;
}
$root
->appendChild($nid);
$vid = $dom
->createAttribute('vid');
if ($tvid != NULL) {
$vid->value = $tvid;
}
else {
$vid->value = $node->vid;
}
$root
->appendChild($vid);
$url = $dom
->createAttribute('pageUrl');
if (isset($node->path) && isset($node->path['source'])) {
$url->value = url($node->path['source'], array(
'absolute' => TRUE,
));
}
else {
$url->value = url('node/' . $node->nid, array(
'absolute' => TRUE,
));
}
$root
->appendChild($url);
$dom
->appendChild($root);
if ($for_display) {
globallink_insert_child_element($dom, $root, 'title', $node->title);
}
elseif (globallink_is_field_configured_for_translation(GLOBALLINK_ENTITY_TYPE_NODE, $node->type, 'title', $node->type) && $is_hook_enabled == 0) {
globallink_insert_child_element($dom, $root, 'title', $node->title);
}
elseif ($is_hook_enabled == 1) {
if (globallink_is_field_translatable($node, 'title', $target_arr)) {
globallink_insert_child_element($dom, $root, 'title', $node->title);
}
}
$path = path_load('node/' . $node->nid);
if (!empty($path)) {
globallink_insert_child_element($dom, $root, 'path', $path['alias']);
}
$field_arr = field_info_instances(GLOBALLINK_ENTITY_TYPE_NODE, $node->type);
$keys = array_keys($field_arr);
foreach ($keys as $field) {
$field_def = field_read_field($field);
$langcode = field_language(GLOBALLINK_ENTITY_TYPE_NODE, $node, $field, NULL);
$items = field_get_items(GLOBALLINK_ENTITY_TYPE_NODE, $node, $field, $langcode);
if ($items) {
$parent_fc = '';
if ($field_def['type'] == 'field_collection') {
$parent_fc = $field;
if (isset($items[0]['revision_id'])) {
$field_collection_item_entity = entity_load('field_collection_item', array(
$items[0]['value'],
), array(
'revision_id' => $items[0]['revision_id'],
));
}
}
globallink_traverse_fields_and_field_collections(GLOBALLINK_ENTITY_TYPE_NODE, $node->type, $parent_fc, $node->type, $node->nid, $items, $field, $dom, $root, $node, $target_arr, $is_hook_enabled, $langcode);
}
}
if (module_exists('metatag')) {
if (isset($node->metatags)) {
$ignore_metatags = [
'geo.position',
'icbm',
'rating',
'referrer',
'revisit-after',
'content-language',
];
if ($for_display) {
if (!empty($node->metatags[$node->language])) {
$metatags = $node->metatags[$node->language];
foreach ($metatags as $name => $value) {
if (isset($value['value']) && !is_array($value['value'])) {
if (in_array($name, $ignore_metatags)) {
continue;
}
globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
'entity_type' => GLOBALLINK_ENTITY_TYPE_NODE,
'name' => $name,
'label' => 'Metatag - ' . ucwords($name),
));
}
}
}
}
elseif (globallink_is_field_configured_for_translation(GLOBALLINK_ENTITY_TYPE_NODE, $node->type, 'metatags', $node->type)) {
if (!empty($node->metatags[$node->language])) {
$metatags = $node->metatags[$node->language];
foreach ($metatags as $name => $value) {
if (isset($value['value']) && !is_array($value['value'])) {
if (in_array($name, $ignore_metatags)) {
continue;
}
globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
'entity_type' => GLOBALLINK_ENTITY_TYPE_NODE,
'name' => $name,
'label' => 'Metatag - ' . ucwords($name),
));
}
}
}
}
}
}
} catch (Exception $e) {
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);
throw $e;
}
$root_element = $dom
->getElementsByTagName('content')
->item(0);
if (!$root_element
->hasChildNodes()) {
return FALSE;
}
return $dom
->saveXML();
}
function globallink_traverse_fields_and_field_collections($entity_type, $content_type, $parent_fc, $bundle, $entity_id, $items, $field, $dom, $root, $source_node, $target_arr, $is_hook_enabled = 0, $langcode = LANGUAGE_NONE) {
if (!$items) {
return;
}
$field_def = field_read_field($field);
$max_length = '0';
if (isset($field_def['settings']) && isset($field_def['settings']['max_length'])) {
$max_length = $field_def['settings']['max_length'];
}
switch ($field_def['type']) {
case 'list_boolean':
case 'file':
case 'entityreference':
case 'taxonomy_term_reference':
break;
case 'field_collection':
if (!module_exists('field_collection')) {
break;
}
foreach ($items as $entity_id_arr) {
if (!isset($entity_id_arr['value'])) {
continue;
}
$fc_entity_id = $entity_id_arr['value'];
$field_collection_item_entity_arr = array();
if (isset($entity_id_arr['revision_id'])) {
$field_collection_item_entity_arr = entity_load('field_collection_item', array(
$fc_entity_id,
), array(
'revision_id' => $entity_id_arr['revision_id'],
));
}
else {
$field_collection_item_entity_arr = entity_load('field_collection_item', array(
$fc_entity_id,
));
}
if (!$field_collection_item_entity_arr || !is_array($field_collection_item_entity_arr) || sizeof($field_collection_item_entity_arr) < 1) {
continue;
}
$field_collection_item_entity = $field_collection_item_entity_arr[$fc_entity_id];
$field_collection_name = $field_collection_item_entity->field_name;
$field_collection_item_array = get_object_vars($field_collection_item_entity);
$arr = array_keys($field_collection_item_array);
foreach ($arr as $key) {
$fc_field_def = field_read_field($key);
if ($fc_field_def && !empty($fc_field_def) && isset($fc_field_def['type'])) {
if ($fc_field_def['type'] != 'list_boolean' && $fc_field_def['type'] != 'file' && $fc_field_def['type'] != 'taxonomy_term_reference') {
$fc_item = field_get_items('field_collection_item', $field_collection_item_entity, $key);
if ($fc_item) {
globallink_traverse_fields_and_field_collections('field_collection_item', $content_type, $parent_fc, $field_collection_name, $fc_entity_id, $fc_item, $key, $dom, $root, $source_node, $target_arr, $is_hook_enabled);
}
}
}
}
}
break;
default:
if ($is_hook_enabled == 0) {
if (!globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
break;
}
foreach ($items as $delta => $item) {
if (isset($item['value']) && is_string($item['value'])) {
$f_label = field_info_instance($entity_type, $field, $bundle);
$f_value = $item['value'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $field,
'label' => $f_label['label'],
'delta' => $delta,
'format' => $f_format,
'langcode' => $langcode,
'max_length' => $max_length,
));
$field_info_all = field_info_field($field);
$sum_field = '';
if ($field_info_all['type'] == 'text_with_summary' && globallink_is_field_configured_for_translation($entity_type, $bundle, $field . '@summary', $content_type)) {
$sum_field = $field . '@summary';
}
if (isset($item['summary']) && is_string($item['summary']) && !empty($sum_field)) {
if (globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
$f_label = $field . ' Summary';
$f_value = $item['summary'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $sum_field,
'label' => $f_label,
'delta' => $delta,
'format' => $f_format,
'langcode' => $langcode,
'max_length' => $max_length,
));
}
}
}
elseif ($field_def['type'] == 'link_field' && isset($item['title']) && is_string($item['title'])) {
$f_label = field_info_instance($entity_type, $field, $bundle);
$f_value = $item['title'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $field,
'label' => $f_label['label'],
'delta' => $delta,
'format' => $f_format,
'langcode' => $langcode,
'max_length' => $max_length,
));
}
elseif ($field_def['type'] == 'image') {
$f_label = field_info_instance($entity_type, $field, $bundle);
if (isset($item['alt'])) {
$f_value = $item['alt'];
globallink_insert_child_element($dom, $root, 'field_image', $f_value, array(
'field_name' => $field,
'type' => 'alt',
'delta' => $delta,
'max_length' => $max_length,
));
}
if (isset($item['title'])) {
$f_value = $item['title'];
globallink_insert_child_element($dom, $root, 'field_image', $f_value, array(
'field_name' => $field,
'type' => 'title',
'delta' => $delta,
'max_length' => $max_length,
));
}
}
}
}
else {
if (!globallink_is_field_translatable($source_node, $field, $target_arr)) {
break;
}
foreach ($items as $delta => $item) {
if (isset($item['value']) && is_string($item['value'])) {
$f_label = field_info_instance($entity_type, $field, $bundle);
$f_value = $item['value'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $field,
'label' => $f_label['label'],
'delta' => $delta,
'format' => $f_format,
'langcode' => $langcode,
'max_length' => $max_length,
));
$field_info_all = field_info_field($field);
$sum_field = '';
if ($field_info_all['type'] == 'text_with_summary' && globallink_is_field_configured_for_translation($entity_type, $bundle, $field . '@summary', $content_type)) {
$sum_field = $field . '@summary';
}
if (isset($item['summary']) && is_string($item['summary']) && !empty($sum_field)) {
if (globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
$f_label = $field . ' Summary';
$f_value = $item['summary'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $sum_field,
'label' => $f_label,
'delta' => $delta,
'format' => $f_format,
'langcode' => $langcode,
'max_length' => $max_length,
));
}
}
}
elseif ($field_def['type'] == 'link_field' && isset($item['title']) && is_string($item['title'])) {
$f_label = field_info_instance($entity_type, $field, $bundle);
$f_value = $item['title'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $field,
'label' => $f_label['label'],
'delta' => $delta,
'format' => $f_format,
'langcode' => $langcode,
'max_length' => $max_length,
));
}
}
}
}
}
function globallink_update_non_translatable_field($node, &$tnode) {
$field_arr = globallink_get_non_translatable_config_fields($node->type);
foreach ($field_arr as $field) {
if (isset($field->field_name)) {
$field_name = $field->field_name;
if (isset($tnode->{$field_name})) {
$tnode->{$field_name} = $node->{$field_name};
}
}
}
}
function globallink_check_node_deleted($nid) {
$node = node_load($nid);
if (!$node || is_null($node) || !is_object($node)) {
return TRUE;
}
return FALSE;
}