. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\I18N; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface */ trait ModuleAnalyticsTrait { /** * @param $view_name * @param $view_data * @param $status * * @return Response */ abstract function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response; /** * Should we add this tracker? * * @return bool */ public function analyticsCanShow(): bool { foreach ($this->analyticsParameters() as $parameter) { if ($parameter === '') { return false; } } return true; } /** * A sentence describing what this module does. * * @return string */ public function description(): string { return I18N::translate('Tracking and analytics'); } /** * Form fields to edit the parameters. * * @return string */ public function analyticsFormFields(): string { return ''; } /** * The parameters that need to be embedded in the snippet. * * @return string[] */ public function analyticsParameters(): array { return []; } /** * Embed placeholders in the snippet. * * @param string[] $parameters * * @return string */ public function analyticsSnippet(array $parameters): string { return ''; } /** * @return Response */ public function getAdminAction(): Response { $this->layout = 'layouts/administration'; return $this->viewResponse('admin/analytics-edit', [ 'form_fields' => $this->analyticsFormFields(), 'preview' => $this->analyticsSnippet($this->analyticsParameters()), 'title' => $this->title(), ]); } /** * @param Request $request * * @return RedirectResponse */ public function postAdminAction(Request $request): RedirectResponse { return new RedirectResponse(route('analytics')); } }