Coverage index

src/Plugin/Block/VisitorsOnlineBlock.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Plugin\Block;
6
7
use Drupal\Core\Block\Attribute\Block;
8
use Drupal\Core\Block\BlockBase;
9
use Drupal\Core\Form\FormStateInterface;
10
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11
use Drupal\Core\StringTranslation\TranslatableMarkup;
12
use Drupal\visitors\VisitorsOnlineInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Provides a 'Visitors Online' block.
17
 */
18
#[Block(
19
  id: "visitors_online",
20
  admin_label: new TranslatableMarkup("Visitors Online")
21
)]
22
final class VisitorsOnlineBlock extends BlockBase implements ContainerFactoryPluginInterface {
23
24
  /**
25
   * Five minutes in seconds.
26
   */
27
  const MINUTE_5 = 300;
28
29
  /**
30
   * The visitors online service.
31
   *
32
   * @var \Drupal\visitors\VisitorsOnlineInterface
33
   */
34
  protected $online;
35
36
  /**
37
   * Constructs a StatisticsPopularBlock object.
38
   *
39
   * @param array $configuration
40
   *   A configuration array containing information about the plugin instance.
41
   * @param string $plugin_id
42
   *   The plugin_id for the plugin instance.
43
   * @param mixed $plugin_definition
44
   *   The plugin implementation definition.
45
   * @param \Drupal\visitors\VisitorsOnlineInterface $visitors_online
46
   *   The visitors online service.
47
   */
48
  public function __construct(array $configuration, $plugin_id, $plugin_definition, VisitorsOnlineInterface $visitors_online) {
497
    parent::__construct($configuration, $plugin_id, $plugin_definition);
50
517
    $this->online = $visitors_online;
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
587
    return new self(
597
      $configuration,
607
      $plugin_id,
617
      $plugin_definition,
627
      $container->get('visitors.online'),
637
    );
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public function defaultConfiguration() {
707
    return [
717
      'now_30_minute' => TRUE,
727
      'now_24_hour' => FALSE,
737
      'yesterday_30_minute' => FALSE,
747
      'yesterday_24_hour' => FALSE,
757
      'last_week_30_minute' => FALSE,
767
      'last_week_24_hour' => FALSE,
777
    ];
78
  }
79
80
  /**
81
   * {@inheritdoc}
82
   */
83
  public function blockForm($form, FormStateInterface $form_state) {
84
851
    $form['now_30_minute'] = [
861
      '#type' => 'checkbox',
871
      '#title' => $this->t('Active visitors (last 30 min)'),
881
      '#description' => $this->t('Show the number of visitors in the last 30 minutes.'),
891
      '#default_value' => $this->configuration['now_30_minute'],
901
    ];
911
    $form['now_24_hour'] = [
921
      '#type' => 'checkbox',
931
      '#title' => $this->t('Visitors in the past 24 hours'),
941
      '#description' => $this->t('Show the number of visitors in the past 24 hours.'),
951
      '#default_value' => $this->configuration['now_24_hour'],
961
    ];
971
    $form['yesterday_30_minute'] = [
981
      '#type' => 'checkbox',
991
      '#title' => $this->t('Visitors yesterday (same 30 min)'),
1001
      '#description' => $this->t('Show the number of visitors in the same 30-minute period yesterday.'),
1011
      '#default_value' => $this->configuration['yesterday_30_minute'],
1021
    ];
1031
    $form['yesterday_24_hour'] = [
1041
      '#type' => 'checkbox',
1051
      '#title' => $this->t('Visitors yesterday (total)'),
1061
      '#description' => $this->t('Show the total number of visitors yesterday.'),
1071
      '#default_value' => $this->configuration['yesterday_24_hour'],
1081
    ];
1091
    $form['last_week_30_minute'] = [
1101
      '#type' => 'checkbox',
1111
      '#title' => $this->t('Visitors last week (same 30 min)'),
1121
      '#description' => $this->t('Show the number of visitors in the same 30-minute period last week.'),
1131
      '#default_value' => $this->configuration['last_week_30_minute'],
1141
    ];
1151
    $form['last_week_24_hour'] = [
1161
      '#type' => 'checkbox',
1171
      '#title' => $this->t('Visitors last week (same day)'),
1181
      '#description' => $this->t('Show the total number of visitors on the same day last week.'),
1191
      '#default_value' => $this->configuration['last_week_24_hour'],
1201
    ];
121
1221
    return $form;
123
  }
124
125
  /**
126
   * {@inheritdoc}
127
   */
128
  public function blockSubmit($form, FormStateInterface $form_state) {
1291
    $this->configuration['now_30_minute'] = $form_state->getValue('now_30_minute');
1301
    $this->configuration['now_24_hour'] = $form_state->getValue('now_24_hour');
1311
    $this->configuration['yesterday_30_minute'] = $form_state->getValue('yesterday_30_minute');
1321
    $this->configuration['yesterday_24_hour'] = $form_state->getValue('yesterday_24_hour');
1331
    $this->configuration['last_week_30_minute'] = $form_state->getValue('last_week_30_minute');
1341
    $this->configuration['last_week_24_hour'] = $form_state->getValue('last_week_24_hour');
135
  }
136
137
  /**
138
   * {@inheritdoc}
139
   */
140
  public function build() {
141
1421
    $items = [];
1431
    if ($this->configuration['now_30_minute']) {
1441
      $items[] = $this->t('Last 30 min: @count', [
1451
        '@count' => $this->online->getLast30Minutes(),
1461
      ]);
147
    }
1481
    if ($this->configuration['now_24_hour']) {
1491
      $items[] = $this->t('Last 24 hours: @count', [
1501
        '@count' => $this->online->getLast24Hours(),
1511
      ]);
152
    }
1531
    if ($this->configuration['yesterday_30_minute']) {
1541
      $items[] = $this->t('Yesterday (same 30 min): @count', [
1551
        '@count' => $this->online->getYesterday30Minutes(),
1561
      ]);
157
    }
1581
    if ($this->configuration['yesterday_24_hour']) {
1591
      $items[] = $this->t('Yesterday (total): @count', [
1601
        '@count' => $this->online->getYesterday24Hours(),
1611
      ]);
162
    }
1631
    if ($this->configuration['last_week_30_minute']) {
1641
      $items[] = $this->t('Last week (same 30 min): @count', [
1651
        '@count' => $this->online->getLastWeek30Minutes(),
1661
      ]);
167
    }
1681
    if ($this->configuration['last_week_24_hour']) {
1691
      $items[] = $this->t('Last week (same day): @count', [
1701
        '@count' => $this->online->getLastWeek24Hours(),
1711
      ]);
172
    }
173
1741
    $content = [
1751
      '#theme' => 'item_list',
1761
      '#items' => $items,
1771
    ];
178
1791
    return $content;
180
  }
181
182
  /**
183
   * {@inheritdoc}
184
   */
185
  public function getCacheMaxAge() {
1861
    return self::MINUTE_5;
187
  }
188
189
}