You are here

function lingotek_bulk_grid_form in Lingotek Translation 7.4

Same name and namespace in other branches
  1. 7.7 lingotek.bulk_grid.inc \lingotek_bulk_grid_form()
  2. 7.5 lingotek.bulk_grid.inc \lingotek_bulk_grid_form()
  3. 7.6 lingotek.bulk_grid.inc \lingotek_bulk_grid_form()

@file Bulk Grid form

1 string reference to 'lingotek_bulk_grid_form'
lingotek_menu in ./lingotek.module
Implements hook_menu().

File

./lingotek.bulk_grid.inc, line 8
Bulk Grid form

Code

function lingotek_bulk_grid_form($form, $form_state) {
  global $language;

  /*
   * Here we store or retrieve the GET parameters so that the state of the table is maintained when leaving and coming back
   * Also makes it so the state is not lost when performing bulk actions
   */
  if (count($_GET) == 1 && isset($_SESSION['grid_custom_parameters']) && !empty($_SESSION['grid_custom_parameters'])) {
    $_SESSION['grid_custom_parameters']['preventloop'] = TRUE;
    drupal_goto('admin/settings/lingotek/manage/list', array(
      'query' => $_SESSION['grid_custom_parameters'],
    ));
  }
  else {
    $_SESSION['grid_custom_parameters'] = $_GET;
    if ($_SESSION['grid_custom_parameters']['q']) {
      unset($_SESSION['grid_custom_parameters']['q']);
    }
  }
  $path_to_lingotek = drupal_get_path('module', 'lingotek');
  lingotek_is_module_setup();

  // Output success messages for actions
  if (isset($_SESSION['lingotek_edit_nodes'])) {
    drupal_set_message(format_plural($_SESSION['lingotek_edit_nodes'], 'Settings changed for one node.', 'Settings changed for @count nodes.'));
    unset($_SESSION['lingotek_edit_nodes']);
  }
  if (isset($_SESSION['lingotek_sync_nodes'])) {
    drupal_set_message(format_plural($_SESSION['lingotek_sync_nodes'], 'Target translations progress checked and updated for one node.', 'Target translations progress checked and updated for @count nodes.'));
    unset($_SESSION['lingotek_sync_nodes']);
  }

  // Populate form_state with filter values so the query can use them
  $form_state['values']['columns'] = lingotek_grid_get_columns(TRUE);
  $form_state['values']['grid_header'] = array();

  // Define source actions - keys are used to decide what action to do in the 'lingotek_grid_action_submit' function
  $action_options = array(
    'select' => t('Select an action'),
    'upload' => t('Upload Source for Translation'),
    'sync' => t('Check Progress of Translations'),
    'reset' => t('Disassociate Translations'),
    //    'delete' => t('Delete Nodes'),
    'edit' => t('Edit Translation Settings'),
    'workflow' => t('Change Workflow'),
    'Download' => array(
      'download_all' => t('Download All Translations'),
    ),
  );
  $target_languages_raw = language_list('language');
  foreach ($target_languages_raw as $target_raw) {
    $action_options['Download']['download_' . $target_raw->lingotek_locale] = 'Download ' . $target_raw->name . ' (' . $target_raw->lingotek_locale . ') Translation';
  }
  $form['lingotek-console'] = array(
    '#markup' => '<div id="lingotek-console"></div>',
  );

  // Run query to get table rows
  $table_data = lingotek_grid_get_rows($form, $form_state);

  // Count of returned results
  $page = pager_find_page();

  // Get current page from url
  $form['customize'] = array(
    '#markup' => l('<i class="fa fa-list-alt fa-2x" ></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/customize', array(
      'html' => TRUE,
      'attributes' => array(
        'title' => t('Customize Table'),
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-customize-table',
          'lingotek-action',
        ),
      ),
    )),
  );

  /*$form['filter_popup'] = array(
      '#markup' => l('<i class="fa-search fa-2x"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/filters', array('html' => TRUE, 'attributes' => array('title' => 'Set Filters', 'class' => array('ctools-use-modal', 'ctools-modal-set-filters', 'lingotek-action')))),
    );*/
  $last_updated = variable_get('lingotek_pending_last_updated', NULL);
  $message = t('Check in-progress translation statuses (Last checked @time)', array(
    '@time' => $last_updated ? lingotek_human_readable_timestamp($last_updated) . ' ago' : 'Never',
  ));
  $form['refresh'] = array(
    '#markup' => l('<i class="fa fa-refresh fa-2x ltk-refresh"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/update', array(
      'html' => TRUE,
      'attributes' => array(
        'class' => 'lingotek-action',
        'title' => $message,
      ),
    )),
  );
  $form['lingotek_update'] = array(
    '#markup' => l('<i class="fa fa-cloud-download fa-2x ltk-download"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/download-ready', array(
      'html' => TRUE,
      'attributes' => array(
        'title' => t('Download complete translations'),
        'class' => array(
          'lingotek-action',
        ),
      ),
    )),
  );
  $form['edit_settings'] = array(
    '#markup' => l(t('Edit Settings'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/edit', array(
      'attributes' => array(
        'id' => 'edit-settings-link',
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-set-filters',
        ),
      ),
    )),
  );
  $form['disassociate_translations'] = array(
    '#markup' => l(t('Disassociate Translations'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/reset', array(
      'attributes' => array(
        'id' => 'reset-translations-link',
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-set-filters',
        ),
      ),
    )),
  );
  if (LingotekAccount::instance()
    ->isEnterprise()) {
    $form['change_workflow'] = array(
      '#markup' => l(t('Change Workflow'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/change-workflow', array(
        'attributes' => array(
          'id' => 'change-workflow-link',
          'class' => array(
            'ctools-use-modal',
            'ctools-modal-set-filters',
          ),
        ),
      )),
    );
  }
  $form['search'] = array(
    '#type' => 'textfield',
    '#default_value' => isset($_SESSION['grid_filters']['search']) ? $_SESSION['grid_filters']['search'] : '',
    '#title' => l('<i class="fa fa-search"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/filters', array(
      'html' => TRUE,
      'attributes' => array(
        'title' => t('Advanced Search'),
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-set-filters',
          'ltk-action',
        ),
      ),
    )) . ' ' . t('Search') . ': ',
    '#size' => 30,
  );
  $search_options = array(
    'all' => 'All',
    'title' => 'Title',
    'body' => 'Body',
  );
  $form['search_type'] = array(
    '#type' => 'select',
    '#options' => $search_options,
    '#default_value' => isset($_SESSION['grid_filters']['search_type']) ? $_SESSION['grid_filters']['search_type'] : 'all',
  );
  $form['search_submit'] = array(
    '#type' => 'submit',
    '#value' => 'Go',
    '#submit' => array(
      'lingotek_grid_filter_inline_submit',
    ),
  );
  $filter_set = FALSE;
  if (isset($_SESSION['grid_filters'])) {
    foreach ($_SESSION['grid_filters'] as $key => $value) {
      if (is_array($value)) {
        $keys = array_keys($value);
        if (count($keys) == 1) {
          if ($keys[0] != '' && $keys[0] != 'all') {
            $filter_set = TRUE;
          }
        }
        elseif (count($keys) > 1) {
          $filter_set = TRUE;
        }
      }
      else {
        if (!empty($value) && $value != 'all') {
          $filter_set = TRUE;
        }
      }
    }
  }
  if ($filter_set) {
    $form['filter_message'] = array(
      '#markup' => ' ' . l('<i class="fa fa-times" style="margin-right: 5px;"></i>' . t('Clear Filters'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/filters/clear', array(
        'html' => TRUE,
      )) . '',
    );
  }

  // Build actions selector
  $form['actions_select'] = array(
    '#type' => 'select',
    '#options' => $action_options,
    '#title' => '<i class="fa fa-asterisk ltk-muted"></i>' . ' ' . t('Actions') . ': ',
  );

  // Actions submit button
  $form['actions_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit Action'),
    '#name' => 'actions_submit',
    '#submit' => array(
      'lingotek_grid_action_submit',
    ),
  );

  // div container for the table and pager
  $form['grid_container'] = array(
    '#type' => 'container',
    '#attached' => array(
      'css' => array(
        '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css' => array(
          'type' => 'external',
        ),
      ),
      'js' => array(
        // We get php errors when TableSort and AJAX are combined (see https://drupal.org/node/1833746).             // So we are faking it with a hidden submit button and some jQuery.
        $path_to_lingotek . '/js/lingotek.bulk_grid.js',
      ),
    ),
  );
  ctools_include('modal');
  ctools_modal_add_js();
  drupal_add_js(array(
    'customize-table' => array(
      'modalSize' => array(),
    ),
    'set-filters' => array(
      'modalSize' => array(
        'type' => 'fixed',
        'width' => 500,
        'height' => 500,
      ),
    ),
  ), 'setting');
  if (!empty($table_data)) {

    // If results, render the table.  Otherwise, output 'No results were returned.'
    // Calculate and output the number of results shown
    // The actual table
    $form['grid_container']['the_grid'] = array(
      '#type' => 'tableselect',
      '#header' => $form_state['values']['grid_header'],
      '#options' => $table_data,
    );

    // The pager
    $form['grid_container']['pager'] = array(
      '#theme' => 'pager',
    );
  }
  else {
    $form['grid_container']['none'] = array(
      '#markup' => '<div class="grid-empty">' . t('No results found.') . '</div>',
    );
  }

  // process limit_select to correctly limit the query and pager
  $limit = 10;
  if (isset($_SESSION['limit_select'])) {
    $limit = $_SESSION['limit_select'];
  }
  $form_state['values']['limit_select'] = $limit;
  $results_first = $page * $form_state['values']['limit_select'] + 1;
  $results_last = $results_first + count($table_data) - 1;
  if ($results_last > 0) {
    $form['count'] = array(
      '#markup' => '<span id="grid-result-summary">' . t('Displaying @first - @last', array(
        '@first' => $results_first,
        '@last' => $results_last,
      )) . ($filter_set ? ' (' . t('filtered results') . ')' : '') . '</span>',
    );
  }
  if (count($table_data) >= 10) {
    $form['limit_select'] = array(
      '#type' => 'select',
      '#prefix' => '<div id="page-limit">',
      '#suffix' => ' ' . t('results per page') . '</div>',
      '#options' => array(
        10 => '10',
        25 => '25',
        50 => '50',
        100 => '100',
        250 => '250',
        500 => '500',
      ),
      '#default_value' => $limit,
    );
  }
  return $form;
}