. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Filter; use Fisharebest\Webtrees\Functions\FunctionsDate; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Tree; use Fisharebest\Webtrees\User; use Illuminate\Database\Capsule\Manager as DB; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** * Class UserMessagesModule */ class UserMessagesModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface { use ModuleBlockTrait; /** {@inheritdoc} */ public function title(): string { /* I18N: Name of a module */ return I18N::translate('Messages'); } /** {@inheritdoc} */ public function description(): string { /* I18N: Description of the “Messages” module */ return I18N::translate('Communicate directly with other users, using private messages.'); } /** * Delete one or messages belonging to a user. * * @param Request $request * @param Tree $tree * * @return Response */ public function postDeleteMessageAction(Request $request, Tree $tree): Response { $message_ids = (array) $request->get('message_id', []); DB::table('message') ->where('user_id', '=', Auth::id()) ->whereIn('message_id', $message_ids) ->delete(); if ($request->get('ctype') === 'user') { $url = route('user-page', ['ged' => $tree->name()]); } else { $url = route('tree-page', ['ged' => $tree->name()]); } return new RedirectResponse($url); } /** * Generate the HTML content of this block. * * @param Tree $tree * @param int $block_id * @param string $ctype * @param string[] $cfg * * @return string */ public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string { $messages = DB::table('message') ->where('user_id', '=', Auth::id()) ->orderByDesc('message_id') ->select(['message_id', 'sender', 'subject', 'body', DB::raw('UNIX_TIMESTAMP(created) AS created')]) ->get(); $users = User::all()->filter(function (User $user) use ($tree): bool { $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; return $user->id() !== Auth::id() && $user->getPreference('verified_by_admin') && $can_see_tree && $user->getPreference('contactmethod') !== 'none'; }); $content = ''; if ($users->isNotEmpty()) { $url = route('user-page', ['ged' => $tree->name()]); $content .= '
'; } $content .= ''; if ($ctype !== '') { $count = $messages->count(); return view('modules/block-template', [ 'block' => str_replace('_', '-', $this->getName()), 'id' => $block_id, 'config_url' => '', 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 'content' => $content, ]); } return $content; } /** {@inheritdoc} */ public function loadAjax(): bool { return false; } /** {@inheritdoc} */ public function isUserBlock(): bool { return true; } /** {@inheritdoc} */ public function isGedcomBlock(): bool { return false; } /** * Update the configuration for a block. * * @param Request $request * @param int $block_id * * @return void */ public function saveBlockConfiguration(Request $request, int $block_id) { } /** * An HTML form to edit block settings * * @param Tree $tree * @param int $block_id * * @return void */ public function editBlockConfiguration(Tree $tree, int $block_id) { } }