Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
90 / 90 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| VisitorsOnlineBlock | |
100.00% |
90 / 90 |
|
100.00% |
7 / 7 |
13 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| defaultConfiguration | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| blockForm | |
100.00% |
37 / 37 |
|
100.00% |
1 / 1 |
1 | |||
| blockSubmit | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| build | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
7 | |||
| getCacheMaxAge | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) { |
| 49 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 50 | |
| 51 | $this->online = $visitors_online; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * {@inheritdoc} |
| 56 | */ |
| 57 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 58 | return new self( |
| 59 | $configuration, |
| 60 | $plugin_id, |
| 61 | $plugin_definition, |
| 62 | $container->get('visitors.online'), |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * {@inheritdoc} |
| 68 | */ |
| 69 | public function defaultConfiguration() { |
| 70 | return [ |
| 71 | 'now_30_minute' => TRUE, |
| 72 | 'now_24_hour' => FALSE, |
| 73 | 'yesterday_30_minute' => FALSE, |
| 74 | 'yesterday_24_hour' => FALSE, |
| 75 | 'last_week_30_minute' => FALSE, |
| 76 | 'last_week_24_hour' => FALSE, |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritdoc} |
| 82 | */ |
| 83 | public function blockForm($form, FormStateInterface $form_state) { |
| 84 | |
| 85 | $form['now_30_minute'] = [ |
| 86 | '#type' => 'checkbox', |
| 87 | '#title' => $this->t('Active visitors (last 30 min)'), |
| 88 | '#description' => $this->t('Show the number of visitors in the last 30 minutes.'), |
| 89 | '#default_value' => $this->configuration['now_30_minute'], |
| 90 | ]; |
| 91 | $form['now_24_hour'] = [ |
| 92 | '#type' => 'checkbox', |
| 93 | '#title' => $this->t('Visitors in the past 24 hours'), |
| 94 | '#description' => $this->t('Show the number of visitors in the past 24 hours.'), |
| 95 | '#default_value' => $this->configuration['now_24_hour'], |
| 96 | ]; |
| 97 | $form['yesterday_30_minute'] = [ |
| 98 | '#type' => 'checkbox', |
| 99 | '#title' => $this->t('Visitors yesterday (same 30 min)'), |
| 100 | '#description' => $this->t('Show the number of visitors in the same 30-minute period yesterday.'), |
| 101 | '#default_value' => $this->configuration['yesterday_30_minute'], |
| 102 | ]; |
| 103 | $form['yesterday_24_hour'] = [ |
| 104 | '#type' => 'checkbox', |
| 105 | '#title' => $this->t('Visitors yesterday (total)'), |
| 106 | '#description' => $this->t('Show the total number of visitors yesterday.'), |
| 107 | '#default_value' => $this->configuration['yesterday_24_hour'], |
| 108 | ]; |
| 109 | $form['last_week_30_minute'] = [ |
| 110 | '#type' => 'checkbox', |
| 111 | '#title' => $this->t('Visitors last week (same 30 min)'), |
| 112 | '#description' => $this->t('Show the number of visitors in the same 30-minute period last week.'), |
| 113 | '#default_value' => $this->configuration['last_week_30_minute'], |
| 114 | ]; |
| 115 | $form['last_week_24_hour'] = [ |
| 116 | '#type' => 'checkbox', |
| 117 | '#title' => $this->t('Visitors last week (same day)'), |
| 118 | '#description' => $this->t('Show the total number of visitors on the same day last week.'), |
| 119 | '#default_value' => $this->configuration['last_week_24_hour'], |
| 120 | ]; |
| 121 | |
| 122 | return $form; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritdoc} |
| 127 | */ |
| 128 | public function blockSubmit($form, FormStateInterface $form_state) { |
| 129 | $this->configuration['now_30_minute'] = $form_state->getValue('now_30_minute'); |
| 130 | $this->configuration['now_24_hour'] = $form_state->getValue('now_24_hour'); |
| 131 | $this->configuration['yesterday_30_minute'] = $form_state->getValue('yesterday_30_minute'); |
| 132 | $this->configuration['yesterday_24_hour'] = $form_state->getValue('yesterday_24_hour'); |
| 133 | $this->configuration['last_week_30_minute'] = $form_state->getValue('last_week_30_minute'); |
| 134 | $this->configuration['last_week_24_hour'] = $form_state->getValue('last_week_24_hour'); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * {@inheritdoc} |
| 139 | */ |
| 140 | public function build() { |
| 141 | |
| 142 | $items = []; |
| 143 | if ($this->configuration['now_30_minute']) { |
| 144 | $items[] = $this->t('Last 30 min: @count', [ |
| 145 | '@count' => $this->online->getLast30Minutes(), |
| 146 | ]); |
| 147 | } |
| 148 | if ($this->configuration['now_24_hour']) { |
| 149 | $items[] = $this->t('Last 24 hours: @count', [ |
| 150 | '@count' => $this->online->getLast24Hours(), |
| 151 | ]); |
| 152 | } |
| 153 | if ($this->configuration['yesterday_30_minute']) { |
| 154 | $items[] = $this->t('Yesterday (same 30 min): @count', [ |
| 155 | '@count' => $this->online->getYesterday30Minutes(), |
| 156 | ]); |
| 157 | } |
| 158 | if ($this->configuration['yesterday_24_hour']) { |
| 159 | $items[] = $this->t('Yesterday (total): @count', [ |
| 160 | '@count' => $this->online->getYesterday24Hours(), |
| 161 | ]); |
| 162 | } |
| 163 | if ($this->configuration['last_week_30_minute']) { |
| 164 | $items[] = $this->t('Last week (same 30 min): @count', [ |
| 165 | '@count' => $this->online->getLastWeek30Minutes(), |
| 166 | ]); |
| 167 | } |
| 168 | if ($this->configuration['last_week_24_hour']) { |
| 169 | $items[] = $this->t('Last week (same day): @count', [ |
| 170 | '@count' => $this->online->getLastWeek24Hours(), |
| 171 | ]); |
| 172 | } |
| 173 | |
| 174 | $content = [ |
| 175 | '#theme' => 'item_list', |
| 176 | '#items' => $items, |
| 177 | ]; |
| 178 | |
| 179 | return $content; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * {@inheritdoc} |
| 184 | */ |
| 185 | public function getCacheMaxAge() { |
| 186 | return self::MINUTE_5; |
| 187 | } |
| 188 | |
| 189 | } |