You are here

function globallink_beans_get_translated in GlobalLink Connect for Drupal 7.6

Gets number of translated beans.

Parameters

string $pd4: The project director details.

array $globallink_arr: Array of GlobalLink objects.

Return value

int The number of translated beans.

1 call to globallink_beans_get_translated()
globallink_beans_receive_form_submit in globallink_beans/globallink_beans_receive.inc
Handles beans form submission.

File

globallink_beans/globallink_beans.inc, line 950

Code

function globallink_beans_get_translated($pd4, &$globallink_arr) {
  module_load_include('inc', 'globallink', 'globallink');
  $count = 0;
  foreach ($globallink_arr as $globallink) {
    $target_xml = globallink_download_target_resource($pd4, $globallink->targetTicket);
    if ($globallink->sourceDeleted || empty($target_xml)) {
      continue;
    }
    $count++;
    $target_locale = globallink_get_drupal_locale_code($globallink->targetLocale);
    $source_locale = globallink_get_drupal_locale_code($globallink->sourceLocale);
    $translated_arr = globallink_beans_get_translated_array($target_xml);
    try {
      $bnid = $translated_arr['object_id'];
      unset($translated_arr['object_id']);
      $entity_arr = entity_load('bean', array(
        $bnid,
      ));
      $key = key($entity_arr);
      $entity = $entity_arr[$key];
      if (empty($entity)) {
        globallink_beans_update_status($globallink, 'Source Deleted');
        continue;
      }
      if (module_exists('metatag')) {
        if (isset($translated_arr['metatag'])) {
          $lang = entity_language('bean', $entity);
          $target_metatag_arr = $translated_arr['metatag'];
          if (!empty($entity->metatags[$lang])) {
            $metatags_names = array_keys($entity->metatags[$lang]);
            $n_metatag =& $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_bean_get_fields($entity->type);
      foreach ($fields as $field) {
        $field_def = field_read_field($field);
        $t_field_lang = LANGUAGE_NONE;
        if (empty($translated_arr[$field])) {
          if (isset($entity->{$field}[$source_locale])) {
            $entity->{$field}[$target_locale] = $entity->{$field}[$source_locale];
          }
          continue;
        }
        if (key($translated_arr[$field]) != LANGUAGE_NONE) {
          $t_field_lang = key($translated_arr[$field]);
        }
        if ($field_def['type'] == 'image') {
          if (!isset($entity->{$field}[$target_locale])) {
            $entity->{$field}[$target_locale] = $entity->{$field}[$source_locale];
          }
        }
        if (empty($entity->{$field}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
          $entity->{$field}[$target_locale] = $entity->{$field}[$t_field_lang];
        }
        $t_field_arr = $translated_arr[$field][$source_locale];
        foreach ($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') {
            $entity->{$field}[$target_locale][$delta]['title'] = $translation;
          }
          elseif ($field_def['type'] == 'image') {
            if (isset($t_field_arr[$delta]->alt)) {
              $entity->{$field}[$target_locale][$delta]['alt'] = $t_field_arr[$delta]->alt;
            }
            if (isset($t_field_arr[$delta]->title)) {
              $entity->{$field}[$target_locale][$delta]['title'] = $t_field_arr[$delta]->title;
            }
          }
          else {
            $entity->{$field}[$target_locale][$delta]['value'] = $translation;
          }
        }
      }
      $entity->revision = 1;
      $entity->translations->data[$target_locale] = array(
        'language' => $target_locale,
        // Target language
        'source' => $source_locale,
        // Source language
        'uid' => $entity->uid,
        'status' => variable_get('globallink_publish_node', 0),
        // publish translation
        'translate' => 0,
        // Translation out of date
        'created' => $entity->created,
      );
      $entity->translations->hook[$target_locale] = array(
        'hook' => 'insert',
        'date' => NULL,
      );
      $result = bean_save($entity);
      if ($result != FALSE) {
        if (module_exists('field_collection') && isset($translated_arr['field_collection'])) {
          globallink_beans_save_field_collections($entity, $translated_arr, $target_locale);
        }
      }
      globallink_beans_update_status($globallink);
      globallink_send_download_confirmation($globallink->targetTicket, $pd4);
    } catch (SoapFault $se) {
      $count--;
      globallink_beans_update_status($globallink, 'Error');
      watchdog('GlobalLink', 'SOAP Exception - %function - Code[%faultcode], Message[%faultstring]', array(
        '%function' => __FUNCTION__,
        '%faultcode' => $se->faultcode,
        '%faultstring' => $se->faultstring,
      ), WATCHDOG_ERROR);
      form_set_error('', t('Web Services Error: @faultcode - @faultstring', array(
        '@faultcode' => $se->faultcode,
        '@faultstring' => $se->faultstring,
      )));
    } catch (Exception $e) {
      $count--;
      globallink_beans_update_status($globallink, 'Error');
      watchdog('GlobalLink', '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);
      form_set_error('', t('Error: @message', array(
        '@message' => $e
          ->getMessage(),
      )));
    }
  }
  return $count;
}