1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fig\Http\Message\StatusCodeInterface; 21use Fisharebest\Webtrees\I18N; 22use Psr\Http\Message\ResponseInterface; 23use Psr\Http\Message\ServerRequestInterface; 24use function app; 25 26/** 27 * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface 28 */ 29trait ModuleAnalyticsTrait 30{ 31 /** 32 * Should we add this tracker? 33 * 34 * @return bool 35 */ 36 public function analyticsCanShow(): bool 37 { 38 $request = app(ServerRequestInterface::class); 39 40 // If the browser sets the DNT header, then we won't use analytics. 41 $dnt = $request->getServerParams()['HTTP_DNT'] ?? ''; 42 43 if ($dnt === '1') { 44 return false; 45 } 46 47 foreach ($this->analyticsParameters() as $parameter) { 48 if ($parameter === '') { 49 return false; 50 } 51 } 52 53 return true; 54 } 55 56 /** 57 * The parameters that need to be embedded in the snippet. 58 * 59 * @return string[] 60 */ 61 public function analyticsParameters(): array 62 { 63 return []; 64 } 65 66 /** 67 * A sentence describing what this module does. 68 * 69 * @return string 70 */ 71 public function description(): string 72 { 73 return I18N::translate('Tracking and analytics'); 74 } 75 76 /** 77 * @return ResponseInterface 78 */ 79 public function getAdminAction(): ResponseInterface 80 { 81 $this->layout = 'layouts/administration'; 82 83 return $this->viewResponse('admin/analytics-edit', [ 84 'form_fields' => $this->analyticsFormFields(), 85 'preview' => $this->analyticsSnippet($this->analyticsParameters()), 86 'title' => $this->title(), 87 ]); 88 } 89 90 /** 91 * @param string $view_name 92 * @param mixed[] $view_data 93 * @param int $status 94 * 95 * @return ResponseInterface 96 */ 97 abstract protected function viewResponse(string $view_name, array $view_data, $status = StatusCodeInterface::STATUS_OK): ResponseInterface; 98 99 /** 100 * Form fields to edit the parameters. 101 * 102 * @return string 103 */ 104 public function analyticsFormFields(): string 105 { 106 return ''; 107 } 108 109 /** 110 * Embed placeholders in the snippet. 111 * 112 * @param string[] $parameters 113 * 114 * @return string 115 */ 116 public function analyticsSnippet(array $parameters): string 117 { 118 return ''; 119 } 120 121 /** 122 * How should this module be identified in the control panel, etc.? 123 * 124 * @return string 125 */ 126 abstract public function title(): string; 127 128 /** 129 * @param ServerRequestInterface $request 130 * 131 * @return ResponseInterface 132 */ 133 public function postAdminAction(ServerRequestInterface $request): ResponseInterface 134 { 135 foreach (array_keys($this->analyticsParameters()) as $parameter) { 136 $new_value = $request->getParsedBody()[$parameter]; 137 138 $this->setPreference($parameter, $new_value); 139 } 140 141 return redirect(route('analytics')); 142 } 143 144 /** 145 * Set a module setting. 146 * Since module settings are NOT NULL, setting a value to NULL will cause 147 * it to be deleted. 148 * 149 * @param string $setting_name 150 * @param string $setting_value 151 * 152 * @return void 153 */ 154 abstract public function setPreference(string $setting_name, string $setting_value): void; 155} 156