137eb8894SGreg Roach<?php 23976b470SGreg Roach 337eb8894SGreg Roach/** 437eb8894SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry; 26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 293976b470SGreg 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 /** 38bfed30e4SGreg Roach * A unique internal name for this module (based on the installation folder). 39bfed30e4SGreg Roach * 40bfed30e4SGreg Roach * @return string 41bfed30e4SGreg Roach */ 42bfed30e4SGreg Roach abstract public function name(): string; 43bfed30e4SGreg Roach 44bfed30e4SGreg Roach /** 45bfed30e4SGreg Roach * How should this module be identified in the control panel, etc.? 46bfed30e4SGreg Roach * 47bfed30e4SGreg Roach * @return string 48bfed30e4SGreg Roach */ 49bfed30e4SGreg Roach abstract public function title(): string; 50bfed30e4SGreg Roach 51bfed30e4SGreg Roach /** 52bfed30e4SGreg Roach * Set a module setting. 53bfed30e4SGreg Roach * 54bfed30e4SGreg Roach * Since module settings are NOT NULL, setting a value to NULL will cause 55bfed30e4SGreg Roach * it to be deleted. 56bfed30e4SGreg Roach * 57bfed30e4SGreg Roach * @param string $setting_name 58bfed30e4SGreg Roach * @param string $setting_value 59bfed30e4SGreg Roach * 60bfed30e4SGreg Roach * @return void 61bfed30e4SGreg Roach */ 62bfed30e4SGreg Roach abstract public function setPreference(string $setting_name, string $setting_value): void; 63bfed30e4SGreg Roach 64bfed30e4SGreg Roach /** 6537eb8894SGreg Roach * Should we add this tracker? 6637eb8894SGreg Roach * 6737eb8894SGreg Roach * @return bool 6837eb8894SGreg Roach */ 6937eb8894SGreg Roach public function analyticsCanShow(): bool 7037eb8894SGreg Roach { 71*d35568b4SGreg Roach $request = Registry::container()->get(ServerRequestInterface::class); 72abafa13cSGreg Roach 736ccdf4f0SGreg Roach // If the browser sets the DNT header, then we won't use analytics. 74b55cbc6bSGreg Roach if (Validator::serverParams($request)->boolean('HTTP_DNT', false)) { 75abafa13cSGreg Roach return false; 76abafa13cSGreg Roach } 77abafa13cSGreg Roach 7837eb8894SGreg Roach foreach ($this->analyticsParameters() as $parameter) { 7937eb8894SGreg Roach if ($parameter === '') { 8037eb8894SGreg Roach return false; 8137eb8894SGreg Roach } 8237eb8894SGreg Roach } 8337eb8894SGreg Roach 8437eb8894SGreg Roach return true; 8537eb8894SGreg Roach } 8637eb8894SGreg Roach 8737eb8894SGreg Roach /** 886ccdf4f0SGreg Roach * The parameters that need to be embedded in the snippet. 896ccdf4f0SGreg Roach * 9024f2a3afSGreg Roach * @return array<string> 916ccdf4f0SGreg Roach */ 926ccdf4f0SGreg Roach public function analyticsParameters(): array 936ccdf4f0SGreg Roach { 946ccdf4f0SGreg Roach return []; 956ccdf4f0SGreg Roach } 966ccdf4f0SGreg Roach 9737eb8894SGreg Roach public function description(): string 9837eb8894SGreg Roach { 9937eb8894SGreg Roach return I18N::translate('Tracking and analytics'); 10037eb8894SGreg Roach } 10137eb8894SGreg Roach 10237eb8894SGreg Roach /** 10357ab2231SGreg Roach * @param ServerRequestInterface $request 10457ab2231SGreg Roach * 1056ccdf4f0SGreg Roach * @return ResponseInterface 1066ccdf4f0SGreg Roach */ 10749528f2bSGreg Roach public function getAdminAction(ServerRequestInterface $request): ResponseInterface 1086ccdf4f0SGreg Roach { 1096ccdf4f0SGreg Roach $this->layout = 'layouts/administration'; 1106ccdf4f0SGreg Roach 1116ccdf4f0SGreg Roach return $this->viewResponse('admin/analytics-edit', [ 11283615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'Admin']), 1136ccdf4f0SGreg Roach 'form_fields' => $this->analyticsFormFields(), 1146ccdf4f0SGreg Roach 'preview' => $this->analyticsSnippet($this->analyticsParameters()), 1156ccdf4f0SGreg Roach 'title' => $this->title(), 1166ccdf4f0SGreg Roach ]); 1176ccdf4f0SGreg Roach } 1186ccdf4f0SGreg Roach 1196ccdf4f0SGreg Roach /** 12037eb8894SGreg Roach * Form fields to edit the parameters. 12137eb8894SGreg Roach * 12237eb8894SGreg Roach * @return string 12337eb8894SGreg Roach */ 12437eb8894SGreg Roach public function analyticsFormFields(): string 12537eb8894SGreg Roach { 12637eb8894SGreg Roach return ''; 12737eb8894SGreg Roach } 12837eb8894SGreg Roach 12937eb8894SGreg Roach /** 13037eb8894SGreg Roach * Embed placeholders in the snippet. 13137eb8894SGreg Roach * 13209482a55SGreg Roach * @param array<string> $parameters 13337eb8894SGreg Roach * 13437eb8894SGreg Roach * @return string 13537eb8894SGreg Roach */ 13649528f2bSGreg Roach public function analyticsSnippet(array $parameters): string 13737eb8894SGreg Roach { 13837eb8894SGreg Roach return ''; 13937eb8894SGreg Roach } 1408e5c5efeSGreg Roach 1418e5c5efeSGreg Roach /** 142b3a775f6SGreg Roach * Is this a tracker, as opposed to just a site-verification. 143b3a775f6SGreg Roach * 144b3a775f6SGreg Roach * @return bool 145b3a775f6SGreg Roach */ 146b3a775f6SGreg Roach public function isTracker(): bool 147b3a775f6SGreg Roach { 148b3a775f6SGreg Roach return true; 149b3a775f6SGreg Roach } 150b3a775f6SGreg Roach 151b3a775f6SGreg Roach /** 1526ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1536ccdf4f0SGreg Roach * 1546ccdf4f0SGreg Roach * @return ResponseInterface 1556ccdf4f0SGreg Roach */ 1566ccdf4f0SGreg Roach public function postAdminAction(ServerRequestInterface $request): ResponseInterface 1578e5c5efeSGreg Roach { 1586b32f095SGreg Roach foreach (array_keys($this->analyticsParameters()) as $parameter) { 159748dbe15SGreg Roach $new_value = Validator::parsedBody($request)->string($parameter); 160e106ff43SGreg Roach 1616b32f095SGreg Roach $this->setPreference($parameter, $new_value); 1626b32f095SGreg Roach } 1636b32f095SGreg Roach 164685bff15SGreg Roach return redirect(route(ModulesAnalyticsPage::class)); 1658e5c5efeSGreg Roach } 16637eb8894SGreg Roach} 167