18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 20*ca52a408SGreg Roachuse Carbon\Carbon; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail; 25e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 28e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\TreeUser; 2977654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 30a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 318c2e8227SGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class ReviewChangesModule 348c2e8227SGreg Roach */ 3537eb8894SGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface 36c1010edaSGreg Roach{ 3749a243cbSGreg Roach use ModuleBlockTrait; 3849a243cbSGreg Roach 39961ec755SGreg Roach /** 40e5a6b4d4SGreg Roach * @var UserService 41e5a6b4d4SGreg Roach */ 42e5a6b4d4SGreg Roach private $user_service; 43e5a6b4d4SGreg Roach 44e5a6b4d4SGreg Roach /** 45e5a6b4d4SGreg Roach * ReviewChangesModule constructor. 46e5a6b4d4SGreg Roach * 47e5a6b4d4SGreg Roach * @param UserService $user_service 48e5a6b4d4SGreg Roach */ 49e5a6b4d4SGreg Roach public function __construct(UserService $user_service) 50e5a6b4d4SGreg Roach { 51e5a6b4d4SGreg Roach $this->user_service = $user_service; 52e5a6b4d4SGreg Roach } 53e5a6b4d4SGreg Roach 54e5a6b4d4SGreg Roach /** 55961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 56961ec755SGreg Roach * 57961ec755SGreg Roach * @return string 58961ec755SGreg Roach */ 5949a243cbSGreg Roach public function title(): string 60c1010edaSGreg Roach { 61bbb76c12SGreg Roach /* I18N: Name of a module */ 62bbb76c12SGreg Roach return I18N::translate('Pending changes'); 638c2e8227SGreg Roach } 648c2e8227SGreg Roach 65961ec755SGreg Roach /** 66961ec755SGreg Roach * A sentence describing what this module does. 67961ec755SGreg Roach * 68961ec755SGreg Roach * @return string 69961ec755SGreg Roach */ 7049a243cbSGreg Roach public function description(): string 71c1010edaSGreg Roach { 72bbb76c12SGreg Roach /* I18N: Description of the “Pending changes” module */ 73bbb76c12SGreg Roach return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 748c2e8227SGreg Roach } 758c2e8227SGreg Roach 7676692c8bSGreg Roach /** 7776692c8bSGreg Roach * Generate the HTML content of this block. 7876692c8bSGreg Roach * 79e490cd80SGreg Roach * @param Tree $tree 8076692c8bSGreg Roach * @param int $block_id 815f2ae573SGreg Roach * @param string $ctype 82727f238cSGreg Roach * @param string[] $cfg 8376692c8bSGreg Roach * 8476692c8bSGreg Roach * @return string 8576692c8bSGreg Roach */ 865f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 87c1010edaSGreg Roach { 88e2a378d3SGreg Roach $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 89e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', '1'); 908c2e8227SGreg Roach 91c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 928c2e8227SGreg Roach 9377654037SGreg Roach $changes_exist = DB::table('change') 9477654037SGreg Roach ->where('status', 'pending') 9577654037SGreg Roach ->exists(); 968c2e8227SGreg Roach 9777654037SGreg Roach if ($changes_exist && $sendmail === '1') { 988c2e8227SGreg Roach // There are pending changes - tell moderators/managers/administrators about them. 9915d603e7SGreg Roach if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) { 1008c2e8227SGreg Roach // Which users have pending changes? 101e5a6b4d4SGreg Roach foreach ($this->user_service->all() as $user) { 1028c2e8227SGreg Roach if ($user->getPreference('contactmethod') !== 'none') { 10386bc3d8aSRico Sonntag foreach (Tree::getAll() as $tmp_tree) { 10486bc3d8aSRico Sonntag if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 1058c2e8227SGreg Roach I18N::init($user->getPreference('language')); 1068857be8bSGreg Roach 1078857be8bSGreg Roach Mail::send( 108e5a6b4d4SGreg Roach new TreeUser($tmp_tree), 1098c2e8227SGreg Roach $user, 110e5a6b4d4SGreg Roach new TreeUser($tmp_tree), 1118c2e8227SGreg Roach I18N::translate('Pending changes'), 112c1010edaSGreg Roach view('emails/pending-changes-text', [ 11386bc3d8aSRico Sonntag 'tree' => $tmp_tree, 114c1010edaSGreg Roach 'user' => $user, 115c1010edaSGreg Roach ]), 116c1010edaSGreg Roach view('emails/pending-changes-html', [ 11786bc3d8aSRico Sonntag 'tree' => $tmp_tree, 118c1010edaSGreg Roach 'user' => $user, 119c1010edaSGreg Roach ]) 1208c2e8227SGreg Roach ); 1218c2e8227SGreg Roach I18N::init(WT_LOCALE); 1228c2e8227SGreg Roach } 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach } 1258c2e8227SGreg Roach } 126f97e27d5SGreg Roach Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP); 1278c2e8227SGreg Roach } 1288c2e8227SGreg Roach } 129e490cd80SGreg Roach if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 1308c2e8227SGreg Roach $content = ''; 131e490cd80SGreg Roach if (Auth::isModerator($tree)) { 132aa6f03bbSGreg Roach $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 1338c2e8227SGreg Roach } 1348ae466efSGreg Roach if ($sendmail === '1') { 135*ca52a408SGreg Roach $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL')); 136*ca52a408SGreg Roach $next_email_timestamp = $last_email_timestamp->copy()->addDays($days); 137*ca52a408SGreg Roach 138*ca52a408SGreg Roach $content .= I18N::translate('Last email reminder was sent ') . I18N::localTime($last_email_timestamp) . '<br>'; 139*ca52a408SGreg Roach $content .= I18N::translate('Next email reminder will be sent after ') . I18N::localTime($next_email_timestamp) . '<br><br>'; 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach $content .= '<ul>'; 14277654037SGreg Roach 14377654037SGreg Roach $changes = DB::table('change') 14477654037SGreg Roach ->where('gedcom_id', '=', $tree->id()) 14577654037SGreg Roach ->select(['xref']) 14677654037SGreg Roach ->get(); 14777654037SGreg Roach 1488c2e8227SGreg Roach foreach ($changes as $change) { 149e490cd80SGreg Roach $record = GedcomRecord::getInstance($change->xref, $tree); 1508c2e8227SGreg Roach if ($record->canShow()) { 15139ca88baSGreg Roach $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>'; 1528c2e8227SGreg Roach } 1538c2e8227SGreg Roach } 1548c2e8227SGreg Roach $content .= '</ul>'; 1558c2e8227SGreg Roach 1566a8879feSGreg Roach if ($ctype !== '') { 157e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 158c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 159c1010edaSGreg Roach 'block_id' => $block_id, 160aa6f03bbSGreg Roach 'ged' => $tree->name(), 161c1010edaSGreg Roach ]); 162397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 163c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 164c1010edaSGreg Roach 'block_id' => $block_id, 165aa6f03bbSGreg Roach 'ged' => $tree->name(), 166c1010edaSGreg Roach ]); 1678cbbfdceSGreg Roach } else { 1688cbbfdceSGreg Roach $config_url = ''; 1698cbbfdceSGreg Roach } 1708cbbfdceSGreg Roach 171147e99aaSGreg Roach return view('modules/block-template', [ 17226684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1739c6524dcSGreg Roach 'id' => $block_id, 1748cbbfdceSGreg Roach 'config_url' => $config_url, 17549a243cbSGreg Roach 'title' => $this->title(), 1769c6524dcSGreg Roach 'content' => $content, 1779c6524dcSGreg Roach ]); 1788c2e8227SGreg Roach } 179b2ce94c6SRico Sonntag 180b2ce94c6SRico Sonntag return $content; 1818c2e8227SGreg Roach } 18215d603e7SGreg Roach 18315d603e7SGreg Roach return ''; 1848c2e8227SGreg Roach } 1858c2e8227SGreg Roach 1868c2e8227SGreg Roach /** {@inheritdoc} */ 187c1010edaSGreg Roach public function loadAjax(): bool 188c1010edaSGreg Roach { 1898c2e8227SGreg Roach return false; 1908c2e8227SGreg Roach } 1918c2e8227SGreg Roach 1928c2e8227SGreg Roach /** {@inheritdoc} */ 193c1010edaSGreg Roach public function isUserBlock(): bool 194c1010edaSGreg Roach { 1958c2e8227SGreg Roach return true; 1968c2e8227SGreg Roach } 1978c2e8227SGreg Roach 1988c2e8227SGreg Roach /** {@inheritdoc} */ 19963276d8fSGreg Roach public function isTreeBlock(): bool 200c1010edaSGreg Roach { 2018c2e8227SGreg Roach return true; 2028c2e8227SGreg Roach } 2038c2e8227SGreg Roach 20476692c8bSGreg Roach /** 205a45f9889SGreg Roach * Update the configuration for a block. 206a45f9889SGreg Roach * 207a45f9889SGreg Roach * @param Request $request 208a45f9889SGreg Roach * @param int $block_id 209a45f9889SGreg Roach * 210a45f9889SGreg Roach * @return void 211a45f9889SGreg Roach */ 212a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 213a45f9889SGreg Roach { 214a45f9889SGreg Roach $this->setBlockSetting($block_id, 'days', $request->get('num', '1')); 215a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', '')); 216a45f9889SGreg Roach } 217a45f9889SGreg Roach 218a45f9889SGreg Roach /** 21976692c8bSGreg Roach * An HTML form to edit block settings 22076692c8bSGreg Roach * 221e490cd80SGreg Roach * @param Tree $tree 22276692c8bSGreg Roach * @param int $block_id 223a9430be8SGreg Roach * 224a9430be8SGreg Roach * @return void 22576692c8bSGreg Roach */ 226a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 227c1010edaSGreg Roach { 228e2a378d3SGreg Roach $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 229e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', '1'); 2308c2e8227SGreg Roach 231147e99aaSGreg Roach echo view('modules/review_changes/config', [ 232c385536dSGreg Roach 'days' => $days, 233c385536dSGreg Roach 'sendmail' => $sendmail, 234c385536dSGreg Roach ]); 2358c2e8227SGreg Roach } 2368c2e8227SGreg Roach} 237