137eb8894SGreg Roach<?php 23976b470SGreg Roach 337eb8894SGreg Roach/** 437eb8894SGreg Roach * webtrees: online genealogy 537eb8894SGreg Roach * Copyright (C) 2019 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 1537eb8894SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1637eb8894SGreg Roach */ 17*fcfa147eSGreg Roach 1837eb8894SGreg Roachdeclare(strict_types=1); 1937eb8894SGreg Roach 2037eb8894SGreg Roachnamespace Fisharebest\Webtrees\Module; 2137eb8894SGreg Roach 226ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 2337eb8894SGreg Roachuse Fisharebest\Webtrees\I18N; 246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 263976b470SGreg Roach 276ccdf4f0SGreg Roachuse function app; 2837eb8894SGreg Roach 2937eb8894SGreg Roach/** 3037eb8894SGreg Roach * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface 3137eb8894SGreg Roach */ 3237eb8894SGreg Roachtrait ModuleAnalyticsTrait 3337eb8894SGreg Roach{ 3437eb8894SGreg Roach /** 35962ead51SGreg Roach * @return string 36962ead51SGreg Roach */ 37962ead51SGreg Roach abstract public function name(): string; 38962ead51SGreg Roach 39962ead51SGreg 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); 47abafa13cSGreg Roach 486ccdf4f0SGreg Roach // If the browser sets the DNT header, then we won't use analytics. 496ccdf4f0SGreg Roach $dnt = $request->getServerParams()['HTTP_DNT'] ?? ''; 506ccdf4f0SGreg Roach 516ccdf4f0SGreg Roach if ($dnt === '1') { 52abafa13cSGreg Roach return false; 53abafa13cSGreg Roach } 54abafa13cSGreg Roach 5537eb8894SGreg Roach foreach ($this->analyticsParameters() as $parameter) { 5637eb8894SGreg Roach if ($parameter === '') { 5737eb8894SGreg Roach return false; 5837eb8894SGreg Roach } 5937eb8894SGreg Roach } 6037eb8894SGreg Roach 6137eb8894SGreg Roach return true; 6237eb8894SGreg Roach } 6337eb8894SGreg Roach 6437eb8894SGreg Roach /** 656ccdf4f0SGreg Roach * The parameters that need to be embedded in the snippet. 666ccdf4f0SGreg Roach * 676ccdf4f0SGreg Roach * @return string[] 686ccdf4f0SGreg Roach */ 696ccdf4f0SGreg Roach public function analyticsParameters(): array 706ccdf4f0SGreg Roach { 716ccdf4f0SGreg Roach return []; 726ccdf4f0SGreg Roach } 736ccdf4f0SGreg Roach 746ccdf4f0SGreg Roach /** 7537eb8894SGreg Roach * A sentence describing what this module does. 7637eb8894SGreg Roach * 7737eb8894SGreg Roach * @return string 7837eb8894SGreg Roach */ 7937eb8894SGreg Roach public function description(): string 8037eb8894SGreg Roach { 8137eb8894SGreg Roach return I18N::translate('Tracking and analytics'); 8237eb8894SGreg Roach } 8337eb8894SGreg Roach 8437eb8894SGreg Roach /** 8557ab2231SGreg Roach * @param ServerRequestInterface $request 8657ab2231SGreg Roach * 876ccdf4f0SGreg Roach * @return ResponseInterface 886ccdf4f0SGreg Roach */ 8957ab2231SGreg Roach public function getAdminAction(ServerRequestInterface $request): ResponseInterface 906ccdf4f0SGreg Roach { 916ccdf4f0SGreg Roach $this->layout = 'layouts/administration'; 926ccdf4f0SGreg Roach 936ccdf4f0SGreg Roach return $this->viewResponse('admin/analytics-edit', [ 9483615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'Admin']), 956ccdf4f0SGreg Roach 'form_fields' => $this->analyticsFormFields(), 966ccdf4f0SGreg Roach 'preview' => $this->analyticsSnippet($this->analyticsParameters()), 976ccdf4f0SGreg Roach 'title' => $this->title(), 986ccdf4f0SGreg Roach ]); 996ccdf4f0SGreg Roach } 1006ccdf4f0SGreg Roach 1016ccdf4f0SGreg Roach /** 102606471d5SGreg Roach * @param string $view_name 103606471d5SGreg Roach * @param mixed[] $view_data 1046ccdf4f0SGreg Roach * @param int $status 1056ccdf4f0SGreg Roach * 1066ccdf4f0SGreg Roach * @return ResponseInterface 1076ccdf4f0SGreg Roach */ 108606471d5SGreg Roach abstract protected function viewResponse(string $view_name, array $view_data, $status = StatusCodeInterface::STATUS_OK): ResponseInterface; 1096ccdf4f0SGreg Roach 1106ccdf4f0SGreg Roach /** 11137eb8894SGreg Roach * Form fields to edit the parameters. 11237eb8894SGreg Roach * 11337eb8894SGreg Roach * @return string 11437eb8894SGreg Roach */ 11537eb8894SGreg Roach public function analyticsFormFields(): string 11637eb8894SGreg Roach { 11737eb8894SGreg Roach return ''; 11837eb8894SGreg Roach } 11937eb8894SGreg Roach 12037eb8894SGreg Roach /** 12137eb8894SGreg Roach * Embed placeholders in the snippet. 12237eb8894SGreg Roach * 12337eb8894SGreg Roach * @param string[] $parameters 12437eb8894SGreg Roach * 12537eb8894SGreg Roach * @return string 12637eb8894SGreg Roach */ 12737eb8894SGreg Roach public function analyticsSnippet(array $parameters): string 12837eb8894SGreg Roach { 12937eb8894SGreg Roach return ''; 13037eb8894SGreg Roach } 1318e5c5efeSGreg Roach 1328e5c5efeSGreg Roach /** 1336ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 1348e5c5efeSGreg Roach * 1356ccdf4f0SGreg Roach * @return string 1368e5c5efeSGreg Roach */ 1376ccdf4f0SGreg Roach abstract public function title(): string; 1386ccdf4f0SGreg Roach 1396ccdf4f0SGreg Roach /** 1406ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1416ccdf4f0SGreg Roach * 1426ccdf4f0SGreg Roach * @return ResponseInterface 1436ccdf4f0SGreg Roach */ 1446ccdf4f0SGreg Roach public function postAdminAction(ServerRequestInterface $request): ResponseInterface 1458e5c5efeSGreg Roach { 1466b32f095SGreg Roach foreach (array_keys($this->analyticsParameters()) as $parameter) { 147e106ff43SGreg Roach $new_value = $request->getParsedBody()[$parameter]; 148e106ff43SGreg Roach 1496b32f095SGreg Roach $this->setPreference($parameter, $new_value); 1506b32f095SGreg Roach } 1516b32f095SGreg Roach 1526ccdf4f0SGreg Roach return redirect(route('analytics')); 1538e5c5efeSGreg Roach } 1546ccdf4f0SGreg Roach 1556ccdf4f0SGreg Roach /** 1566ccdf4f0SGreg Roach * Set a module setting. 1576ccdf4f0SGreg Roach * Since module settings are NOT NULL, setting a value to NULL will cause 1586ccdf4f0SGreg Roach * it to be deleted. 1596ccdf4f0SGreg Roach * 1606ccdf4f0SGreg Roach * @param string $setting_name 1616ccdf4f0SGreg Roach * @param string $setting_value 1626ccdf4f0SGreg Roach * 1636ccdf4f0SGreg Roach * @return void 1646ccdf4f0SGreg Roach */ 1656ccdf4f0SGreg Roach abstract public function setPreference(string $setting_name, string $setting_value): void; 16637eb8894SGreg Roach} 167