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 * @param ServerRequestInterface $request 78 * 79 * @return ResponseInterface 80 */ 81 public function getAdminAction(ServerRequestInterface $request): ResponseInterface 82 { 83 $this->layout = 'layouts/administration'; 84 85 return $this->viewResponse('admin/analytics-edit', [ 86 'form_fields' => $this->analyticsFormFields(), 87 'preview' => $this->analyticsSnippet($this->analyticsParameters()), 88 'title' => $this->title(), 89 ]); 90 } 91 92 /** 93 * @param string $view_name 94 * @param mixed[] $view_data 95 * @param int $status 96 * 97 * @return ResponseInterface 98 */ 99 abstract protected function viewResponse(string $view_name, array $view_data, $status = StatusCodeInterface::STATUS_OK): ResponseInterface; 100 101 /** 102 * Form fields to edit the parameters. 103 * 104 * @return string 105 */ 106 public function analyticsFormFields(): string 107 { 108 return ''; 109 } 110 111 /** 112 * Embed placeholders in the snippet. 113 * 114 * @param string[] $parameters 115 * 116 * @return string 117 */ 118 public function analyticsSnippet(array $parameters): string 119 { 120 return ''; 121 } 122 123 /** 124 * How should this module be identified in the control panel, etc.? 125 * 126 * @return string 127 */ 128 abstract public function title(): string; 129 130 /** 131 * @param ServerRequestInterface $request 132 * 133 * @return ResponseInterface 134 */ 135 public function postAdminAction(ServerRequestInterface $request): ResponseInterface 136 { 137 foreach (array_keys($this->analyticsParameters()) as $parameter) { 138 $new_value = $request->getParsedBody()[$parameter]; 139 140 $this->setPreference($parameter, $new_value); 141 } 142 143 return redirect(route('analytics')); 144 } 145 146 /** 147 * Set a module setting. 148 * Since module settings are NOT NULL, setting a value to NULL will cause 149 * it to be deleted. 150 * 151 * @param string $setting_name 152 * @param string $setting_value 153 * 154 * @return void 155 */ 156 abstract public function setPreference(string $setting_name, string $setting_value): void; 157} 158