View source
<?php
function globallink_get_all_translatable_file_types() {
$entity_info = entity_get_info('file');
$arr = array();
if (isset($entity_info) && isset($entity_info['bundles'])) {
$bundles = $entity_info['bundles'];
foreach ($bundles as $key => $bundle) {
$arr['file:' . $key] = $bundle['label'];
}
asort($arr);
}
return $arr;
}
function globallink_get_sent_file_entity_rows_by_fid($fid) {
$result = db_select('globallink_document', 'gd')
->fields('gd')
->condition('object_id', $fid, '=')
->condition('entity_type', GLOBALLINK_ENTITY_TYPE_FILE_ENTITY, '=')
->condition('target_status', array(
GLOBALLINK_STATUS_TRANSLATION_SENT,
GLOBALLINK_STATUS_TRANSLATION_ERROR,
GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
), 'IN')
->execute();
$rows = array();
foreach ($result as $row) {
$rows[] = $row;
}
return $rows;
}
function globallink_send_file_entity_for_translations($fids, $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 ($fids as $fid) {
$rows = globallink_get_sent_file_entity_rows_by_fid($fid);
$target_arr = $target_locale_arr;
$file_info = globallink_file_entity_get_info($fid);
$title = $file_info->filename;
foreach ($rows as $row) {
if (array_search($row->target, $target_locale_arr)) {
unset($target_arr[$row->target]);
watchdog(GLOBALLINK_MODULE, 'Skipping File Entity Id - %id for locales %locale', array(
'%id' => $fid,
'%locale' => $row->target_lang_code,
), WATCHDOG_DEBUG);
}
}
if (empty($target_arr)) {
continue;
}
$xml = globallink_get_file_entity_xml($fid);
$name = "File_Entity_" . $fid . ".xml";
$globallink = new GlobalLink();
$globallink->type = GLOBALLINK_ENTITY_TYPE_FILE_ENTITY;
$globallink->metadata = GLOBALLINK_ENTITY_TYPE_FILE_ENTITY;
$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 = $fid;
$globallink->submissionInstructions = $submission_details['instructions'] . "\nSubmitter: " . $submitter;
$globallink_arr[GLOBALLINK_ENTITY_TYPE_FILE_ENTITY][] = $globallink;
}
return $globallink_arr;
}
function globallink_get_file_entity_xml($fid, $path = NULL) {
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = TRUE;
$root = $dom
->createElement('content');
$dom
->appendChild($root);
$id = $dom
->createAttribute('fid');
$id->value = $fid;
$root
->appendChild($id);
$file_entity = entity_load('file', FALSE, array(
'fid' => $fid,
));
if (module_exists('metatag')) {
$lang = entity_language('file', $file_entity[$fid]);
if (isset($file_entity[$fid]->metatags)) {
if (globallink_is_field_configured_for_translation('file', 'file:' . $file_entity[$fid]->type, 'metatags', 'file:' . $file_entity[$fid]->type)) {
if (!empty($file_entity[$fid]->metatags[$lang])) {
$metatags = $file_entity[$fid]->metatags[$lang];
foreach ($metatags as $name => $value) {
if (!empty($value['value']) && !is_array($value['value'])) {
globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
'entity_type' => 'file',
'name' => $name,
'label' => 'Metatag - ' . ucwords($name),
));
}
}
}
}
}
}
$fields = globallink_file_entity_get_fields($file_entity[$fid]->type);
$configured_fields = globallink_get_configured_fields_for_file($file_entity[$fid]->type);
foreach ($fields as $field) {
if (empty($file_entity[$fid]->{$field})) {
continue;
}
if (array_key_exists($field, $configured_fields) && $configured_fields[$field]->translatable == 1) {
$field_values = field_get_items('file', $file_entity[$fid], $field);
$field_info = field_info_field($field);
if (is_array($field_values) && !empty($field_values)) {
foreach ($field_values as $delta => $values) {
if ($field_info['type'] == 'link_field') {
globallink_insert_child_element($dom, $root, 'field', $values['title'], array(
'field' => $field,
'delta' => $delta,
));
}
elseif ($field_info['type'] == 'image' && !module_exists('file_entity')) {
if (isset($values['alt'])) {
globallink_insert_child_element($dom, $root, 'field_image', $values['alt'], array(
'field_name' => $field,
'type' => 'alt',
'delta' => $delta,
));
}
if (isset($values['title'])) {
globallink_insert_child_element($dom, $root, 'field_image', $values['title'], array(
'field_name' => $field,
'type' => 'title',
'delta' => $delta,
));
}
}
elseif ($field_info['type'] == 'image' && module_exists('file_entity')) {
$parent_info = field_info_instance('file', $field, $file_entity->type);
$file_entity_info = field_info_field($field);
$file_fields = field_info_instances('file', $file_entity_info['type']);
foreach ($file_fields as $file_field) {
$f_value = field_get_items('file', (object) $values, $file_field['field_name']);
if (empty($f_value)) {
continue;
}
globallink_insert_child_element($dom, $root, 'field_image', $f_value[0]['value'], array(
'field_name' => $field,
'subfield' => $file_field['field_name'],
'delta' => $delta,
'label' => $parent_info['label'],
'subfield_label' => $file_field['label'],
));
}
}
elseif (!empty($values['value'])) {
globallink_insert_child_element($dom, $root, 'field', $values['value'], array(
'field' => $field,
'delta' => $delta,
));
}
}
}
elseif (!empty($field_values) && is_string($field_values)) {
globallink_insert_child_element($dom, $root, 'field', $field_values, array(
'field' => $field,
));
}
}
}
$xml = $dom
->saveXML();
return $xml;
}
function globallink_file_entity_get_fields($bundle) {
$fields = array();
$info = entity_get_info('file');
foreach ($info['bundles'] as $bundle_name => $bundle_info) {
if ($bundle_name == $bundle) {
foreach (field_info_instances('file', $bundle_name) as $field) {
$fields[] = $field['field_name'];
}
}
}
return $fields;
}
function globallink_get_configured_fields_for_file($file_type) {
$result = db_select('globallink_field_config', 'gfc')
->fields('gfc')
->condition('content_type', 'file:' . $file_type, '=')
->condition('entity_type', 'file', '=')
->execute();
$arr = array();
foreach ($result as $row) {
$arr[$row->field_name] = $row;
}
return $arr;
}
function globallink_file_entity_import(&$globallink) {
module_load_include('inc', 'globallink', 'globallink');
$target_xml = $globallink->targetXML;
try {
if (empty($target_xml)) {
throw new Exception("Target XML is missing.");
}
$target_locale = globallink_get_drupal_locale_code($globallink->targetLocale);
$source_locale = globallink_get_drupal_locale_code($globallink->sourceLocale);
$translated_arr = globallink_file_entity_get_translated_array($target_xml);
$fid = $translated_arr['fid'];
unset($translated_arr['fid']);
$entity_arr = entity_load('file', FALSE, array(
'fid' => $fid,
));
$key = key($entity_arr);
$file_entity = $entity_arr[$key];
if (empty($file_entity)) {
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
$globallink->sourceDeleted = TRUE;
return;
}
if (module_exists('metatag')) {
if (isset($translated_arr['metatag'])) {
$lang = entity_language('file', $file_entity);
$target_metatag_arr = $translated_arr['metatag'];
if (!empty($file_entity->metatags[$lang])) {
$metatags_names = array_keys($file_entity->metatags[$lang]);
$n_metatag =& $file_entity->metatags;
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[$target_locale][$name] = array(
'value' => $translated_content,
);
}
}
}
}
}
$fields = globallink_file_entity_get_fields($file_entity->type);
foreach ($fields as $field) {
$field_def = field_read_field($field);
$t_field_lang = LANGUAGE_NONE;
if (empty($translated_arr[$field])) {
if (isset($file_entity->{$field}[$source_locale])) {
$file_entity->{$field}[$target_locale] = $file_entity->{$field}[$source_locale];
}
else {
$file_entity->{$field}[$target_locale] = $file_entity->{$field}[$t_field_lang];
}
continue;
}
if (empty($file_entity->{$field}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
if (isset($file_entity->{$field}[$t_field_lang])) {
$file_entity->{$field}[$target_locale] = $file_entity->{$field}[$t_field_lang];
}
else {
$file_entity->{$field}[$target_locale] = $file_entity->{$field}[$source_locale];
}
}
if (key($translated_arr[$field]) !== LANGUAGE_NONE) {
$t_field_lang = key($translated_arr[$field]);
}
$t_field_arr = $translated_arr[$field][$t_field_lang];
foreach ($file_entity->{$field}[$target_locale] as $delta => $fp_field) {
if (empty($t_field_arr[$delta])) {
continue;
}
if (isset($t_field_arr[$delta]->translatedContent)) {
$translation = $t_field_arr[$delta]->translatedContent;
}
if ($field_def['type'] == 'link_field') {
$file_entity->{$field}[$target_locale][$delta]['title'] = $translation;
}
elseif ($field_def['type'] == 'image') {
if (isset($t_field_arr[$delta]->alt)) {
$file_entity->{$field}[$target_locale][$delta]['alt'] = $t_field_arr[$delta]->alt;
}
if (isset($t_field_arr[$delta]->title)) {
$file_entity->{$field}[$target_locale][$delta]['title'] = $t_field_arr[$delta]->title;
}
}
else {
$file_entity->{$field}[$target_locale][$delta]['value'] = $translation;
}
}
}
$file_entity->revision = 1;
$file_entity->translations->data[$target_locale] = array(
'language' => $target_locale,
'source' => $source_locale,
'uid' => $file_entity->uid,
'status' => variable_get('globallink_publish_node', 0),
'translate' => 0,
);
$file_entity->translations->hook[$target_locale] = array(
'hook' => 'insert',
'date' => NULL,
);
file_save($file_entity);
} catch (SoapFault $se) {
$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_file_entity_get_translated_array($xml) {
if (is_null($xml) || !is_string($xml) || $xml == '') {
return array();
}
$dom = new DomDocument();
$dom->preserveWhiteSpace = FALSE;
$dom
->loadXML($xml);
$arr = array();
$titles = $dom
->getElementsByTagName('title');
foreach ($titles as $title) {
$arr['title'] = $title->nodeValue;
}
$contents = $dom
->getElementsByTagName('content');
$fid = '';
foreach ($contents as $content) {
if (!is_null($content->attributes)) {
foreach ($content->attributes as $attr_name => $attr_node) {
if ($attr_name == 'fid') {
$fid = $attr_node->value;
}
}
}
}
if ($fid == '') {
return array();
}
$arr['fid'] = $fid;
$field_image = $dom
->getElementsByTagName('field_image');
foreach ($field_image as $attr) {
$field_image_object = new GLFieldImage();
if (!is_null($attr->attributes)) {
foreach ($attr->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'type':
if ($attr_node->value == 'title') {
$field_image_object->title = $attr->nodeValue;
}
elseif ($attr_node->value == 'alt') {
$field_image_object->alt = $attr->nodeValue;
}
continue 2;
case 'delta':
$field_image_object->delta = $attr_node->value;
continue 2;
case 'field_name':
$field_image_object->field_name = $attr_node->value;
continue 2;
case 'subfield':
$field_image_object->subfield = $attr_node->value;
$field_image_object->{$field_image_object->subfield} = $attr->nodeValue;
$field_image_object->type = 'file_entity';
continue 2;
case 'langcode':
$field_image_object->langcode = $attr_node->value;
case 'label':
case 'subfield_label':
$field_image_object->{$attr_name} = $attr_node->value;
continue 2;
}
}
if (is_null($field_image_object->delta)) {
$field_image_object->delta = '0';
}
if (isset($field_image_object->field_name)) {
if (!isset($arr[$field_image_object->field_name][$field_image_object->langcode][$field_image_object->delta])) {
$arr[$field_image_object->field_name][$field_image_object->langcode][$field_image_object->delta] = new GLFieldImage();
}
if (isset($field_image_object->title)) {
$arr[$field_image_object->field_name][$field_image_object->langcode][$field_image_object->delta]->title = $field_image_object->title;
}
if (isset($field_image_object->alt)) {
$arr[$field_image_object->field_name][$field_image_object->langcode][$field_image_object->delta]->alt = $field_image_object->alt;
}
}
else {
$arr[$field_image_object->field_name][$field_image_object->langcode][$field_image_object->delta] = $field_image_object;
}
}
}
$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 'field':
$field_obj->fieldName = $attr_node->value;
continue 2;
case 'delta':
$field_obj->delta = $attr_node->value;
continue 2;
case 'langcode':
$field_obj->langcode = $attr_node->value;
continue 2;
}
}
if (is_null($field_obj->langcode)) {
$field_obj->langcode = LANGUAGE_NONE;
}
if (is_null($field_obj->delta)) {
$field_obj->delta = '0';
}
$arr[$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_file_entity_get_info($fid) {
$query = db_select('file_managed', 'fm')
->fields('fm', array(
'filename',
))
->condition('fid', $fid, '=');
$results = $query
->execute();
$arr = new stdClass();
foreach ($results as $result) {
$arr = $result;
}
return $arr;
}
function globallink_file_entity_check_delete($fid) {
$entity = entity_load('file', FALSE, array(
'fid' => $fid,
));
if (empty($entity)) {
return TRUE;
}
return FALSE;
}
function globallink_file_entity_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_FILE_ENTITY, '=');
$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;
}