You are here

globallink_workbench_all_submissions.inc in GlobalLink Connect for Drupal 7.7

File

globallink_workbench_all_submissions.inc
View source
<?php

/**
 * Builds forms on GlobalLink active submissions dashboard.
 *
 * @param string $type
 *   The type of task we are working on.
 *
 * @return array
 *   Array of forms for the GlobalLink active submissions dashboard.
 */
function globallink_dashboard_active_submissions_page($type = GLOBALLINK_ENTITY_TYPE_NODE) {
  $_SESSION['transpefect_dashboard_active_type'] = $type;
  $_SESSION['transpefect_dashboard_receive_type'] = $type;
  $array = array();
  if (arg(3) == 'sync') {
    $sub_rid = arg(4);
    $uri = request_uri();
    $ex_uri = explode(arg(3), $uri);
    $curr_uri = explode('admin', $ex_uri[0]);
    if ($sub_rid != '') {
      module_load_include('inc', 'globallink', 'globallink_settings');
      $pd4 = globallink_get_project_director_details();
      if (module_exists('background_process') && module_exists('background_batch')) {
        $operations = array();
        $operations[] = array(
          'globallink_background_pull',
          array(
            $pd4,
            $sub_rid,
            NULL,
            0,
          ),
        );
        $batch = array(
          'title' => t('Pulling Status'),
          '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('The submission(s) 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, $sub_rid, NULL, 0);
        drupal_set_message('The submission has been synced with the latest available status');
      }
    }
    drupal_goto('admin' . $curr_uri[1]);
  }
  if (arg(3) == 'import') {
    $sub_rid = arg(4);
    $uri = request_uri();
    $ex_uri = explode(arg(3), $uri);
    $curr_uri = explode('admin', $ex_uri[0]);
    if ($sub_rid != '') {
      module_load_include('inc', 'globallink', 'globallink_settings');
      $pd4 = globallink_get_project_director_details();
      if (module_exists('background_process') && module_exists('background_batch')) {
        $operations = array();
        $operations[] = array(
          'globallink_background_import',
          array(
            $pd4,
            $sub_rid,
            NULL,
          ),
        );
        $batch = array(
          'title' => t('Importing'),
          'operations' => $operations,
          'error_message' => t('Import 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('The submission(s) are being imported in the background. Please refresh the page for the progress.');
      }
      else {
        module_load_include('inc', 'globallink', 'globallink_background_jobs');
        globallink_background_import($pd4, $sub_rid, NULL);
        drupal_set_message('The submission has been imported');
      }
    }
    drupal_goto('admin' . $curr_uri[1]);
  }
  if (arg(3) == 'completed' && arg(4) == 'import') {
    $sub_rid = arg(5);
    $uri = request_uri();
    $ex_uri = explode(arg(4), $uri);
    $curr_uri = explode('admin', $ex_uri[0]);
    if ($sub_rid != '') {
      module_load_include('inc', 'globallink', 'globallink_settings');
      $pd4 = globallink_get_project_director_details();
      if (module_exists('background_process') && module_exists('background_batch')) {
        $operations = array();
        $operations[] = array(
          'globallink_background_import',
          array(
            $pd4,
            $sub_rid,
            NULL,
          ),
        );
        $batch = array(
          'title' => t('Importing'),
          'operations' => $operations,
          'error_message' => t('Import 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('The submission(s) are being imported in the background. Please refresh the page for the progress.');
      }
      else {
        module_load_include('inc', 'globallink', 'globallink_background_jobs');
        globallink_background_import($pd4, $sub_rid, NULL);
        drupal_set_message('The submission has been imported');
      }
    }
    drupal_goto('admin' . $curr_uri[1]);
  }
  $f1 = drupal_get_form('globallink_all_active_filter_form');
  $f2 = drupal_get_form('globallink_node_active_pager_form');
  $f3 = drupal_get_form('globallink_dashboard_active');
  $html = drupal_render($f1);
  $html .= drupal_render($f2);
  $html .= drupal_render($f3);
  return $html;
}

/**
 * Builds form to filter GlobalLink submissions
 */
function globallink_all_active_filter_form() {
  module_load_include('inc', 'globallink', 'globallink');
  $form = array();
  $filter = FALSE;
  if (arg(3) == 'completed') {
    $default_status = '[completed]';
    $status_arr = array(
      '[completed]' => t('All Completed'),
      GLOBALLINK_STATUS_TRANSLATION_IMPORTED => GLOBALLINK_STATUS_TRANSLATION_IMPORTED,
      GLOBALLINK_STATUS_TRANSLATION_SOURCE_CANCELLED => GLOBALLINK_STATUS_TRANSLATION_SOURCE_CANCELLED,
    );
  }
  else {
    $default_status = '[active]';
    $status_arr = array(
      '[active]' => t('All Active'),
      GLOBALLINK_STATUS_TRANSLATION_ERROR => GLOBALLINK_STATUS_TRANSLATION_ERROR,
      GLOBALLINK_STATUS_TRANSLATION_SENT => GLOBALLINK_STATUS_TRANSLATION_SENT,
      GLOBALLINK_STATUS_TRANSLATION_COMPLETED => GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
    );
  }
  $lang_filter = array();
  $languages = globallink_get_mapped_locales_with_drupal_desc(TRUE);
  $lang_filter['[all]'] = ' All ';
  $default_tgt_lang = '[all]';
  foreach ($languages as $key => $lang) {
    $lang_filter[$key] = $lang;
  }
  if (!empty($_SESSION['globallink_all_receive_filter_tgt_lang'])) {
    $default_tgt_lang = $_SESSION['globallink_all_receive_filter_tgt_lang'];
    $filter = TRUE;
  }
  if (!empty($_SESSION['globallink_all_receive_filter_status'])) {
    $default_status = $_SESSION['globallink_all_receive_filter_status'];
    $filter = TRUE;
  }
  $default_submission = '';
  if (!empty($_SESSION['globallink_all_receive_filter_submission'])) {
    $default_submission = $_SESSION['globallink_all_receive_filter_submission'];
    $filter = TRUE;
  }
  $form['node_receive_filter'] = array(
    '#type' => 'fieldset',
    '#title' => t('FILTER RECORDS'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['node_receive_filter']['filters'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
  );
  $form['node_receive_filter']['filters'][] = array(
    '#markup' => '<table>',
  );
  $form['node_receive_filter']['filters']['status'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#options' => $status_arr,
    '#default_value' => $default_status,
    '#field_suffix' => '&nbsp;&nbsp;&nbsp;&nbsp;',
    '#prefix' => '<tr><td style="text-align: left;width: 16%;vertical-align: middle;border: none;">',
    '#suffix' => '</tr>',
    '#field_suffix' => '</td>',
    '#field_prefix' => '</td><td>',
  );
  $form['node_receive_filter']['filters']['tgt_lang'] = array(
    '#type' => 'select',
    '#title' => t('Target Language'),
    '#options' => $lang_filter,
    '#default_value' => $default_tgt_lang,
    '#field_suffix' => '&nbsp;&nbsp;&nbsp;&nbsp;',
    '#prefix' => '<tr><td style="text-align: left;width: 16%;vertical-align: middle;border: none;">',
    '#suffix' => '</tr>',
    '#field_suffix' => '</td>',
    '#field_prefix' => '</td><td>',
  );
  $form['node_receive_filter']['filters']['submission'] = array(
    '#type' => 'textfield',
    '#title' => t('Submission Name Contains'),
    '#size' => 40,
    '#field_suffix' => '&nbsp;&nbsp;&nbsp;&nbsp;',
    '#prefix' => '<tr><td style="text-align: left;width: 16%;vertical-align: middle;border: none;">',
    '#suffix' => '</tr>',
    '#field_suffix' => '</td>',
    '#field_prefix' => '<BR/><div class="description">' . t('The filter is case sensitive.') . '</div></td><td>',
  );
  $form['node_receive_filter']['filters'][] = array(
    '#markup' => '</table>',
  );
  $disable_submit = $filter ? TRUE : FALSE;
  $form['node_receive_filter']['filters']['filter'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
    '#disabled' => $disable_submit,
  );
  if ($filter) {
    $form['filter-search'] = array(
      '#type' => 'fieldset',
      '#title' => t('FILTER CRITERIA'),
    );
    $form['filter-search']['filter-markup'] = array(
      '#type' => 'container',
    );
    $form['filter-search']['filter-markup'][] = array(
      '#markup' => '<table>',
    );
    $form['filter-search']['filter-markup'][] = array(
      '#prefix' => '<tr><td style="text-align: left;width: 16%;vertical-align: middle;border: none;"><b>',
      '#markup' => t('Status'),
      '#suffix' => '</b>:&nbsp;</td><td style="text-align: left;width: 84%;vertical-align: middle;border: none;">' . $status_arr[$default_status] . '</td></tr>',
    );
    $form['filter-search']['filter-markup'][] = array(
      '#prefix' => '<tr><td style="text-align: left;width: 16%;vertical-align: middle;border: none;"><b>',
      '#markup' => t('Target Language'),
      '#suffix' => '</b>:&nbsp;</td><td style="text-align: left;width: 84%;vertical-align: middle;border: none;">' . $lang_filter[$default_tgt_lang] . '</td></tr>',
    );
    $form['filter-search']['filter-markup'][] = array(
      '#prefix' => '<tr><td style="text-align: left;width: 16%;vertical-align: middle;border: none;"><b>',
      '#markup' => t('Submission Name Contains'),
      '#suffix' => '</b>:&nbsp;</td><td style="text-align: left;width: 84%;vertical-align: middle;border: none;">' . $default_submission . '</td></tr>',
    );
    $form['filter-search']['filter-markup'][] = array(
      '#markup' => '</table>',
    );
    $form['filter-search']['status']['actions'] = array(
      '#type' => 'actions',
      '#attributes' => array(
        'class' => array(
          'container-inline',
        ),
      ),
    );
    $form['filter-search']['status']['actions']['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset'),
    );
  }
  return $form;
}

/**
 * Handles submission of filter form.
 */
function globallink_all_active_filter_form_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  switch ($op) {
    case t('Filter'):
      $_SESSION['globallink_all_receive_filter_status'] = $form_state['values']['status'];
      $_SESSION['globallink_all_receive_filter_tgt_lang'] = $form_state['values']['tgt_lang'];
      $_SESSION['globallink_all_receive_filter_submission'] = $form_state['values']['submission'];
      break;
    case t('Reset'):
      $_SESSION['globallink_all_receive_filter_status'] = '';
      $_SESSION['globallink_all_receive_filter_tgt_lang'] = '';
      $_SESSION['globallink_all_receive_filter_submission'] = '';
      break;
  }
}

/**
 * Builds form to add pagination to GlobalLink send dashboard.
 */
function globallink_node_active_pager_form() {
  $module_path = drupal_get_path('module', 'globallink');
  drupal_add_css($module_path . '/css/globallink_active_submissions.css');
  $form = array();
  $form['page_counter']['markup'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'tpt-center',
      ),
    ),
  );
  $form['page_counter']['markup']['p_count'] = array(
    '#type' => 'textfield',
    '#size' => 5,
    '#default_value' => isset($_SESSION['globallink_node_active_page_count']) ? $_SESSION['globallink_node_active_page_count'][0] : TPT_PAGER_LIMIT,
    '#field_prefix' => t('Show') . '&nbsp;&nbsp;',
    '#field_suffix' => '&nbsp;&nbsp;' . t('records') . '&nbsp;&nbsp;&nbsp;&nbsp;',
    '#prefix' => '<div class="container-inline">',
  );
  $form['page_counter']['markup']['action'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
    '#suffix' => '</div>',
    '#limit_validation_errors' => array(),
    '#submit' => array(
      'globallink_node_active_pager_form_submit',
    ),
    '#access' => TRUE,
  );
  return $form;
}

/**
 * Handles submission of pager form.
 */
function globallink_node_active_pager_form_submit($form, &$form_state) {
  $op = $form_state['values']['op'];
  if ($op != t('Go')) {
    return;
  }
  $page_count = $form_state['input']['p_count'];
  if (!is_numeric($page_count) || $page_count < 1) {
    form_set_error('', t('Invalid Page Count.'));
  }
  else {
    $_SESSION['globallink_node_active_page_count'] = array(
      $page_count,
    );
    return;
  }
}

/**
 * Form to show all active GlobalLink submissions.
 */
function globallink_dashboard_active() {
  $module_path = drupal_get_path('module', 'globallink');
  drupal_add_css($module_path . '/css/globallink_active_submissions.css');
  module_load_include('inc', 'globallink', 'globallink');
  module_load_include('inc', 'globallink', 'globallink_node');
  module_load_include('inc', 'globallink_entity', 'globallink_entity');
  module_load_include('inc', 'globallink_block', 'globallink_block');
  module_load_include('inc', 'globallink_fieldable_panels', 'globallink_fieldable_panels');
  module_load_include('inc', 'globallink_interface', 'globallink_interface');
  module_load_include('inc', 'globallink_menu', 'globallink_menu');
  module_load_include('inc', 'globallink_taxonomy', 'globallink_taxonomy');
  module_load_include('inc', 'globallink_webform', 'globallink_webform');
  module_load_include('inc', 'globallink_beans', 'globallink_beans');
  module_load_include('inc', 'globallink_file_entity', 'globallink_file_entity');
  module_load_include('inc', 'globallink_commerce', 'globallink_commerce');
  module_load_include('inc', 'globallink', 'globallink_settings');
  module_load_include('inc', 'globallink', 'gl_ws/gl_ws_receive_translations');
  $form = array();
  $breadcrumb = array();
  $breadcrumb[] = l('Home', '');
  $breadcrumb[] = l('Administration', 'admin');
  $breadcrumb[] = l('Globallink', 'admin/globallink-translations');
  if (arg(3) == '') {
    $breadcrumb[] = t('Workbench');
    $breadcrumb[] = t('Active Submissions');
  }
  elseif (arg(3) == 'completed') {
    $breadcrumb[] = t('Workbench');
    $breadcrumb[] = t('Completed Submissions');
  }

  // Set Breadcrumbs
  drupal_set_breadcrumb($breadcrumb);
  $page_count = TPT_PAGER_LIMIT;
  if (isset($_SESSION['globallink_node_active_page_count'])) {
    $page_count = $_SESSION['globallink_node_active_page_count'][0];
  }
  $header = array(
    'submission' => array(
      'field' => 'submission',
      'data' => t('Submission Name'),
      'width' => '15%',
    ),
    'project_code' => array(
      'field' => 'project_name',
      'data' => t('Project'),
      'width' => '10%',
    ),
    'source_name' => array(
      'field' => 'source_lang_name',
      'data' => t('Source Language'),
      'width' => '12%',
    ),
    'target_name' => array(
      'field' => 'sub_target_lang_name',
      'data' => t('Target Language'),
      'width' => '12%',
    ),
    'status' => array(
      'field' => 'status',
      'data' => t('Status'),
      'width' => '14%',
    ),
    'timestamp' => array(
      'field' => 'updated',
      'data' => t('Last Updated'),
      'width' => '12%',
    ),
    'progress' => array(
      'data' => '',
      'width' => '15%',
    ),
    'operation' => array(
      'data' => 'Operations',
      'width' => '112px',
    ),
  );
  drupal_add_js('jQuery(document).ready(function() {jQuery("ul.tabs.secondary li").each(function() { console.log(jQuery(this).text());if(!(jQuery(this).text().indexOf("Active") >= 0 || jQuery(this).text().indexOf("Completed") >= 0)) { jQuery(this).css("display", "none");  } });  });', 'inline');
  if (arg(3) == 'completed') {
    $filter_status = '[completed]';
  }
  else {
    $filter_status = '[active]';
  }
  if (!empty($_SESSION['globallink_all_receive_filter_status'])) {
    $filter_status = $_SESSION['globallink_all_receive_filter_status'];
  }
  $status_arr = array();
  if (arg(3) == '' && $filter_status == '[active]') {
    $status_arr = array(
      GLOBALLINK_STATUS_TRANSLATION_SENT,
      GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
      GLOBALLINK_STATUS_TRANSLATION_ERROR,
    );
  }
  elseif ($filter_status == '[completed]') {
    $status_arr = array(
      GLOBALLINK_STATUS_TRANSLATION_IMPORTED,
      GLOBALLINK_STATUS_TRANSLATION_SOURCE_CANCELLED,
    );
  }
  else {
    $status_arr = array(
      $filter_status,
    );
  }
  $filter_tgt_lang = '';
  if (!empty($_SESSION['globallink_all_receive_filter_tgt_lang'])) {
    if ($_SESSION['globallink_all_receive_filter_tgt_lang'] != '' && $_SESSION['globallink_all_receive_filter_tgt_lang'] != '[all]') {
      $filter_tgt_lang = $_SESSION['globallink_all_receive_filter_tgt_lang'];
    }
  }
  $filter_submission = '';
  if (!empty($_SESSION['globallink_all_receive_filter_submission'])) {
    $filter_submission = $_SESSION['globallink_all_receive_filter_submission'];
  }
  $sort_order = isset($_GET['order']) ? $_GET['order'] : '';
  $rows = array();

  // Handling node submissions
  $query_node = db_select('globallink_submission', 'gs')
    ->extend('PagerDefault')
    ->limit($page_count)
    ->extend('TableSort');
  $query_node
    ->condition('gs.status', $status_arr, 'IN');
  if (!empty($filter_submission)) {
    $query_node
      ->condition('gs.submission', '%' . db_like($filter_submission) . '%', 'LIKE');
  }
  if (!empty($filter_tgt_lang)) {
    $query_node
      ->condition('gs.sub_target_lang_code', $filter_tgt_lang, '=');
  }
  $query_node
    ->fields('gs');
  if ($sort_order == '') {
    $query_node
      ->orderBy('updated', 'DESC');
  }
  else {
    $query_node
      ->orderByHeader($header);
  }
  $results_node = $query_node
    ->execute()
    ->fetchAll();
  $count = 0;
  foreach ($results_node as $item) {
    $count++;
    $total_docs = 0;
    $total_active_docs = 0;
    $doc_query = db_select('globallink_document', 'gd');
    $doc_query
      ->condition('gd.submission_rid', $item->rid, '=');
    $doc_query
      ->fields('gd');
    $doc_results = $doc_query
      ->execute()
      ->fetchAll();
    foreach ($doc_results as $doc_row) {
      if ($doc_row->target_status == GLOBALLINK_STATUS_TRANSLATION_COMPLETED || $doc_row->target_status == GLOBALLINK_STATUS_TRANSLATION_SENT || $doc_row->target_status == GLOBALLINK_STATUS_TRANSLATION_ERROR) {
        $total_active_docs++;
      }
      $total_docs++;
    }
    $percent_width = 0;
    $percent_label = 0;
    if ($total_docs != 0) {
      $percent_width = 100 - round($total_active_docs / $total_docs * 100, 2);
      $percent_label = $percent_width;
    }
    $w3_class = 'w3-green';
    $status = $item->status;
    $submission_name = l($item->submission, "admin/globallink-translations/workbench/all/" . $item->rid . "/" . $item->sub_target_lang_code);
    if ($item->status == GLOBALLINK_STATUS_TRANSLATION_SOURCE_CANCELLED) {
      $w3_class = 'w3-red';
      $percent_label = 0;
    }
    elseif ($item->status == GLOBALLINK_STATUS_TRANSLATION_ERROR) {
      $status = $status . '&nbsp;&nbsp;' . '<img src="' . $GLOBALS['base_url'] . '/' . $module_path . '/css/exclamation.png"" title="Error" height="14" width="14" style="vertical-align: middle;">';
    }
    if ($percent_width < 1) {
      $progress_html = '<div class="w3-dark-grey" style="margin-left: 10px; margin-right: 10px;"><div class="' . $w3_class . '" style="width:' . $percent_width . '%"><span style="padding-left: 10px;">' . $percent_label . '%</span></div></div>';
    }
    else {
      $progress_html = '<div class="w3-dark-grey" style="margin-left: 10px; margin-right: 10px;"><div class="' . $w3_class . '" style="width:' . $percent_width . '%"><span style="padding-left: 10px;">' . $percent_label . '%</span></div></div>';
    }
    $sync = '';
    $import = '';
    $view_variables = array(
      'path' => drupal_get_path('module', 'globallink') . '/css/gridview16.png',
      'width' => '16px',
      'height' => '16px',
      'attributes' => array(
        'title' => 'View',
        'class' => array(
          'gl-workbench-row-link',
        ),
      ),
    );
    $view_image_html = theme_image($view_variables);

    //$view = '<span style="display:inline-block; width: 16px;"></span>' . l($view_image_html, "admin/globallink-translations/workbench/all/" . $item->rid . "/" . $item->sub_target_lang_code, array('html' => TRUE));
    if ($item->status != GLOBALLINK_STATUS_TRANSLATION_IMPORTED && $item->status != GLOBALLINK_STATUS_TRANSLATION_SOURCE_CANCELLED) {
      $import_variables = array(
        'path' => drupal_get_path('module', 'globallink') . '/css/import-sub16.png',
        'width' => '16px',
        'height' => '16px',
        'attributes' => array(
          'title' => 'Import',
          'class' => array(
            'gl-workbench-row-link',
          ),
        ),
      );
      $import_image_html = theme_image($import_variables);
      $import = '<span title="Imports translated content" style="display:inline-block; width: 16px;"></span>' . l('Import', request_path() . '/import/' . $item->rid, array(
        'html' => TRUE,
      ));
      $sync_variables = array(
        'path' => drupal_get_path('module', 'globallink') . '/css/if_refresh_293697.png',
        'width' => '16px',
        'height' => '16px',
        'attributes' => array(
          'title' => 'Sync',
          'class' => array(
            'gl-workbench-row-link',
          ),
        ),
      );
      $sync_image_html = theme_image($sync_variables);
      $sync = '<span title="Gets latest status" style="display:inline-block; width: 16px;"></span>' . l('Sync', request_path() . '/sync/' . $item->rid, array(
        'html' => TRUE,
      ));
    }
    elseif ($item->status == GLOBALLINK_STATUS_TRANSLATION_IMPORTED) {
      $import_variables = array(
        'path' => drupal_get_path('module', 'globallink') . '/css/import-sub16.png',
        'width' => '16px',
        'height' => '16px',
        'attributes' => array(
          'title' => 'Re-Import',
          'class' => array(
            'gl-workbench-row-link',
          ),
        ),
      );
      $import_image_html = theme_image($import_variables);
      $import = '<span title="Imports translated content" style="display:inline-block; width: 16px;"></span>' . l('Re - Import', request_path() . '/import/' . $item->rid, array(
        'html' => TRUE,
      ));

      //$sync_variables = array('path' => drupal_get_path('module', 'globallink') . '/css/archive16.png', 'width' => '16px', 'height' => '16px', 'attributes' => array('title' => 'Archive', 'class' => array('gl-workbench-row-link')));

      //$sync_image_html = theme_image($sync_variables);
      $sync = '<span title="Gets latest status" style="display:inline-block; width: 16px;"></span>';
    }
    $link = '<div style="text-align: left;">' . $sync . $import . '</div>';
    $rows[$item->rid] = array(
      'submission' => $submission_name,
      'project_code' => $item->project_name,
      'source_name' => $item->source_lang_name,
      'target_name' => $item->sub_target_lang_name,
      'status' => $status,
      'timestamp' => format_date($item->updated, 'custom', 'Y-m-d H:i:s'),
      'progress' => $progress_html,
      //'<progress value="' . $active_count . '" max="' . $total_docs . '"><span>' . $active_count . '</span> of ' . $total_docs . '</progress>',//$active_count . ' of ' . $total_docs . ' Ready for Import',
      'operation' => $link,
    );
  }
  drupal_add_js("\n  (function(\$) {\n    \$(function() {\n      \$('.progressbar').each(function() { \n        var val = \$(this).attr('rel');\n        \$(this).progressbar({ value: parseInt(val) });\n      });\n    });\n  })(jQuery);", 'inline');
  if (arg(3) == 'completed') {
    $form['submit_pull_all_submission'] = array(
      '#type' => 'submit',
      '#value' => t('Archive All'),
      '#prefix' => '<div class="form-operations">',
      '#suffix' => '</div>',
      '#attributes' => array(
        'title' => t('Sync Status'),
        'class' => array(
          'globallink-edit-submit-pull-all-submission',
        ),
      ),
    );
    $form['submit_import_all_submission'] = array(
      '#type' => 'submit',
      '#value' => t('Re-Import All'),
      '#prefix' => '<div class="form-operations">',
      '#suffix' => '</div>',
      '#attributes' => array(
        'title' => t('Import Translations'),
        'class' => array(
          'globallink-edit-submit-import-all-submission',
        ),
      ),
    );
  }
  else {
    $form['submit_pull_all_submission'] = array(
      '#type' => 'submit',
      '#value' => t('Sync All'),
      '#prefix' => '<div class="form-operations">',
      '#suffix' => '</div>',
      '#attributes' => array(
        'title' => t('Sync Status'),
        'class' => array(
          'globallink-edit-submit-pull-all-submission',
        ),
      ),
    );
    $form['submit_import_all_submission'] = array(
      '#type' => 'submit',
      '#value' => t('Import All'),
      '#prefix' => '<div class="form-operations">',
      '#suffix' => '</div>',
      '#attributes' => array(
        'title' => t('Import Translations'),
        'class' => array(
          'globallink-edit-submit-import-all-submission',
        ),
      ),
    );
  }
  $form['table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No items available'),
    '#attributes' => array(),
  );
  $form['pager'] = array(
    '#markup' => theme('pager'),
  );
  return $form;
}

/**
 * Handles submission of active GlobalLink form.
 */
function globallink_dashboard_active_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  module_load_include('inc', 'globallink', 'globallink_node');
  $query = db_select('globallink_submission', 'gs');
  $query
    ->fields('gs', array(
    'rid',
  ));
  if (arg(3) == 'completed') {
    $query
      ->condition('status', array(
      GLOBALLINK_STATUS_TRANSLATION_SENT,
      GLOBALLINK_STATUS_TRANSLATION_ERROR,
      GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
      GLOBALLINK_STATUS_TRANSLATION_IMPORTED,
    ), 'IN');
  }
  else {
    $query
      ->condition('status', array(
      GLOBALLINK_STATUS_TRANSLATION_SENT,
      GLOBALLINK_STATUS_TRANSLATION_ERROR,
      GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
    ), 'IN');
  }
  $results = $query
    ->execute();
  foreach ($results as $value) {
    $sub_rids[] = $value->rid;
  }
  if (!$results
    ->rowCount()) {
    drupal_set_message("No Results available");
  }
  else {
    switch ($op) {
      case t('Re-Import All'):
      case t('Import All'):
        $pd4 = globallink_get_project_director_details();
        try {
          foreach ($sub_rids as $sub_rid) {
            if (module_exists('background_process') && module_exists('background_batch')) {
              $operations = array();
              $operations[] = array(
                'globallink_background_import',
                array(
                  $pd4,
                  $sub_rid,
                  NULL,
                ),
              );
              $batch = array(
                'title' => t('Importing'),
                'operations' => $operations,
                'error_message' => t('Import has encountered an error.'),
                '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('The submission(s) 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, $sub_rid, NULL);
              drupal_set_message('The submisions have been imported');
            }
          }
        } 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;
      case t('Sync All'):
        $pd4 = globallink_get_project_director_details();
        try {
          foreach ($sub_rids as $sub_rid) {
            if (module_exists('background_process') && module_exists('background_batch')) {
              $operations = array();
              $operations[] = array(
                'globallink_background_pull',
                array(
                  $pd4,
                  $sub_rid,
                  NULL,
                  0,
                ),
              );
              $batch = array(
                'title' => t('Pulling Status'),
                '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('The submission(s) 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, $sub_rid, NULL, 0);
              drupal_set_message('The submissions 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(),
          )));
        }
    }
  }
}

