Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
52 / 52 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| StatisticsMigrateForm | |
100.00% |
52 / 52 |
|
100.00% |
9 / 9 |
11 | |
100.00% |
1 / 1 |
| getFormId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getQuestion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDescription | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCancelUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| submitForm | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
2 | |||
| delete | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| insert | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| uninstallStatistics | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| enableVisitorsLogging | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 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 { |
| 21 | return 'visitors_statistics_migrate'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * {@inheritdoc} |
| 26 | */ |
| 27 | public function getQuestion(): TranslatableMarkup { |
| 28 | return $this->t('Migrate statistics node view count?'); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * {@inheritdoc} |
| 33 | */ |
| 34 | public function getDescription(): TranslatableMarkup { |
| 35 | 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 { |
| 42 | return new Url('visitors.settings'); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritdoc} |
| 47 | */ |
| 48 | public function submitForm(array &$form, FormStateInterface $form_state): void { |
| 49 | |
| 50 | $operations = []; |
| 51 | $operations[] = [ |
| 52 | ['Drupal\visitors\Form\StatisticsMigrateForm', 'delete'], |
| 53 | [], |
| 54 | ]; |
| 55 | $operations[] = [ |
| 56 | ['Drupal\visitors\Form\StatisticsMigrateForm', 'insert'], |
| 57 | [], |
| 58 | ]; |
| 59 | $operations[] = [ |
| 60 | ['Drupal\visitors\Form\StatisticsMigrateForm', 'uninstallStatistics'], |
| 61 | [], |
| 62 | ]; |
| 63 | $operations[] = [ |
| 64 | ['Drupal\visitors\Form\StatisticsMigrateForm', 'enableVisitorsLogging'], |
| 65 | [], |
| 66 | ]; |
| 67 | |
| 68 | // Define the batch. |
| 69 | $batch = [ |
| 70 | 'title' => t('Migrating statistics...'), |
| 71 | 'operations' => $operations, |
| 72 | ]; |
| 73 | |
| 74 | // batch_set may not be available in tests. |
| 75 | if (function_exists('batch_set')) { |
| 76 | // Set the batch. |
| 77 | batch_set($batch); |
| 78 | } |
| 79 | $form_state->setRedirectUrl(Url::fromRoute('visitors.settings')); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Delete the statistics table. |
| 84 | */ |
| 85 | public static function delete() { |
| 86 | $database = \Drupal::database(); |
| 87 | $database |
| 88 | ->delete('visitors_counter') |
| 89 | ->condition('entity_type', 'node') |
| 90 | ->execute(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Insert the statistics table into the visitors table. |
| 95 | */ |
| 96 | public static function insert() { |
| 97 | $database = \Drupal::database(); |
| 98 | |
| 99 | $query = $database->select('node_counter', 's'); |
| 100 | $query->addExpression("'node'", 'entity_type'); |
| 101 | $query->addField('s', 'nid', 'entity_id'); |
| 102 | $query->addField('s', 'totalcount', 'total'); |
| 103 | $query->addField('s', 'daycount', 'today'); |
| 104 | $query->addField('s', 'timestamp', 'timestamp'); |
| 105 | |
| 106 | $insert = $database->insert('visitors_counter') |
| 107 | ->fields([ |
| 108 | 'entity_id', |
| 109 | 'total', |
| 110 | 'today', |
| 111 | 'timestamp', |
| 112 | 'entity_type', |
| 113 | ]) |
| 114 | ->from($query); |
| 115 | |
| 116 | $insert->execute(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Uninstall the Statistics module. |
| 121 | */ |
| 122 | public static function uninstallStatistics() { |
| 123 | $module_installer = \Drupal::service('module_installer'); |
| 124 | if ($module_installer->uninstall(['statistics'])) { |
| 125 | \Drupal::messenger()->addStatus(t('The Statistics module has been uninstalled.')); |
| 126 | } |
| 127 | else { |
| 128 | \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() { |
| 136 | $config = \Drupal::configFactory()->getEditable('visitors.settings'); |
| 137 | $config->set('counter.enabled', TRUE); |
| 138 | $config->save(); |
| 139 | } |
| 140 | |
| 141 | } |