You are here

public function DownloadCountResetForm::submitForm in Download Count 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/DownloadCountResetForm.php, line 163

Class

DownloadCountResetForm
Implements the reset form controller.

Namespace

Drupal\download_count\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $result = NULL;
  if ($form['dcid']['#value'] == 'all') {
    $result = db_truncate('download_count')
      ->execute();
    if ($result) {
      db_truncate('download_count_cache')
        ->execute();
      drupal_set_message($this
        ->t('All download counts have been reset.'));
      Drupal::logger('download_count')
        ->notice('All download counts have been reset.');
    }
    else {
      drupal_set_message($this
        ->t('Unable to reset all download counts.'), 'error');
      Drupal::logger('download_count')
        ->error('Unable to reset all download counts.');
    }
  }
  else {
    $result = db_delete('download_count')
      ->condition('fid', $form['fid']['#value'])
      ->condition('type', $form['type']['#value'])
      ->condition('id', $form['id']['#value'])
      ->execute();
    if ($result) {
      db_delete('download_count_cache')
        ->condition('fid', $form['fid']['#value'])
        ->condition('type', $form['type']['#value'])
        ->condition('id', $form['id']['#value'])
        ->execute();
      drupal_set_message($this
        ->t('Download count for %filename on %type %id was reset.', [
        '%filename' => $form['filename']['#value'],
        '%type' => $form['type']['#value'],
        '%id' => $form['id']['#value'],
      ]));
      Drupal::logger('download_count')
        ->notice('Download count for %filename on %type %id was reset.', [
        '%filename' => $form['filename']['#value'],
        '%type' => $form['type']['#value'],
        '%id' => $form['id']['#value'],
      ]);
    }
    else {
      drupal_set_message($this
        ->t('Unable to reset download count for %filename on %type %id.', [
        '%filename' => $form['filename']['#value'],
        '%type' => $form['type']['#value'],
        '%id' => $form['id']['#value'],
      ]), 'error');
      Drupal::logger('download_count')
        ->error('Unable to reset download count for %filename on %type %id.', [
        '%filename' => $form['filename']['#value'],
        '%type' => $form['type']['#value'],
        '%id' => $form['id']['#value'],
      ]);
    }
  }
  $form_state
    ->setRedirect('download_count.reports');
}