Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
622 / 622
100.00% covered (success)
100.00%
32 / 32
CRAP
100.00% covered (success)
100.00%
1 / 1
ReportController
100.00% covered (success)
100.00%
622 / 622
100.00% covered (success)
100.00%
32 / 32
57
100.00% covered (success)
100.00%
1 / 1
 create
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 report
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 software
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
1 / 1
1
 time
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
1
 path
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 entryPath
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 exitPath
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 topHost
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 recentHost
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 getHostTitle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 topRoute
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 entryRoute
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 exitRoute
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 recentRoute
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 getRouteTitle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 recentViews
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 nodeViews
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 device
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
1
 location
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
1 / 1
1
 getContinentTitle
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 continent
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
2
 getCountryTitle
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 country
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
3
 performance
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
5
 getRegionTitle
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 region
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
4
 getCityTitle
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 city
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
6
 user
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 social
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 ai
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Drupal\visitors\Controller\Report;
4
5use Drupal\Core\Ajax\AjaxResponse;
6use Drupal\Core\Ajax\ReplaceCommand;
7use Drupal\Core\Config\ConfigFactoryInterface;
8use Drupal\Core\Extension\ModuleHandlerInterface;
9use Drupal\Core\Form\FormBuilderInterface;
10use Drupal\Core\Messenger\MessengerInterface;
11use Drupal\Core\Session\AccountProxyInterface;
12use Drupal\Core\StringTranslation\TranslationInterface;
13use Drupal\visitors\VisitorsLocationInterface;
14use Symfony\Component\DependencyInjection\ContainerInterface;
15use Symfony\Component\HttpFoundation\Request;
16
17/**
18 * Generic Report controller.
19 */
20class ReportController extends ReportBaseController {
21
22
23  /**
24   * The date service.
25   *
26   * @var \Drupal\Core\Datetime\DateFormatterInterface
27   */
28  protected $date;
29
30  /**
31   * The form builder service.
32   *
33   * @var \Drupal\Core\Form\FormBuilderInterface
34   */
35  protected $formBuilder;
36
37  /**
38   * The settings.
39   *
40   * @var \Drupal\Core\Config\Config
41   */
42  protected $settings;
43
44  /**
45   * The current user.
46   *
47   * @var \Drupal\Core\Session\AccountProxyInterface
48   */
49  protected $account;
50
51  /**
52   * The location service.
53   *
54   * @var \Drupal\visitors\VisitorsLocationInterface
55   */
56  protected $location;
57
58  /**
59   * The module handler.
60   *
61   * @var \Drupal\Core\Extension\ModuleHandlerInterface
62   */
63  protected $moduleHandler;
64
65  /**
66   * {@inheritdoc}
67   */
68  public static function create(ContainerInterface $container) {
69    return new self(
70      $container->get('form_builder'),
71      $container->get('string_translation'),
72      $container->get('config.factory'),
73      $container->get('messenger'),
74      $container->get('current_user'),
75      $container->get('visitors.location'),
76      $container->get('module_handler')
77    );
78  }
79
80  /**
81   * Constructs the report controller.
82   *
83   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
84   *   The form builder service.
85   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
86   *   The string translation service.
87   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
88   *   The config factory.
89   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
90   *   The messenger service.
91   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
92   *   The current user.
93   * @param \Drupal\visitors\VisitorsLocationInterface $location
94   *   The location service.
95   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
96   *   The module handler.
97   */
98  public function __construct(FormBuilderInterface $form_builder, TranslationInterface $string_translation, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, AccountProxyInterface $current_user, VisitorsLocationInterface $location, ModuleHandlerInterface $module_handler) {
99
100    $this->formBuilder   = $form_builder;
101    $this->account       = $current_user;
102    $this->location      = $location;
103    $this->moduleHandler = $module_handler;
104
105    $this->settings = $config_factory->get('visitors.settings');
106
107    $this->setStringTranslation($string_translation);
108    $this->setMessenger($messenger);
109  }
110
111  /**
112   * Returns an Ajax response with the rendered view.
113   */
114  public function report(Request $request, string $view_id, string $display_id) {
115    $blocks['path'] = [
116      '#view_id'      => $view_id,
117      '#view_display' => $display_id,
118    ];
119
120    $rendered = \views_embed_view($view_id, $display_id);
121
122    $settings = NULL;
123    $selector = $request->query->get('class');
124
125    $response = new AjaxResponse();
126    $response->addCommand(new ReplaceCommand($selector, $rendered, $settings));
127
128    return $response;
129  }
130
131  /**
132   * Returns software report.
133   *
134   * @return array
135   *   A render array representing the days of month page content.
136   */
137  public function software(): array {
138
139    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
140    $first_row = [];
141    $second_row = [];
142
143    $first_row['os_version_table'] = [
144      '#view_id'      => 'visitors',
145      '#view_display' => 'os_version_table',
146      '#attributes'   => [
147        'class' => ['layout-column--half'],
148      ],
149    ];
150    $first_row['browser_version_table'] = [
151      '#view_id'      => 'visitors',
152      '#view_display' => 'browser_version_table',
153      '#attributes'   => [
154        'class' => ['layout-column--half'],
155      ],
156    ];
157
158    $second_row['device_config_table'] = [
159      '#view_id'      => 'visitors',
160      '#view_display' => 'device_config_table',
161      '#attributes'   => [
162        'class' => ['layout-column--half'],
163      ],
164    ];
165    $second_row['browser_engine_pie'] = [
166      '#view_id'      => 'visitors',
167      '#view_display' => 'browser_engine_pie',
168      '#attributes'   => [
169        'class' => ['layout-column--half'],
170      ],
171    ];
172
173    $third_row['browser_plugin_list'] = [
174      '#view_id'      => 'visitors',
175      '#view_display' => 'browser_plugin_list',
176      '#attributes'   => [
177        'class' => ['layout-column--half'],
178      ],
179    ];
180
181    return [
182      'visitors_date_filter_form' => $form,
183      'main' => [
184        '#type' => 'container',
185        '#attributes' => [
186          'class' => ['visitors-main'],
187        ],
188        '1' => [
189          $this->renderViews($first_row, 'layout-row'),
190        ],
191        '2' => [
192          $this->renderViews($second_row, 'layout-row'),
193        ],
194        '3' => [
195          $this->renderViews($third_row, 'layout-row'),
196        ],
197      ],
198      '#attached' => [
199        'library' => [
200          'visitors/visitors.report',
201        ],
202      ],
203    ];
204  }
205
206  /**
207   * Returns a time report.
208   *
209   * @return array
210   *   A render array representing the hours page content.
211   */
212  public function time(): array {
213    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
214
215    $first_row['daily_column'] = [
216      '#view_id'      => 'visitors',
217      '#view_display' => 'daily_column',
218    ];
219
220    $second_row['local_hour_column'] = [
221      '#view_id'      => 'visitors',
222      '#view_display' => 'local_hour_column',
223      '#attributes'   => [
224        'class' => ['layout-column--half'],
225      ],
226    ];
227    $second_row['hour_column'] = [
228      '#view_id'      => 'visitors',
229      '#view_display' => 'hour_column',
230      '#attributes'   => [
231        'class' => ['layout-column--half'],
232      ],
233    ];
234
235    $third_row['day_of_week_column'] = [
236      '#view_id'      => 'visitors',
237      '#view_display' => 'day_of_week_column',
238      '#attributes'   => [
239        'class' => ['layout-column--half'],
240      ],
241    ];
242    $third_row['day_of_month_column'] = [
243      '#view_id'      => 'visitors',
244      '#view_display' => 'day_of_month_column',
245      '#attributes'   => [
246        'class' => ['layout-column--half'],
247      ],
248    ];
249
250    $fourth_row['monthly_column'] = [
251      '#view_id'      => 'visitors',
252      '#view_display' => 'monthly_column',
253    ];
254
255    return [
256      'visitors_date_filter_form' => $form,
257
258      'main' => [
259        '#type' => 'container',
260        '#attributes' => [
261          'class' => ['visitors-main'],
262        ],
263        '1' => [
264          $this->renderViews($first_row),
265        ],
266        '2' => [
267          $this->renderViews($second_row, 'layout-row'),
268        ],
269        '3' => [
270          $this->renderViews($third_row, 'layout-row'),
271        ],
272        '4' => [
273          $this->renderViews($fourth_row),
274        ],
275      ],
276      '#attached' => [
277        'library' => [
278          'visitors/visitors.report',
279        ],
280      ],
281    ];
282  }
283
284  /**
285   * Returns a path report.
286   *
287   * @return array
288   *   A render array representing the paths page content.
289   */
290  public function path(): array {
291    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
292    $blocks['path_table'] = [
293      '#view_id'      => 'visitors',
294      '#view_display' => 'path_table',
295    ];
296
297    return [
298      'visitors_date_filter_form' => $form,
299      'main' => [
300        '#type' => 'container',
301        '#attributes' => [
302          'class' => ['visitors-main'],
303        ],
304        '1' => [
305          $this->renderViews($blocks),
306        ],
307      ],
308    ];
309  }
310
311  /**
312   * Returns a path report.
313   *
314   * @return array
315   *   A render array representing the paths page content.
316   */
317  public function entryPath(): array {
318    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
319    $blocks['entry_path_table'] = [
320      '#view_id'      => 'visitors',
321      '#view_display' => 'entry_path_table',
322    ];
323
324    return [
325      'visitors_date_filter_form' => $form,
326      'main' => [
327        '#type' => 'container',
328        '#attributes' => [
329          'class' => ['visitors-main'],
330        ],
331        '1' => [
332          $this->renderViews($blocks),
333        ],
334      ],
335    ];
336  }
337
338  /**
339   * Returns a path report.
340   *
341   * @return array
342   *   A render array representing the paths page content.
343   */
344  public function exitPath(): array {
345    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
346    $blocks['exit_path_table'] = [
347      '#view_id'      => 'visitors',
348      '#view_display' => 'exit_path_table',
349    ];
350
351    return [
352      'visitors_date_filter_form' => $form,
353      'main' => [
354        '#type' => 'container',
355        '#attributes' => [
356          'class' => ['visitors-main'],
357        ],
358        '1' => [
359          $this->renderViews($blocks),
360        ],
361      ],
362    ];
363  }
364
365  /**
366   * Returns a hosts page.
367   *
368   * @return array
369   *   A render array representing the hosts page content.
370   */
371  public function topHost(): array {
372    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
373    $blocks['path'] = [
374      '#view_id'      => 'visitors',
375      '#view_display' => 'top_host_table',
376    ];
377
378    return [
379      'visitors_date_filter_form' => $form,
380      'main' => [
381        '#type' => 'container',
382        '#attributes' => [
383          'class' => ['visitors-main'],
384        ],
385        '1' => [
386          $this->renderViews($blocks),
387        ],
388      ],
389    ];
390  }
391
392  /**
393   * Returns a hosts report.
394   *
395   * @return array
396   *   A render array representing the hosts page content.
397   */
398  public function recentHost($host): array {
399    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
400    $blocks['path'] = [
401      '#view_id'      => 'visitors',
402      '#view_display' => 'recent_view_table',
403    ];
404
405    return [
406      'visitors_date_filter_form' => $form,
407      'main' => [
408        '#type' => 'container',
409        '#attributes' => [
410          'class' => ['visitors-main'],
411        ],
412        '1' => [
413          $this->renderViews($blocks, NULL, [NULL, $host]),
414        ],
415      ],
416    ];
417  }
418
419  /**
420   * Returns a title for the page.
421   */
422  public function getHostTitle(string $host) {
423    $title = $this->stringTranslation
424      ->translate('Visits from @host', ['@host' => $host]);
425
426    return $title;
427  }
428
429  /**
430   * Returns a top route page.
431   *
432   * @return array
433   *   A render array representing the top pages page content.
434   */
435  public function topRoute(): array {
436    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
437    $blocks['route'] = [
438      '#view_id'      => 'visitors',
439      '#view_display' => 'route_table',
440    ];
441
442    return [
443      'visitors_date_filter_form' => $form,
444      'main' => [
445        '#type' => 'container',
446        '#attributes' => [
447          'class' => ['visitors-main'],
448        ],
449        '1' => [
450          $this->renderViews($blocks),
451        ],
452      ],
453    ];
454  }
455
456  /**
457   * Returns a path report.
458   *
459   * @return array
460   *   A render array representing the paths page content.
461   */
462  public function entryRoute(): array {
463    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
464    $blocks['entry_route_table'] = [
465      '#view_id'      => 'visitors',
466      '#view_display' => 'entry_route_table',
467    ];
468
469    return [
470      'visitors_date_filter_form' => $form,
471      'main' => [
472        '#type' => 'container',
473        '#attributes' => [
474          'class' => ['visitors-main'],
475        ],
476        '1' => [
477          $this->renderViews($blocks),
478        ],
479      ],
480    ];
481  }
482
483  /**
484   * Returns a exit route report.
485   *
486   * @return array
487   *   A render array representing the exit route page content.
488   */
489  public function exitRoute(): array {
490    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
491    $blocks['exit_route_table'] = [
492      '#view_id'      => 'visitors',
493      '#view_display' => 'exit_route_table',
494    ];
495
496    return [
497      'visitors_date_filter_form' => $form,
498      'main' => [
499        '#type' => 'container',
500        '#attributes' => [
501          'class' => ['visitors-main'],
502        ],
503        '1' => [
504          $this->renderViews($blocks),
505        ],
506      ],
507    ];
508  }
509
510  /**
511   * Returns recent visitors filtered by route.
512   *
513   * @return array
514   *   A render array representing page views.
515   */
516  public function recentRoute(string $route) {
517    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
518    $blocks['route'] = [
519      '#view_id'      => 'visitors',
520      '#view_display' => 'recent_view_table',
521    ];
522
523    return [
524      'visitors_date_filter_form' => $form,
525      'main' => [
526        '#type' => 'container',
527        '#attributes' => [
528          'class' => ['visitors-main'],
529        ],
530        '1' => [
531          $this->renderViews($blocks, NULL, [$route]),
532        ],
533      ],
534    ];
535  }
536
537  /**
538   * Returns a title for the page.
539   */
540  public function getRouteTitle(string $route) {
541    $title = $this->stringTranslation
542      ->translate('Route @route', ['@route' => $route]);
543
544    return $title;
545  }
546
547  /**
548   * Returns a recent hits page.
549   *
550   * @return array
551   *   A render array representing the recent hits page content.
552   */
553  public function recentViews(): array {
554    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
555    $blocks['path'] = [
556      '#view_id'      => 'visitors',
557      '#view_display' => 'recent_view_table',
558    ];
559
560    return [
561      'visitors_date_filter_form' => $form,
562      'main' => [
563        '#type' => 'container',
564        '#attributes' => [
565          'class' => ['visitors-main'],
566        ],
567        '1' => [
568          $this->renderViews($blocks),
569        ],
570      ],
571    ];
572  }
573
574  /**
575   * Returns referrer report.
576   */
577  public function nodeViews(int $node): array {
578
579    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
580    $blocks['path'] = [
581      '#view_id'      => 'visitors',
582      '#view_display' => 'referrer_table',
583    ];
584
585    return [
586      'visitors_date_filter_form' => $form,
587      'main' => [
588        '#type' => 'container',
589        '#attributes' => [
590          'class' => ['visitors-main'],
591        ],
592        '1' => [
593          $this->renderViews($blocks),
594        ],
595      ],
596    ];
597  }
598
599  /**
600   * Shows report related to devices.
601   *
602   * @return array
603   *   A render array representing the days of month page content.
604   */
605  public function device(): array {
606
607    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
608    $first_blocks = [];
609    $second_blocks = [];
610
611    $first_blocks['device_type_table'] = [
612      '#view_id'      => 'visitors',
613      '#view_display' => 'device_type_table',
614      '#attributes'   => [
615        'class' => ['layout-column--half'],
616      ],
617    ];
618    $first_blocks['device_model_table'] = [
619      '#view_id'      => 'visitors',
620      '#view_display' => 'device_model_table',
621      '#attributes'   => [
622        'class' => ['layout-column--half'],
623      ],
624    ];
625
626    $second_blocks['device_brand_table'] = [
627      '#view_id'      => 'visitors',
628      '#view_display' => 'device_brand_table',
629      '#attributes'   => [
630        'class' => ['layout-column--half'],
631      ],
632    ];
633    $second_blocks['device_resolution_table'] = [
634      '#view_id'      => 'visitors',
635      '#view_display' => 'device_resolution_table',
636      '#attributes'   => [
637        'class' => ['layout-column--half'],
638      ],
639    ];
640
641    return [
642      'visitors_date_filter_form' => $form,
643      'main' => [
644        '#type' => 'container',
645        '#attributes' => [
646          'class' => ['visitors-main'],
647        ],
648        '1' => [
649          $this->renderViews($first_blocks, 'layout-row'),
650        ],
651        '2' => [
652          $this->renderViews($second_blocks, 'layout-row'),
653        ],
654      ],
655      '#attached' => [
656        'library' => [
657          'visitors/visitors.report',
658        ],
659      ],
660    ];
661  }
662
663  /**
664   * Returns a hours page.
665   *
666   * @return array
667   *   A render array representing the hours page content.
668   */
669  public function location(): array {
670    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
671
672    $first_row['continent_table'] = [
673      '#view_id'      => 'visitors',
674      '#view_display' => 'continent_table',
675      '#attributes'   => [
676        'class' => ['layout-column--half'],
677      ],
678    ];
679
680    $first_row['country_table'] = [
681      '#view_id'      => 'visitors',
682      '#view_display' => 'country_table',
683      '#attributes'   => [
684        'class' => ['layout-column--half'],
685      ],
686    ];
687
688    $second_row['distinct_countries_list'] = [
689      '#view_id'      => 'visitors',
690      '#view_display' => 'distinct_countries_list',
691      '#attributes'   => [
692        'class' => ['layout-column--half'],
693      ],
694    ];
695    $second_row['region_table'] = [
696      '#view_id'      => 'visitors',
697      '#view_display' => 'region_table',
698      '#attributes'   => [
699        'class' => ['layout-column--half'],
700      ],
701    ];
702
703    $third_row['language_table'] = [
704      '#view_id'      => 'visitors',
705      '#view_display' => 'language_table',
706      '#attributes'   => [
707        'class' => ['layout-column--half'],
708      ],
709    ];
710    $third_row['city_table'] = [
711      '#view_id'      => 'visitors',
712      '#view_display' => 'city_table',
713      '#attributes'   => [
714        'class' => ['layout-column--half'],
715      ],
716    ];
717
718    return [
719      'visitors_date_filter_form' => $form,
720
721      'main' => [
722        '#type' => 'container',
723        '#attributes' => [
724          'class' => ['visitors-main'],
725        ],
726        '1' => [
727          $this->renderViews($first_row, 'layout-row'),
728        ],
729        '2' => [
730          $this->renderViews($second_row, 'layout-row'),
731        ],
732        '3' => [
733          $this->renderViews($third_row, 'layout-row'),
734        ],
735      ],
736      '#attached' => [
737        'library' => [
738          'visitors/visitors.report',
739        ],
740      ],
741    ];
742  }
743
744  /**
745   * Returns the Continent as the title for the page.
746   */
747  public function getContinentTitle($continent) {
748    $title = $this->t('Continent');
749    if ($continent) {
750      $title = $this->location->getContinentLabel($continent);
751    }
752
753    return $title;
754  }
755
756  /**
757   * Returns a continent page.
758   *
759   * @return array
760   *   A render array representing the continent page content.
761   */
762  public function continent($continent): array {
763    $args = [];
764    $view_display = 'continent_table';
765    if ($continent) {
766      $args[] = $continent;
767      $view_display = 'country_table';
768    }
769    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
770
771    $first_row['continent_table'] = [
772      '#view_id'      => 'visitors',
773      '#view_display' => $view_display,
774    ];
775
776    return [
777      'visitors_date_filter_form' => $form,
778
779      'main' => [
780        '#type' => 'container',
781        '#attributes' => [
782          'class' => ['visitors-main'],
783        ],
784        '1' => [
785          $this->renderViews($first_row, NULL, $args),
786        ],
787      ],
788      '#attached' => [
789        'library' => [
790          'visitors/visitors.report',
791        ],
792      ],
793    ];
794  }
795
796  /**
797   * Returns the Country as the title for the page.
798   */
799  public function getCountryTitle($country) {
800    $title = $this->t('Country');
801    if ($country) {
802      $title = $this->location->getCountryLabel($country);
803    }
804
805    return $title;
806  }
807
808  /**
809   * Returns a country page.
810   *
811   * @return array
812   *   A render array representing the country page content.
813   */
814  public function country($country): array {
815    $args = [];
816    $view_id = 'visitors';
817    $view_display = 'country_table';
818    if ($country) {
819      $args[] = NULL;
820      $args[] = NULL;
821      $args[] = $country;
822      $view_display = 'recent_view_table';
823    }
824    if ($this->moduleHandler->moduleExists('visitors_geoip')) {
825      $args = [$country];
826      $view_id = 'visitors';
827      $view_display = 'region_table';
828    }
829
830    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
831    $first_row['country_table'] = [
832      '#view_id'      => $view_id,
833      '#view_display' => $view_display,
834    ];
835
836    return [
837      'visitors_date_filter_form' => $form,
838
839      'main' => [
840        '#type' => 'container',
841        '#attributes' => [
842          'class' => ['visitors-main'],
843        ],
844        '1' => [
845          $this->renderViews($first_row, NULL, $args),
846        ],
847      ],
848      '#attached' => [
849        'library' => [
850          'visitors/visitors.report',
851        ],
852      ],
853    ];
854  }
855
856  /**
857   * Display the performance report.
858   */
859  public function performance($sequence = NULL): array {
860    $view_display = NULL;
861    switch ($sequence) {
862      case 'hour':
863        $view_display = 'performance_hourly_column';
864        break;
865
866      case 'day':
867        $view_display = 'performance_daily_column';
868        break;
869
870      case 'week':
871      default:
872        $view_display = 'performance_weekly_column';
873        break;
874    }
875
876    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
877
878    $first_row['performance_column'] = [
879      '#view_id'      => 'visitors',
880      '#view_display' => $view_display,
881    ];
882
883    return [
884      'visitors_date_filter_form' => $form,
885      'visitors_performance' => [
886        '1' => $this->renderViews($first_row, 'layout-row'),
887      ],
888    ];
889  }
890
891  /**
892   * The region page title.
893   */
894  public function getRegionTitle($country, $region) {
895    $title = 'Region';
896    if ($country) {
897      $title = $this->location->getCountryLabel($country);
898    }
899    if ($region) {
900      if ($region == '_none') {
901        $region = 'Unknown';
902      }
903      $title = "$region$title";
904    }
905    return $title;
906  }
907
908  /**
909   * Country report.
910   *
911   * @param string $country
912   *   The country code.
913   * @param string|null $region
914   *   The region code.
915   */
916  public function region($country, $region): array {
917    $args = [];
918    $view_display = 'region_table';
919    if ($country) {
920      $args[] = $country;
921    }
922    if ($region) {
923      if ($region != '_none') {
924        $args[] = $region;
925      }
926      $view_display = 'city_table';
927    }
928    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
929    $first_row['region_table'] = [
930      '#view_id'      => 'visitors',
931      '#view_display' => $view_display,
932    ];
933
934    return [
935      'visitors_date_filter_form' => $form,
936
937      'main' => [
938        '#type' => 'container',
939        '#attributes' => [
940          'class' => ['visitors-main'],
941        ],
942        '1' => [
943          $this->renderViews($first_row, NULL, $args),
944        ],
945      ],
946      '#attached' => [
947        'library' => [
948          'visitors/visitors.report',
949        ],
950      ],
951    ];
952  }
953
954  /**
955   * The city page title.
956   */
957  public function getCityTitle($country, $region, $city) {
958    $title = 'City';
959    if ($country) {
960      $title = $this->location->getCountryLabel($country);
961    }
962    if ($region && $region != '_none') {
963      $title = "$region$title";
964    }
965    if ($city && $city != '_none') {
966      $title = "$city$title";
967    }
968
969    return $title;
970  }
971
972  /**
973   * City report.
974   *
975   * @param string|null $country
976   *   The country code.
977   * @param string|null $region
978   *   The region code.
979   * @param string|null $city
980   *   The city name.
981   */
982  public function city($country, $region, $city): array {
983
984    $args = [
985      NULL,
986      NULL,
987    ];
988    $view_display = 'city_table';
989    if ($country) {
990      $args[] = $country;
991    }
992    if ($region) {
993      if ($region = '_none') {
994        $args[] = NULL;
995      }
996      else {
997        $args[] = $region;
998      }
999    }
1000    if ($city) {
1001      if ($city = '_none') {
1002        $args[] = NULL;
1003      }
1004      else {
1005        $args[] = $city;
1006      }
1007      $view_display = 'recent_view_table';
1008    }
1009
1010    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
1011    $first_row['region_table'] = [
1012      '#view_id'      => 'visitors',
1013      '#view_display' => $view_display,
1014    ];
1015
1016    return [
1017      'visitors_date_filter_form' => $form,
1018
1019      'main' => [
1020        '#type' => 'container',
1021        '#attributes' => [
1022          'class' => ['visitors-main'],
1023        ],
1024        '1' => [
1025          $this->renderViews($first_row, NULL, $args),
1026        ],
1027      ],
1028      '#attached' => [
1029        'library' => [
1030          'visitors/visitors.report',
1031        ],
1032      ],
1033    ];
1034  }
1035
1036  /**
1037   * Returns a user report.
1038   *
1039   * @return array
1040   *   A render array representing the user page content.
1041   */
1042  public function user(): array {
1043    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
1044
1045    $first_row['users_table'] = [
1046      '#view_id'      => 'visitors',
1047      '#view_display' => 'users_table',
1048    ];
1049
1050    return [
1051      'visitors_date_filter_form' => $form,
1052
1053      'main' => [
1054        '#type' => 'container',
1055        '#attributes' => [
1056          'class' => ['visitors-main'],
1057        ],
1058        '1' => [
1059          $this->renderViews($first_row),
1060        ],
1061      ],
1062      '#attached' => [
1063        'library' => [
1064          'visitors/visitors.report',
1065        ],
1066      ],
1067    ];
1068  }
1069
1070  /**
1071   * Returns a social network report.
1072   *
1073   * @return array
1074   *   A render array representing the social network page content.
1075   */
1076  public function social(): array {
1077    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
1078
1079    $first_row['social_visitors_pie'] = [
1080      '#view_id'      => 'visitors',
1081      '#view_display' => 'social_visitors_pie',
1082    ];
1083
1084    return [
1085      'visitors_date_filter_form' => $form,
1086
1087      'main' => [
1088        '#type' => 'container',
1089        '#attributes' => [
1090          'class' => ['visitors-main'],
1091        ],
1092        '1' => [
1093          $this->renderViews($first_row),
1094        ],
1095      ],
1096      '#attached' => [
1097        'library' => [
1098          'visitors/visitors.report',
1099        ],
1100      ],
1101    ];
1102  }
1103
1104  /**
1105   * Returns a AI assistant report.
1106   *
1107   * @return array
1108   *   A render array representing the AI assistant page content.
1109   */
1110  public function ai(): array {
1111    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilterForm');
1112
1113    $first_row['ai_visitors_pie'] = [
1114      '#view_id'      => 'visitors',
1115      '#view_display' => 'ai_visitors_pie',
1116    ];
1117
1118    return [
1119      'visitors_date_filter_form' => $form,
1120
1121      'main' => [
1122        '#type' => 'container',
1123        '#attributes' => [
1124          'class' => ['visitors-main'],
1125        ],
1126        '1' => [
1127          $this->renderViews($first_row),
1128        ],
1129      ],
1130      '#attached' => [
1131        'library' => [
1132          'visitors/visitors.report',
1133        ],
1134      ],
1135    ];
1136  }
1137
1138}