You are here

function lingotek_bulk_grid_form in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.4 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()
1 string reference to 'lingotek_bulk_grid_form'
lingotek_menu in ./lingotek.module
Implements hook_menu().

File

./lingotek.bulk_grid.inc, line 14

Code

function lingotek_bulk_grid_form($form, $form_state) {
  if (variable_get('lingotek_use_stage_servers', FALSE)) {
    $server_url = 'cms.lingotek.com';
    drupal_set_message(t('Warning: Lingotek\'s sandbox TMS environment (@server) will be discontinued soon. Please plan accordingly.', array(
      '@server' => $server_url,
    )), 'warning');
  }
  $entity_type = arg(4);
  if (empty($entity_type)) {
    $entity_type = 'node';
  }
  global $language;
  if (!isset($_SESSION['grid_entity_type'])) {
    $_SESSION['grid_entity_type'] = $entity_type;
  }
  elseif ($_SESSION['grid_entity_type'] != $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'][$entity_type]) && !empty($_SESSION['grid_custom_parameters'][$entity_type])) {
    $_SESSION['grid_custom_parameters'][$entity_type]['preventloop'] = TRUE;
    drupal_goto('admin/settings/lingotek/manage/' . $entity_type, array(
      'query' => $_SESSION['grid_custom_parameters'][$entity_type],
    ));
  }
  else {
    if (!isset($_SESSION['grid_custom_parameters'])) {
      $_SESSION['grid_custom_parameters'] = array();
    }
    $temp = array();
    foreach ($_GET as $key => $value) {
      $temp[$key] = filter_xss($value);
    }
    $_SESSION['grid_custom_parameters'][$entity_type] = $temp;
    if ($_SESSION['grid_custom_parameters'][$entity_type]['q']) {
      unset($_SESSION['grid_custom_parameters'][$entity_type]['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,
    '#attributes' => array(
      'id' => 'entity-type',
    ),
  );
  $form['async_update'] = array(
    '#markup' => l(t(''), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/async-update/' . $entity_type, array(
      'html' => TRUE,
      'attributes' => array(
        'id' => 'async-update',
        'type' => 'hidden',
      ),
    )),
  );
  $form['auto_download'] = array(
    '#markup' => l(t(''), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/auto-download/' . $entity_type, array(
      'html' => TRUE,
      'attributes' => array(
        'id' => 'auto-download',
        'type' => 'hidden',
      ),
    )),
  );
  $page = pager_find_page() + 1;

  // Get current page from url
  $limit_select = isset($_SESSION['limit_select']) ? (int) $_SESSION['limit_select'] : 0;

  // 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);
  $total_entity_rows = count($table_data);
  if ((int) $limit_select < $total_entity_rows || $total_entity_rows == 0) {

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

      // used by PagerDefault class to get page number
    }

    /**
     *Run the query again with the new pages. If not then this code is not worth
     *having.
     *@author Unknown
     *
     */
    $table_data = $entity_type == 'config' ? lingotek_config_get_rows($entity_type, $form, $form_state) : lingotek_grid_get_rows($entity_type, $form, $form_state);
  }
  $filter_set = lingotek_filter_set_check($entity_type);
  $results_first = ($page - 1) * $limit_select + 1;
  $results_last = $results_first + count($table_data) - 1;
  if ($entity_type === 'config') {
    $form['lingotek_options'] = array(
      '#markup' => '<i title="More options" class="fa fa-chevron-left fa-2x lingotek-action ltk-options" id="more-options"></i>',
    );
    $form['lingotek_force_down_all'] = array(
      '#markup' => l('<i class="fa fa-sort-amount-asc fa-2x"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/force-down-all/true', array(
        'html' => TRUE,
        'attributes' => array(
          'title' => t('Download all regardless of status'),
          'class' => array(
            'lingotek-action ltk-force-down',
          ),
          'id' => array(
            'force-down',
          ),
        ),
      )),
    );
  }
  $form['lingotek_legend'] = array(
    '#markup' => l('<i class="fa fa-question-circle fa-2x ltk-legend"></i>', LINGOTEK_MENU_MAIN_BASE_URL . '/manage/legend', array(
      'html' => TRUE,
      'attributes' => array(
        'title' => t('Icon descriptions'),
        'class' => array(
          'ctools-use-modal',
          'ctools-modal-lingotek-large',
          'lingotek-action',
        ),
        'id' => array(
          'legend',
        ),
      ),
    )),
  );
  $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-filter fa-1x notify-filtered-action"></i>' . '<i class="fa fa-check-square-o fa-1x notify-checked-action"></i>' . '<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 Ready translations'),
        'class' => array(
          'lingotek-action',
        ),
        'id' => array(
          'download-ready',
        ),
      ),
    )),
  );
  $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,
        'id' => array(
          'refresh',
        ),
      ),
    )),
  );
  $form['lingotek_upload'] = array(
    '#markup' => l('<i class="fa fa-filter fa-1x notify-filtered-action"></i>' . '<i class="fa fa-check-square-o fa-1x notify-checked-action"></i>' . '<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('Re-upload all edited source content'),
        'class' => array(
          'lingotek-action',
        ),
        'id' => array(
          'upload-edited',
        ),
      ),
    )),
  );
  $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_translations'] = array(
    '#markup' => l(t('Delete local translations'), LINGOTEK_MENU_MAIN_BASE_URL . '/manage/delete-translations/' . $entity_type, array(
      'attributes' => array(
        'id' => 'delete-translations-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' => '<div style="">' . l('<i class="fa fa-plus" style="padding-right: 4px;"></i>' . t('Add content') . '<div style="margin: 15px 0;"></div>', 'node/add', array(
        'html' => TRUE,
        'attributes' => array(
          'title' => t('Add content'),
        ),
      )) . '</div>',
    );
  }
  $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,
    ),
  ));

  // Coder review error: we don't want to translate te font awesome classes
  $form['search'] = array(
    '#type' => 'textfield',
    '#default_value' => isset($_SESSION['grid_filters'][$entity_type]['search']) ? $_SESSION['grid_filters'][$entity_type]['search'] : '',
    '#title' => filter_xss($search_title),
    '#size' => 30,
    '#attributes' => array(
      'placeholder' => t('Search'),
    ),
  );
  $search_options = $entity_type == 'config' ? lingotek_config_search_options() : lingotek_grid_search_options($entity_type);
  $grid_term = isset($_SESSION['grid_filters'][$entity_type]['search_type']) ? $_SESSION['grid_filters'][$entity_type]['search_type'] : 'all';
  $config_term = isset($_SESSION['grid_filters'][$entity_type]['textgroup']) ? $_SESSION['grid_filters'][$entity_type]['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' => decode_entities('&#xf002;'),
    '#attributes' => array(
      'title' => 'Search',
    ),
    '#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,
        'attributes' => array(
          'id' => array(
            'clear-filters',
          ),
        ),
      )) . '</span>',
    );
  }

  // Build actions selector
  $form['select_actions'] = array(
    '#type' => 'select',
    '#options' => $action_options,
  );
  $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;
}