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