18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 2290a2f718SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 244459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 2622e73debSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\PendingChanges; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 28e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService; 293df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 30e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site; 324c9729fbSGreg Roachuse Fisharebest\Webtrees\SiteUser; 330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 34e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\TreeUser; 35*7c4add84SGreg Roachuse Fisharebest\Webtrees\User; 3677654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 371e7a7a28SGreg Roachuse Illuminate\Support\Str; 386ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 398c2e8227SGreg Roach 4090a2f718SGreg Roachuse function assert; 4190a2f718SGreg Roach 428c2e8227SGreg Roach/** 438c2e8227SGreg Roach * Class ReviewChangesModule 448c2e8227SGreg Roach */ 4537eb8894SGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface 46c1010edaSGreg Roach{ 4749a243cbSGreg Roach use ModuleBlockTrait; 4849a243cbSGreg Roach 49e381f98dSGreg Roach /** @var EmailService */ 50e381f98dSGreg Roach private $email_service; 510f97530bSGreg Roach 523df1e584SGreg Roach /** @var UserService */ 53e5a6b4d4SGreg Roach private $user_service; 54e5a6b4d4SGreg Roach 553df1e584SGreg Roach /** @var TreeService */ 563df1e584SGreg Roach private $tree_service; 573df1e584SGreg Roach 58e5a6b4d4SGreg Roach /** 59e5a6b4d4SGreg Roach * ReviewChangesModule constructor. 60e5a6b4d4SGreg Roach * 61e381f98dSGreg Roach * @param EmailService $email_service 623df1e584SGreg Roach * @param TreeService $tree_service 63e5a6b4d4SGreg Roach * @param UserService $user_service 64e5a6b4d4SGreg Roach */ 653df1e584SGreg Roach public function __construct( 66e381f98dSGreg Roach EmailService $email_service, 673df1e584SGreg Roach TreeService $tree_service, 683df1e584SGreg Roach UserService $user_service 693df1e584SGreg Roach ) { 70e381f98dSGreg Roach $this->email_service = $email_service; 713df1e584SGreg Roach $this->tree_service = $tree_service; 72e5a6b4d4SGreg Roach $this->user_service = $user_service; 73e5a6b4d4SGreg Roach } 74e5a6b4d4SGreg Roach 75e5a6b4d4SGreg Roach /** 760cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 77961ec755SGreg Roach * 78961ec755SGreg Roach * @return string 79961ec755SGreg Roach */ 8049a243cbSGreg Roach public function title(): string 81c1010edaSGreg Roach { 82bbb76c12SGreg Roach /* I18N: Name of a module */ 83bbb76c12SGreg Roach return I18N::translate('Pending changes'); 848c2e8227SGreg Roach } 858c2e8227SGreg Roach 86961ec755SGreg Roach /** 87961ec755SGreg Roach * A sentence describing what this module does. 88961ec755SGreg Roach * 89961ec755SGreg Roach * @return string 90961ec755SGreg Roach */ 9149a243cbSGreg Roach public function description(): string 92c1010edaSGreg Roach { 93bbb76c12SGreg Roach /* I18N: Description of the “Pending changes” module */ 94bbb76c12SGreg Roach return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 958c2e8227SGreg Roach } 968c2e8227SGreg Roach 9776692c8bSGreg Roach /** 9876692c8bSGreg Roach * Generate the HTML content of this block. 9976692c8bSGreg Roach * 100e490cd80SGreg Roach * @param Tree $tree 10176692c8bSGreg Roach * @param int $block_id 1023caaa4d2SGreg Roach * @param string $context 1033caaa4d2SGreg Roach * @param string[] $config 10476692c8bSGreg Roach * 10576692c8bSGreg Roach * @return string 10676692c8bSGreg Roach */ 1073caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 108c1010edaSGreg Roach { 10990a2f718SGreg Roach $locale = app(ServerRequestInterface::class)->getAttribute('locale'); 11090a2f718SGreg Roach assert($locale instanceof LocaleInterface); 11190a2f718SGreg Roach 1124459dc9aSGreg Roach $sendmail = (bool) $this->getBlockSetting($block_id, 'sendmail', '1'); 1134459dc9aSGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', '1'); 1148c2e8227SGreg Roach 1153caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1168c2e8227SGreg Roach 11777654037SGreg Roach $changes_exist = DB::table('change') 11877654037SGreg Roach ->where('status', 'pending') 11977654037SGreg Roach ->exists(); 1208c2e8227SGreg Roach 1214459dc9aSGreg Roach if ($changes_exist && $sendmail) { 122ad98d39dSGreg Roach $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL')); 123ad98d39dSGreg Roach $next_email_timestamp = $last_email_timestamp->addDays($days); 124ad98d39dSGreg Roach 1258c2e8227SGreg Roach // There are pending changes - tell moderators/managers/administrators about them. 126ad98d39dSGreg Roach if ($next_email_timestamp < Carbon::now()) { 1278c2e8227SGreg Roach // Which users have pending changes? 128e5a6b4d4SGreg Roach foreach ($this->user_service->all() as $user) { 129*7c4add84SGreg Roach if ($user->getPreference(User::PREF_CONTACT_METHOD) !== 'none') { 1303df1e584SGreg Roach foreach ($this->tree_service->all() as $tmp_tree) { 13186bc3d8aSRico Sonntag if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 132*7c4add84SGreg Roach I18N::init($user->getPreference(User::PREF_LANGUAGE)); 1338857be8bSGreg Roach 134e381f98dSGreg Roach $this->email_service->send( 1354c9729fbSGreg Roach new SiteUser(), 1368c2e8227SGreg Roach $user, 137e5a6b4d4SGreg Roach new TreeUser($tmp_tree), 1388c2e8227SGreg Roach I18N::translate('Pending changes'), 139c1010edaSGreg Roach view('emails/pending-changes-text', [ 14086bc3d8aSRico Sonntag 'tree' => $tmp_tree, 141c1010edaSGreg Roach 'user' => $user, 142c1010edaSGreg Roach ]), 143c1010edaSGreg Roach view('emails/pending-changes-html', [ 14486bc3d8aSRico Sonntag 'tree' => $tmp_tree, 145c1010edaSGreg Roach 'user' => $user, 146c1010edaSGreg Roach ]) 1478c2e8227SGreg Roach ); 14890a2f718SGreg Roach I18N::init($locale->languageTag()); 1498c2e8227SGreg Roach } 1508c2e8227SGreg Roach } 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach } 1534459dc9aSGreg Roach Site::setPreference('LAST_CHANGE_EMAIL', (string) Carbon::now()->unix()); 1548c2e8227SGreg Roach } 1558c2e8227SGreg Roach } 156e490cd80SGreg Roach if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 1578c2e8227SGreg Roach $content = ''; 158e490cd80SGreg Roach if (Auth::isModerator($tree)) { 15922e73debSGreg Roach $content .= '<a href="' . e(route(PendingChanges::class, ['tree' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 1608c2e8227SGreg Roach } 1614459dc9aSGreg Roach if ($sendmail) { 162ca52a408SGreg Roach $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL')); 163ca52a408SGreg Roach $next_email_timestamp = $last_email_timestamp->copy()->addDays($days); 164ca52a408SGreg Roach 1654459dc9aSGreg Roach $content .= I18N::translate('Last email reminder was sent ') . view('components/datetime', ['timestamp' => $last_email_timestamp]) . '<br>'; 1664459dc9aSGreg Roach $content .= I18N::translate('Next email reminder will be sent after ') . view('components/datetime', ['timestamp' => $next_email_timestamp]) . '<br><br>'; 1678c2e8227SGreg Roach } 1688c2e8227SGreg Roach $content .= '<ul>'; 16977654037SGreg Roach 17077654037SGreg Roach $changes = DB::table('change') 17177654037SGreg Roach ->where('gedcom_id', '=', $tree->id()) 1728cef4ae1SGreg Roach ->where('status', '=', 'pending') 17377654037SGreg Roach ->select(['xref']) 17477654037SGreg Roach ->get(); 17577654037SGreg Roach 1768c2e8227SGreg Roach foreach ($changes as $change) { 177e490cd80SGreg Roach $record = GedcomRecord::getInstance($change->xref, $tree); 1788c2e8227SGreg Roach if ($record->canShow()) { 17939ca88baSGreg Roach $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>'; 1808c2e8227SGreg Roach } 1818c2e8227SGreg Roach } 1828c2e8227SGreg Roach $content .= '</ul>'; 1838c2e8227SGreg Roach 1843caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 185147e99aaSGreg Roach return view('modules/block-template', [ 1861e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1879c6524dcSGreg Roach 'id' => $block_id, 1883caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 18949a243cbSGreg Roach 'title' => $this->title(), 1909c6524dcSGreg Roach 'content' => $content, 1919c6524dcSGreg Roach ]); 1928c2e8227SGreg Roach } 193b2ce94c6SRico Sonntag 194b2ce94c6SRico Sonntag return $content; 1958c2e8227SGreg Roach } 19615d603e7SGreg Roach 19715d603e7SGreg Roach return ''; 1988c2e8227SGreg Roach } 1998c2e8227SGreg Roach 2003caaa4d2SGreg Roach /** 2013caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 2023caaa4d2SGreg Roach * 2033caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 2043caaa4d2SGreg Roach * 2053caaa4d2SGreg Roach * @return bool 2063caaa4d2SGreg Roach */ 207c1010edaSGreg Roach public function loadAjax(): bool 208c1010edaSGreg Roach { 2098c2e8227SGreg Roach return false; 2108c2e8227SGreg Roach } 2118c2e8227SGreg Roach 2123caaa4d2SGreg Roach /** 2133caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2143caaa4d2SGreg Roach * 2153caaa4d2SGreg Roach * @return bool 2163caaa4d2SGreg Roach */ 217c1010edaSGreg Roach public function isUserBlock(): bool 218c1010edaSGreg Roach { 2198c2e8227SGreg Roach return true; 2208c2e8227SGreg Roach } 2218c2e8227SGreg Roach 2223caaa4d2SGreg Roach /** 2233caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2243caaa4d2SGreg Roach * 2253caaa4d2SGreg Roach * @return bool 2263caaa4d2SGreg Roach */ 22763276d8fSGreg Roach public function isTreeBlock(): bool 228c1010edaSGreg Roach { 2298c2e8227SGreg Roach return true; 2308c2e8227SGreg Roach } 2318c2e8227SGreg Roach 23276692c8bSGreg Roach /** 233a45f9889SGreg Roach * Update the configuration for a block. 234a45f9889SGreg Roach * 2356ccdf4f0SGreg Roach * @param ServerRequestInterface $request 236a45f9889SGreg Roach * @param int $block_id 237a45f9889SGreg Roach * 238a45f9889SGreg Roach * @return void 239a45f9889SGreg Roach */ 2406ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 241a45f9889SGreg Roach { 242b6b9dcc9SGreg Roach $params = $request->getParsedBody(); 243b6b9dcc9SGreg Roach 244b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'days', $params['days']); 245b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'sendmail', $params['sendmail']); 246a45f9889SGreg Roach } 247a45f9889SGreg Roach 248a45f9889SGreg Roach /** 24976692c8bSGreg Roach * An HTML form to edit block settings 25076692c8bSGreg Roach * 251e490cd80SGreg Roach * @param Tree $tree 25276692c8bSGreg Roach * @param int $block_id 253a9430be8SGreg Roach * 2543caaa4d2SGreg Roach * @return string 25576692c8bSGreg Roach */ 2563caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 257c1010edaSGreg Roach { 258e2a378d3SGreg Roach $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 259e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', '1'); 2608c2e8227SGreg Roach 2613caaa4d2SGreg Roach return view('modules/review_changes/config', [ 262c385536dSGreg Roach 'days' => $days, 263c385536dSGreg Roach 'sendmail' => $sendmail, 264c385536dSGreg Roach ]); 2658c2e8227SGreg Roach } 2668c2e8227SGreg Roach} 267