xref: /webtrees/app/Module/UserMessagesModule.php (revision b0f03dfe90252b729e1ddaafe0b37dc485b3c221)
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\Auth;
23use Fisharebest\Webtrees\Contracts\UserInterface;
24use Fisharebest\Webtrees\DB;
25use Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
26use Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Registry;
29use Fisharebest\Webtrees\Services\MessageService;
30use Fisharebest\Webtrees\Services\UserService;
31use Fisharebest\Webtrees\Tree;
32use Fisharebest\Webtrees\Validator;
33use Illuminate\Support\Str;
34use Psr\Http\Message\ResponseInterface;
35use Psr\Http\Message\ServerRequestInterface;
36
37use function route;
38use function view;
39
40/**
41 * Class UserMessagesModule
42 */
43class UserMessagesModule extends AbstractModule implements ModuleBlockInterface
44{
45    use ModuleBlockTrait;
46
47    private UserService $user_service;
48
49    /**
50     * @param UserService $user_service
51     */
52    public function __construct(UserService $user_service)
53    {
54        $this->user_service = $user_service;
55    }
56
57    /**
58     * How should this module be identified in the control panel, etc.?
59     *
60     * @return string
61     */
62    public function title(): string
63    {
64        /* I18N: Name of a module */
65        return I18N::translate('Messages');
66    }
67
68    public function description(): string
69    {
70        /* I18N: Description of the “Messages” module */
71        return I18N::translate('Communicate directly with other users, using private messages.');
72    }
73
74    /**
75     * Delete one or messages belonging to a user.
76     *
77     * @param ServerRequestInterface $request
78     *
79     * @return ResponseInterface
80     */
81    public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface
82    {
83        $tree        = Validator::attributes($request)->tree();
84        $context     = Validator::queryParams($request)->string('context');
85        $message_ids = Validator::parsedBody($request)->array('message_id');
86
87        DB::table('message')
88            ->where('user_id', '=', Auth::id())
89            ->whereIn('message_id', $message_ids)
90            ->delete();
91
92        if ($context === ModuleBlockInterface::CONTEXT_USER_PAGE) {
93            $url = route(UserPage::class, ['tree' => $tree->name()]);
94        } else {
95            $url = route(TreePage::class, ['tree' => $tree->name()]);
96        }
97
98        return redirect($url);
99    }
100
101    /**
102     * Generate the HTML content of this block.
103     *
104     * @param Tree                 $tree
105     * @param int                  $block_id
106     * @param string               $context
107     * @param array<string,string> $config
108     *
109     * @return string
110     */
111    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
112    {
113        $messages = DB::table('message')
114            ->where('user_id', '=', Auth::id())
115            ->orderByDesc('message_id')
116            ->get()
117            ->map(static function (object $row): object {
118                $row->created = Registry::timestampFactory()->fromString($row->created);
119
120                return $row;
121            });
122
123        $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
124            $public_tree  = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1';
125            $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER;
126
127            return
128                $user->id() !== Auth::id() &&
129                $user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) &&
130                $can_see_tree &&
131                $user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== MessageService::CONTACT_METHOD_NONE;
132        });
133
134        $content = view('modules/user-messages/user-messages', [
135            'block_id'     => $block_id,
136            'context'      => $context,
137            'messages'     => $messages,
138            'module'       => $this,
139            'tree'         => $tree,
140            'user_service' => $this->user_service,
141            'users'        => $users,
142        ]);
143
144        if ($context !== self::CONTEXT_EMBED) {
145            $count = $messages->count();
146
147            return view('modules/block-template', [
148                'block'      => Str::kebab($this->name()),
149                'id'         => $block_id,
150                'config_url' => '',
151                'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
152                'content'    => $content,
153            ]);
154        }
155
156        return $content;
157    }
158
159    /**
160     * Should this block load asynchronously using AJAX?
161     *
162     * Simple blocks are faster in-line, more complex ones can be loaded later.
163     *
164     * @return bool
165     */
166    public function loadAjax(): bool
167    {
168        return false;
169    }
170
171    /**
172     * Can this block be shown on the user’s home page?
173     *
174     * @return bool
175     */
176    public function isUserBlock(): bool
177    {
178        return true;
179    }
180
181    /**
182     * Can this block be shown on the tree’s home page?
183     *
184     * @return bool
185     */
186    public function isTreeBlock(): bool
187    {
188        return false;
189    }
190}
191