Coverage index

src/Controller/ReportController.php

Lines: 84.74%

LineHitsSource
1
<?php
2
3
namespace Drupal\visitors\Controller;
4
5
use Drupal\Core\Config\ConfigFactoryInterface;
6
use Drupal\Core\Controller\ControllerBase;
7
use Drupal\Core\Extension\ModuleHandlerInterface;
8
use Drupal\Core\Form\FormBuilderInterface;
9
use Drupal\Core\Messenger\MessengerInterface;
10
use Drupal\Core\Session\AccountProxyInterface;
11
use Drupal\Core\StringTranslation\TranslationInterface;
12
use Drupal\visitors\VisitorsLocationInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Generic Report controller.
17
 */
18
class ReportController extends ControllerBase {
19
20
21
  /**
22
   * The date service.
23
   *
24
   * @var \Drupal\Core\Datetime\DateFormatterInterface
25
   */
26
  protected $date;
27
28
  /**
29
   * The form builder service.
30
   *
31
   * @var \Drupal\Core\Form\FormBuilderInterface
32
   */
33
  protected $formBuilder;
34
35
  /**
36
   * The settings.
37
   *
38
   * @var \Drupal\Core\Config\Config
39
   */
40
  protected $settings;
41
42
  /**
43
   * The current user.
44
   *
45
   * @var \Drupal\Core\Session\AccountProxyInterface
46
   */
47
  protected $account;
48
49
  /**
50
   * The location service.
51
   *
52
   * @var \Drupal\visitors\VisitorsLocationInterface
53
   */
54
  protected $location;
55
56
  /**
57
   * The module handler.
58
   *
59
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
60
   */
61
  protected $moduleHandler;
62
63
  /**
64
   * {@inheritdoc}
65
   */
66
  public static function create(ContainerInterface $container) {
6740
    return new self(
6840
      $container->get('form_builder'),
6940
      $container->get('string_translation'),
7040
      $container->get('config.factory'),
7140
      $container->get('messenger'),
7240
      $container->get('current_user'),
7340
      $container->get('visitors.location'),
7440
      $container->get('module_handler')
7540
    );
76
  }
77
78
  /**
79
   * Constructs the report controller.
80
   *
81
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
82
   *   The form builder service.
83
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
84
   *   The string translation service.
85
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
86
   *   The config factory.
87
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
88
   *   The messenger service.
89
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
90
   *   The current user.
91
   * @param \Drupal\visitors\VisitorsLocationInterface $location
92
   *   The location service.
93
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
94
   *   The module handler.
95
   */
96
  public function __construct(FormBuilderInterface $form_builder, TranslationInterface $string_translation, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, AccountProxyInterface $current_user, VisitorsLocationInterface $location, ModuleHandlerInterface $module_handler) {
97
9840
    $this->formBuilder   = $form_builder;
9940
    $this->account       = $current_user;
10040
    $this->location      = $location;
10140
    $this->moduleHandler = $module_handler;
102
10340
    $this->settings = $config_factory->get('visitors.settings');
104
10540
    $this->setStringTranslation($string_translation);
10640
    $this->setMessenger($messenger);
107
  }
108
109
  /**
110
   * Render the registered blocks as output.
111
   *
112
   * @param array $blocks
113
   *   An array of block items formatted for rendering a view.
114
   * @param string|null $class
115
   *   An optional class to add to the wrapper div.
116
   * @param array|null $args
117
   *   An optional array of arguments to pass to the view.
118
   */
119
  public function renderViews(array $blocks, $class = NULL, $args = NULL) {
12032
    $output = [];
121
    // Render each block element.
12232
    foreach ($blocks as $block) {
12332
      if (empty($block['#view_id'])) {
124
1251
        $build = $block;
126
      }
127
      else {
12831
        $view_id = $block['#view_id'];
12931
        $display_id = $block['#view_display'];
130
13131
        $arguments = [];
13231
        if (!is_null($args)) {
1339
          $arguments = is_array($args) ? $args : [$args];
134
        }
135
        // The view render element is what views_embed_view() returns, and
136
        // it is available on every supported Drupal release.
13731
        $build = [
13831
          '#type'       => 'view',
13931
          '#name'       => $view_id,
14031
          '#display_id' => $display_id,
14131
          '#arguments'  => $arguments,
14231
        ];
143
144
      }
14532
      if (!isset($build['#attributes']) && isset($block['#attributes'])) {
1467
        $build['#attributes'] = $block['#attributes'];
147
      }
148
14932
      $output[] = $build;
150
    }
15132
    $prefix = '<div>';
15232
    if (!empty($class)) {
15311
      $prefix = '<div class="' . $class . '">';
154
    }
15532
    return [
15632
      '#prefix'   => $prefix,
15732
      'blocks'    => $output,
15832
      '#suffix'   => '</div>',
15932
    ];
160
  }
161
162
  /**
163
   * Returns software report.
164
   *
165
   * @return array
166
   *   A render array representing the days of month page content.
167
   */
168
  public function software(): array {
169
1701
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
171
    $first_row = [];
172
    $second_row = [];
173
174
    $first_row['os_version_table'] = [
175
      '#view_id'      => 'visitors',
176
      '#view_display' => 'os_version_table',
177
      '#attributes'   => [
178
        'class' => ['layout-column--half'],
179
      ],
180
    ];
1811
    $first_row['browser_version_table'] = [
1821
      '#view_id'      => 'visitors',
1831
      '#view_display' => 'browser_version_table',
1841
      '#attributes'   => [
1851
        'class' => ['layout-column--half'],
1861
      ],
1871
    ];
188
189
    $second_row['device_config_table'] = [
190
      '#view_id'      => 'visitors',
191
      '#view_display' => 'device_config_table',
192
      '#attributes'   => [
193
        'class' => ['layout-column--half'],
194
      ],
195
    ];
1961
    $second_row['browser_engine_pie'] = [
1971
      '#view_id'      => 'visitors',
1981
      '#view_display' => 'browser_engine_pie',
1991
      '#attributes'   => [
2001
        'class' => ['layout-column--half'],
2011
      ],
2021
    ];
203
204
    $third_row['browser_plugin_list'] = [
205
      '#view_id'      => 'visitors',
206
      '#view_display' => 'browser_plugin_list',
207
      '#attributes'   => [
208
        'class' => ['layout-column--half'],
209
      ],
210
    ];
211
2121
    return [
2131
      'visitors_date_filter_form' => $form,
2141
      'main' => [
2151
        '#type' => 'container',
2161
        '#attributes' => [
2171
          'class' => ['visitors-main'],
2181
        ],
2191
        '1' => [
2201
          $this->renderViews($first_row, 'layout-row'),
2211
        ],
2221
        '2' => [
2231
          $this->renderViews($second_row, 'layout-row'),
2241
        ],
2251
        '3' => [
2261
          $this->renderViews($third_row, 'layout-row'),
2271
        ],
2281
      ],
2291
      '#attached' => [
2301
        'library' => [
2311
          'visitors/visitors.report',
2321
        ],
2331
      ],
2341
    ];
235
  }
236
237
  /**
238
   * Returns a time report.
239
   *
240
   * @return array
241
   *   A render array representing the hours page content.
242
   */
243
  public function time($scope = 'visit'): array {
2441
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
245
246
    $first_row['daily_column'] = [
247
      '#view_id'      => 'visitors',
248
      '#view_display' => "daily_column",
249
    ];
250
251
    $second_row['local_hour_column'] = [
252
      '#view_id'      => 'visitors',
253
      '#view_display' => "local_hour_column",
254
      '#attributes'   => [
255
        'class' => ['layout-column--half'],
256
      ],
257
    ];
2581
    $second_row['hour_column'] = [
2591
      '#view_id'      => 'visitors',
2601
      '#view_display' => "hour_column",
2611
      '#attributes'   => [
2621
        'class' => ['layout-column--half'],
2631
      ],
2641
    ];
265
266
    $third_row['day_of_week_column'] = [
267
      '#view_id'      => 'visitors',
268
      '#view_display' => "day_of_week_column",
269
      '#attributes'   => [
270
        'class' => ['layout-column--half'],
271
      ],
272
    ];
2731
    $third_row['day_of_month_column'] = [
2741
      '#view_id'      => 'visitors',
2751
      '#view_display' => "day_of_month_column",
2761
      '#attributes'   => [
2771
        'class' => ['layout-column--half'],
2781
      ],
2791
    ];
280
281
    $fourth_row['monthly_column'] = [
282
      '#view_id'      => 'visitors',
283
      '#view_display' => "monthly_column",
284
    ];
285
2861
    return [
2871
      'visitors_date_filter_form' => $form,
288
2891
      'main' => [
2901
        '#type' => 'container',
2911
        '#attributes' => [
2921
          'class' => ['visitors-main'],
2931
        ],
2941
        '1' => [
2951
          $this->renderViews($first_row),
2961
        ],
2971
        '2' => [
2981
          $this->renderViews($second_row, 'layout-row'),
2991
        ],
3001
        '3' => [
3011
          $this->renderViews($third_row, 'layout-row'),
3021
        ],
3031
        '4' => [
3041
          $this->renderViews($fourth_row),
3051
        ],
3061
      ],
3071
      '#attached' => [
3081
        'library' => [
3091
          'visitors/visitors.report',
3101
        ],
3111
      ],
3121
    ];
313
  }
314
315
  /**
316
   * Returns the outbound link report.
317
   *
318
   * @return array
319
   *   A render array representing the outlinks page content.
320
   */
321
  public function outlinks(): array {
3220
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
323
    $blocks['outlinks_table'] = [
324
      '#view_id'      => 'visitors',
325
      '#view_display' => 'outlinks_table',
326
    ];
327
3280
    return [
3290
      'visitors_date_filter_form' => $form,
3300
      'main' => [
3310
        '#type' => 'container',
3320
        '#attributes' => [
3330
          'class' => ['visitors-main'],
3340
        ],
3350
        '1' => [
3360
          $this->renderViews($blocks),
3370
        ],
3380
      ],
3390
    ];
340
  }
341
342
  /**
343
   * Returns the audio and video report.
344
   *
345
   * @return array
346
   *   A render array representing the media page content.
347
   */
348
  public function media(): array {
3490
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
350
    $blocks['media_table'] = [
351
      '#view_id' => 'visitors',
352
      '#view_display' => 'media_table',
353
    ];
354
3550
    return [
3560
      'visitors_date_filter_form' => $form,
3570
      'main' => [
3580
        '#type' => 'container',
3590
        '#attributes' => [
3600
          'class' => ['visitors-main'],
3610
        ],
3620
        '1' => [
3630
          $this->renderViews($blocks),
3640
        ],
3650
      ],
3660
    ];
367
  }
368
369
  /**
370
   * Returns the file download report.
371
   *
372
   * @return array
373
   *   A render array representing the downloads page content.
374
   */
375
  public function downloads(): array {
3760
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
377
    $blocks['downloads_table'] = [
378
      '#view_id'      => 'visitors',
379
      '#view_display' => 'downloads_table',
380
    ];
381
3820
    return [
3830
      'visitors_date_filter_form' => $form,
3840
      'main' => [
3850
        '#type' => 'container',
3860
        '#attributes' => [
3870
          'class' => ['visitors-main'],
3880
        ],
3890
        '1' => [
3900
          $this->renderViews($blocks),
3910
        ],
3920
      ],
3930
    ];
394
  }
395
396
  /**
397
   * Returns custom events grouped by category.
398
   *
399
   * @return array
400
   *   A render array representing the events page.
401
   */
402
  public function events(): array {
4030
    return $this->eventsReport('event_category_table');
404
  }
405
406
  /**
407
   * Returns site search keywords, referrers, and the daily chart.
408
   *
409
   * @return array
410
   *   A render array representing the site search page.
411
   */
412
  public function siteSearch(): array {
4131
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
414
    $blocks = [
415
      'site_search_chart' => [
416
        '#view_id' => 'visitors',
417
        '#view_display' => 'site_search_chart',
418
      ],
419
      'site_search_keywords' => [
420
        '#view_id' => 'visitors',
421
        '#view_display' => 'site_search_keywords',
422
      ],
423
      'site_search_referrers' => [
424
        '#view_id' => 'visitors',
425
        '#view_display' => 'site_search_referrers',
426
      ],
427
    ];
428
4291
    return [
4301
      'visitors_date_filter_form' => $form,
4311
      'main' => [
4321
        '#type' => 'container',
4331
        '#attributes' => [
4341
          'class' => ['visitors-main'],
4351
        ],
4361
        '1' => [
4371
          $this->renderViews($blocks),
4381
        ],
4391
      ],
4401
      '#attached' => [
4411
        'library' => [
4421
          'visitors/visitors.report',
4431
        ],
4441
      ],
4451
    ];
446
  }
447
448
  /**
449
   * Returns custom events grouped by action.
450
   *
451
   * @param string|null $category
452
   *   The category to limit the report to.
453
   *
454
   * @return array
455
   *   A render array representing the event actions page.
456
   */
457
  public function eventActions(?string $category = NULL): array {
4580
    $args = ($category === NULL || $category === '') ? NULL : [$category];
4590
    return $this->eventsReport('event_action_table', $args);
460
  }
461
462
  /**
463
   * Returns custom events grouped by name.
464
   *
465
   * @param string|null $category
466
   *   The category to limit the report to.
467
   * @param string|null $action
468
   *   The action to limit the report to.
469
   *
470
   * @return array
471
   *   A render array representing the event names page.
472
   */
473
  public function eventNames(?string $category = NULL, ?string $action = NULL): array {
4740
    $args = NULL;
4750
    $has_category = $category !== NULL && $category !== '';
4760
    $has_action = $action !== NULL && $action !== '';
4770
    if ($has_category && $has_action) {
4780
      $args = [$category, $action];
479
    }
4800
    return $this->eventsReport('event_name_table', $args);
481
  }
482
483
  /**
484
   * Builds an events page from one Visitors view display.
485
   *
486
   * @param string $display_id
487
   *   The view display id.
488
   * @param array|null $args
489
   *   Contextual filter values.
490
   *
491
   * @return array
492
   *   A render array with the date filter and the display.
493
   */
494
  protected function eventsReport(string $display_id, ?array $args = NULL): array {
4950
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
4960
    $blocks[$display_id] = [
4970
      '#view_id' => 'visitors',
4980
      '#view_display' => $display_id,
4990
    ];
500
5010
    return [
5020
      'visitors_date_filter_form' => $form,
5030
      'main' => [
5040
        '#type' => 'container',
5050
        '#attributes' => [
5060
          'class' => ['visitors-main'],
5070
        ],
5080
        '1' => [
5090
          $this->renderViews($blocks, NULL, $args),
5100
        ],
5110
      ],
5120
      '#attached' => [
5130
        'library' => [
5140
          'visitors/visitors.report',
5150
        ],
5160
      ],
5170
    ];
518
  }
519
520
  /**
521
   * Returns the bot report.
522
   *
523
   * @return array
524
   *   A render array for bot names and the paths they hit.
525
   */
526
  public function bots(): array {
5270
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
528
    $blocks['bot_table'] = [
529
      '#view_id'      => 'visitors',
530
      '#view_display' => 'bot_table',
531
    ];
5320
    $blocks['bot_path_table'] = [
5330
      '#view_id'      => 'visitors',
5340
      '#view_display' => 'bot_path_table',
5350
    ];
536
5370
    return [
5380
      'visitors_date_filter_form' => $form,
5390
      'main' => [
5400
        '#type' => 'container',
5410
        '#attributes' => [
5420
          'class' => ['visitors-main'],
5430
        ],
5440
        '1' => [
5450
          $this->renderViews($blocks),
5460
        ],
5470
      ],
5480
    ];
549
  }
550
551
  /**
552
   * Returns a path report.
553
   *
554
   * @return array
555
   *   A render array representing the paths page content.
556
   */
557
  public function path(): array {
5581
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
559
    $blocks['path_table'] = [
560
      '#view_id'      => 'visitors',
561
      '#view_display' => 'path_table',
562
    ];
563
5641
    return [
5651
      'visitors_date_filter_form' => $form,
5661
      'main' => [
5671
        '#type' => 'container',
5681
        '#attributes' => [
5691
          'class' => ['visitors-main'],
5701
        ],
5711
        '1' => [
5721
          $this->renderViews($blocks),
5731
        ],
5741
      ],
5751
    ];
576
  }
577
578
  /**
579
   * Returns a path report.
580
   *
581
   * @return array
582
   *   A render array representing the paths page content.
583
   */
584
  public function entryPath(): array {
5851
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
586
    $blocks['entry_path_table'] = [
587
      '#view_id'      => 'visitors',
588
      '#view_display' => 'entry_path_table',
589
    ];
590
5911
    return [
5921
      'visitors_date_filter_form' => $form,
5931
      'main' => [
5941
        '#type' => 'container',
5951
        '#attributes' => [
5961
          'class' => ['visitors-main'],
5971
        ],
5981
        '1' => [
5991
          $this->renderViews($blocks),
6001
        ],
6011
      ],
6021
    ];
603
  }
604
605
  /**
606
   * Returns a path report.
607
   *
608
   * @return array
609
   *   A render array representing the paths page content.
610
   */
611
  public function exitPath(): array {
6121
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
613
    $blocks['exit_path_table'] = [
614
      '#view_id'      => 'visitors',
615
      '#view_display' => 'exit_path_table',
616
    ];
617
6181
    return [
6191
      'visitors_date_filter_form' => $form,
6201
      'main' => [
6211
        '#type' => 'container',
6221
        '#attributes' => [
6231
          'class' => ['visitors-main'],
6241
        ],
6251
        '1' => [
6261
          $this->renderViews($blocks),
6271
        ],
6281
      ],
6291
    ];
630
  }
631
632
  /**
633
   * Returns a hosts page.
634
   *
635
   * @return array
636
   *   A render array representing the hosts page content.
637
   */
638
  public function topHost(): array {
6391
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
640
    $blocks['path'] = [
641
      '#view_id'      => 'visitors',
642
      '#view_display' => 'top_host_table',
643
    ];
644
6451
    return [
6461
      'visitors_date_filter_form' => $form,
6471
      'main' => [
6481
        '#type' => 'container',
6491
        '#attributes' => [
6501
          'class' => ['visitors-main'],
6511
        ],
6521
        '1' => [
6531
          $this->renderViews($blocks),
6541
        ],
6551
      ],
6561
    ];
657
  }
658
659
  /**
660
   * Returns a hosts report.
661
   *
662
   * @return array
663
   *   A render array representing the hosts page content.
664
   */
665
  public function recentHost($host): array {
6661
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
667
    $blocks['path'] = [
668
      '#view_id'      => 'visitors',
669
      '#view_display' => 'recent_view_table',
670
    ];
671
6721
    return [
6731
      'visitors_date_filter_form' => $form,
6741
      'main' => [
6751
        '#type' => 'container',
6761
        '#attributes' => [
6771
          'class' => ['visitors-main'],
6781
        ],
6791
        '1' => [
6801
          $this->renderViews($blocks, NULL, [NULL, $host]),
6811
        ],
6821
      ],
6831
    ];
684
  }
685
686
  /**
687
   * Returns a title for the page.
688
   */
689
  public function getHostTitle(string $host) {
6901
    $title = $this->stringTranslation
6911
      ->translate('Visits from @host', ['@host' => $host]);
692
6931
    return $title;
694
  }
695
696
  /**
697
   * Returns a top route page.
698
   *
699
   * @return array
700
   *   A render array representing the top pages page content.
701
   */
702
  public function topRoute(): array {
7031
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
704
    $blocks['route'] = [
705
      '#view_id'      => 'visitors',
706
      '#view_display' => 'route_table',
707
    ];
708
7091
    return [
7101
      'visitors_date_filter_form' => $form,
7111
      'main' => [
7121
        '#type' => 'container',
7131
        '#attributes' => [
7141
          'class' => ['visitors-main'],
7151
        ],
7161
        '1' => [
7171
          $this->renderViews($blocks),
7181
        ],
7191
      ],
7201
    ];
721
  }
722
723
  /**
724
   * Returns a path report.
725
   *
726
   * @return array
727
   *   A render array representing the paths page content.
728
   */
729
  public function entryRoute(): array {
7301
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
731
    $blocks['entry_route_table'] = [
732
      '#view_id'      => 'visitors',
733
      '#view_display' => 'entry_route_table',
734
    ];
735
7361
    return [
7371
      'visitors_date_filter_form' => $form,
7381
      'main' => [
7391
        '#type' => 'container',
7401
        '#attributes' => [
7411
          'class' => ['visitors-main'],
7421
        ],
7431
        '1' => [
7441
          $this->renderViews($blocks),
7451
        ],
7461
      ],
7471
    ];
748
  }
749
750
  /**
751
   * Returns a exit route report.
752
   *
753
   * @return array
754
   *   A render array representing the exit route page content.
755
   */
756
  public function exitRoute(): array {
7571
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
758
    $blocks['exit_route_table'] = [
759
      '#view_id'      => 'visitors',
760
      '#view_display' => 'exit_route_table',
761
    ];
762
7631
    return [
7641
      'visitors_date_filter_form' => $form,
7651
      'main' => [
7661
        '#type' => 'container',
7671
        '#attributes' => [
7681
          'class' => ['visitors-main'],
7691
        ],
7701
        '1' => [
7711
          $this->renderViews($blocks),
7721
        ],
7731
      ],
7741
    ];
775
  }
776
777
  /**
778
   * Returns recent visitors filtered by route.
779
   *
780
   * @return array
781
   *   A render array representing page views.
782
   */
783
  public function recentRoute(string $route) {
7841
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
785
    $blocks['route'] = [
786
      '#view_id'      => 'visitors',
787
      '#view_display' => 'recent_view_table',
788
    ];
789
7901
    return [
7911
      'visitors_date_filter_form' => $form,
7921
      'main' => [
7931
        '#type' => 'container',
7941
        '#attributes' => [
7951
          'class' => ['visitors-main'],
7961
        ],
7971
        '1' => [
7981
          $this->renderViews($blocks, NULL, [$route]),
7991
        ],
8001
      ],
8011
    ];
802
  }
803
804
  /**
805
   * Returns a title for the page.
806
   */
807
  public function getRouteTitle(string $route) {
8081
    $title = $this->stringTranslation
8091
      ->translate('Route @route', ['@route' => $route]);
810
8111
    return $title;
812
  }
813
814
  /**
815
   * Returns referrer report.
816
   */
817
  public function nodeViews(int $node): array {
818
8191
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
820
    $blocks['path'] = [
821
      '#view_id'      => 'visitors',
822
      '#view_display' => 'referrer_table',
823
    ];
824
8251
    return [
8261
      'visitors_date_filter_form' => $form,
8271
      'main' => [
8281
        '#type' => 'container',
8291
        '#attributes' => [
8301
          'class' => ['visitors-main'],
8311
        ],
8321
        '1' => [
8331
          $this->renderViews($blocks),
8341
        ],
8351
      ],
8361
    ];
837
  }
838
839
  /**
840
   * Shows report related to devices.
841
   *
842
   * @return array
843
   *   A render array representing the days of month page content.
844
   */
845
  public function device(): array {
846
8471
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
848
    $first_blocks = [];
849
    $second_blocks = [];
850
851
    $first_blocks['device_type_table'] = [
852
      '#view_id'      => 'visitors',
853
      '#view_display' => 'device_type_table',
854
      '#attributes'   => [
855
        'class' => ['layout-column--half'],
856
      ],
857
    ];
8581
    $first_blocks['device_model_table'] = [
8591
      '#view_id'      => 'visitors',
8601
      '#view_display' => 'device_model_table',
8611
      '#attributes'   => [
8621
        'class' => ['layout-column--half'],
8631
      ],
8641
    ];
865
866
    $second_blocks['device_brand_table'] = [
867
      '#view_id'      => 'visitors',
868
      '#view_display' => 'device_brand_table',
869
      '#attributes'   => [
870
        'class' => ['layout-column--half'],
871
      ],
872
    ];
8731
    $second_blocks['device_resolution_table'] = [
8741
      '#view_id'      => 'visitors',
8751
      '#view_display' => 'device_resolution_table',
8761
      '#attributes'   => [
8771
        'class' => ['layout-column--half'],
8781
      ],
8791
    ];
880
8811
    return [
8821
      'visitors_date_filter_form' => $form,
8831
      'main' => [
8841
        '#type' => 'container',
8851
        '#attributes' => [
8861
          'class' => ['visitors-main'],
8871
        ],
8881
        '1' => [
8891
          $this->renderViews($first_blocks, 'layout-row'),
8901
        ],
8911
        '2' => [
8921
          $this->renderViews($second_blocks, 'layout-row'),
8931
        ],
8941
      ],
8951
      '#attached' => [
8961
        'library' => [
8971
          'visitors/visitors.report',
8981
        ],
8991
      ],
9001
    ];
901
  }
902
903
  /**
904
   * Returns a hours page.
905
   *
906
   * @return array
907
   *   A render array representing the hours page content.
908
   */
909
  public function location(): array {
9101
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
911
912
    $first_row['continent_table'] = [
913
      '#view_id'      => 'visitors',
914
      '#view_display' => 'continent_table',
915
      '#attributes'   => [
916
        'class' => ['layout-column--half'],
917
      ],
918
    ];
919
9201
    $first_row['country_table'] = [
9211
      '#view_id'      => 'visitors',
9221
      '#view_display' => 'country_table',
9231
      '#attributes'   => [
9241
        'class' => ['layout-column--half'],
9251
      ],
9261
    ];
927
928
    $second_row['distinct_countries_list'] = [
929
      '#view_id'      => 'visitors',
930
      '#view_display' => 'distinct_countries_list',
931
      '#attributes'   => [
932
        'class' => ['layout-column--half'],
933
      ],
934
    ];
9351
    $second_row['region_table'] = [
9361
      '#view_id'      => 'visitors',
9371
      '#view_display' => 'region_table',
9381
      '#attributes'   => [
9391
        'class' => ['layout-column--half'],
9401
      ],
9411
    ];
942
943
    $third_row['language_table'] = [
944
      '#view_id'      => 'visitors',
945
      '#view_display' => 'language_table',
946
      '#attributes'   => [
947
        'class' => ['layout-column--half'],
948
      ],
949
    ];
9501
    $third_row['city_table'] = [
9511
      '#view_id'      => 'visitors',
9521
      '#view_display' => 'city_table',
9531
      '#attributes'   => [
9541
        'class' => ['layout-column--half'],
9551
      ],
9561
    ];
957
9581
    return [
9591
      'visitors_date_filter_form' => $form,
960
9611
      'main' => [
9621
        '#type' => 'container',
9631
        '#attributes' => [
9641
          'class' => ['visitors-main'],
9651
        ],
9661
        '1' => [
9671
          $this->renderViews($first_row, 'layout-row'),
9681
        ],
9691
        '2' => [
9701
          $this->renderViews($second_row, 'layout-row'),
9711
        ],
9721
        '3' => [
9731
          $this->renderViews($third_row, 'layout-row'),
9741
        ],
9751
      ],
9761
      '#attached' => [
9771
        'library' => [
9781
          'visitors/visitors.report',
9791
        ],
9801
      ],
9811
    ];
982
  }
983
984
  /**
985
   * Returns the Continent as the title for the page.
986
   */
987
  public function getContinentTitle($continent) {
9881
    $title = $this->t('Continent');
9891
    if ($continent) {
9901
      $title = $this->location->getContinentLabel($continent);
991
    }
992
9931
    return $title;
994
  }
995
996
  /**
997
   * Returns a continent page.
998
   *
999
   * @return array
1000
   *   A render array representing the continent page content.
1001
   */
1002
  public function continent($continent): array {
10031
    $args = [];
10041
    $view_display = 'continent_table';
10051
    if ($continent) {
10061
      $args[] = $continent;
10071
      $view_display = 'country_table';
1008
    }
10091
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
1010
10111
    $first_row['continent_table'] = [
10121
      '#view_id'      => 'visitors',
10131
      '#view_display' => $view_display,
10141
    ];
1015
10161
    return [
10171
      'visitors_date_filter_form' => $form,
1018
10191
      'main' => [
10201
        '#type' => 'container',
10211
        '#attributes' => [
10221
          'class' => ['visitors-main'],
10231
        ],
10241
        '1' => [
10251
          $this->renderViews($first_row, NULL, $args),
10261
        ],
10271
      ],
10281
      '#attached' => [
10291
        'library' => [
10301
          'visitors/visitors.report',
10311
        ],
10321
      ],
10331
    ];
1034
  }
1035
1036
  /**
1037
   * Returns the Country as the title for the page.
1038
   */
1039
  public function getCountryTitle($country) {
10401
    $title = $this->t('Country');
10411
    if ($country) {
10421
      $title = $this->location->getCountryLabel($country);
1043
    }
1044
10451
    return $title;
1046
  }
1047
1048
  /**
1049
   * Returns a country page.
1050
   *
1051
   * @return array
1052
   *   A render array representing the country page content.
1053
   */
1054
  public function country($country): array {
10552
    $args = [];
1056
    $view_id = 'visitors';
10572
    $view_display = 'country_table';
10582
    if ($country) {
1059
      $args[] = NULL;
10602
      $args[] = NULL;
10612
      $args[] = $country;
10622
      $view_display = 'recent_view_table';
1063
    }
10642
    if ($this->moduleHandler->moduleExists('visitors_geoip')) {
10651
      $args = [$country];
1066
      $view_id = 'visitors';
10671
      $view_display = 'region_table';
1068
    }
1069
10702
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
10712
    $first_row['country_table'] = [
10722
      '#view_id'      => $view_id,
10732
      '#view_display' => $view_display,
10742
    ];
1075
10762
    return [
10772
      'visitors_date_filter_form' => $form,
1078
10792
      'main' => [
10802
        '#type' => 'container',
10812
        '#attributes' => [
10822
          'class' => ['visitors-main'],
10832
        ],
10842
        '1' => [
10852
          $this->renderViews($first_row, NULL, $args),
10862
        ],
10872
      ],
10882
      '#attached' => [
10892
        'library' => [
10902
          'visitors/visitors.report',
10912
        ],
10922
      ],
10932
    ];
1094
  }
1095
1096
  /**
1097
   * Display the performance report.
1098
   */
1099
  public function performance($sequence = NULL): array {
1100
    $view_display = NULL;
1101
    switch ($sequence) {
11023
      case 'hour':
11031
        $view_display = 'performance_hourly_column';
11041
        break;
1105
11062
      case 'day':
11071
        $view_display = 'performance_daily_column';
11081
        break;
1109
11101
      case 'week':
1111
      default:
11121
        $view_display = 'performance_weekly_column';
1113
        break;
1114
    }
1115
11163
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
1117
11183
    $first_row['performance_column'] = [
11193
      '#view_id'      => 'visitors',
11203
      '#view_display' => $view_display,
11213
    ];
1122
11233
    return [
11243
      'visitors_date_filter_form' => $form,
11253
      'visitors_performance' => [
11263
        '1' => $this->renderViews($first_row, 'layout-row'),
11273
      ],
11283
    ];
1129
  }
1130
1131
  /**
1132
   * The region page title.
1133
   */
1134
  public function getRegionTitle($country, $region) {
11351
    $title = 'Region';
11361
    if ($country) {
11371
      $title = $this->location->getCountryLabel($country);
1138
    }
11391
    if ($region) {
11401
      if ($region == '_none') {
11411
        $region = 'Unknown';
1142
      }
11431
      $title = "$region, $title";
1144
    }
11451
    return $title;
1146
  }
1147
1148
  /**
1149
   * Country report.
1150
   *
1151
   * @param string $country
1152
   *   The country code.
1153
   * @param string|null $region
1154
   *   The region code.
1155
   */
1156
  public function region($country, $region): array {
11571
    $args = [];
11581
    $view_display = 'region_table';
11591
    if ($country) {
11601
      $args[] = $country;
1161
    }
11621
    if ($region) {
11631
      if ($region != '_none') {
11641
        $args[] = $region;
1165
      }
11661
      $view_display = 'city_table';
1167
    }
11681
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
11691
    $first_row['region_table'] = [
11701
      '#view_id'      => 'visitors',
11711
      '#view_display' => $view_display,
11721
    ];
1173
11741
    return [
11751
      'visitors_date_filter_form' => $form,
1176
11771
      'main' => [
11781
        '#type' => 'container',
11791
        '#attributes' => [
11801
          'class' => ['visitors-main'],
11811
        ],
11821
        '1' => [
11831
          $this->renderViews($first_row, NULL, $args),
11841
        ],
11851
      ],
11861
      '#attached' => [
11871
        'library' => [
11881
          'visitors/visitors.report',
11891
        ],
11901
      ],
11911
    ];
1192
  }
1193
1194
  /**
1195
   * The city page title.
1196
   */
1197
  public function getCityTitle($country, $region, $city) {
11981
    $title = 'City';
11991
    if ($country) {
12001
      $title = $this->location->getCountryLabel($country);
1201
    }
12021
    if ($region && $region != '_none') {
12031
      $title = "$region, $title";
1204
    }
12051
    if ($city && $city != '_none') {
12061
      $title = "$city, $title";
1207
    }
1208
12091
    return $title;
1210
  }
1211
1212
  /**
1213
   * City report.
1214
   *
1215
   * @param string|null $country
1216
   *   The country code.
1217
   * @param string|null $region
1218
   *   The region code.
1219
   * @param string|null $city
1220
   *   The city name.
1221
   */
1222
  public function city($country, $region, $city): array {
1223
12241
    $args = [
12251
      NULL,
12261
      NULL,
12271
    ];
12281
    $view_display = 'city_table';
12291
    if ($country) {
12301
      $args[] = $country;
1231
    }
12321
    if ($region) {
12331
      if ($region = '_none') {
12341
        $args[] = NULL;
1235
      }
1236
      else {
1237
        $args[] = $region;
1238
      }
1239
    }
12401
    if ($city) {
12411
      if ($city = '_none') {
12421
        $args[] = NULL;
1243
      }
1244
      else {
1245
        $args[] = $city;
1246
      }
12471
      $view_display = 'recent_view_table';
1248
    }
1249
12501
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
12511
    $first_row['region_table'] = [
12521
      '#view_id'      => 'visitors',
12531
      '#view_display' => $view_display,
12541
    ];
1255
12561
    return [
12571
      'visitors_date_filter_form' => $form,
1258
12591
      'main' => [
12601
        '#type' => 'container',
12611
        '#attributes' => [
12621
          'class' => ['visitors-main'],
12631
        ],
12641
        '1' => [
12651
          $this->renderViews($first_row, NULL, $args),
12661
        ],
12671
      ],
12681
      '#attached' => [
12691
        'library' => [
12701
          'visitors/visitors.report',
12711
        ],
12721
      ],
12731
    ];
1274
  }
1275
1276
  /**
1277
   * Returns a user report.
1278
   *
1279
   * @return array
1280
   *   A render array representing the user page content.
1281
   */
1282
  public function user(): array {
12831
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
1284
1285
    $first_row['users_table'] = [
1286
      '#view_id'      => 'visitors',
1287
      '#view_display' => 'users_table',
1288
    ];
1289
12901
    return [
12911
      'visitors_date_filter_form' => $form,
1292
12931
      'main' => [
12941
        '#type' => 'container',
12951
        '#attributes' => [
12961
          'class' => ['visitors-main'],
12971
        ],
12981
        '1' => [
12991
          $this->renderViews($first_row),
13001
        ],
13011
      ],
13021
      '#attached' => [
13031
        'library' => [
13041
          'visitors/visitors.report',
13051
        ],
13061
      ],
13071
    ];
1308
  }
1309
1310
  /**
1311
   * Returns the referrer channel overview.
1312
   *
1313
   * @return array
1314
   *   A render array representing the overview page.
1315
   */
1316
  public function referrers(): array {
13171
    return $this->acquisitionReport([
13181
      'referrer_overview_pie',
13191
      'referrer_overview_table',
13201
    ]);
1321
  }
1322
1323
  /**
1324
   * Returns every referrer channel and name.
1325
   *
1326
   * @return array
1327
   *   A render array representing the all-referrers page.
1328
   */
1329
  public function allReferrers(): array {
13300
    return $this->acquisitionReport(['referrers_all_table']);
1331
  }
1332
1333
  /**
1334
   * Returns search engines and keywords.
1335
   *
1336
   * @return array
1337
   *   A render array representing the search engines page.
1338
   */
1339
  public function searchEngines(): array {
13400
    return $this->acquisitionReport([
13410
      'search_engines_table',
13420
      'search_keywords_table',
13430
    ]);
1344
  }
1345
1346
  /**
1347
   * Returns keywords for one search engine.
1348
   *
1349
   * @param string $referer_name
1350
   *   The search engine name.
1351
   *
1352
   * @return array
1353
   *   A render array representing the keyword page.
1354
   */
1355
  public function searchEngineKeywords(string $referer_name): array {
13560
    return $this->acquisitionReport(
13570
      ['search_engine_keywords'],
13580
      [$referer_name],
13590
    );
1360
  }
1361
1362
  /**
1363
   * Returns search engines for one keyword.
1364
   *
1365
   * @param string $referer_keyword
1366
   *   The keyword, or "not-defined" when none was stored.
1367
   *
1368
   * @return array
1369
   *   A render array representing the search engine page.
1370
   */
1371
  public function searchKeywordEngines(string $referer_keyword): array {
13720
    return $this->acquisitionReport(
13730
      ['search_keyword_engines'],
13740
      [$referer_keyword],
13750
    );
1376
  }
1377
1378
  /**
1379
   * Returns referring websites.
1380
   *
1381
   * @return array
1382
   *   A render array representing the websites page.
1383
   */
1384
  public function websites(): array {
13850
    return $this->acquisitionReport(['websites_table']);
1386
  }
1387
1388
  /**
1389
   * Returns URLs for one referring website.
1390
   *
1391
   * @param string $referer_name
1392
   *   The website host.
1393
   *
1394
   * @return array
1395
   *   A render array representing the URL page.
1396
   */
1397
  public function websiteUrls(string $referer_name): array {
13980
    return $this->acquisitionReport(['website_urls'], [$referer_name]);
1399
  }
1400
1401
  /**
1402
   * Returns a social network report.
1403
   *
1404
   * @return array
1405
   *   A render array representing the social network page content.
1406
   */
1407
  public function social(): array {
14081
    return $this->acquisitionReport([
14091
      'social_visitors_pie',
14101
      'social_referrers_table',
14111
    ]);
1412
  }
1413
1414
  /**
1415
   * Returns URLs for one social network.
1416
   *
1417
   * @param string $referer_name
1418
   *   The social network name.
1419
   *
1420
   * @return array
1421
   *   A render array representing the URL page.
1422
   */
1423
  public function socialUrls(string $referer_name): array {
14240
    return $this->acquisitionReport(['social_urls'], [$referer_name]);
1425
  }
1426
1427
  /**
1428
   * Returns a AI assistant report.
1429
   *
1430
   * @return array
1431
   *   A render array representing the AI assistant page content.
1432
   */
1433
  public function ai(): array {
14341
    return $this->acquisitionReport([
14351
      'ai_visitors_pie',
14361
      'ai_visitors_table',
14371
    ]);
1438
  }
1439
1440
  /**
1441
   * Returns human visits compared with AI agent visits.
1442
   *
1443
   * @return array
1444
   *   A render array representing the AI agent page.
1445
   */
1446
  public function aiAgents(): array {
14470
    return $this->acquisitionReport(['ai_agent_table']);
1448
  }
1449
1450
  /**
1451
   * Returns entry pages for one AI assistant.
1452
   *
1453
   * @param string $referer_name
1454
   *   The AI assistant name.
1455
   *
1456
   * @return array
1457
   *   A render array representing the entry page report.
1458
   */
1459
  public function aiEntryPages(string $referer_name): array {
14600
    return $this->acquisitionReport(['ai_entry_pages'], [$referer_name]);
1461
  }
1462
1463
  /**
1464
   * Returns campaigns.
1465
   *
1466
   * @return array
1467
   *   A render array representing the campaigns page.
1468
   */
1469
  public function campaigns(): array {
14700
    return $this->acquisitionReport(['campaigns_table']);
1471
  }
1472
1473
  /**
1474
   * Returns keywords for one campaign.
1475
   *
1476
   * @param string $referer_name
1477
   *   The campaign name.
1478
   *
1479
   * @return array
1480
   *   A render array representing the keyword page.
1481
   */
1482
  public function campaignKeywords(string $referer_name): array {
14830
    return $this->acquisitionReport(
14840
      ['campaign_keywords'],
14850
      [$referer_name],
14860
    );
1487
  }
1488
1489
  /**
1490
   * Builds an acquisition page from Visitors view displays.
1491
   *
1492
   * @param string[] $displays
1493
   *   View display ids.
1494
   * @param string[] $args
1495
   *   Contextual filter values.
1496
   *
1497
   * @return array
1498
   *   A render array with the date filter and the displays.
1499
   */
1500
  protected function acquisitionReport(array $displays, array $args = []): array {
15013
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
15023
    $blocks = [];
15033
    foreach ($displays as $display_id) {
15043
      $blocks[$display_id] = [
15053
        '#view_id' => 'visitors',
15063
        '#view_display' => $display_id,
15073
      ];
1508
    }
1509
15103
    return [
15113
      'visitors_date_filter_form' => $form,
15123
      'main' => [
15133
        '#type' => 'container',
15143
        '#attributes' => [
15153
          'class' => ['visitors-main'],
15163
        ],
15173
        '1' => [
15183
          $this->renderViews($blocks, NULL, $args === [] ? NULL : $args),
15193
        ],
15203
      ],
15213
      '#attached' => [
15223
        'library' => [
15233
          'visitors/visitors.report',
15243
        ],
15253
      ],
15263
    ];
1527
  }
1528
1529
  /**
1530
   * Returns a engagement report.
1531
   *
1532
   * @return array
1533
   *   A render array representing the engagement page content.
1534
   */
1535
  public function engagement(): array {
15361
    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
1537
1538
    $first_row['duration_table'] = [
1539
      '#view_id'      => 'visitors',
1540
      '#view_display' => 'visits_by_duration_table',
1541
      '#attributes'   => [
1542
        'class' => ['layout-column--half'],
1543
      ],
1544
    ];
1545
15461
    $first_row['pages_table'] = [
15471
      '#view_id'      => 'visitors',
15481
      '#view_display' => 'visits_by_pages_table',
15491
      '#attributes'   => [
15501
        'class' => ['layout-column--half'],
15511
      ],
15521
    ];
1553
1554
    $second_row['visits_by_visit_table'] = [
1555
      '#view_id'      => 'visitors',
1556
      '#view_display' => 'visits_by_visit_table',
1557
      '#attributes'   => [
1558
        'class' => ['layout-column--half'],
1559
      ],
1560
    ];
15611
    $second_row['visits_by_last_visit_table'] = [
15621
      '#view_id'      => 'visitors',
15631
      '#view_display' => 'visits_by_last_visit_table',
15641
      '#attributes'   => [
15651
        'class' => ['layout-column--half'],
15661
      ],
15671
    ];
1568
15691
    return [
15701
      'visitors_date_filter_form' => $form,
15711
      'main' => [
15721
        '#type' => 'container',
15731
        '#attributes' => [
15741
          'class' => ['visitors-main'],
15751
        ],
15761
        '1' => [
15771
          $this->renderViews($first_row, 'layout-row'),
15781
        ],
15791
        '2' => [
15801
          $this->renderViews($second_row, 'layout-row'),
15811
        ],
15821
      ],
15831
      '#attached' => [
15841
        'library' => [
15851
          'visitors/visitors.report',
15861
        ],
15871
      ],
15881
    ];
1589
  }
1590
1591
}