You are here

function auditfiles_not_in_database_form in Audit Files 7.4

Same name and namespace in other branches
  1. 7.3 auditfiles.notindatabase.inc \auditfiles_not_in_database_form()

Generates the report.

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_not_in_database_form'
auditfiles_menu in ./auditfiles.module
Implements hook_menu().

File

./auditfiles.notindatabase.inc, line 22
Generates a report showing files on the server, but not in the database.

Code

function auditfiles_not_in_database_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']['confirm'])) {
    return _auditfiles_not_in_database_confirm_operation($form, $form_state);
  }
  $output = '<p>' . t('Files in "temporary" folders such as those created by
    the image module are included in order to check that they are not filling
    up.') . '</p>';
  $output .= '<p>' . t(' You can choose to delete files on this report, but
    remember that if you do that, the action cannot be undone.');
  $output .= t(' You can also add files to the file_managed table - you
    might do that, if you have uploaded files outside of Drupal (e.g., via FTP)
    and will be attaching them to nodes through other tools.') . '</p>';
  $output .= t('The files in this list are using the %scheme scheme and are
    relative to the files directory path, located at %path.', array(
    '%scheme' => file_default_scheme(),
    '%path' => variable_get('file_' . file_default_scheme() . '_path', conf_path() . '/files'),
  )) . '</p>';
  $form['introduction'] = array(
    '#markup' => $output,
  );

  // Get the records to display.
  // Check to see if the data has been stored.
  if (!empty($form_state['storage']['saved_rows'])) {

    // The data is currently saved, so use that.
    $rows = unserialize($form_state['storage']['saved_rows']);
  }
  else {

    // Check to see if the batch operation was just run. If so, use that data.
    $rows = variable_get('auditfiles_not_in_database_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_ids = _auditfiles_not_in_database_get_file_list(drupal_realpath(file_default_scheme() . '://'), TRUE);
      if (!empty($file_ids)) {
        $date_format = variable_get('auditfiles_report_options_date_format', 'long');
        foreach ($file_ids as $file_id) {
          $row = _auditfiles_not_in_database_get_file_data($file_id, $date_format);
          if (isset($row)) {
            $rows[$file_id] = $row;
          }
        }
      }
    }
  }

  // Save the data for later retrieval.
  $form['saved_rows'] = array(
    '#type' => 'hidden',
    '#value' => serialize($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);
    }
  }

  // Get any specified record selection limiters.
  $record_limiters = _auditfiles_not_in_database_get_record_limiters();
  $record_selection = $record_limiters['record_selection'];
  $maximum_records = $record_limiters['maximum_records'];

  // Define the form.
  // Setup the record count and related messages.
  if (!empty($rows)) {
    if ($record_selection == 'batch_sets') {
      $file_count_message = 'Found @count files in this batch set that are on the server but not in the database.';
    }
    elseif ($maximum_records > 0) {
      $file_count_message = 'Found at least @count files on the server that are not in the database.';
    }
    else {
      $file_count_message = 'Found @count files on the server that are not in the database.';
    }
    $form_count = format_plural(count($rows), 'Found 1 file on the server that is not in the database.', $file_count_message);
  }
  else {
    $form_count = 'Found no files on the server that are not in the database.';
  }

  // Add the button to batch process the list of results.
  if ($record_selection == 'limited') {
    $batch_size = variable_get('auditfiles_report_options_batch_size', 0);
    if ($batch_size > 0) {
      $form['batch_process'] = array(
        '#type' => 'submit',
        '#value' => t('Load first batch set'),
      );
    }
    else {
      $form['batch_process'] = array(
        '#type' => 'submit',
        '#value' => t('Load all records'),
      );
    }
  }
  elseif ($record_selection == 'batch_sets') {

    // Add the button to load the previous batch set.
    $form['batch_process_prev'] = array(
      '#type' => 'submit',
      '#value' => t('Load previous batch set'),
    );

    // Add the button to load the next batch set.
    $form['batch_process_next'] = array(
      '#type' => 'submit',
      '#value' => t('Load next batch set'),
    );
  }

  // Add the button to reset the record selection.
  if ($record_selection != 'normal') {
    $form['reset_records'] = array(
      '#type' => 'submit',
      '#value' => t('Reset record selection'),
    );
  }
  $form['files'] = array(
    '#type' => 'tableselect',
    '#header' => _auditfiles_not_in_database_get_header(),
    '#empty' => t('No items found.'),
    '#prefix' => '<div><em>' . $form_count . '</em></div>',
  );
  if (!empty($rows) && !empty($pages)) {
    $form['files']['#options'] = $pages[$current_page];
  }
  elseif (!empty($rows)) {
    $form['files']['#options'] = $rows;
  }
  else {
    $form['files']['#options'] = array();
  }
  if (!empty($rows)) {
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['add'] = array(
      '#type' => 'submit',
      '#value' => t('Add selected items to the database'),
    );
    $form['actions']['markup'] = array(
      '#markup' => '&nbsp;' . t('or') . '&nbsp;',
    );
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete selected items from the server'),
    );
    $form['pager'] = array(
      '#markup' => theme('pager'),
    );
  }
  return $form;
}