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; 231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 24*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 2522e73debSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\PendingChanges; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2709482a55SGreg Roachuse Fisharebest\Webtrees\Registry; 28e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService; 298cfb5e7bSGreg Roachuse Fisharebest\Webtrees\Services\MessageService; 303df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 31e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 320e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site; 334c9729fbSGreg Roachuse Fisharebest\Webtrees\SiteUser; 340e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 35e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\TreeUser; 36748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator; 3746786cf3SGreg Roachuse Illuminate\Database\Query\Builder; 3846786cf3SGreg Roachuse Illuminate\Database\Query\Expression; 391e7a7a28SGreg Roachuse Illuminate\Support\Str; 406ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 418c2e8227SGreg Roach 42d97083feSGreg Roachuse function time; 43d97083feSGreg Roach 448c2e8227SGreg Roach/** 458c2e8227SGreg Roach * Class ReviewChangesModule 468c2e8227SGreg Roach */ 4737eb8894SGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface 48c1010edaSGreg Roach{ 4949a243cbSGreg Roach use ModuleBlockTrait; 5049a243cbSGreg Roach 5143f2f523SGreg Roach private EmailService $email_service; 520f97530bSGreg Roach 5343f2f523SGreg Roach private UserService $user_service; 54e5a6b4d4SGreg Roach 5543f2f523SGreg Roach private TreeService $tree_service; 563df1e584SGreg Roach 57e5a6b4d4SGreg Roach /** 58e381f98dSGreg Roach * @param EmailService $email_service 593df1e584SGreg Roach * @param TreeService $tree_service 60e5a6b4d4SGreg Roach * @param UserService $user_service 61e5a6b4d4SGreg Roach */ 623df1e584SGreg Roach public function __construct( 63e381f98dSGreg Roach EmailService $email_service, 643df1e584SGreg Roach TreeService $tree_service, 653df1e584SGreg Roach UserService $user_service 663df1e584SGreg Roach ) { 67e381f98dSGreg Roach $this->email_service = $email_service; 683df1e584SGreg Roach $this->tree_service = $tree_service; 69e5a6b4d4SGreg Roach $this->user_service = $user_service; 70e5a6b4d4SGreg Roach } 71e5a6b4d4SGreg Roach 72e5a6b4d4SGreg Roach /** 730cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 74961ec755SGreg Roach * 75961ec755SGreg Roach * @return string 76961ec755SGreg Roach */ 7749a243cbSGreg Roach public function title(): string 78c1010edaSGreg Roach { 79bbb76c12SGreg Roach /* I18N: Name of a module */ 80bbb76c12SGreg Roach return I18N::translate('Pending changes'); 818c2e8227SGreg Roach } 828c2e8227SGreg Roach 8349a243cbSGreg Roach public function description(): string 84c1010edaSGreg Roach { 85bbb76c12SGreg Roach /* I18N: Description of the “Pending changes” module */ 86bbb76c12SGreg Roach return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 878c2e8227SGreg Roach } 888c2e8227SGreg Roach 8976692c8bSGreg Roach /** 9076692c8bSGreg Roach * Generate the HTML content of this block. 9176692c8bSGreg Roach * 92e490cd80SGreg Roach * @param Tree $tree 9376692c8bSGreg Roach * @param int $block_id 943caaa4d2SGreg Roach * @param string $context 9576d39c55SGreg Roach * @param array<string,string> $config 9676692c8bSGreg Roach * 9776692c8bSGreg Roach * @return string 9876692c8bSGreg Roach */ 993caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 100c1010edaSGreg Roach { 10165cf5706SGreg Roach $old_language = I18N::languageTag(); 10290a2f718SGreg Roach 1034459dc9aSGreg Roach $sendmail = (bool) $this->getBlockSetting($block_id, 'sendmail', '1'); 1044459dc9aSGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', '1'); 1058c2e8227SGreg Roach 1063caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1078c2e8227SGreg Roach 10877654037SGreg Roach $changes_exist = DB::table('change') 10977654037SGreg Roach ->where('status', 'pending') 11077654037SGreg Roach ->exists(); 1118c2e8227SGreg Roach 1124459dc9aSGreg Roach if ($changes_exist && $sendmail) { 113d97083feSGreg Roach $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL'); 114d97083feSGreg Roach $next_email_timestamp = $last_email_timestamp + 86400 * $days; 115ad98d39dSGreg Roach 1168c2e8227SGreg Roach // There are pending changes - tell moderators/managers/administrators about them. 117d97083feSGreg Roach if ($next_email_timestamp < time()) { 1188c2e8227SGreg Roach // Which users have pending changes? 119e5a6b4d4SGreg Roach foreach ($this->user_service->all() as $user) { 1208cfb5e7bSGreg Roach if ($user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== MessageService::CONTACT_METHOD_NONE) { 1213df1e584SGreg Roach foreach ($this->tree_service->all() as $tmp_tree) { 12286bc3d8aSRico Sonntag if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 1231fe542e9SGreg Roach I18N::init($user->getPreference(UserInterface::PREF_LANGUAGE)); 1248857be8bSGreg Roach 125e381f98dSGreg Roach $this->email_service->send( 1264c9729fbSGreg Roach new SiteUser(), 1278c2e8227SGreg Roach $user, 128e5a6b4d4SGreg Roach new TreeUser($tmp_tree), 1298c2e8227SGreg Roach I18N::translate('Pending changes'), 130c1010edaSGreg Roach view('emails/pending-changes-text', [ 13186bc3d8aSRico Sonntag 'tree' => $tmp_tree, 132c1010edaSGreg Roach 'user' => $user, 133c1010edaSGreg Roach ]), 134c1010edaSGreg Roach view('emails/pending-changes-html', [ 13586bc3d8aSRico Sonntag 'tree' => $tmp_tree, 136c1010edaSGreg Roach 'user' => $user, 137c1010edaSGreg Roach ]) 1388c2e8227SGreg Roach ); 1398c2e8227SGreg Roach } 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach } 14365cf5706SGreg Roach I18N::init($old_language); 144d97083feSGreg Roach Site::setPreference('LAST_CHANGE_EMAIL', (string) time()); 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach } 147e490cd80SGreg Roach if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 1488c2e8227SGreg Roach $content = ''; 149e490cd80SGreg Roach if (Auth::isModerator($tree)) { 15022e73debSGreg Roach $content .= '<a href="' . e(route(PendingChanges::class, ['tree' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 1518c2e8227SGreg Roach } 1524459dc9aSGreg Roach if ($sendmail) { 153d97083feSGreg Roach $last_email_timestamp = Registry::timestampFactory()->make((int) Site::getPreference('LAST_CHANGE_EMAIL')); 154d97083feSGreg Roach $next_email_timestamp = $last_email_timestamp->addDays($days); 155ca52a408SGreg Roach 1564459dc9aSGreg Roach $content .= I18N::translate('Last email reminder was sent ') . view('components/datetime', ['timestamp' => $last_email_timestamp]) . '<br>'; 1574459dc9aSGreg Roach $content .= I18N::translate('Next email reminder will be sent after ') . view('components/datetime', ['timestamp' => $next_email_timestamp]) . '<br><br>'; 1588c2e8227SGreg Roach } 1598c2e8227SGreg Roach $content .= '<ul>'; 16077654037SGreg Roach 16177654037SGreg Roach $changes = DB::table('change') 16277654037SGreg Roach ->where('gedcom_id', '=', $tree->id()) 16346786cf3SGreg Roach ->whereIn('change_id', static function (Builder $query) use ($tree): void { 164059898c9SGreg Roach $query->select([new Expression('MAX(change_id)')]) 16546786cf3SGreg Roach ->from('change') 16646786cf3SGreg Roach ->where('gedcom_id', '=', $tree->id()) 1678cef4ae1SGreg Roach ->where('status', '=', 'pending') 16846786cf3SGreg Roach ->groupBy(['xref']); 16946786cf3SGreg Roach }) 17077654037SGreg Roach ->get(); 17177654037SGreg Roach 1728c2e8227SGreg Roach foreach ($changes as $change) { 17346786cf3SGreg Roach $record = Registry::gedcomRecordFactory()->make($change->xref, $tree, $change->new_gedcom ?: $change->old_gedcom); 1748c2e8227SGreg Roach if ($record->canShow()) { 17539ca88baSGreg Roach $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>'; 1768c2e8227SGreg Roach } 1778c2e8227SGreg Roach } 1788c2e8227SGreg Roach $content .= '</ul>'; 1798c2e8227SGreg Roach 1803caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 181147e99aaSGreg Roach return view('modules/block-template', [ 1821e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1839c6524dcSGreg Roach 'id' => $block_id, 1843caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 18549a243cbSGreg Roach 'title' => $this->title(), 1869c6524dcSGreg Roach 'content' => $content, 1879c6524dcSGreg Roach ]); 1888c2e8227SGreg Roach } 189b2ce94c6SRico Sonntag 190b2ce94c6SRico Sonntag return $content; 1918c2e8227SGreg Roach } 19215d603e7SGreg Roach 19315d603e7SGreg Roach return ''; 1948c2e8227SGreg Roach } 1958c2e8227SGreg Roach 1963caaa4d2SGreg Roach /** 1973caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1983caaa4d2SGreg Roach * 1993caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 2003caaa4d2SGreg Roach * 2013caaa4d2SGreg Roach * @return bool 2023caaa4d2SGreg Roach */ 203c1010edaSGreg Roach public function loadAjax(): bool 204c1010edaSGreg Roach { 2058c2e8227SGreg Roach return false; 2068c2e8227SGreg Roach } 2078c2e8227SGreg Roach 2083caaa4d2SGreg Roach /** 2093caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2103caaa4d2SGreg Roach * 2113caaa4d2SGreg Roach * @return bool 2123caaa4d2SGreg Roach */ 213c1010edaSGreg Roach public function isUserBlock(): bool 214c1010edaSGreg Roach { 2158c2e8227SGreg Roach return true; 2168c2e8227SGreg Roach } 2178c2e8227SGreg Roach 2183caaa4d2SGreg Roach /** 2193caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2203caaa4d2SGreg Roach * 2213caaa4d2SGreg Roach * @return bool 2223caaa4d2SGreg Roach */ 22363276d8fSGreg Roach public function isTreeBlock(): bool 224c1010edaSGreg Roach { 2258c2e8227SGreg Roach return true; 2268c2e8227SGreg Roach } 2278c2e8227SGreg Roach 22876692c8bSGreg Roach /** 229a45f9889SGreg Roach * Update the configuration for a block. 230a45f9889SGreg Roach * 2316ccdf4f0SGreg Roach * @param ServerRequestInterface $request 232a45f9889SGreg Roach * @param int $block_id 233a45f9889SGreg Roach * 234a45f9889SGreg Roach * @return void 235a45f9889SGreg Roach */ 2366ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 237a45f9889SGreg Roach { 238748dbe15SGreg Roach $days = Validator::parsedBody($request)->integer('days'); 239748dbe15SGreg Roach $sendmail = Validator::parsedBody($request)->string('sendmail'); 240b6b9dcc9SGreg Roach 241748dbe15SGreg Roach $this->setBlockSetting($block_id, 'days', (string) $days); 242748dbe15SGreg Roach $this->setBlockSetting($block_id, 'sendmail', $sendmail); 243a45f9889SGreg Roach } 244a45f9889SGreg Roach 245a45f9889SGreg Roach /** 24676692c8bSGreg Roach * An HTML form to edit block settings 24776692c8bSGreg Roach * 248e490cd80SGreg Roach * @param Tree $tree 24976692c8bSGreg Roach * @param int $block_id 250a9430be8SGreg Roach * 2513caaa4d2SGreg Roach * @return string 25276692c8bSGreg Roach */ 2533caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 254c1010edaSGreg Roach { 255e2a378d3SGreg Roach $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 256e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', '1'); 2578c2e8227SGreg Roach 2583caaa4d2SGreg Roach return view('modules/review_changes/config', [ 259c385536dSGreg Roach 'days' => $days, 260c385536dSGreg Roach 'sendmail' => $sendmail, 261c385536dSGreg Roach ]); 2628c2e8227SGreg Roach } 2638c2e8227SGreg Roach} 264