xref: /webtrees/app/Module/ContactsFooterModule.php (revision e873f434551745f888937263ff89e80db3b0f785)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Services\UserService;
24use Fisharebest\Webtrees\User;
25use Fisharebest\Webtrees\Validator;
26use Psr\Http\Message\ServerRequestInterface;
27
28/**
29 * Class ContactsFooterModule - provide a link to the site owner.
30 */
31class ContactsFooterModule extends AbstractModule implements ModuleFooterInterface
32{
33    use ModuleFooterTrait;
34
35    private UserService $user_service;
36
37    /**
38     * Dependency injection.
39     *
40     * @param UserService $user_service
41     */
42    public function __construct(UserService $user_service)
43    {
44        $this->user_service = $user_service;
45    }
46
47    /**
48     * How should this module be labelled on tabs, footers, etc.?
49     *
50     * @return string
51     */
52    public function title(): string
53    {
54        /* I18N: Name of a module */
55        return I18N::translate('Contact information');
56    }
57
58    public function description(): string
59    {
60        /* I18N: Description of the “Contact information” module */
61        return I18N::translate('A link to the site contacts.');
62    }
63
64    /**
65     * The default position for this footer.  It can be changed in the control panel.
66     *
67     * @return int
68     */
69    public function defaultFooterOrder(): int
70    {
71        return 2;
72    }
73
74    /**
75     * A footer, to be added at the bottom of every page.
76     *
77     * @param ServerRequestInterface $request
78     *
79     * @return string
80     */
81    public function getFooter(ServerRequestInterface $request): string
82    {
83        $tree = Validator::attributes($request)->treeOptional();
84
85        if ($tree === null) {
86            return '';
87        }
88
89        $contact_user   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
90        $webmaster_user = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID'));
91
92        if ($contact_user instanceof User && $contact_user === $webmaster_user) {
93            return view('modules/contact-links/footer', [
94                'contact_links' => $this->contactLinkEverything($contact_user, $request),
95            ]);
96        }
97
98        if ($contact_user instanceof User && $webmaster_user instanceof User) {
99            return view('modules/contact-links/footer', [
100                'contact_links' => $this->contactLinkGenealogy($contact_user, $request) . '<br>' . $this->contactLinkTechnical($webmaster_user, $request),
101            ]);
102        }
103
104        if ($contact_user instanceof User) {
105            return view('modules/contact-links/footer', [
106                'contact_links' => $this->contactLinkGenealogy($contact_user, $request),
107            ]);
108        }
109
110        if ($webmaster_user instanceof User) {
111            return view('modules/contact-links/footer', [
112                'contact_links' => $this->contactLinkTechnical($webmaster_user, $request),
113            ]);
114        }
115
116        return '';
117    }
118
119    /**
120     * Create contact link for both technical and genealogy support.
121     *
122     * @param User                   $user
123     * @param ServerRequestInterface $request
124     *
125     * @return string
126     */
127    public function contactLinkEverything(User $user, ServerRequestInterface $request): string
128    {
129        return I18N::translate('For technical support or genealogy questions contact %s.', $this->user_service->contactLink($user, $request));
130    }
131
132    /**
133     * Create contact link for genealogy support.
134     *
135     * @param User                   $user
136     * @param ServerRequestInterface $request
137     *
138     * @return string
139     */
140    public function contactLinkGenealogy(User $user, ServerRequestInterface $request): string
141    {
142        return I18N::translate('For help with genealogy questions contact %s.', $this->user_service->contactLink($user, $request));
143    }
144
145    /**
146     * Create contact link for technical support.
147     *
148     * @param User                   $user
149     * @param ServerRequestInterface $request
150     *
151     * @return string
152     */
153    public function contactLinkTechnical(User $user, ServerRequestInterface $request): string
154    {
155        return I18N::translate('For technical support and information contact %s.', $this->user_service->contactLink($user, $request));
156    }
157}
158