You are here

function globallink_create_submission_form_submit in GlobalLink Connect for Drupal 7.7

Handles GlobalLink form submission.

File

./globallink_send_translations.inc, line 1887

Code

function globallink_create_submission_form_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  module_load_include('inc', 'globallink', 'globallink_node');
  module_load_include('inc', 'globallink', 'globallink_settings');
  $submission_details = array();
  $pd4 = globallink_get_project_director_details();
  if ($op != t('Request Translation')) {
    return;
  }
  try {
    $content_ty = array();
    if (isset($_SESSION['nids'])) {
      $content_ty['nids'] = $_SESSION['nids'];
    }
    if (isset($_SESSION['eids'])) {
      $content_ty['eids'] = $_SESSION['eids'];
    }
    if (isset($_SESSION['bids'])) {
      $content_ty['bids'] = $_SESSION['bids'];
    }
    if (isset($_SESSION['int_ids'])) {
      $content_ty['int_ids'] = $_SESSION['int_ids'];
    }
    if (isset($_SESSION['fpids'])) {
      $content_ty['fpids'] = $_SESSION['fpids'];
    }
    if (isset($_SESSION['mids'])) {
      $content_ty['mids'] = $_SESSION['mids'];
    }
    if (isset($_SESSION['tids'])) {
      $content_ty['tids'] = $_SESSION['tids'];
    }
    if (isset($_SESSION['wfids'])) {
      $content_ty['wfids'] = $_SESSION['wfids'];
    }
    if (isset($_SESSION['bnids'])) {
      $content_ty['bnids'] = $_SESSION['bnids'];
    }
    if (isset($_SESSION['fids'])) {
      $content_ty['fids'] = $_SESSION['fids'];
    }
    if (isset($_SESSION['pids'])) {
      $content_ty['pids'] = $_SESSION['pids'];
    }
    $parents = array();
    if (isset($_SESSION['wfids'])) {
      $wfids = $_SESSION['wfids'];
      if (count($wfids) > 0) {
        foreach ($wfids as $value) {
          $key = $form_state['complete form']['table-content']['#options']['wfids|' . $value]['nid'];
          if (isset($parents[$key])) {
            $l_arr = $parents[$key];
            array_push($l_arr, $value);
            $parents[$key] = $l_arr;
          }
          else {
            $l_arr = array();
            array_push($l_arr, $value);
            $parents[$key] = $l_arr;
          }
        }
      }
    }
    $submission_name = $form_state['values']['submission_name'];
    $source_locale = $form_state['values']['submission_source_locale'];
    $target_locale_arr = $form_state['values']['submission_target_locale'];
    $submission_details['instructions'] = $form_state['values']['submission_instructions'];
    $submission_details['submitter'] = $form_state['values']['submission_submitter'];
    $submission_priority = $form_state['values']['submission_pd_priority'];
    foreach ($target_locale_arr as $key => $target) {
      if ($target == FALSE) {
        unset($target_locale_arr[$key]);
      }
    }
    watchdog(GLOBALLINK_MODULE, 'Processing User submission - %submission_name', array(
      '%submission_name' => $submission_name,
    ), WATCHDOG_INFO);
    $due_date = globallink_convert_date_to_timestamp($form_state['values']['submission_due_date']);
    $project_code = $form_state['values']['submission_pd_project'];
    $globallink_arr = array();
    foreach ($content_ty as $content => $val) {
      if (isset($val) && is_array($val)) {
        foreach ($val as $value) {
          $globallink = new stdClass();
          $globallink->id = $value;
          $globallink->type = $content;
          $globallink_arr[] = $globallink;
        }
      }
    }
    $items = array();
    $numberOfitems = variable_get('globallink_batch', 1000);
    $chunked_arr = array_chunk($globallink_arr, $numberOfitems);
    $chunked_count = count($chunked_arr);
    $suffix = 1;
    $name = $submission_name;
    foreach ($chunked_arr as $gl_arr) {
      foreach ($gl_arr as $globallink) {
        $items[$globallink->type][] = $globallink->id;
      }
      if ($chunked_count > 1) {
        $submission_name = $name . '_' . $suffix;
        $suffix++;
      }
      if (module_exists('background_process') && module_exists('background_batch')) {
        $operations[] = array(
          'globallink_background_send',
          array(
            $items,
            $pd4,
            $submission_name,
            $due_date,
            $project_code,
            $source_locale,
            $target_locale_arr,
            $submission_details,
            $submission_priority,
            $parents,
          ),
        );
      }
      else {
        module_load_include('inc', 'globallink', 'globallink_background_jobs');
        globallink_background_send($items, $pd4, $submission_name, $due_date, $project_code, $source_locale, $target_locale_arr, $submission_details, $submission_priority, $parents);
        drupal_set_message(t('Content has been successfully sent for translations.'));
      }
    }
    if (isset($operations)) {
      $batch = array(
        'title' => t('Sending GlobalLink submissions for translation'),
        'operations' => $operations,
        'error_message' => t('GlobalLink Error.'),
        'file' => drupal_get_path('module', 'globallink') . '/globallink_background_jobs.inc',
      );
      batch_set($batch);
      $batch['progressive'] = FALSE;
      background_batch_process_batch('admin/globallink-translations/activeSubmissions');
      drupal_set_message('The submissions are being sent. The status will be updated on the dashboard, once they are sent out. For more information check watchdog');
    }
    unset($_SESSION['nids']);
    unset($_SESSION['eids']);
    unset($_SESSION['bids']);
    unset($_SESSION['int_ids']);
    unset($_SESSION['fpids']);
    unset($_SESSION['mids']);
    unset($_SESSION['tids']);
    unset($_SESSION['wfids']);
    unset($_SESSION['bnids']);
    unset($_SESSION['fids']);
    unset($_SESSION['pids']);
    unset($_SESSION['eck']);
    if (isset($_GET['rnid'])) {
      unset($_GET['rnid']);
    }
    ctools_include('ajax');
    ctools_include('modal');
    if (!empty($form_state['submitted'])) {
      $commands[] = ctools_modal_command_dismiss();
      $commands[] = ctools_ajax_command_reload();
      print ajax_render($commands);
      drupal_exit();
    }
  } 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(),
    )));
  }
}