xref: /webtrees/app/Module/ContactsFooterModule.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 Psr\Http\Message\ServerRequestInterface;
26
27/**
28 * Class ContactsFooterModule - provide a link to the site owner.
29 */
30class ContactsFooterModule extends AbstractModule implements ModuleFooterInterface
31{
32    use ModuleFooterTrait;
33
34    private UserService $user_service;
35
36    /**
37     * Dependency injection.
38     *
39     * @param UserService $user_service
40     */
41    public function __construct(UserService $user_service)
42    {
43        $this->user_service = $user_service;
44    }
45
46    /**
47     * How should this module be labelled on tabs, footers, etc.?
48     *
49     * @return string
50     */
51    public function title(): string
52    {
53        /* I18N: Name of a module */
54        return I18N::translate('Contact information');
55    }
56
57    /**
58     * A sentence describing what this module does.
59     *
60     * @return string
61     */
62    public function description(): string
63    {
64        /* I18N: Description of the “Contact information” module */
65        return I18N::translate('A link to the site contacts.');
66    }
67
68    /**
69     * The default position for this footer.  It can be changed in the control panel.
70     *
71     * @return int
72     */
73    public function defaultFooterOrder(): int
74    {
75        return 2;
76    }
77
78    /**
79     * A footer, to be added at the bottom of every page.
80     *
81     * @param ServerRequestInterface $request
82     *
83     * @return string
84     */
85    public function getFooter(ServerRequestInterface $request): string
86    {
87        $tree = $request->getAttribute('tree');
88
89        if ($tree === null) {
90            return '';
91        }
92
93        $contact_user   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
94        $webmaster_user = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID'));
95
96        if ($contact_user instanceof User && $contact_user === $webmaster_user) {
97            return view('modules/contact-links/footer', [
98                'contact_links' => $this->contactLinkEverything($contact_user, $request),
99            ]);
100        }
101
102        if ($contact_user instanceof User && $webmaster_user instanceof User) {
103            return view('modules/contact-links/footer', [
104                'contact_links' => $this->contactLinkGenealogy($contact_user, $request) . '<br>' . $this->contactLinkTechnical($webmaster_user, $request),
105            ]);
106        }
107
108        if ($contact_user instanceof User) {
109            return view('modules/contact-links/footer', [
110                'contact_links' => $this->contactLinkGenealogy($contact_user, $request),
111            ]);
112        }
113
114        if ($webmaster_user instanceof User) {
115            return view('modules/contact-links/footer', [
116                'contact_links' => $this->contactLinkTechnical($webmaster_user, $request),
117            ]);
118        }
119
120        return '';
121    }
122
123    /**
124     * Create contact link for both technical and genealogy support.
125     *
126     * @param User                   $user
127     * @param ServerRequestInterface $request
128     *
129     * @return string
130     */
131    public function contactLinkEverything(User $user, ServerRequestInterface $request): string
132    {
133        return I18N::translate('For technical support or genealogy questions contact %s.', $this->user_service->contactLink($user, $request));
134    }
135
136    /**
137     * Create contact link for genealogy support.
138     *
139     * @param User                   $user
140     * @param ServerRequestInterface $request
141     *
142     * @return string
143     */
144    public function contactLinkGenealogy(User $user, ServerRequestInterface $request): string
145    {
146        return I18N::translate('For help with genealogy questions contact %s.', $this->user_service->contactLink($user, $request));
147    }
148
149    /**
150     * Create contact link for technical support.
151     *
152     * @param User                   $user
153     * @param ServerRequestInterface $request
154     *
155     * @return string
156     */
157    public function contactLinkTechnical(User $user, ServerRequestInterface $request): string
158    {
159        return I18N::translate('For technical support and information contact %s.', $this->user_service->contactLink($user, $request));
160    }
161}
162