/**
 * Upload XML file for direct translation
 */
function globallink_upload_translation_node_form() {
  if (arg(3) != '' && (isset($_SESSION['active_node_count']) && $_SESSION['active_node_count'] > 0)) {
    $form['translation_file'] = array(
      '#type' => 'file',
      '#name' => 'files[]',
      '#title' => t('Select Translation File'),
      '#attributes' => array(
        'multiple' => 'multiple',
      ),
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Upload'),
    );
    return $form;
  }
}

/**
 * Validate handler of XML file upload for direct translation
 */
function globallink_upload_translation_node_form_validate($form, &$form_state) {
  if (!empty($_SESSION['globallink_globalLink_arr'])) {
    $globallink_arr = unserialize($_SESSION['globallink_globalLink_arr']);
  }
  else {
    $globallink_arr = array();
  }

  // Save multiple files
  $num_files = count($_FILES['files']['name']);
  for ($i = 0; $i < $num_files; $i++) {
    $nid = '';
    $vid = '';
    $file = file_save_upload($i, array(
      'file_validate_extensions' => array(
        'xml',
      ),
      FILE_EXISTS_RENAME,
    ));
    if ($file) {

      // Make it a permanent file so it doesn't get deleted by cron.
      $file->status = 1;
      $xml = file_get_contents($file->uri);
      $dom = new DomDocument();
      $dom->preserveWhiteSpace = FALSE;
      $dom
        ->loadXML($xml);
      $contents = $dom
        ->getElementsByTagName('content');
      foreach ($contents as $content) {
        foreach ($content->attributes as $attr_name => $attr_node) {
          if ($attr_name == 'nid') {
            $nid = $attr_node->value;
          }
          if ($attr_name == 'vid') {
            $vid = $attr_node->value;
          }
        }
      }
      if ($nid != '' && $vid != '') {
        $node = node_load($nid);
        module_load_include('inc', 'globallink', 'globallink');
        $node_types = globallink_get_translatable_node_types(TRUE);
        if (in_array($node->type, $node_types)) {
          if ($file = file_move($file, 'public://')) {
            $form_state['values']['file'][$i] = $file;
          }
          $form_state['values']['nodes'][$i]['nid'] = $nid;
          $form_state['values']['nodes'][$i]['vid'] = $vid;
          $query = db_select('globallink_core', 'tc')
            ->fields('tc')
            ->condition('nid', $nid, '=')
            ->condition('vid', $vid, '=');
          $result = $query
            ->execute();
          $result = $result
            ->fetchAll();
          if (count($result) > 0) {
            foreach ($result as $val) {
              if ($val->submission == arg(4) && $val->target == arg(5)) {
                if ($val->status == GLOBALLINK_STATUS_TRANSLATION_IMPORTED) {
                  form_set_error('', $file->filename . t(' file can not upload. Translation is already published.'));
                }
                else {
                  foreach ($globallink_arr as $key => $globallink) {
                    if ($globallink->type != GLOBALLINK_ENTITY_TYPE_NODE) {
                      continue;
                    }
                    if ($globallink->submissionName == $val->submission && $globallink->targetLocale == $val->target && $globallink->nid == $val->nid) {
                      form_set_error('', $file->filename . t(' file can not upload. Translation is already available.'));
                    }
                  }
                }
              }
              else {
                form_set_error('', $file->filename . t(' file can not upload. It is not a part of this submission.'));
              }
            }
          }
          else {
            drupal_set_message('', t('No record(s) exists for ' . $file->filename . ' file.'), 'warning');
            unset($form_state['values']['file'][$i]);
            unset($form_state['values']['nodes'][$i]);
          }
        }
        else {
          drupal_set_message($file->filename . t(' file does not belongs to node submission.'), 'warning');
        }
      }
      else {
        drupal_set_message($file->filename . t(' file does not belongs to node submission.'), 'warning');
      }
    }
    else {
      form_set_error('translation_file', t('No file was uploaded.'));
    }
  }
}

