You are here

function lingotek_grid_get_rows in Lingotek Translation 7.7

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

Dynamic query processing function for the grid Since the header defines which columns are shown, this query gets all possible values and refines the header using the columns selected in the UI The filters are also processed here

Return value

array $table_data Returns array of rows Populates The Grid

1 call to lingotek_grid_get_rows()
lingotek_bulk_grid_form in ./lingotek.bulk_grid.inc

File

./lingotek.bulk_grid.inc, line 1768

Code

function lingotek_grid_get_rows($entity_type, $form, &$form_state, $count_only = FALSE) {
  $info = entity_get_info($entity_type);
  $entity_id_key = $info['entity keys']['id'];
  $eid = 'n.' . $entity_id_key;
  $entity_properties = array_flip($info['schema_fields_sql']['base table']);
  $label_col = isset($info['entity keys']['label']) && $info['entity keys']['label'] ? $info['entity keys']['label'] : NULL;
  if ($entity_type == 'menu_link') {
    $label_col = 'link_title';
  }
  if ($entity_type == 'paragraphs_item') {
    $label_col = 'field_name';
  }
  $bundle_col = isset($info['entity keys']['bundle']) && $info['entity keys']['bundle'] && isset($entity_properties[$info['entity keys']['bundle']]) ? $info['entity keys']['bundle'] : NULL;

  // All managed entity types should have a language column.
  // Field collection entities have one, but it is called 'langcode'.
  // Message type entities have one, and it is called 'language'; but currently it does not appear in their entity keys.
  // So, the $language_col looks for the correct language field, and then guesses it is 'language' if it doesn't find one.
  $language_col = !empty($info['entity keys']['language']) ? $info['entity keys']['language'] : 'language';
  $entity_properties['type'] = $entity_type;
  $entity_properties['label_col'] = $label_col;
  $entity_properties['info'] = $info;
  $entity_properties['language_col'] = $language_col;
  $limit = isset($_SESSION['limit_select']) ? $_SESSION['limit_select'] : 10;
  $columns = isset($form_state['values']['columns']) ? $form_state['values']['columns'] : array();
  $header = array(
    // Define the tentative source header
    'marked' => array(
      'data' => t('Marked'),
    ),
    'nid' => array(
      'data' => t('ID'),
      'field' => 'n.' . $entity_id_key,
    ),
    'content_type' => array(
      'data' => t('Content Type'),
      'field' => 'type',
    ),
    'parent_node_id' => array(
      'data' => t('Parent Node ID'),
    ),
    'title' => array(
      'data' => t('Title'),
      'field' => 'n.' . $label_col,
    ),
    'paragraph_snippet' => array(
      'data' => t('Paragraph Snippet'),
    ),
    'label' => array(
      'data' => t('Label'),
      'field' => 'n.label',
    ),
    'description' => array(
      'data' => t('Description'),
    ),
    'language' => array(
      'data' => t('Source'),
    ),
    //, 'field' => 'upload_status'),
    'translations' => array(
      'data' => t('Translations'),
      'field' => 't_current_c',
    ),
    'configuration' => array(
      'data' => t('Profile'),
    ),
    'workflow_id' => array(
      'data' => t('Workflow ID'),
      'field' => 'workflow_id',
    ),
    'workflow_name' => array(
      'data' => t('Workflow Name'),
      'field' => 'workflow_name',
    ),
    'document_id' => array(
      'data' => t('Doc ID'),
      'field' => 'document_id',
    ),
    'changed' => array(
      'data' => t('Last Modified'),
      'field' => 'changed',
    ),
    'last_uploaded' => array(
      'data' => t('Last Uploaded'),
      'field' => 'last_uploaded',
    ),
    'actions' => array(
      'data' => t('Actions'),
    ),
    'translation_status' => array(
      'data' => t('Translation Status'),
    ),
    'last_downloaded' => array(
      'data' => t('Last Downloaded'),
      'field' => 'last_downloaded',
    ),
    'translate_link' => array(
      'data' => t('Translate'),
    ),
  );
  if ($entity_type == 'paragraphs_item') {
    $header['title'] = array(
      'data' => t('Parent Node Title'),
    );
  }
  if (!isset($entity_properties['changed'])) {
    unset($header['changed']);
  }
  if (!$label_col) {
    unset($header['title']);
  }

  // Taxonomy terms require special handling of bundles because they do not
  // have a bundle column in their table.
  if (!$bundle_col && $entity_type != 'taxonomy_term') {
    unset($header['content_type']);
  }
  if ($entity_type != 'bean') {
    unset($header['label']);
  }
  if (isset($entity_properties['changed'])) {
    $header['changed']['sort'] = 'desc';
  }
  $form_state['values']['grid_header'] = lingotek_bulk_grid_refine_source_header($header, $columns);
  $query = lingotek_bulk_grid_query($form_state, $count_only, $entity_id_key, $label_col, $entity_type, $limit, $bundle_col, $info, $entity_properties, $eid);

  //picked an arbitrarily large number to ensure all results are gotten

  // Initialize Query and extend paginator and tablesort if necessary
  lingotek_bulk_grid_filter_query($query, $entity_type, $eid, $label_col, $info);

  // If count only, return the count
  if ($count_only) {
    $result = $query
      ->execute();
    return $result
      ->rowCount();
  }

  // Execute the query
  $table_data_raw = $query
    ->distinct()
    ->execute()
    ->fetchAllAssoc($entity_id_key);

  //save filtered id's to session for group download/upload
  if (lingotek_filter_set_check($entity_type)) {
    $no_limit_query = lingotek_bulk_grid_query($form_state, $count_only, $entity_id_key, $label_col, $entity_type, 500000, $bundle_col, $info, $entity_properties, $eid);
    lingotek_bulk_grid_filter_query($no_limit_query, $entity_type, $eid, $label_col, $info);
    $no_limit_query_raw = $no_limit_query
      ->distinct()
      ->execute();
    $all_filtered_entity_ids = array();
    foreach ($no_limit_query_raw as $value) {
      $all_filtered_entity_ids[$value->{$entity_id_key}] = $value->{$entity_id_key};
    }
    $_SESSION['grid_filters'][$entity_type]['filtered_ids'] = $all_filtered_entity_ids;
  }
  lingotek_entity_load($table_data_raw, $entity_type);

  // Parse returned objects and make them arrays keyed by the Node ID for clean use in The Grid.
  $table_data = lingotek_bulk_grid_parse_table_data($table_data_raw, $entity_properties, $entity_id_key);
  return $table_data;
}