Coverage index

src/Form/StatisticsMigrateForm.php

Lines: 91.23%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Form;
6
7
use Drupal\Core\Form\ConfirmFormBase;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\Core\StringTranslation\TranslatableMarkup;
10
use Drupal\Core\Url;
11
12
/**
13
 * Form for migrating statistics to visitors.
14
 */
15
final class StatisticsMigrateForm extends ConfirmFormBase {
16
17
  /**
18
   * {@inheritdoc}
19
   */
20
  public function getFormId(): string {
211
    return 'visitors_statistics_migrate';
22
  }
23
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function getQuestion(): TranslatableMarkup {
281
    return $this->t('Migrate statistics node view count?');
29
  }
30
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function getDescription(): TranslatableMarkup {
351
    return $this->t('This will migrate the Statistics view count to Visitors, and uninstall Statistics.');
36
  }
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public function getCancelUrl(): Url {
421
    return new Url('visitors.settings');
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function submitForm(array &$form, FormStateInterface $form_state): void {
49
500
    $operations = [];
510
    $operations[] = [
520
      ['Drupal\visitors\Form\StatisticsMigrateForm', 'delete'],
530
      [],
540
    ];
551
    $operations[] = [
561
      ['Drupal\visitors\Form\StatisticsMigrateForm', 'insert'],
571
      [],
581
    ];
591
    $operations[] = [
601
      ['Drupal\visitors\Form\StatisticsMigrateForm', 'uninstallStatistics'],
611
      [],
621
    ];
631
    $operations[] = [
641
      ['Drupal\visitors\Form\StatisticsMigrateForm', 'enableVisitorsLogging'],
651
      [],
661
    ];
67
68
    // Define the batch.
691
    $batch = [
701
      'title' => t('Migrating statistics...'),
711
      'operations' => $operations,
721
    ];
73
74
    // batch_set may not be available in tests.
751
    if (function_exists('batch_set')) {
76
      // Set the batch.
771
      batch_set($batch);
78
    }
791
    $form_state->setRedirectUrl(Url::fromRoute('visitors.settings'));
80
  }
81
82
  /**
83
   * Delete the statistics table.
84
   */
85
  public static function delete() {
861
    $database = \Drupal::database();
871
    $database
881
      ->delete('visitors_counter')
891
      ->condition('entity_type', 'node')
901
      ->execute();
91
  }
92
93
  /**
94
   * Insert the statistics table into the visitors table.
95
   */
96
  public static function insert() {
971
    $database = \Drupal::database();
98
991
    $query = $database->select('node_counter', 's');
1001
    $query->addExpression("'node'", 'entity_type');
1011
    $query->addField('s', 'nid', 'entity_id');
1021
    $query->addField('s', 'totalcount', 'total');
1031
    $query->addField('s', 'daycount', 'today');
1041
    $query->addField('s', 'timestamp', 'timestamp');
105
1061
    $insert = $database->insert('visitors_counter')
1071
      ->fields([
1081
        'entity_id',
1091
        'total',
1101
        'today',
1111
        'timestamp',
1121
        'entity_type',
1131
      ])
1141
      ->from($query);
115
1161
    $insert->execute();
117
  }
118
119
  /**
120
   * Uninstall the Statistics module.
121
   */
122
  public static function uninstallStatistics() {
1232
    $module_installer = \Drupal::service('module_installer');
1242
    if ($module_installer->uninstall(['statistics'])) {
1251
      \Drupal::messenger()->addStatus(t('The Statistics module has been uninstalled.'));
126
    }
127
    else {
1281
      \Drupal::messenger()->addError(t('The Statistics module could not be uninstalled.'));
129
    }
130
  }
131
132
  /**
133
   * Enable visitors logging.
134
   */
135
  public static function enableVisitorsLogging() {
1361
    $config = \Drupal::configFactory()->getEditable('visitors.settings');
1371
    $config->set('counter.enabled', TRUE);
1381
    $config->save();
139
  }
140
141
}