xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision 606471d5101bcf787c03697c5d514ea070b86571)
137eb8894SGreg Roach<?php
237eb8894SGreg Roach/**
337eb8894SGreg Roach * webtrees: online genealogy
437eb8894SGreg Roach * Copyright (C) 2019 webtrees development team
537eb8894SGreg Roach * This program is free software: you can redistribute it and/or modify
637eb8894SGreg Roach * it under the terms of the GNU General Public License as published by
737eb8894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
837eb8894SGreg Roach * (at your option) any later version.
937eb8894SGreg Roach * This program is distributed in the hope that it will be useful,
1037eb8894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1137eb8894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1237eb8894SGreg Roach * GNU General Public License for more details.
1337eb8894SGreg Roach * You should have received a copy of the GNU General Public License
1437eb8894SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1537eb8894SGreg Roach */
1637eb8894SGreg Roachdeclare(strict_types=1);
1737eb8894SGreg Roach
1837eb8894SGreg Roachnamespace Fisharebest\Webtrees\Module;
1937eb8894SGreg Roach
206ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2137eb8894SGreg Roachuse Fisharebest\Webtrees\I18N;
226ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
236ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
246ccdf4f0SGreg Roachuse function app;
2537eb8894SGreg Roach
2637eb8894SGreg Roach/**
2737eb8894SGreg Roach * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface
2837eb8894SGreg Roach */
2937eb8894SGreg Roachtrait ModuleAnalyticsTrait
3037eb8894SGreg Roach{
3137eb8894SGreg Roach    /**
3237eb8894SGreg Roach     * Should we add this tracker?
3337eb8894SGreg Roach     *
3437eb8894SGreg Roach     * @return bool
3537eb8894SGreg Roach     */
3637eb8894SGreg Roach    public function analyticsCanShow(): bool
3737eb8894SGreg Roach    {
386ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
39abafa13cSGreg Roach
406ccdf4f0SGreg Roach        // If the browser sets the DNT header, then we won't use analytics.
416ccdf4f0SGreg Roach        $dnt = $request->getServerParams()['HTTP_DNT'] ?? '';
426ccdf4f0SGreg Roach
436ccdf4f0SGreg Roach        if ($dnt === '1') {
44abafa13cSGreg Roach            return false;
45abafa13cSGreg Roach        }
46abafa13cSGreg Roach
4737eb8894SGreg Roach        foreach ($this->analyticsParameters() as $parameter) {
4837eb8894SGreg Roach            if ($parameter === '') {
4937eb8894SGreg Roach                return false;
5037eb8894SGreg Roach            }
5137eb8894SGreg Roach        }
5237eb8894SGreg Roach
5337eb8894SGreg Roach        return true;
5437eb8894SGreg Roach    }
5537eb8894SGreg Roach
5637eb8894SGreg Roach    /**
576ccdf4f0SGreg Roach     * The parameters that need to be embedded in the snippet.
586ccdf4f0SGreg Roach     *
596ccdf4f0SGreg Roach     * @return string[]
606ccdf4f0SGreg Roach     */
616ccdf4f0SGreg Roach    public function analyticsParameters(): array
626ccdf4f0SGreg Roach    {
636ccdf4f0SGreg Roach        return [];
646ccdf4f0SGreg Roach    }
656ccdf4f0SGreg Roach
666ccdf4f0SGreg Roach    /**
6737eb8894SGreg Roach     * A sentence describing what this module does.
6837eb8894SGreg Roach     *
6937eb8894SGreg Roach     * @return string
7037eb8894SGreg Roach     */
7137eb8894SGreg Roach    public function description(): string
7237eb8894SGreg Roach    {
7337eb8894SGreg Roach        return I18N::translate('Tracking and analytics');
7437eb8894SGreg Roach    }
7537eb8894SGreg Roach
7637eb8894SGreg Roach    /**
776ccdf4f0SGreg Roach     * @return ResponseInterface
786ccdf4f0SGreg Roach     */
796ccdf4f0SGreg Roach    public function getAdminAction(): ResponseInterface
806ccdf4f0SGreg Roach    {
816ccdf4f0SGreg Roach        $this->layout = 'layouts/administration';
826ccdf4f0SGreg Roach
836ccdf4f0SGreg Roach        return $this->viewResponse('admin/analytics-edit', [
846ccdf4f0SGreg Roach            'form_fields' => $this->analyticsFormFields(),
856ccdf4f0SGreg Roach            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
866ccdf4f0SGreg Roach            'title'       => $this->title(),
876ccdf4f0SGreg Roach        ]);
886ccdf4f0SGreg Roach    }
896ccdf4f0SGreg Roach
906ccdf4f0SGreg Roach    /**
91*606471d5SGreg Roach     * @param string  $view_name
92*606471d5SGreg Roach     * @param mixed[] $view_data
936ccdf4f0SGreg Roach     * @param int     $status
946ccdf4f0SGreg Roach     *
956ccdf4f0SGreg Roach     * @return ResponseInterface
966ccdf4f0SGreg Roach     */
97*606471d5SGreg Roach    abstract protected function viewResponse(string $view_name, array $view_data, $status = StatusCodeInterface::STATUS_OK): ResponseInterface;
986ccdf4f0SGreg Roach
996ccdf4f0SGreg Roach    /**
10037eb8894SGreg Roach     * Form fields to edit the parameters.
10137eb8894SGreg Roach     *
10237eb8894SGreg Roach     * @return string
10337eb8894SGreg Roach     */
10437eb8894SGreg Roach    public function analyticsFormFields(): string
10537eb8894SGreg Roach    {
10637eb8894SGreg Roach        return '';
10737eb8894SGreg Roach    }
10837eb8894SGreg Roach
10937eb8894SGreg Roach    /**
11037eb8894SGreg Roach     * Embed placeholders in the snippet.
11137eb8894SGreg Roach     *
11237eb8894SGreg Roach     * @param string[] $parameters
11337eb8894SGreg Roach     *
11437eb8894SGreg Roach     * @return string
11537eb8894SGreg Roach     */
11637eb8894SGreg Roach    public function analyticsSnippet(array $parameters): string
11737eb8894SGreg Roach    {
11837eb8894SGreg Roach        return '';
11937eb8894SGreg Roach    }
1208e5c5efeSGreg Roach
1218e5c5efeSGreg Roach    /**
1226ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1238e5c5efeSGreg Roach     *
1246ccdf4f0SGreg Roach     * @return string
1258e5c5efeSGreg Roach     */
1266ccdf4f0SGreg Roach    abstract public function title(): string;
1276ccdf4f0SGreg Roach
1286ccdf4f0SGreg Roach    /**
1296ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1306ccdf4f0SGreg Roach     *
1316ccdf4f0SGreg Roach     * @return ResponseInterface
1326ccdf4f0SGreg Roach     */
1336ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
1348e5c5efeSGreg Roach    {
1356b32f095SGreg Roach        foreach (array_keys($this->analyticsParameters()) as $parameter) {
1366b32f095SGreg Roach            $new_value = $request->get($parameter, '');
1376b32f095SGreg Roach            $this->setPreference($parameter, $new_value);
1386b32f095SGreg Roach        }
1396b32f095SGreg Roach
1406ccdf4f0SGreg Roach        return redirect(route('analytics'));
1418e5c5efeSGreg Roach    }
1426ccdf4f0SGreg Roach
1436ccdf4f0SGreg Roach    /**
1446ccdf4f0SGreg Roach     * Set a module setting.
1456ccdf4f0SGreg Roach     * Since module settings are NOT NULL, setting a value to NULL will cause
1466ccdf4f0SGreg Roach     * it to be deleted.
1476ccdf4f0SGreg Roach     *
1486ccdf4f0SGreg Roach     * @param string $setting_name
1496ccdf4f0SGreg Roach     * @param string $setting_value
1506ccdf4f0SGreg Roach     *
1516ccdf4f0SGreg Roach     * @return void
1526ccdf4f0SGreg Roach     */
1536ccdf4f0SGreg Roach    abstract public function setPreference(string $setting_name, string $setting_value): void;
15437eb8894SGreg Roach}
155