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 Fisharebest\Webtrees\I18N; 21use Symfony\Component\HttpFoundation\RedirectResponse; 22use Symfony\Component\HttpFoundation\Request; 23use Symfony\Component\HttpFoundation\Response; 24 25/** 26 * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface 27 */ 28trait ModuleAnalyticsTrait 29{ 30 /** 31 * @param $view_name 32 * @param $view_data 33 * @param $status 34 * 35 * @return Response 36 */ 37 abstract protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response; 38 39 /** 40 * How should this module be labelled on tabs, menus, etc.? 41 * 42 * @return string 43 */ 44 abstract public function title(): string; 45 46 /** 47 * Should we add this tracker? 48 * 49 * @return bool 50 */ 51 public function analyticsCanShow(): bool 52 { 53 foreach ($this->analyticsParameters() as $parameter) { 54 if ($parameter === '') { 55 return false; 56 } 57 } 58 59 return true; 60 } 61 62 /** 63 * A sentence describing what this module does. 64 * 65 * @return string 66 */ 67 public function description(): string 68 { 69 return I18N::translate('Tracking and analytics'); 70 } 71 72 /** 73 * Form fields to edit the parameters. 74 * 75 * @return string 76 */ 77 public function analyticsFormFields(): string 78 { 79 return ''; 80 } 81 82 /** 83 * The parameters that need to be embedded in the snippet. 84 * 85 * @return string[] 86 */ 87 public function analyticsParameters(): array 88 { 89 return []; 90 } 91 92 /** 93 * Embed placeholders in the snippet. 94 * 95 * @param string[] $parameters 96 * 97 * @return string 98 */ 99 public function analyticsSnippet(array $parameters): string 100 { 101 return ''; 102 } 103 104 105 /** 106 * @return Response 107 */ 108 public function getAdminAction(): Response 109 { 110 $this->layout = 'layouts/administration'; 111 112 return $this->viewResponse('admin/analytics-edit', [ 113 'form_fields' => $this->analyticsFormFields(), 114 'preview' => $this->analyticsSnippet($this->analyticsParameters()), 115 'title' => $this->title(), 116 ]); 117 } 118 119 /** 120 * @param Request $request 121 * 122 * @return RedirectResponse 123 */ 124 public function postAdminAction(Request $request): RedirectResponse 125 { 126 foreach (array_keys($this->analyticsParameters()) as $parameter) { 127 $new_value = $request->get($parameter, ''); 128 $this->setPreference($parameter, $new_value); 129 } 130 131 return new RedirectResponse(route('analytics')); 132 } 133} 134