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