xref: /webtrees/app/Module/ContactsFooterModule.php (revision d11be7027e34e3121be11cc025421873364403f9)
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    /**
59     * A sentence describing what this module does.
60     *
61     * @return string
62     */
63    public function description(): string
64    {
65        /* I18N: Description of the “Contact information” module */
66        return I18N::translate('A link to the site contacts.');
67    }
68
69    /**
70     * The default position for this footer.  It can be changed in the control panel.
71     *
72     * @return int
73     */
74    public function defaultFooterOrder(): int
75    {
76        return 2;
77    }
78
79    /**
80     * A footer, to be added at the bottom of every page.
81     *
82     * @param ServerRequestInterface $request
83     *
84     * @return string
85     */
86    public function getFooter(ServerRequestInterface $request): string
87    {
88        $tree = Validator::attributes($request)->treeOptional();
89
90        if ($tree === null) {
91            return '';
92        }
93
94        $contact_user   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
95        $webmaster_user = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID'));
96
97        if ($contact_user instanceof User && $contact_user === $webmaster_user) {
98            return view('modules/contact-links/footer', [
99                'contact_links' => $this->contactLinkEverything($contact_user, $request),
100            ]);
101        }
102
103        if ($contact_user instanceof User && $webmaster_user instanceof User) {
104            return view('modules/contact-links/footer', [
105                'contact_links' => $this->contactLinkGenealogy($contact_user, $request) . '<br>' . $this->contactLinkTechnical($webmaster_user, $request),
106            ]);
107        }
108
109        if ($contact_user instanceof User) {
110            return view('modules/contact-links/footer', [
111                'contact_links' => $this->contactLinkGenealogy($contact_user, $request),
112            ]);
113        }
114
115        if ($webmaster_user instanceof User) {
116            return view('modules/contact-links/footer', [
117                'contact_links' => $this->contactLinkTechnical($webmaster_user, $request),
118            ]);
119        }
120
121        return '';
122    }
123
124    /**
125     * Create contact link for both technical and genealogy support.
126     *
127     * @param User                   $user
128     * @param ServerRequestInterface $request
129     *
130     * @return string
131     */
132    public function contactLinkEverything(User $user, ServerRequestInterface $request): string
133    {
134        return I18N::translate('For technical support or genealogy questions contact %s.', $this->user_service->contactLink($user, $request));
135    }
136
137    /**
138     * Create contact link for genealogy support.
139     *
140     * @param User                   $user
141     * @param ServerRequestInterface $request
142     *
143     * @return string
144     */
145    public function contactLinkGenealogy(User $user, ServerRequestInterface $request): string
146    {
147        return I18N::translate('For help with genealogy questions contact %s.', $this->user_service->contactLink($user, $request));
148    }
149
150    /**
151     * Create contact link for technical support.
152     *
153     * @param User                   $user
154     * @param ServerRequestInterface $request
155     *
156     * @return string
157     */
158    public function contactLinkTechnical(User $user, ServerRequestInterface $request): string
159    {
160        return I18N::translate('For technical support and information contact %s.', $this->user_service->contactLink($user, $request));
161    }
162}
163