/**
 * Submit handler of XML file upload for direct translation
 */
function globallink_upload_translation_node_form_submit($form, &$form_state) {
  module_load_include('inc', 'globallink', 'globallink_node');
  $num_files = count($form_state['values']['file']);
  for ($i = 0; $i < $num_files; $i++) {
    $file_path = $form_state['values']['file'][$i]->uri;
    $file_name = $form_state['values']['file'][$i]->filename;
    $contents = file_get_contents($file_path);
    $nid = $form_state['values']['nodes'][$i]['nid'];
    $vid = $form_state['values']['nodes'][$i]['vid'];
    $result = db_select('globallink_core', 'tc')
      ->fields('tc', array(
      'nid',
      'vid',
      'source',
      'target',
      'status',
    ))
      ->condition('nid', $nid, '=')
      ->condition('vid', $vid, '=')
      ->execute();
    $globallink = new stdClass();
    foreach ($result as $item) {
      $globallink->nid = $item->nid;
      $globallink->vid = $item->vid;
      $globallink->sourceLocale = $item->source;
      $globallink->targetLocale = $item->target;
      $globallink->status = $item->status;
      $globallink->targetXML = $contents;
    }
    globallink_import_node($globallink);
    globallink_update_status($globallink);
    drupal_set_message(t($file_name . ' file uploaded and published.'));
    $_SESSION['globallink_globalLink_arr'] = array();
  }
}

Functions

Namesort descending Description
globallink_all_active_filter_form Builds form to filter GlobalLink submissions
globallink_all_active_filter_form_submit Handles submission of filter form.
globallink_dashboard_active Form to show all active GlobalLink submissions.
globallink_dashboard_active_submissions_page Builds forms on GlobalLink active submissions dashboard.
globallink_dashboard_active_submit Handles submission of active GlobalLink form.
globallink_node_active_pager_form Builds form to add pagination to GlobalLink send dashboard.
globallink_node_active_pager_form_submit Handles submission of pager form.
globallink_upload_translation_node_form Upload XML file for direct translation
globallink_upload_translation_node_form_submit Submit handler of XML file upload for direct translation
globallink_upload_translation_node_form_validate Validate handler of XML file upload for direct translation