137eb8894SGreg Roach<?php 23976b470SGreg Roach 337eb8894SGreg Roach/** 437eb8894SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 637eb8894SGreg Roach * This program is free software: you can redistribute it and/or modify 737eb8894SGreg Roach * it under the terms of the GNU General Public License as published by 837eb8894SGreg Roach * the Free Software Foundation, either version 3 of the License, or 937eb8894SGreg Roach * (at your option) any later version. 1037eb8894SGreg Roach * This program is distributed in the hope that it will be useful, 1137eb8894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1237eb8894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1337eb8894SGreg Roach * GNU General Public License for more details. 1437eb8894SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 1637eb8894SGreg Roach */ 17fcfa147eSGreg Roach 1837eb8894SGreg Roachdeclare(strict_types=1); 1937eb8894SGreg Roach 2037eb8894SGreg Roachnamespace Fisharebest\Webtrees\Module; 2137eb8894SGreg Roach 22685bff15SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ModulesAnalyticsPage; 23f4917837SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 2437eb8894SGreg Roachuse Fisharebest\Webtrees\I18N; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 266ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 273976b470SGreg Roach 286ccdf4f0SGreg Roachuse function app; 2937eb8894SGreg Roach 3037eb8894SGreg Roach/** 3137eb8894SGreg Roach * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface 3237eb8894SGreg Roach */ 3337eb8894SGreg Roachtrait ModuleAnalyticsTrait 3437eb8894SGreg Roach{ 35f4917837SGreg Roach use ViewResponseTrait; 36f4917837SGreg Roach 3737eb8894SGreg Roach /** 3837eb8894SGreg Roach * Should we add this tracker? 3937eb8894SGreg Roach * 4037eb8894SGreg Roach * @return bool 4137eb8894SGreg Roach */ 4237eb8894SGreg Roach public function analyticsCanShow(): bool 4337eb8894SGreg Roach { 446ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 45abafa13cSGreg Roach 466ccdf4f0SGreg Roach // If the browser sets the DNT header, then we won't use analytics. 476ccdf4f0SGreg Roach $dnt = $request->getServerParams()['HTTP_DNT'] ?? ''; 486ccdf4f0SGreg Roach 496ccdf4f0SGreg Roach if ($dnt === '1') { 50abafa13cSGreg Roach return false; 51abafa13cSGreg Roach } 52abafa13cSGreg Roach 5337eb8894SGreg Roach foreach ($this->analyticsParameters() as $parameter) { 5437eb8894SGreg Roach if ($parameter === '') { 5537eb8894SGreg Roach return false; 5637eb8894SGreg Roach } 5737eb8894SGreg Roach } 5837eb8894SGreg Roach 5937eb8894SGreg Roach return true; 6037eb8894SGreg Roach } 6137eb8894SGreg Roach 6237eb8894SGreg Roach /** 636ccdf4f0SGreg Roach * The parameters that need to be embedded in the snippet. 646ccdf4f0SGreg Roach * 65*24f2a3afSGreg Roach * @return array<string> 666ccdf4f0SGreg Roach */ 676ccdf4f0SGreg Roach public function analyticsParameters(): array 686ccdf4f0SGreg Roach { 696ccdf4f0SGreg Roach return []; 706ccdf4f0SGreg Roach } 716ccdf4f0SGreg Roach 726ccdf4f0SGreg Roach /** 7337eb8894SGreg Roach * A sentence describing what this module does. 7437eb8894SGreg Roach * 7537eb8894SGreg Roach * @return string 7637eb8894SGreg Roach */ 7737eb8894SGreg Roach public function description(): string 7837eb8894SGreg Roach { 7937eb8894SGreg Roach return I18N::translate('Tracking and analytics'); 8037eb8894SGreg Roach } 8137eb8894SGreg Roach 8237eb8894SGreg Roach /** 8357ab2231SGreg Roach * @param ServerRequestInterface $request 8457ab2231SGreg Roach * 856ccdf4f0SGreg Roach * @return ResponseInterface 866ccdf4f0SGreg Roach */ 8757ab2231SGreg Roach public function getAdminAction(ServerRequestInterface $request): ResponseInterface 886ccdf4f0SGreg Roach { 896ccdf4f0SGreg Roach $this->layout = 'layouts/administration'; 906ccdf4f0SGreg Roach 916ccdf4f0SGreg Roach return $this->viewResponse('admin/analytics-edit', [ 9283615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'Admin']), 936ccdf4f0SGreg Roach 'form_fields' => $this->analyticsFormFields(), 946ccdf4f0SGreg Roach 'preview' => $this->analyticsSnippet($this->analyticsParameters()), 956ccdf4f0SGreg Roach 'title' => $this->title(), 966ccdf4f0SGreg Roach ]); 976ccdf4f0SGreg Roach } 986ccdf4f0SGreg Roach 996ccdf4f0SGreg Roach /** 10037eb8894SGreg Roach * Form fields to edit the parameters. 10137eb8894SGreg Roach * 10237eb8894SGreg Roach * @return string 10337eb8894SGreg Roach */ 10437eb8894SGreg Roach public function analyticsFormFields(): string 10537eb8894SGreg Roach { 10637eb8894SGreg Roach return ''; 10737eb8894SGreg Roach } 10837eb8894SGreg Roach 10937eb8894SGreg Roach /** 11037eb8894SGreg Roach * Embed placeholders in the snippet. 11137eb8894SGreg Roach * 11237eb8894SGreg Roach * @param string[] $parameters 11337eb8894SGreg Roach * 11437eb8894SGreg Roach * @return string 11537eb8894SGreg Roach */ 11637eb8894SGreg Roach public function analyticsSnippet(array $parameters): string 11737eb8894SGreg Roach { 11837eb8894SGreg Roach return ''; 11937eb8894SGreg Roach } 1208e5c5efeSGreg Roach 1218e5c5efeSGreg Roach /** 122b3a775f6SGreg Roach * Is this a tracker, as opposed to just a site-verification. 123b3a775f6SGreg Roach * 124b3a775f6SGreg Roach * @return bool 125b3a775f6SGreg Roach */ 126b3a775f6SGreg Roach public function isTracker(): bool 127b3a775f6SGreg Roach { 128b3a775f6SGreg Roach return true; 129b3a775f6SGreg Roach } 130b3a775f6SGreg Roach 131b3a775f6SGreg Roach /** 1326ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1336ccdf4f0SGreg Roach * 1346ccdf4f0SGreg Roach * @return ResponseInterface 1356ccdf4f0SGreg Roach */ 1366ccdf4f0SGreg Roach public function postAdminAction(ServerRequestInterface $request): ResponseInterface 1378e5c5efeSGreg Roach { 138b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 139b46c87bdSGreg Roach 1406b32f095SGreg Roach foreach (array_keys($this->analyticsParameters()) as $parameter) { 141b46c87bdSGreg Roach $new_value = $params[$parameter]; 142e106ff43SGreg Roach 1436b32f095SGreg Roach $this->setPreference($parameter, $new_value); 1446b32f095SGreg Roach } 1456b32f095SGreg Roach 146685bff15SGreg Roach return redirect(route(ModulesAnalyticsPage::class)); 1478e5c5efeSGreg Roach } 14837eb8894SGreg Roach} 149