You are here

function globallink_all_active_form_submit in GlobalLink Connect for Drupal 7.7

Handles submission of active entity form.

File

./globallink_workbench_all_active_submissions.inc, line 701

Code

function globallink_all_active_form_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  $pd4 = globallink_get_project_director_details();

  //$rids = array_filter($form_state['values']['table']);
  module_load_include('inc', 'globallink', 'globallink_entity');
  $submission_rid = arg(4);
  $target_locale = arg(5);
  $entity_type = arg(3);
  $select_qry = db_select('globallink_submission', 'gs');
  $select_qry
    ->fields('gs', array(
    'status',
  ));
  $select_qry
    ->condition('rid', $submission_rid, '=');
  $result_set = $select_qry
    ->execute();
  foreach ($result_set as $res) {
    $submission_status = $res->status;
  }
  $query = db_select('globallink_document', 'gd');
  $query
    ->fields('gd', array(
    'rid',
  ));
  $query
    ->condition('submission_rid', $submission_rid, '=');
  $query
    ->condition('target_lang_code', $target_locale, '=');
  if ($submission_status == 'Translation Imported') {
    $query
      ->condition('target_status', array(
      GLOBALLINK_STATUS_TRANSLATION_IMPORTED,
      GLOBALLINK_STATUS_TRANSLATION_SOURCE_CANCELLED,
      GLOBALLINK_STATUS_TRANSLATION_ERROR,
    ), 'IN');
  }
  else {
    $query
      ->condition('target_status', array(
      GLOBALLINK_STATUS_TRANSLATION_SENT,
      GLOBALLINK_STATUS_TRANSLATION_ERROR,
      GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
    ), 'IN');
  }
  if ($entity_type != 'all') {
    $query
      ->condition('entity_type', $entity_type, '=');
  }
  $results = $query
    ->execute();
  if (!$results
    ->rowCount()) {
    drupal_set_message("No Results available");
  }
  else {
    foreach ($results as $value) {
      $document_rids[] = $value->rid;
    }
    switch ($op) {
      case t('Re-Import'):
      case t('Import'):

        //will work as background process if module available, else regular form submit
        try {
          if (module_exists('background_process') && module_exists('background_batch')) {
            $operations[] = array(
              'globallink_background_import',
              array(
                $pd4,
                $submission_rid,
                $document_rids,
              ),
            );
            $batch = array(
              'title' => t('Importing ' . $submission_rid . ' for locale ' . $target_locale),
              'operations' => $operations,
              'error_message' => t('Import has encountered an error for ' . $submission_rid . ' for locale ' . $target_locale),
              'file' => drupal_get_path('module', 'globallink') . '/globallink_background_jobs.inc',
            );
            $batch['progressive'] = FALSE;
            batch_set($batch);
            background_batch_process_batch('admin/globallink-translations/workbench');
            drupal_set_message('Translated files are being imported in the background. Please refresh the page for the progress. For more information check watchdog.');
          }
          else {
            module_load_include('inc', 'globallink', 'globallink_background_jobs');
            globallink_background_import($pd4, $submission_rid, $document_rids);
            drupal_set_message('The translated files have been imported');
          }
        } 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);
          form_set_error('', t('Error: @message', array(
            '@message' => $e
              ->getMessage(),
          )));
        }
        break;
      case t('Sync'):

        //will be a background process if module background process available.
        try {
          if (module_exists('background_process') && module_exists('background_batch')) {
            $operations[] = array(
              'globallink_background_pull',
              array(
                $pd4,
                $submission_rid,
                $document_rids,
              ),
            );
            $batch = array(
              'title' => t('Sync translations'),
              'operations' => $operations,
              'error_message' => t('Sync has encountered an error'),
              'file' => drupal_get_path('module', 'globallink') . '/globallink_background_jobs.inc',
            );
            batch_set($batch);
            $batch['progressive'] = FALSE;
            background_batch_process_batch('admin/globallink-translations/workbench');
            drupal_set_message('Translated files are being synced in the background. Please refresh the page for the progress.');
          }
          else {
            module_load_include('inc', 'globallink', 'globallink_background_jobs');
            globallink_background_pull($pd4, $submission_rid, $document_rids);
            drupal_set_message('Documents have been synced to the latest available status');
          }
        } catch (SoapFault $se) {
          watchdog(GLOBALLINK_MODULE, '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) {
          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);
          form_set_error('', t('Error: @message', array(
            '@message' => $e
              ->getMessage(),
          )));
        }
        break;
    }
  }
}