xref: /webtrees/app/Module/UserMessagesModule.php (revision 09482a558a7989d76059e7f9911605cf836b77ba)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
51fe542e9SGreg Roach * Copyright (C) 2021 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;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
268e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
28e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
29cf700360SGreg Roachuse Fisharebest\Webtrees\Tree;
30d39b150cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
311e7a7a28SGreg Roachuse Illuminate\Support\Str;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
34ca52a408SGreg Roachuse stdClass;
3571378461SGreg Roach
364ea62551SGreg Roachuse function assert;
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
47961ec755SGreg Roach    /**
48e5a6b4d4SGreg Roach     * @var UserService
49e5a6b4d4SGreg Roach     */
50e5a6b4d4SGreg Roach    private $user_service;
51e5a6b4d4SGreg Roach
52e5a6b4d4SGreg Roach    /**
53e5a6b4d4SGreg Roach     * UserMessagesModule constructor.
54e5a6b4d4SGreg Roach     *
55e5a6b4d4SGreg Roach     * @param UserService $user_service
56e5a6b4d4SGreg Roach     */
57e2cbf57aSGreg Roach    public function __construct(UserService $user_service)
58e2cbf57aSGreg Roach    {
59e5a6b4d4SGreg Roach        $this->user_service = $user_service;
60e5a6b4d4SGreg Roach    }
61e5a6b4d4SGreg Roach
62e5a6b4d4SGreg Roach    /**
630cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
64961ec755SGreg Roach     *
65961ec755SGreg Roach     * @return string
66961ec755SGreg Roach     */
6749a243cbSGreg Roach    public function title(): string
68c1010edaSGreg Roach    {
69bbb76c12SGreg Roach        /* I18N: Name of a module */
70bbb76c12SGreg Roach        return I18N::translate('Messages');
718c2e8227SGreg Roach    }
728c2e8227SGreg Roach
73961ec755SGreg Roach    /**
74961ec755SGreg Roach     * A sentence describing what this module does.
75961ec755SGreg Roach     *
76961ec755SGreg Roach     * @return string
77961ec755SGreg Roach     */
7849a243cbSGreg Roach    public function description(): string
79c1010edaSGreg Roach    {
80bbb76c12SGreg Roach        /* I18N: Description of the “Messages” module */
81bbb76c12SGreg Roach        return I18N::translate('Communicate directly with other users, using private messages.');
828c2e8227SGreg Roach    }
838c2e8227SGreg Roach
8476692c8bSGreg Roach    /**
85cf700360SGreg Roach     * Delete one or messages belonging to a user.
862da2404eSGreg Roach     *
876ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
88cf700360SGreg Roach     *
896ccdf4f0SGreg Roach     * @return ResponseInterface
902da2404eSGreg Roach     */
9157ab2231SGreg Roach    public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface
92c1010edaSGreg Roach    {
9357ab2231SGreg Roach        $tree = $request->getAttribute('tree');
944ea62551SGreg Roach        assert($tree instanceof Tree);
954ea62551SGreg Roach
96b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
97b46c87bdSGreg Roach
98b46c87bdSGreg Roach        $message_ids = $params['message_id'] ?? [];
99cf700360SGreg Roach
100d39b150cSGreg Roach        DB::table('message')
101d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
102d39b150cSGreg Roach            ->whereIn('message_id', $message_ids)
103d39b150cSGreg Roach            ->delete();
104cf700360SGreg Roach
1053caaa4d2SGreg Roach        if ($request->getQueryParams()['context'] === ModuleBlockInterface::CONTEXT_USER_PAGE) {
1068e0e1b25SGreg Roach            $url = route(UserPage::class, ['tree' => $tree->name()]);
107cf700360SGreg Roach        } else {
1088e0e1b25SGreg Roach            $url = route(TreePage::class, ['tree' => $tree->name()]);
1092da2404eSGreg Roach        }
1102da2404eSGreg Roach
1116ccdf4f0SGreg Roach        return redirect($url);
1122da2404eSGreg Roach    }
1132da2404eSGreg Roach
1142da2404eSGreg Roach    /**
11576692c8bSGreg Roach     * Generate the HTML content of this block.
11676692c8bSGreg Roach     *
117e490cd80SGreg Roach     * @param Tree          $tree
11876692c8bSGreg Roach     * @param int           $block_id
1193caaa4d2SGreg Roach     * @param string        $context
120*09482a55SGreg Roach     * @param array<string> $config
12176692c8bSGreg Roach     *
12276692c8bSGreg Roach     * @return string
12376692c8bSGreg Roach     */
1243caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
125c1010edaSGreg Roach    {
126d39b150cSGreg Roach        $messages = DB::table('message')
127d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
128d39b150cSGreg Roach            ->orderByDesc('message_id')
129ca52a408SGreg Roach            ->get()
1300b5fd0a6SGreg Roach            ->map(static function (stdClass $row): stdClass {
1314459dc9aSGreg Roach                $row->created = Carbon::make($row->created);
132ca52a408SGreg Roach
133ca52a408SGreg Roach                return $row;
134ca52a408SGreg Roach            });
1358c2e8227SGreg Roach
1360b5fd0a6SGreg Roach        $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
13776a8e0f3SGreg Roach            $public_tree  = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1';
13876a8e0f3SGreg Roach            $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER;
13976a8e0f3SGreg Roach
14076a8e0f3SGreg Roach            return
141895230eeSGreg Roach                $user->id() !== Auth::id() &&
1421fe542e9SGreg Roach                $user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) &&
14376a8e0f3SGreg Roach                $can_see_tree &&
1441fe542e9SGreg Roach                $user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== 'none';
1458c2e8227SGreg Roach        });
1468c2e8227SGreg Roach
147b47837c4SGreg Roach        $content = view('modules/user-messages/user-messages', [
148b47837c4SGreg Roach            'block_id'     => $block_id,
1493caaa4d2SGreg Roach            'context'      => $context,
150b47837c4SGreg Roach            'messages'     => $messages,
151b47837c4SGreg Roach            'module'       => $this,
152b47837c4SGreg Roach            'tree'         => $tree,
153b47837c4SGreg Roach            'user_service' => $this->user_service,
154b47837c4SGreg Roach            'users'        => $users,
155c1010edaSGreg Roach        ]);
156d39b150cSGreg Roach
1573caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
158d39b150cSGreg Roach            $count = $messages->count();
159d39b150cSGreg Roach
160147e99aaSGreg Roach            return view('modules/block-template', [
1611e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1629c6524dcSGreg Roach                'id'         => $block_id,
1639c6524dcSGreg Roach                'config_url' => '',
164f7f4b984SGreg Roach                'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
1659c6524dcSGreg Roach                'content'    => $content,
1669c6524dcSGreg Roach            ]);
1678c2e8227SGreg Roach        }
168b2ce94c6SRico Sonntag
169b2ce94c6SRico Sonntag        return $content;
1708c2e8227SGreg Roach    }
1718c2e8227SGreg Roach
1723caaa4d2SGreg Roach    /**
1733caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1743caaa4d2SGreg Roach     *
1753caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1763caaa4d2SGreg Roach     *
1773caaa4d2SGreg Roach     * @return bool
1783caaa4d2SGreg Roach     */
179c1010edaSGreg Roach    public function loadAjax(): bool
180c1010edaSGreg Roach    {
1818c2e8227SGreg Roach        return false;
1828c2e8227SGreg Roach    }
1838c2e8227SGreg Roach
1843caaa4d2SGreg Roach    /**
1853caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1863caaa4d2SGreg Roach     *
1873caaa4d2SGreg Roach     * @return bool
1883caaa4d2SGreg Roach     */
189c1010edaSGreg Roach    public function isUserBlock(): bool
190c1010edaSGreg Roach    {
1918c2e8227SGreg Roach        return true;
1928c2e8227SGreg Roach    }
1938c2e8227SGreg Roach
1943caaa4d2SGreg Roach    /**
1953caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1963caaa4d2SGreg Roach     *
1973caaa4d2SGreg Roach     * @return bool
1983caaa4d2SGreg Roach     */
19963276d8fSGreg Roach    public function isTreeBlock(): bool
200c1010edaSGreg Roach    {
2018c2e8227SGreg Roach        return false;
2028c2e8227SGreg Roach    }
2038c2e8227SGreg Roach}
204