You are here

function lingotek_bulk_grid_form in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.bulk_grid.inc \lingotek_bulk_grid_form()
  2. 7.4 lingotek.bulk_grid.inc \lingotek_bulk_grid_form()
  3. 7.5 lingotek.bulk_grid.inc \lingotek_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 13

Code

function lingotek_bulk_grid_form($form, $form_state) {
  $entity_type = arg(4);
  $entity_type = empty($entity_type) ? 'node' : $entity_type;
  global $language;
  if (isset($_SESSION['grid_entity_type']) && $_SESSION['grid_entity_type'] != $entity_type) {
    $_SESSION['grid_entity_type'] = $entity_type;
    lingotek_grid_clear_filters();
  }
  elseif (!isset($_SESSION['grid_entity_type'])) {
    $_SESSION['grid_entity_type'] = $entity_type;
  }

  /*
   * 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/' . $entity_type, 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();
  lingotek_notify_if_no_languages_added();

  // 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($entity_type);
  $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 = lingotek_grid_action_options($entity_type);
  $form['lingotek-console'] = array(
    '#markup' => '<div id="lingotek-console"></div>',
  );
  $form['entity_type'] = array(
    '#type' => 'hidden',
    '#value' => $entity_type,
  );
  $page = pager_find_page() + 1;

  // Get current page from url
  $limit_select = isset($_SESSION['limit_select']) ? (int) $_SESSION['limit_select'] : 0;
  if ($entity_type == 'config') {
    $total_entity_rows = lingotek_config_get_rows($entity_type, $form, $form_state, TRUE);
  }
  else {
    $total_entity_rows = lingotek_grid_get_rows($entity_type, $form, $form_state, TRUE);
  }
  if ((int) ($page - 1) * $limit_select > $total_entity_rows) {

    // reset the page to be the last set of results
    $page = 1;
    $_SESSION['grid_custom_parameters']['page'] = 0;
    if (isset($_GET['page'])) {
      $_GET['page'] = 0;

      // used by PagerDefault class to get page number
    }
  }
  $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 {

        // $value === '0' accounts for the case of the automatic profile
        if (!empty($value) && $value !== 'all' || $value === '0') {
          $filter_set = TRUE;
        }
      }
    }
  }

  // Run query to get table rows
  $table_data = $entity_type == 'config' ? lingotek_config_get_rows($entity_type, $form, $form_state) : lingotek_grid_get_rows($entity_type, $form, $form_state);
  $results_first = ($page - 1) * $limit_select + 1;
  $results_last = $results_first + count($table_data) - 1;
  $form['customize'] = array(
    '#markup' => l('<i class="fa fa-list-alt fa-2x" ></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/customize/' . $entity_type, array(
      'html' => TRUE,
      'attributes' => array(
        'title' => t('Customize Table'),
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-lingotek-large',
          'lingotek-action',
        ),
      ),
    )),
  );
  $last_updated = variable_get('lingotek_pending_last_updated', NULL);
  $message = t('Check status of in-progress translations (Last checked @time)', array(
    '@time' => $last_updated ? lingotek_human_readable_timestamp($last_updated) . ' ' . t('ago') : t('Never'),
  ));
  $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/' . $entity_type, array(
      'html' => TRUE,
      'attributes' => array(
        'title' => t('Download complete translations'),
        'class' => array(
          'lingotek-action',
        ),
      ),
    )),
  );
  $form['refresh'] = array(
    '#markup' => l('<i class="fa fa-refresh fa-2x ltk-refresh"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/update/' . $entity_type, array(
      'html' => TRUE,
      'attributes' => array(
        'class' => 'lingotek-action',
        'title' => $message,
      ),
    )),
  );
  $form['lingotek_upload'] = array(
    '#markup' => l('<i class="fa fa-cloud-upload fa-2x ltk-upload"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/upload-edited/' . $entity_type, array(
      'html' => TRUE,
      'attributes' => array(
        'title' => t('Upload all pending source content'),
        'class' => array(
          'lingotek-action',
        ),
      ),
    )),
  );
  $form['edit_settings'] = array(
    '#markup' => l(t('Edit Settings'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/edit/' . $entity_type, array(
      'attributes' => array(
        'id' => 'edit-settings-link',
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-lingotek-large',
        ),
      ),
    )),
  );
  $form['disassociate_translations'] = array(
    '#markup' => l(t('Disassociate translations'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/reset/' . $entity_type, array(
      'attributes' => array(
        'id' => 'reset-link',
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-lingotek-small',
          'ltk-hidden-modal-trigger',
        ),
      ),
    )),
  );
  $form['delete'] = array(
    '#markup' => l(t('Delete selected content'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/delete/' . $entity_type, array(
      'attributes' => array(
        'id' => 'delete-link',
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-lingotek-small',
          'ltk-hidden-modal-trigger',
        ),
      ),
    )),
  );
  $form['pop_up_link'] = array(
    '#markup' => l(t('Hidden pop-up'), '', array(
      'attributes' => array(
        'id' => 'popup-link',
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-lingotek-large',
          'ltk-hidden-modal-trigger',
        ),
      ),
    )),
  );
  $api = LingotekApi::instance();
  $workflows = $api
    ->listWorkflows();
  if (is_array($workflows) && count($workflows) > 1) {
    $form['change_workflow'] = array(
      '#markup' => l(t('Change Workflow'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/change-workflow/' . $entity_type, array(
        'attributes' => array(
          'id' => 'change-workflow-link',
          'class' => array(
            'ctools-use-modal',
            'ctools-modal-lingotek-large',
          ),
        ),
      )),
    );
  }
  if ($entity_type == 'node') {
    $form['node/add'] = array(
      '#markup' => l('<i class="fa fa-plus" style="margin-left: 15px; padding-right: 4px;"></i>' . t('Add content') . '<div style="margin: 15px 0;"></div>', 'node/add', array(
        'html' => TRUE,
        'attributes' => array(
          'title' => t('Add content'),
        ),
      )),
    );
  }
  $modal_size = $entity_type == 'config' ? 'ctools-modal-lingotek-small' : 'ctools-modal-lingotek-large';
  $modal_classes = array(
    'ctools-use-modal',
    $modal_size,
    'ltk-action',
  );
  $search_title = l('<i class="fa fa-search"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/filters/' . $entity_type, array(
    'html' => TRUE,
    'attributes' => array(
      'title' => t('Advanced Search'),
      'class' => $modal_classes,
    ),
  )) . ' ' . t('Search') . ': ';
  $form['search'] = array(
    '#type' => 'textfield',
    '#default_value' => isset($_SESSION['grid_filters']['search']) ? $_SESSION['grid_filters']['search'] : '',
    '#title' => filter_xss($search_title),
    '#size' => 30,
  );
  $search_options = $entity_type == 'config' ? lingotek_config_search_options() : lingotek_grid_search_options();
  $grid_term = isset($_SESSION['grid_filters']['search_type']) ? $_SESSION['grid_filters']['search_type'] : 'all';
  $config_term = isset($_SESSION['grid_filters']['textgroup']) ? $_SESSION['grid_filters']['textgroup'] : 'all';
  $search_term = $entity_type == 'config' ? $config_term : $grid_term;
  $form['search_type'] = array(
    '#type' => 'select',
    '#options' => $search_options,
    '#default_value' => $search_term,
  );
  $config_search_submit = array(
    'lingotek_config_filter_inline_submit',
  );
  $grid_search_submit = array(
    'lingotek_grid_filter_inline_submit',
  );
  $search_submit = $entity_type == 'config' ? $config_search_submit : $grid_search_submit;
  $form['search_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
    '#submit' => $search_submit,
  );
  $form['advanced_link'] = array(
    '#markup' => l(t('Advanced') . '<span style="margin: 0 5px;"></span>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/filters/' . $entity_type, array(
      'html' => TRUE,
      'attributes' => array(
        'title' => t('Advanced Search'),
        'class' => $modal_classes,
      ),
    )),
  );
  if ($filter_set) {
    $form['filter_message'] = array(
      '#markup' => '<span style="white-space:nowrap;">' . l('<i class="fa fa-times" style="margin: 0 5px;"></i>' . t('Clear Filters') . '</span>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/clear/filters/' . $entity_type, array(
        'html' => TRUE,
      )) . '</span>',
    );
  }

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

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

  // 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(
    'lingotek-small' => array(
      'modalSize' => array(
        'type' => 'fixed',
        'width' => 450,
        'height' => 450,
      ),
      'closeImage' => theme('image', array(
        'path' => drupal_get_path('module', 'lingotek') . '/images/close.png',
        'alt' => t('Close window'),
        'title' => t('Close window'),
      )),
      'closeText' => '',
      'animation' => 'fadeIn',
    ),
  ), 'setting');
  drupal_add_js(array(
    'lingotek-large' => array(
      'modalSize' => array(
        'type' => 'scale',
        'width' => 0.6,
        'height' => 0.8,
      ),
      'closeImage' => theme('image', array(
        'path' => drupal_get_path('module', 'lingotek') . '/images/close.png',
        'alt' => t('Close window'),
        'title' => t('Close window'),
      )),
      'closeText' => '',
      'animation' => 'fadeIn',
    ),
  ), '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;
  if ($results_last > 0) {
    $form['count'] = array(
      '#markup' => '<span class="grid-result-summary">' . t('Displaying @first - @last', array(
        '@first' => $results_first,
        '@last' => $results_last,
      )) . ($filter_set ? ' (' . t('filtered results') . ')' : '') . '</span>',
    );
  }
  $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;
}