You are here

function auditfiles_merge_file_references_form in Audit Files 7.3

Generates the report.

This cannot be sorted, because a result set that is too large will time out.

Parameters

array $form: The form definition.

array $form_state: The current state of the form.

Return value

array The form definition.

1 string reference to 'auditfiles_merge_file_references_form'
auditfiles_menu in ./auditfiles.module
Implements hook_menu().

File

./auditfiles.mergefilereferences.inc, line 25
Generates a report showing & allowing for merging potential duplicate files.

Code

function auditfiles_merge_file_references_form(array $form, array &$form_state) {

  // Check to see if the confirmation form needs to be displayed instead of the
  // normal form.
  if (isset($form_state['storage']['stage'])) {
    if ($form_state['storage']['stage'] == 'confirm') {
      return _auditfiles_merge_file_references_confirm_operation($form, $form_state);
    }
    elseif ($form_state['storage']['stage'] == 'preconfirm') {
      return _auditfiles_merge_file_references_pre_confirm_operation($form, $form_state);
    }
  }

  // Get the records to display.
  // Check to see if there is saved data, and if so, use that.
  $rows = variable_get('auditfiles_merge_file_references_files_to_display', array());
  if (empty($rows)) {

    // The data is not saved and the batch operation has not been run, so get
    // the data using the default options.
    $file_names = _auditfiles_merge_file_references_get_file_list();
    if (!empty($file_names)) {
      $date_format = variable_get('auditfiles_report_options_date_format', 'long');
      foreach ($file_names as $file_name) {
        $rows[$file_name] = _auditfiles_merge_file_references_get_file_data($file_name, $date_format);
      }
    }

    // Save the data for persistent use.
    variable_set('auditfiles_merge_file_references_files_to_display', $rows);
  }

  // Set up the pager.
  if (!empty($rows)) {
    $items_per_page = variable_get('auditfiles_report_options_items_per_page', 50);
    if (!empty($items_per_page)) {
      $current_page = pager_default_initialize(count($rows), $items_per_page);

      // Break the total data set into page sized chunks.
      $pages = array_chunk($rows, $items_per_page, TRUE);
    }
  }

  // Define the form.
  // Setup the record count and related messages.
  $maximum_records = variable_get('auditfiles_report_options_maximum_records', 250);
  if (!empty($rows)) {
    if ($maximum_records > 0) {
      $file_count_message = 'Found at least @count files in the file_managed table with duplicate file names.';
    }
    else {
      $file_count_message = 'Found @count files in the file_managed table with duplicate file names.';
    }
    $form_count = format_plural(count($rows), 'Found 1 file in the file_managed table with a duplicate file name.', $file_count_message);
  }
  else {
    $form_count = t('Found no files in the file_managed table with duplicate file names.');
  }

  // Add filtering options.
  $form['filter']['single_file_names']['auditfiles_merge_file_references_show_single_file_names'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show files without duplicate names'),
    '#default_value' => variable_get('auditfiles_merge_file_references_show_single_file_names', 0),
    '#suffix' => '<div>' . t("Use this button to reset this report's variables and load the page anew.") . '</div>',
  );

  // Add the button to reset the record selection.
  $form['reset_records'] = array(
    '#type' => 'submit',
    '#value' => t('Reset file list'),
    '#suffix' => '<div>' . t("Use this button to reset this report's variables and load it anew. This is also useful, if you have loaded data via the \"Load all files\" button and want to clear it.") . '</div>',
  );

  // Add the button to batch process the list of results.
  if ($maximum_records > 0) {
    $form['batch_process'] = array(
      '#type' => 'submit',
      '#value' => t('Load all files'),
      '#suffix' => '<div>' . t('Use this button to load the number of records specified with the "Batch size" administrative configuration setting.') . '</div>',
    );
  }

  // Create the form table.
  $form['files'] = array(
    '#type' => 'tableselect',
    '#header' => _auditfiles_merge_file_references_get_header(),
    '#empty' => t('No items found.'),
    '#prefix' => '<div><em>' . $form_count . '</em></div>',
  );

  // Add the data.
  if (!empty($rows) && !empty($pages)) {
    $form['files']['#options'] = $pages[$current_page];
  }
  elseif (!empty($rows)) {
    $form['files']['#options'] = $rows;
  }
  else {
    $form['files']['#options'] = array();
  }

  // Add any action buttons.
  if (!empty($rows)) {
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Merge selected items'),
    );

    // Add the pager.
    $form['pager'] = array(
      '#markup' => theme('pager'),
    );
  }
  return $form;
}