Lines: 81.71%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Plugin\views\row; | |
| 6 | ||
| 7 | use DeviceDetector\Parser\Client\Browser; | |
| 8 | use DeviceDetector\Parser\OperatingSystem; | |
| 9 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 10 | use Drupal\Core\Database\Connection; | |
| 11 | use Drupal\Core\Datetime\DateFormatterInterface; | |
| 12 | use Drupal\Core\Extension\ModuleExtensionList; | |
| 13 | use Drupal\Core\StringTranslation\TranslatableMarkup; | |
| 14 | use Drupal\views\Attribute\ViewsRow; | |
| 15 | use Drupal\views\Plugin\views\query\Sql; | |
| 16 | use Drupal\views\Plugin\views\row\RowPluginBase; | |
| 17 | use Drupal\views\ResultRow; | |
| 18 | use Drupal\visitors\VisitorsLocationInterface; | |
| 19 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 20 | use Symfony\Component\HttpFoundation\RequestStack; | |
| 21 | ||
| 22 | /** | |
| 23 | * Renders one visit as a visitor log card. | |
| 24 | */ | |
| 25 | #[ViewsRow( | |
| 26 | id: 'visitors_long', | |
| 27 | title: new TranslatableMarkup('Long'), | |
| 28 | help: new TranslatableMarkup('Shows one visit and the actions in that visit.'), | |
| 29 | display_types: ['normal'], | |
| 30 | base: ['visitors_visit'], | |
| 31 | register_theme: FALSE, | |
| 32 | )] | |
| 33 | final class VisitLong extends RowPluginBase { | |
| 34 | ||
| 35 | /** | |
| 36 | * {@inheritdoc} | |
| 37 | */ | |
| 38 | protected $usesOptions = FALSE; | |
| 39 | ||
| 40 | /** | |
| 41 | * Visit columns the card reads. | |
| 42 | */ | |
| 43 | private const FIELDS = [ | |
| 44 | 'id', | |
| 45 | 'entry_time', | |
| 46 | 'total_time', | |
| 47 | 'returning', | |
| 48 | 'total_visits', | |
| 49 | 'referer_name', | |
| 50 | 'referer_type', | |
| 51 | 'referer_url', | |
| 52 | 'location_country', | |
| 53 | 'location_city', | |
| 54 | 'config_browser_name', | |
| 55 | 'config_os', | |
| 56 | 'config_device_type', | |
| 57 | 'uid', | |
| 58 | ]; | |
| 59 | ||
| 60 | /** | |
| 61 | * Event columns loaded for the visits on the page. | |
| 62 | */ | |
| 63 | private const EVENT_FIELDS = [ | |
| 64 | 'id', | |
| 65 | 'visit_id', | |
| 66 | 'page_view', | |
| 67 | 'event', | |
| 68 | 'title', | |
| 69 | 'url', | |
| 70 | 'url_prefix', | |
| 71 | 'event_category', | |
| 72 | 'event_action', | |
| 73 | 'event_name', | |
| 74 | 'created', | |
| 75 | ]; | |
| 76 | ||
| 77 | /** | |
| 78 | * Query aliases keyed by column name. | |
| 79 | * | |
| 80 | * @var array<string, string> | |
| 81 | */ | |
| 82 | protected array $aliases = []; | |
| 83 | ||
| 84 | /** | |
| 85 | * Actions keyed by visit id. | |
| 86 | * | |
| 87 | * @var array<int, array<int, array<string, mixed>>> | |
| 88 | */ | |
| 89 | protected array $actions = []; | |
| 90 | ||
| 91 | /** | |
| 92 | * Root-relative path to the module. | |
| 93 | */ | |
| 94 | protected string $iconBase; | |
| 95 | ||
| 96 | /** | |
| 97 | * Constructs a visit log row. | |
| 98 | * | |
| 99 | * @param array $configuration | |
| 100 | * Plugin configuration. | |
| 101 | * @param string $plugin_id | |
| 102 | * The plugin id. | |
| 103 | * @param mixed $plugin_definition | |
| 104 | * The plugin definition. | |
| 105 | * @param \Drupal\Core\Database\Connection $database | |
| 106 | * The database connection. | |
| 107 | * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter | |
| 108 | * The date formatter. | |
| 109 | * @param \Drupal\Core\Extension\ModuleExtensionList $moduleExtensionList | |
| 110 | * The module list. | |
| 111 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack | |
| 112 | * The request stack. | |
| 113 | * @param \Drupal\visitors\VisitorsLocationInterface $location | |
| 114 | * The location service. | |
| 115 | * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | |
| 116 | * The config factory. | |
| 117 | */ | |
| 118 | public function __construct( | |
| 119 | array $configuration, | |
| 120 | $plugin_id, | |
| 121 | $plugin_definition, | |
| 122 | protected Connection $database, | |
| 123 | protected DateFormatterInterface $dateFormatter, | |
| 124 | ModuleExtensionList $moduleExtensionList, | |
| 125 | RequestStack $requestStack, | |
| 126 | protected VisitorsLocationInterface $location, | |
| 127 | protected ConfigFactoryInterface $configFactory, | |
| 128 | ) { | |
| 129 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 130 | 11 | $base_path = ''; |
| 131 | 11 | $request = $requestStack->getCurrentRequest(); |
| 132 | 11 | if ($request) { |
| 133 | 11 | $base_path = $request->getBasePath(); |
| 134 | } | |
| 135 | 11 | $this->iconBase = $base_path . '/' . $moduleExtensionList->getPath('visitors'); |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * {@inheritdoc} | |
| 140 | */ | |
| 141 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
| 142 | 11 | return new self( |
| 143 | 11 | $configuration, |
| 144 | 11 | $plugin_id, |
| 145 | 11 | $plugin_definition, |
| 146 | 11 | $container->get('database'), |
| 147 | 11 | $container->get('date.formatter'), |
| 148 | 11 | $container->get('extension.list.module'), |
| 149 | 11 | $container->get('request_stack'), |
| 150 | 11 | $container->get('visitors.location'), |
| 151 | 11 | $container->get('config.factory'), |
| 152 | 11 | ); |
| 153 | } | |
| 154 | ||
| 155 | /** | |
| 156 | * {@inheritdoc} | |
| 157 | */ | |
| 158 | public function query() { | |
| 159 | 3 | $query = $this->view->query; |
| 160 | 3 | if (!$query instanceof Sql) { |
| 161 | 0 | return; |
| 162 | } | |
| 163 | 3 | foreach (self::FIELDS as $field) { |
| 164 | 3 | $this->aliases[$field] = $query->addField('visitors_visit', $field); |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 168 | /** | |
| 169 | * {@inheritdoc} | |
| 170 | */ | |
| 171 | public function preRender($result) { | |
| 172 | 3 | $this->actions = []; |
| 173 | 3 | $ids = []; |
| 174 | 3 | foreach ($result as $row) { |
| 175 | 3 | $id = (int) $this->column($row, 'id'); |
| 176 | 3 | if ($id > 0) { |
| 177 | 3 | $ids[] = $id; |
| 178 | } | |
| 179 | } | |
| 180 | 3 | if ($ids === []) { |
| 181 | 0 | return; |
| 182 | } | |
| 183 | ||
| 184 | 3 | $query = $this->database->select('visitors_event', 'e'); |
| 185 | 3 | $query->fields('e', self::EVENT_FIELDS); |
| 186 | 3 | $query->condition('visit_id', $ids, 'IN'); |
| 187 | 3 | $query->orderBy('created'); |
| 188 | 3 | $query->orderBy('id'); |
| 189 | 3 | $by_visit = []; |
| 190 | 3 | foreach ($query->execute() as $event) { |
| 191 | 3 | $by_visit[(int) $event->visit_id][] = $event; |
| 192 | } | |
| 193 | 3 | foreach ($by_visit as $visit_id => $events) { |
| 194 | 3 | $this->actions[$visit_id] = $this->groupActions($events); |
| 195 | } | |
| 196 | } | |
| 197 | ||
| 198 | /** | |
| 199 | * {@inheritdoc} | |
| 200 | */ | |
| 201 | public function render($row) { | |
| 202 | 3 | $visit_id = (int) $this->column($row, 'id'); |
| 203 | 3 | $actions = $this->actions[$visit_id] ?? []; |
| 204 | 3 | return [ |
| 205 | 3 | '#theme' => 'visitors_visit_long', |
| 206 | 3 | '#visit' => $this->visit($row, $this->countActions($actions)), |
| 207 | 3 | '#actions' => $actions, |
| 208 | 3 | '#attached' => [ |
| 209 | 3 | 'library' => ['visitors/visitors.report'], |
| 210 | 3 | ], |
| 211 | 3 | ]; |
| 212 | } | |
| 213 | ||
| 214 | /** | |
| 215 | * Builds the visit values for the template. | |
| 216 | * | |
| 217 | * @param \Drupal\views\ResultRow $row | |
| 218 | * The visit row. | |
| 219 | * @param int $count | |
| 220 | * The number of actions in the visit. | |
| 221 | * | |
| 222 | * @return array<string, mixed> | |
| 223 | * Template values. | |
| 224 | */ | |
| 225 | private function visit(ResultRow $row, int $count): array { | |
| 226 | 3 | $seconds = (int) $this->column($row, 'total_time'); |
| 227 | 3 | $summary = $this->formatPlural($count, '1 Action', '@count Actions'); |
| 228 | 3 | if ($seconds > 0) { |
| 229 | 3 | $summary = $this->t('@actions - @duration', [ |
| 230 | 3 | '@actions' => $summary, |
| 231 | 3 | '@duration' => $this->formatDuration($seconds), |
| 232 | 3 | ]); |
| 233 | } | |
| 234 | ||
| 235 | 3 | $referrer = $this->referrer($row); |
| 236 | 3 | $entry = (int) $this->column($row, 'entry_time'); |
| 237 | 3 | return [ |
| 238 | 3 | 'datetime' => $entry > 0 |
| 239 | 3 | ? $this->dateFormatter->format($entry, 'custom', 'l, F j, Y - H:i:s') |
| 240 | 3 | : '', |
| 241 | 3 | 'site_name' => (string) $this->configFactory->get('system.site')->get('name'), |
| 242 | 3 | 'referrer_label' => $referrer['label'], |
| 243 | 3 | 'referrer_url' => $referrer['url'], |
| 244 | 3 | 'visitor_type' => $this->visitorType($row), |
| 245 | 3 | 'user' => $this->userLabel($row), |
| 246 | 3 | 'icons' => $this->icons($row), |
| 247 | 3 | 'summary' => $summary, |
| 248 | 3 | ]; |
| 249 | } | |
| 250 | ||
| 251 | /** | |
| 252 | * Reads one selected visit column. | |
| 253 | * | |
| 254 | * @param \Drupal\views\ResultRow $row | |
| 255 | * The visit row. | |
| 256 | * @param string $field | |
| 257 | * The column name. | |
| 258 | * | |
| 259 | * @return mixed | |
| 260 | * The column value, or NULL when it was not selected. | |
| 261 | */ | |
| 262 | private function column(ResultRow $row, string $field): mixed { | |
| 263 | 3 | $alias = $this->aliases[$field] ?? ''; |
| 264 | 3 | if ($alias === '' || !isset($row->{$alias})) { |
| 265 | 0 | return NULL; |
| 266 | } | |
| 267 | 3 | return $row->{$alias}; |
| 268 | } | |
| 269 | ||
| 270 | /** | |
| 271 | * Builds the referrer label and a safe link. | |
| 272 | * | |
| 273 | * @param \Drupal\views\ResultRow $row | |
| 274 | * The visit row. | |
| 275 | * | |
| 276 | * @return array{label: string, url: string} | |
| 277 | * The label and an http(s) URL, or an empty URL. | |
| 278 | */ | |
| 279 | private function referrer(ResultRow $row): array { | |
| 280 | 3 | $name = trim((string) $this->column($row, 'referer_name')); |
| 281 | 3 | $label = $name !== '' ? $name : $this->channelLabel((string) $this->column($row, 'referer_type')); |
| 282 | 3 | $url = ''; |
| 283 | 3 | if ($name !== '') { |
| 284 | 3 | $url = $this->safeUrl((string) $this->column($row, 'referer_url')); |
| 285 | } | |
| 286 | 3 | return [ |
| 287 | 3 | 'label' => $label, |
| 288 | 3 | 'url' => $url, |
| 289 | 3 | ]; |
| 290 | } | |
| 291 | ||
| 292 | /** | |
| 293 | * Maps a referrer channel to the report label. | |
| 294 | * | |
| 295 | * @param string $type | |
| 296 | * The stored referer_type. | |
| 297 | * | |
| 298 | * @return string | |
| 299 | * The channel label, or an empty string when the type is empty. | |
| 300 | */ | |
| 301 | private function channelLabel(string $type): string { | |
| 302 | 0 | return match ($type) { |
| 303 | 0 | 'direct' => (string) $this->t('Direct Entry'), |
| 304 | 0 | 'website' => (string) $this->t('Website'), |
| 305 | 0 | 'internal' => (string) $this->t('Internal'), |
| 306 | 0 | 'search_engine' => (string) $this->t('Search Engine'), |
| 307 | 0 | 'spam' => (string) $this->t('Spam'), |
| 308 | 0 | 'social_network' => (string) $this->t('Social Network'), |
| 309 | 0 | 'ai_assistant' => (string) $this->t('AI Assistant'), |
| 310 | 0 | 'campaign' => (string) $this->t('Campaign'), |
| 311 | 0 | '' => '', |
| 312 | 0 | default => (string) $this->t('Unknown'), |
| 313 | 0 | }; |
| 314 | } | |
| 315 | ||
| 316 | /** | |
| 317 | * Returns New or Returning. | |
| 318 | * | |
| 319 | * @param \Drupal\views\ResultRow $row | |
| 320 | * The visit row. | |
| 321 | * | |
| 322 | * @return string | |
| 323 | * The visitor type label. | |
| 324 | */ | |
| 325 | private function visitorType(ResultRow $row): string { | |
| 326 | 3 | $returning = (int) $this->column($row, 'returning'); |
| 327 | 3 | $visits = (int) $this->column($row, 'total_visits'); |
| 328 | 3 | if ($returning === 1 || $visits > 1) { |
| 329 | 0 | return (string) $this->t('Returning'); |
| 330 | } | |
| 331 | 3 | return (string) $this->t('New'); |
| 332 | } | |
| 333 | ||
| 334 | /** | |
| 335 | * Returns the stored user id when one was recorded. | |
| 336 | * | |
| 337 | * @param \Drupal\views\ResultRow $row | |
| 338 | * The visit row. | |
| 339 | * | |
| 340 | * @return string | |
| 341 | * The user id, or an empty string. | |
| 342 | */ | |
| 343 | private function userLabel(ResultRow $row): string { | |
| 344 | 3 | $uid = trim((string) $this->column($row, 'uid')); |
| 345 | 3 | if ($uid === '' || $uid === '0') { |
| 346 | 0 | return ''; |
| 347 | } | |
| 348 | 3 | return $uid; |
| 349 | } | |
| 350 | ||
| 351 | /** | |
| 352 | * Builds the device icon list. | |
| 353 | * | |
| 354 | * @param \Drupal\views\ResultRow $row | |
| 355 | * The visit row. | |
| 356 | * | |
| 357 | * @return array<int, array{src: string, alt: string}> | |
| 358 | * Icon URL and accessible name. | |
| 359 | */ | |
| 360 | private function icons(ResultRow $row): array { | |
| 361 | 3 | $icons = []; |
| 362 | 3 | $country = strtoupper(trim((string) $this->column($row, 'location_country'))); |
| 363 | 3 | $flag = $this->iconFile('icons/flags', strtolower($country), 'png'); |
| 364 | 3 | if ($flag !== NULL && $country !== '') { |
| 365 | 3 | $place = (string) $this->location->getCountryLabel($country); |
| 366 | 3 | $city = trim((string) $this->column($row, 'location_city')); |
| 367 | 3 | if ($city !== '') { |
| 368 | 0 | $place = $city . ', ' . $place; |
| 369 | } | |
| 370 | 3 | $icons[] = [ |
| 371 | 3 | 'src' => $flag, |
| 372 | 3 | 'alt' => $place, |
| 373 | 3 | ]; |
| 374 | } | |
| 375 | ||
| 376 | 3 | $browser_code = trim((string) $this->column($row, 'config_browser_name')); |
| 377 | 3 | $browser = $this->iconFile('icons/browsers', $browser_code, 'png'); |
| 378 | 3 | if ($browser !== NULL) { |
| 379 | 3 | $names = Browser::getAvailableBrowsers(); |
| 380 | 3 | $icons[] = [ |
| 381 | 3 | 'src' => $browser, |
| 382 | 3 | 'alt' => (string) ($names[$browser_code] ?? $browser_code), |
| 383 | 3 | ]; |
| 384 | } | |
| 385 | ||
| 386 | 3 | $os_code = trim((string) $this->column($row, 'config_os')); |
| 387 | 3 | $os = $this->iconFile('icons/os', $os_code, 'png'); |
| 388 | 3 | if ($os !== NULL) { |
| 389 | 3 | $os_name = (string) OperatingSystem::getOsFamily($os_code); |
| 390 | 3 | $icons[] = [ |
| 391 | 3 | 'src' => $os, |
| 392 | 3 | 'alt' => $os_name !== '' ? $os_name : $os_code, |
| 393 | 3 | ]; |
| 394 | } | |
| 395 | ||
| 396 | 3 | $device_code = trim((string) $this->column($row, 'config_device_type')); |
| 397 | 3 | if ($device_code === '') { |
| 398 | 0 | $device_code = 'unknown'; |
| 399 | } | |
| 400 | 3 | $device = $this->iconFile('icons/devices', $device_code, 'png'); |
| 401 | 3 | if ($device !== NULL) { |
| 402 | 3 | $icons[] = [ |
| 403 | 3 | 'src' => $device, |
| 404 | 3 | 'alt' => ucwords($device_code), |
| 405 | 3 | ]; |
| 406 | } | |
| 407 | 3 | return $icons; |
| 408 | } | |
| 409 | ||
| 410 | /** | |
| 411 | * Groups hits that share a page view under that page view. | |
| 412 | * | |
| 413 | * @param array<int, object> $events | |
| 414 | * Event rows in chronological order. | |
| 415 | * | |
| 416 | * @return array<int, array<string, mixed>> | |
| 417 | * Top-level actions. Nested hits are in children. | |
| 418 | */ | |
| 419 | private function groupActions(array $events): array { | |
| 420 | 3 | $groups = []; |
| 421 | 3 | $index_by_page = []; |
| 422 | 3 | foreach ($events as $event) { |
| 423 | 3 | $item = $this->action($event); |
| 424 | 3 | $page_view = trim((string) ($event->page_view ?? '')); |
| 425 | 3 | if ($item['type'] === 'page') { |
| 426 | 3 | $groups[] = $item; |
| 427 | 3 | if ($page_view !== '' && !isset($index_by_page[$page_view])) { |
| 428 | 3 | $index_by_page[$page_view] = count($groups) - 1; |
| 429 | } | |
| 430 | 3 | continue; |
| 431 | } | |
| 432 | 3 | if ($page_view !== '' && isset($index_by_page[$page_view])) { |
| 433 | 3 | $groups[$index_by_page[$page_view]]['children'][] = $item; |
| 434 | 3 | continue; |
| 435 | } | |
| 436 | 3 | $groups[] = $item; |
| 437 | } | |
| 438 | 3 | return $groups; |
| 439 | } | |
| 440 | ||
| 441 | /** | |
| 442 | * Builds one action for the template. | |
| 443 | * | |
| 444 | * @param object $event | |
| 445 | * The event row. | |
| 446 | * | |
| 447 | * @return array<string, mixed> | |
| 448 | * Action values, including an empty children list. | |
| 449 | */ | |
| 450 | private function action(object $event): array { | |
| 451 | 3 | $type = $this->actionType($event); |
| 452 | 3 | return [ |
| 453 | 3 | 'type' => $type, |
| 454 | 3 | 'icon' => $this->iconBase . '/icons/actions/' . $this->actionIcon($type), |
| 455 | 3 | 'alt' => $this->actionAlt($type), |
| 456 | 3 | 'title' => $this->actionTitle($event, $type), |
| 457 | 3 | 'url' => $this->actionUrl($event), |
| 458 | 3 | 'children' => [], |
| 459 | 3 | ]; |
| 460 | } | |
| 461 | ||
| 462 | /** | |
| 463 | * Classifies an event row. | |
| 464 | * | |
| 465 | * @param object $event | |
| 466 | * The event row. | |
| 467 | * | |
| 468 | * @return string | |
| 469 | * page, download, outlink, search, or event. | |
| 470 | */ | |
| 471 | private function actionType(object $event): string { | |
| 472 | 3 | $name = strtolower(trim((string) ($event->event ?? ''))); |
| 473 | 3 | if (in_array($name, ['download', 'outlink', 'search'], TRUE)) { |
| 474 | 3 | return $name; |
| 475 | } | |
| 476 | 3 | $category = trim((string) ($event->event_category ?? '')); |
| 477 | 3 | if ($category !== '' || $name !== '') { |
| 478 | 0 | return 'event'; |
| 479 | } | |
| 480 | 3 | return 'page'; |
| 481 | } | |
| 482 | ||
| 483 | /** | |
| 484 | * Returns the action icon file name. | |
| 485 | * | |
| 486 | * @param string $type | |
| 487 | * The action type. | |
| 488 | * | |
| 489 | * @return string | |
| 490 | * The SVG file name. | |
| 491 | */ | |
| 492 | private function actionIcon(string $type): string { | |
| 493 | 3 | return match ($type) { |
| 494 | 3 | 'download' => 'download.svg', |
| 495 | 3 | 'outlink' => 'link.svg', |
| 496 | 0 | 'search' => 'search.svg', |
| 497 | 0 | 'event' => 'event.svg', |
| 498 | 3 | default => 'action.svg', |
| 499 | 3 | }; |
| 500 | } | |
| 501 | ||
| 502 | /** | |
| 503 | * Returns the accessible name for an action icon. | |
| 504 | * | |
| 505 | * @param string $type | |
| 506 | * The action type. | |
| 507 | * | |
| 508 | * @return string | |
| 509 | * The icon alt text. | |
| 510 | */ | |
| 511 | private function actionAlt(string $type): string { | |
| 512 | 3 | return match ($type) { |
| 513 | 3 | 'download' => (string) $this->t('Download'), |
| 514 | 3 | 'outlink' => (string) $this->t('Outlink'), |
| 515 | 0 | 'search' => (string) $this->t('Search'), |
| 516 | 0 | 'event' => (string) $this->t('Event'), |
| 517 | 3 | default => (string) $this->t('Page view'), |
| 518 | 3 | }; |
| 519 | } | |
| 520 | ||
| 521 | /** | |
| 522 | * Returns the visible title for an action. | |
| 523 | * | |
| 524 | * @param object $event | |
| 525 | * The event row. | |
| 526 | * @param string $type | |
| 527 | * The action type. | |
| 528 | * | |
| 529 | * @return string | |
| 530 | * The title. | |
| 531 | */ | |
| 532 | private function actionTitle(object $event, string $type): string { | |
| 533 | 3 | if ($type === 'page') { |
| 534 | 3 | return trim((string) ($event->title ?? '')); |
| 535 | } | |
| 536 | 3 | if ($type !== 'event') { |
| 537 | 3 | return $this->actionAlt($type); |
| 538 | } | |
| 539 | ||
| 540 | 0 | $parts = array_filter([ |
| 541 | 0 | trim((string) ($event->event_category ?? '')), |
| 542 | 0 | trim((string) ($event->event_action ?? '')), |
| 543 | 0 | ], static fn (string $part): bool => $part !== ''); |
| 544 | 0 | $label = implode(' - ', $parts); |
| 545 | 0 | $name = trim((string) ($event->event_name ?? '')); |
| 546 | 0 | if ($name !== '') { |
| 547 | 0 | $label = $label === '' ? $name : $label . ': ' . $name; |
| 548 | } | |
| 549 | 0 | if ($label !== '') { |
| 550 | 0 | return $label; |
| 551 | } | |
| 552 | 0 | $title = trim((string) ($event->title ?? '')); |
| 553 | 0 | if ($title !== '') { |
| 554 | 0 | return $title; |
| 555 | } | |
| 556 | 0 | return trim((string) ($event->event ?? '')); |
| 557 | } | |
| 558 | ||
| 559 | /** | |
| 560 | * Rebuilds an event URL from the stored prefix. | |
| 561 | * | |
| 562 | * @param object $event | |
| 563 | * The event row. | |
| 564 | * | |
| 565 | * @return string | |
| 566 | * An http(s) URL, or an empty string. | |
| 567 | */ | |
| 568 | private function actionUrl(object $event): string { | |
| 569 | 3 | $url = trim((string) ($event->url ?? '')); |
| 570 | 3 | if ($url === '') { |
| 571 | 0 | return ''; |
| 572 | } | |
| 573 | 3 | if (!preg_match('#^https?://#i', $url)) { |
| 574 | 3 | $url = $this->urlPrefix((int) ($event->url_prefix ?? 0)) . $url; |
| 575 | } | |
| 576 | 3 | return $this->safeUrl($url); |
| 577 | } | |
| 578 | ||
| 579 | /** | |
| 580 | * Maps a stored URL prefix to a scheme and host prefix. | |
| 581 | * | |
| 582 | * @param int $prefix | |
| 583 | * The visitors URL prefix. | |
| 584 | * | |
| 585 | * @return string | |
| 586 | * The prefix to prepend. | |
| 587 | */ | |
| 588 | private function urlPrefix(int $prefix): string { | |
| 589 | 3 | return match ($prefix) { |
| 590 | 0 | 3 => 'https://www.', |
| 591 | 3 | 2 => 'https://', |
| 592 | 0 | 1 => 'http://www.', |
| 593 | 3 | default => 'http://', |
| 594 | 3 | }; |
| 595 | } | |
| 596 | ||
| 597 | /** | |
| 598 | * Keeps only http and https URLs. | |
| 599 | * | |
| 600 | * @param string $url | |
| 601 | * A candidate URL. | |
| 602 | * | |
| 603 | * @return string | |
| 604 | * The URL, or an empty string when the scheme is not allowed. | |
| 605 | */ | |
| 606 | private function safeUrl(string $url): string { | |
| 607 | 3 | $url = trim($url); |
| 608 | 3 | $scheme = parse_url($url, PHP_URL_SCHEME); |
| 609 | 3 | if (!is_string($scheme)) { |
| 610 | 0 | return ''; |
| 611 | } | |
| 612 | 3 | if (!in_array(strtolower($scheme), ['http', 'https'], TRUE)) { |
| 613 | 0 | return ''; |
| 614 | } | |
| 615 | 3 | return $url; |
| 616 | } | |
| 617 | ||
| 618 | /** | |
| 619 | * Builds a module icon URL when the name is a single path segment. | |
| 620 | * | |
| 621 | * @param string $directory | |
| 622 | * The icon directory under the module. | |
| 623 | * @param string $name | |
| 624 | * The file name without an extension. | |
| 625 | * @param string $extension | |
| 626 | * The file extension. | |
| 627 | * | |
| 628 | * @return string|null | |
| 629 | * The icon URL, or NULL when the name is empty or unsafe. | |
| 630 | */ | |
| 631 | private function iconFile(string $directory, string $name, string $extension): ?string { | |
| 632 | 3 | if ($name === '' || !preg_match('/^[A-Za-z0-9_-]+$/', $name)) { |
| 633 | 0 | return NULL; |
| 634 | } | |
| 635 | 3 | return $this->iconBase . '/' . $directory . '/' . $name . '.' . $extension; |
| 636 | } | |
| 637 | ||
| 638 | /** | |
| 639 | * Formats a duration as seconds, minutes, and hours. | |
| 640 | * | |
| 641 | * @param int $seconds | |
| 642 | * The duration in seconds. | |
| 643 | * | |
| 644 | * @return string | |
| 645 | * A duration such as "49s" or "1 min 25s". | |
| 646 | */ | |
| 647 | private function formatDuration(int $seconds): string { | |
| 648 | 3 | $hours = intdiv($seconds, 3600); |
| 649 | 3 | $minutes = intdiv($seconds % 3600, 60); |
| 650 | 3 | $remainder = $seconds % 60; |
| 651 | 3 | $parts = []; |
| 652 | 3 | if ($hours > 0) { |
| 653 | 0 | $parts[] = $hours . ' ' . $this->t('h'); |
| 654 | } | |
| 655 | 3 | if ($minutes > 0) { |
| 656 | 3 | $parts[] = $minutes . ' ' . $this->t('min'); |
| 657 | } | |
| 658 | 3 | if ($remainder > 0 || $parts === []) { |
| 659 | 3 | $parts[] = $remainder . 's'; |
| 660 | } | |
| 661 | 3 | return implode(' ', $parts); |
| 662 | } | |
| 663 | ||
| 664 | /** | |
| 665 | * Counts actions, including hits nested under a page view. | |
| 666 | * | |
| 667 | * @param array<int, array<string, mixed>> $actions | |
| 668 | * Top-level actions. | |
| 669 | * | |
| 670 | * @return int | |
| 671 | * The number of actions. | |
| 672 | */ | |
| 673 | private function countActions(array $actions): int { | |
| 674 | 3 | $count = 0; |
| 675 | 3 | foreach ($actions as $action) { |
| 676 | 3 | $count++; |
| 677 | 3 | $children = $action['children'] ?? []; |
| 678 | 3 | if (is_array($children)) { |
| 679 | 3 | $count += count($children); |
| 680 | } | |
| 681 | } | |
| 682 | 3 | return $count; |
| 683 | } | |
| 684 | ||
| 685 | } |