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