Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
408 / 408
100.00% covered (success)
100.00%
21 / 21
CRAP
100.00% covered (success)
100.00%
1 / 1
ReportController
100.00% covered (success)
100.00%
408 / 408
100.00% covered (success)
100.00%
21 / 21
30
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
 topPages
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
 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%
33 / 33
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
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 $locationService;
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->locationService = $location;
103    $this->moduleHandler   = $module_handler;
104
105    $this->settings = $config_factory->get('visitors.config');
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\DateFilter');
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\DateFilter');
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 top pages report.
286   *
287   * @return array
288   *   A render array representing the top pages page content.
289   */
290  public function topPages(): array {
291    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
292    $blocks['top_path_table'] = [
293      '#view_id'      => 'visitors',
294      '#view_display' => 'top_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 hosts page.
313   *
314   * @return array
315   *   A render array representing the hosts page content.
316   */
317  public function topHost(): array {
318    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
319    $blocks['path'] = [
320      '#view_id'      => 'visitors',
321      '#view_display' => 'top_host_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 hosts report.
340   *
341   * @return array
342   *   A render array representing the hosts page content.
343   */
344  public function recentHost($host): array {
345    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
346    $blocks['path'] = [
347      '#view_id'      => 'visitors',
348      '#view_display' => 'recent_view_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, NULL, [NULL, $host]),
360        ],
361      ],
362    ];
363  }
364
365  /**
366   * Returns a title for the page.
367   */
368  public function getHostTitle(string $host) {
369    $title = $this->stringTranslation
370      ->translate('Visits from @host', ['@host' => $host]);
371
372    return $title;
373  }
374
375  /**
376   * Returns a top route page.
377   *
378   * @return array
379   *   A render array representing the top pages page content.
380   */
381  public function topRoute(): array {
382    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
383    $blocks['route'] = [
384      '#view_id'      => 'visitors',
385      '#view_display' => 'top_route_table',
386    ];
387
388    return [
389      'visitors_date_filter_form' => $form,
390      'main' => [
391        '#type' => 'container',
392        '#attributes' => [
393          'class' => ['visitors-main'],
394        ],
395        '1' => [
396          $this->renderViews($blocks),
397        ],
398      ],
399    ];
400  }
401
402  /**
403   * Returns recent visitors filtered by route.
404   *
405   * @return array
406   *   A render array representing page views.
407   */
408  public function recentRoute(string $route) {
409    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
410    $blocks['route'] = [
411      '#view_id'      => 'visitors',
412      '#view_display' => 'recent_view_table',
413    ];
414
415    return [
416      'visitors_date_filter_form' => $form,
417      'main' => [
418        '#type' => 'container',
419        '#attributes' => [
420          'class' => ['visitors-main'],
421        ],
422        '1' => [
423          $this->renderViews($blocks, NULL, [$route]),
424        ],
425      ],
426    ];
427  }
428
429  /**
430   * Returns a title for the page.
431   */
432  public function getRouteTitle(string $route) {
433    $title = $this->stringTranslation
434      ->translate('Route @route', ['@route' => $route]);
435
436    return $title;
437  }
438
439  /**
440   * Returns a recent hits page.
441   *
442   * @return array
443   *   A render array representing the recent hits page content.
444   */
445  public function recentViews(): array {
446    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
447    $blocks['path'] = [
448      '#view_id'      => 'visitors',
449      '#view_display' => 'recent_view_table',
450    ];
451
452    return [
453      'visitors_date_filter_form' => $form,
454      'main' => [
455        '#type' => 'container',
456        '#attributes' => [
457          'class' => ['visitors-main'],
458        ],
459        '1' => [
460          $this->renderViews($blocks),
461        ],
462      ],
463    ];
464  }
465
466  /**
467   * Returns referrer report.
468   */
469  public function nodeViews(int $node): array {
470
471    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
472    $blocks['path'] = [
473      '#view_id'      => 'visitors',
474      '#view_display' => 'referrer_table',
475    ];
476
477    return [
478      'visitors_date_filter_form' => $form,
479      'main' => [
480        '#type' => 'container',
481        '#attributes' => [
482          'class' => ['visitors-main'],
483        ],
484        '1' => [
485          $this->renderViews($blocks),
486        ],
487      ],
488    ];
489  }
490
491  /**
492   * Shows report related to devices.
493   *
494   * @return array
495   *   A render array representing the days of month page content.
496   */
497  public function device(): array {
498
499    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
500    $first_blocks = [];
501    $second_blocks = [];
502
503    $first_blocks['device_type_table'] = [
504      '#view_id'      => 'visitors',
505      '#view_display' => 'device_type_table',
506      '#attributes'   => [
507        'class' => ['layout-column--half'],
508      ],
509    ];
510    $first_blocks['device_model_table'] = [
511      '#view_id'      => 'visitors',
512      '#view_display' => 'device_model_table',
513      '#attributes'   => [
514        'class' => ['layout-column--half'],
515      ],
516    ];
517
518    $second_blocks['device_brand_table'] = [
519      '#view_id'      => 'visitors',
520      '#view_display' => 'device_brand_table',
521      '#attributes'   => [
522        'class' => ['layout-column--half'],
523      ],
524    ];
525    $second_blocks['device_resolution_table'] = [
526      '#view_id'      => 'visitors',
527      '#view_display' => 'device_resolution_table',
528      '#attributes'   => [
529        'class' => ['layout-column--half'],
530      ],
531    ];
532
533    return [
534      'visitors_date_filter_form' => $form,
535      'main' => [
536        '#type' => 'container',
537        '#attributes' => [
538          'class' => ['visitors-main'],
539        ],
540        '1' => [
541          $this->renderViews($first_blocks, 'layout-row'),
542        ],
543        '2' => [
544          $this->renderViews($second_blocks, 'layout-row'),
545        ],
546      ],
547      '#attached' => [
548        'library' => [
549          'visitors/visitors.report',
550        ],
551      ],
552    ];
553  }
554
555  /**
556   * Returns a hours page.
557   *
558   * @return array
559   *   A render array representing the hours page content.
560   */
561  public function location(): array {
562    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
563
564    $first_row['continent_table'] = [
565      '#view_id'      => 'visitors',
566      '#view_display' => 'continent_table',
567      '#attributes'   => [
568        'class' => ['layout-column--half'],
569      ],
570    ];
571
572    $first_row['country_table'] = [
573      '#view_id'      => 'visitors',
574      '#view_display' => 'country_table',
575      '#attributes'   => [
576        'class' => ['layout-column--half'],
577      ],
578    ];
579
580    $second_row['distinct_countries_list'] = [
581      '#view_id'      => 'visitors',
582      '#view_display' => 'distinct_countries_list',
583      '#attributes'   => [
584        'class' => ['layout-column--half'],
585      ],
586    ];
587    $second_row['region_table'] = [
588      '#view_id'      => 'visitors_geoip',
589      '#view_display' => 'region_table',
590      '#attributes'   => [
591        'class' => ['layout-column--half'],
592      ],
593    ];
594
595    $third_row['language_table'] = [
596      '#view_id'      => 'visitors',
597      '#view_display' => 'language_table',
598      '#attributes'   => [
599        'class' => ['layout-column--half'],
600      ],
601    ];
602    $third_row['city_table'] = [
603      '#view_id'      => 'visitors_geoip',
604      '#view_display' => 'city_table',
605      '#attributes'   => [
606        'class' => ['layout-column--half'],
607      ],
608    ];
609
610    return [
611      'visitors_date_filter_form' => $form,
612
613      'main' => [
614        '#type' => 'container',
615        '#attributes' => [
616          'class' => ['visitors-main'],
617        ],
618        '1' => [
619          $this->renderViews($first_row, 'layout-row'),
620        ],
621        '2' => [
622          $this->renderViews($second_row, 'layout-row'),
623        ],
624        '3' => [
625          $this->renderViews($third_row, 'layout-row'),
626        ],
627      ],
628      '#attached' => [
629        'library' => [
630          'visitors/visitors.report',
631        ],
632      ],
633    ];
634  }
635
636  /**
637   * Returns the Continent as the title for the page.
638   */
639  public function getContinentTitle($continent) {
640    $title = $this->t('Continent');
641    if ($continent) {
642      $title = $this->locationService->getContinentLabel($continent);
643    }
644
645    return $title;
646  }
647
648  /**
649   * Returns a continent page.
650   *
651   * @return array
652   *   A render array representing the continent page content.
653   */
654  public function continent($continent): array {
655    $args = [];
656    $view_display = 'continent_table';
657    if ($continent) {
658      $args[] = $continent;
659      $view_display = 'country_table';
660    }
661    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
662
663    $first_row['continent_table'] = [
664      '#view_id'      => 'visitors',
665      '#view_display' => $view_display,
666    ];
667
668    return [
669      'visitors_date_filter_form' => $form,
670
671      'main' => [
672        '#type' => 'container',
673        '#attributes' => [
674          'class' => ['visitors-main'],
675        ],
676        '1' => [
677          $this->renderViews($first_row, NULL, $args),
678        ],
679      ],
680      '#attached' => [
681        'library' => [
682          'visitors/visitors.report',
683        ],
684      ],
685    ];
686  }
687
688  /**
689   * Returns the Country as the title for the page.
690   */
691  public function getCountryTitle($country) {
692    $title = $this->t('Country');
693    if ($country) {
694      $title = $this->locationService->getCountryLabel($country);
695    }
696
697    return $title;
698  }
699
700  /**
701   * Returns a country page.
702   *
703   * @return array
704   *   A render array representing the country page content.
705   */
706  public function country($country): array {
707    $args = [];
708    $view_id = 'visitors';
709    $view_display = 'country_table';
710    if ($country) {
711      $args[] = NULL;
712      $args[] = NULL;
713      $args[] = $country;
714      $view_display = 'recent_view_table';
715    }
716    if ($this->moduleHandler->moduleExists('visitors_geoip')) {
717      $args = [$country];
718      $view_id = 'visitors_geoip';
719      $view_display = 'region_table';
720    }
721    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
722    $first_row['country_table'] = [
723      '#view_id'      => $view_id,
724      '#view_display' => $view_display,
725    ];
726
727    return [
728      'visitors_date_filter_form' => $form,
729
730      'main' => [
731        '#type' => 'container',
732        '#attributes' => [
733          'class' => ['visitors-main'],
734        ],
735        '1' => [
736          $this->renderViews($first_row, NULL, $args),
737        ],
738      ],
739      '#attached' => [
740        'library' => [
741          'visitors/visitors.report',
742        ],
743      ],
744    ];
745  }
746
747  /**
748   * Display the performance report.
749   */
750  public function performance($sequence = NULL): array {
751    $view_display = NULL;
752    switch ($sequence) {
753      case 'hour':
754        $view_display = 'performance_hourly_column';
755        break;
756
757      case 'day':
758        $view_display = 'performance_daily_column';
759        break;
760
761      case 'week':
762      default:
763        $view_display = 'performance_weekly_column';
764        break;
765    }
766
767    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
768
769    $first_row['performance_daily_column'] = [
770      '#view_id'      => 'visitors',
771      '#view_display' => $view_display,
772    ];
773
774    return [
775      'visitors_date_filter_form' => $form,
776      'visitors_performance' => [
777        '1' => $this->renderViews($first_row, 'layout-row'),
778      ],
779    ];
780  }
781
782}