xref: /webtrees/app/Module/UserMessagesModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
268e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
28d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
298cfb5e7bSGreg Roachuse Fisharebest\Webtrees\Services\MessageService;
30e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
31cf700360SGreg Roachuse Fisharebest\Webtrees\Tree;
32b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
331e7a7a28SGreg Roachuse Illuminate\Support\Str;
346ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3671378461SGreg Roach
3783615acfSGreg Roachuse function route;
38a427f55aSGreg Roachuse function view;
398c2e8227SGreg Roach
408c2e8227SGreg Roach/**
418c2e8227SGreg Roach * Class UserMessagesModule
428c2e8227SGreg Roach */
4337eb8894SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface
44c1010edaSGreg Roach{
4549a243cbSGreg Roach    use ModuleBlockTrait;
4649a243cbSGreg Roach
4743f2f523SGreg Roach    private UserService $user_service;
48e5a6b4d4SGreg Roach
49e5a6b4d4SGreg Roach    /**
50e5a6b4d4SGreg Roach     * @param UserService $user_service
51e5a6b4d4SGreg Roach     */
52e2cbf57aSGreg Roach    public function __construct(UserService $user_service)
53e2cbf57aSGreg Roach    {
54e5a6b4d4SGreg Roach        $this->user_service = $user_service;
55e5a6b4d4SGreg Roach    }
56e5a6b4d4SGreg Roach
57e5a6b4d4SGreg Roach    /**
580cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
59961ec755SGreg Roach     *
60961ec755SGreg Roach     * @return string
61961ec755SGreg Roach     */
6249a243cbSGreg Roach    public function title(): string
63c1010edaSGreg Roach    {
64bbb76c12SGreg Roach        /* I18N: Name of a module */
65bbb76c12SGreg Roach        return I18N::translate('Messages');
668c2e8227SGreg Roach    }
678c2e8227SGreg Roach
6849a243cbSGreg Roach    public function description(): string
69c1010edaSGreg Roach    {
70bbb76c12SGreg Roach        /* I18N: Description of the “Messages” module */
71bbb76c12SGreg Roach        return I18N::translate('Communicate directly with other users, using private messages.');
728c2e8227SGreg Roach    }
738c2e8227SGreg Roach
7476692c8bSGreg Roach    /**
75cf700360SGreg Roach     * Delete one or messages belonging to a user.
762da2404eSGreg Roach     *
776ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
78cf700360SGreg Roach     *
796ccdf4f0SGreg Roach     * @return ResponseInterface
802da2404eSGreg Roach     */
8157ab2231SGreg Roach    public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface
82c1010edaSGreg Roach    {
83b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
84b55cbc6bSGreg Roach        $context     = Validator::queryParams($request)->string('context');
8507e9e923SGreg Roach        $message_ids = Validator::parsedBody($request)->array('message_id');
86cf700360SGreg Roach
87d39b150cSGreg Roach        DB::table('message')
88d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
89d39b150cSGreg Roach            ->whereIn('message_id', $message_ids)
90d39b150cSGreg Roach            ->delete();
91cf700360SGreg Roach
92b55cbc6bSGreg Roach        if ($context === ModuleBlockInterface::CONTEXT_USER_PAGE) {
938e0e1b25SGreg Roach            $url = route(UserPage::class, ['tree' => $tree->name()]);
94cf700360SGreg Roach        } else {
958e0e1b25SGreg Roach            $url = route(TreePage::class, ['tree' => $tree->name()]);
962da2404eSGreg Roach        }
972da2404eSGreg Roach
986ccdf4f0SGreg Roach        return redirect($url);
992da2404eSGreg Roach    }
1002da2404eSGreg Roach
1012da2404eSGreg Roach    /**
10276692c8bSGreg Roach     * Generate the HTML content of this block.
10376692c8bSGreg Roach     *
104e490cd80SGreg Roach     * @param Tree                 $tree
10576692c8bSGreg Roach     * @param int                  $block_id
1063caaa4d2SGreg Roach     * @param string               $context
10776d39c55SGreg Roach     * @param array<string,string> $config
10876692c8bSGreg Roach     *
10976692c8bSGreg Roach     * @return string
11076692c8bSGreg Roach     */
1113caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
112c1010edaSGreg Roach    {
113d39b150cSGreg Roach        $messages = DB::table('message')
114d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
115d39b150cSGreg Roach            ->orderByDesc('message_id')
116ca52a408SGreg Roach            ->get()
117f70bcff5SGreg Roach            ->map(static function (object $row): object {
118d97083feSGreg Roach                $row->created = Registry::timestampFactory()->fromString($row->created);
119ca52a408SGreg Roach
120ca52a408SGreg Roach                return $row;
121ca52a408SGreg Roach            });
1228c2e8227SGreg Roach
1230b5fd0a6SGreg Roach        $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
12476a8e0f3SGreg Roach            $public_tree  = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1';
12576a8e0f3SGreg Roach            $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER;
12676a8e0f3SGreg Roach
12776a8e0f3SGreg Roach            return
128895230eeSGreg Roach                $user->id() !== Auth::id() &&
1291fe542e9SGreg Roach                $user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) &&
13076a8e0f3SGreg Roach                $can_see_tree &&
1318cfb5e7bSGreg Roach                $user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== MessageService::CONTACT_METHOD_NONE;
1328c2e8227SGreg Roach        });
1338c2e8227SGreg Roach
134b47837c4SGreg Roach        $content = view('modules/user-messages/user-messages', [
135b47837c4SGreg Roach            'block_id'     => $block_id,
1363caaa4d2SGreg Roach            'context'      => $context,
137b47837c4SGreg Roach            'messages'     => $messages,
138b47837c4SGreg Roach            'module'       => $this,
139b47837c4SGreg Roach            'tree'         => $tree,
140b47837c4SGreg Roach            'user_service' => $this->user_service,
141b47837c4SGreg Roach            'users'        => $users,
142c1010edaSGreg Roach        ]);
143d39b150cSGreg Roach
1443caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
145d39b150cSGreg Roach            $count = $messages->count();
146d39b150cSGreg Roach
147147e99aaSGreg Roach            return view('modules/block-template', [
1481e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1499c6524dcSGreg Roach                'id'         => $block_id,
1509c6524dcSGreg Roach                'config_url' => '',
151f7f4b984SGreg Roach                'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
1529c6524dcSGreg Roach                'content'    => $content,
1539c6524dcSGreg Roach            ]);
1548c2e8227SGreg Roach        }
155b2ce94c6SRico Sonntag
156b2ce94c6SRico Sonntag        return $content;
1578c2e8227SGreg Roach    }
1588c2e8227SGreg Roach
1593caaa4d2SGreg Roach    /**
1603caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1613caaa4d2SGreg Roach     *
1623caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1633caaa4d2SGreg Roach     *
1643caaa4d2SGreg Roach     * @return bool
1653caaa4d2SGreg Roach     */
166c1010edaSGreg Roach    public function loadAjax(): bool
167c1010edaSGreg Roach    {
1688c2e8227SGreg Roach        return false;
1698c2e8227SGreg Roach    }
1708c2e8227SGreg Roach
1713caaa4d2SGreg Roach    /**
1723caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1733caaa4d2SGreg Roach     *
1743caaa4d2SGreg Roach     * @return bool
1753caaa4d2SGreg Roach     */
176c1010edaSGreg Roach    public function isUserBlock(): bool
177c1010edaSGreg Roach    {
1788c2e8227SGreg Roach        return true;
1798c2e8227SGreg Roach    }
1808c2e8227SGreg Roach
1813caaa4d2SGreg Roach    /**
1823caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1833caaa4d2SGreg Roach     *
1843caaa4d2SGreg Roach     * @return bool
1853caaa4d2SGreg Roach     */
18663276d8fSGreg Roach    public function isTreeBlock(): bool
187c1010edaSGreg Roach    {
1888c2e8227SGreg Roach        return false;
1898c2e8227SGreg Roach    }
1908c2e8227SGreg Roach}
191