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; 25*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 266ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 283976b470SGreg Roach 296ccdf4f0SGreg Roachuse function app; 30*b55cbc6bSGreg Roachuse function assert; 3137eb8894SGreg Roach 3237eb8894SGreg Roach/** 3337eb8894SGreg Roach * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface 3437eb8894SGreg Roach */ 3537eb8894SGreg Roachtrait ModuleAnalyticsTrait 3637eb8894SGreg Roach{ 37f4917837SGreg Roach use ViewResponseTrait; 38f4917837SGreg Roach 3937eb8894SGreg Roach /** 4037eb8894SGreg Roach * Should we add this tracker? 4137eb8894SGreg Roach * 4237eb8894SGreg Roach * @return bool 4337eb8894SGreg Roach */ 4437eb8894SGreg Roach public function analyticsCanShow(): bool 4537eb8894SGreg Roach { 466ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 47*b55cbc6bSGreg Roach assert($request instanceof ServerRequestInterface); 48abafa13cSGreg Roach 496ccdf4f0SGreg Roach // If the browser sets the DNT header, then we won't use analytics. 50*b55cbc6bSGreg Roach if (Validator::serverParams($request)->boolean('HTTP_DNT', false)) { 51abafa13cSGreg Roach return false; 52abafa13cSGreg Roach } 53abafa13cSGreg Roach 5437eb8894SGreg Roach foreach ($this->analyticsParameters() as $parameter) { 5537eb8894SGreg Roach if ($parameter === '') { 5637eb8894SGreg Roach return false; 5737eb8894SGreg Roach } 5837eb8894SGreg Roach } 5937eb8894SGreg Roach 6037eb8894SGreg Roach return true; 6137eb8894SGreg Roach } 6237eb8894SGreg Roach 6337eb8894SGreg Roach /** 646ccdf4f0SGreg Roach * The parameters that need to be embedded in the snippet. 656ccdf4f0SGreg Roach * 6624f2a3afSGreg Roach * @return array<string> 676ccdf4f0SGreg Roach */ 686ccdf4f0SGreg Roach public function analyticsParameters(): array 696ccdf4f0SGreg Roach { 706ccdf4f0SGreg Roach return []; 716ccdf4f0SGreg Roach } 726ccdf4f0SGreg Roach 736ccdf4f0SGreg Roach /** 7437eb8894SGreg Roach * A sentence describing what this module does. 7537eb8894SGreg Roach * 7637eb8894SGreg Roach * @return string 7737eb8894SGreg Roach */ 7837eb8894SGreg Roach public function description(): string 7937eb8894SGreg Roach { 8037eb8894SGreg Roach return I18N::translate('Tracking and analytics'); 8137eb8894SGreg Roach } 8237eb8894SGreg Roach 8337eb8894SGreg Roach /** 8457ab2231SGreg Roach * @param ServerRequestInterface $request 8557ab2231SGreg Roach * 866ccdf4f0SGreg Roach * @return ResponseInterface 876ccdf4f0SGreg Roach */ 88a09ea7ccSGreg Roach public function getAdminAction(/** @scrutinizer ignore-unused */ ServerRequestInterface $request): ResponseInterface 896ccdf4f0SGreg Roach { 906ccdf4f0SGreg Roach $this->layout = 'layouts/administration'; 916ccdf4f0SGreg Roach 926ccdf4f0SGreg Roach return $this->viewResponse('admin/analytics-edit', [ 9383615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'Admin']), 946ccdf4f0SGreg Roach 'form_fields' => $this->analyticsFormFields(), 956ccdf4f0SGreg Roach 'preview' => $this->analyticsSnippet($this->analyticsParameters()), 966ccdf4f0SGreg Roach 'title' => $this->title(), 976ccdf4f0SGreg Roach ]); 986ccdf4f0SGreg Roach } 996ccdf4f0SGreg Roach 1006ccdf4f0SGreg Roach /** 10137eb8894SGreg Roach * Form fields to edit the parameters. 10237eb8894SGreg Roach * 10337eb8894SGreg Roach * @return string 10437eb8894SGreg Roach */ 10537eb8894SGreg Roach public function analyticsFormFields(): string 10637eb8894SGreg Roach { 10737eb8894SGreg Roach return ''; 10837eb8894SGreg Roach } 10937eb8894SGreg Roach 11037eb8894SGreg Roach /** 11137eb8894SGreg Roach * Embed placeholders in the snippet. 11237eb8894SGreg Roach * 11309482a55SGreg Roach * @param array<string> $parameters 11437eb8894SGreg Roach * 11537eb8894SGreg Roach * @return string 11637eb8894SGreg Roach */ 117a09ea7ccSGreg Roach public function analyticsSnippet(/** @scrutinizer ignore-unused */ array $parameters): string 11837eb8894SGreg Roach { 11937eb8894SGreg Roach return ''; 12037eb8894SGreg Roach } 1218e5c5efeSGreg Roach 1228e5c5efeSGreg Roach /** 123b3a775f6SGreg Roach * Is this a tracker, as opposed to just a site-verification. 124b3a775f6SGreg Roach * 125b3a775f6SGreg Roach * @return bool 126b3a775f6SGreg Roach */ 127b3a775f6SGreg Roach public function isTracker(): bool 128b3a775f6SGreg Roach { 129b3a775f6SGreg Roach return true; 130b3a775f6SGreg Roach } 131b3a775f6SGreg Roach 132b3a775f6SGreg Roach /** 1336ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1346ccdf4f0SGreg Roach * 1356ccdf4f0SGreg Roach * @return ResponseInterface 1366ccdf4f0SGreg Roach */ 1376ccdf4f0SGreg Roach public function postAdminAction(ServerRequestInterface $request): ResponseInterface 1388e5c5efeSGreg Roach { 139b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 140b46c87bdSGreg Roach 1416b32f095SGreg Roach foreach (array_keys($this->analyticsParameters()) as $parameter) { 142b46c87bdSGreg Roach $new_value = $params[$parameter]; 143e106ff43SGreg Roach 1446b32f095SGreg Roach $this->setPreference($parameter, $new_value); 1456b32f095SGreg Roach } 1466b32f095SGreg Roach 147685bff15SGreg Roach return redirect(route(ModulesAnalyticsPage::class)); 1488e5c5efeSGreg Roach } 14937eb8894SGreg Roach} 150