You are here

function bulkdelete_form_submit in Bulk Delete 7

Same name and namespace in other branches
  1. 6 bulkdelete.admin.inc \bulkdelete_form_submit()

Implements hook_form_submit().

File

./bulkdelete.admin.inc, line 59
Administrative interface for the bulkdelete module.

Code

function bulkdelete_form_submit($form, &$form_state) {
  $quick = $form_state['values']['quick'];

  // Process the form results.
  $types = array_filter($form_state['values']['types']);
  if (count($types) > 0) {
    $result = db_select('node')
      ->fields('node', array(
      'nid',
    ))
      ->condition('type', $types)
      ->execute()
      ->fetchAll();
    $operations = array();

    // Doing an empty operation at the beginning makes the "initialization"
    // phase go quickly.
    $operations[] = array(
      'trim',
      array(
        '',
      ),
    );
    $count = 1;
    $last_row = count($result);
    foreach ($result as $row) {
      $nids[] = $row->nid;
      if ($count % 20 === 0 || $count === $last_row) {
        $operations[] = array(
          'node_delete_multiple',
          array(
            $nids,
          ),
        );
        $nids = array();
      }
      ++$count;
    }

    // Clear the cache once at the end.
    // How many operations are we going to do?
    $count_operations = count($operations);

    // Set up the Batch API.
    $batch = array(
      'operations' => $operations,
      'finished' => '_bulkdelete_batch_finished',
      'title' => t('Deleting !count nodes in !count2 operations.', array(
        '!count' => --$count,
        '!count2' => $count_operations,
      )),
    );
    batch_set($batch);
    batch_process();
  }
}