1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Contracts\UserInterface; 24use Fisharebest\Webtrees\DB; 25use Fisharebest\Webtrees\Http\RequestHandlers\PendingChanges; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Services\EmailService; 29use Fisharebest\Webtrees\Services\MessageService; 30use Fisharebest\Webtrees\Services\TreeService; 31use Fisharebest\Webtrees\Services\UserService; 32use Fisharebest\Webtrees\Site; 33use Fisharebest\Webtrees\SiteUser; 34use Fisharebest\Webtrees\Tree; 35use Fisharebest\Webtrees\TreeUser; 36use Fisharebest\Webtrees\Validator; 37use Illuminate\Database\Query\Builder; 38use Illuminate\Database\Query\Expression; 39use Illuminate\Support\Str; 40use Psr\Http\Message\ServerRequestInterface; 41 42use function time; 43 44/** 45 * Class ReviewChangesModule 46 */ 47class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface 48{ 49 use ModuleBlockTrait; 50 51 private EmailService $email_service; 52 53 private UserService $user_service; 54 55 private TreeService $tree_service; 56 57 /** 58 * @param EmailService $email_service 59 * @param TreeService $tree_service 60 * @param UserService $user_service 61 */ 62 public function __construct( 63 EmailService $email_service, 64 TreeService $tree_service, 65 UserService $user_service 66 ) { 67 $this->email_service = $email_service; 68 $this->tree_service = $tree_service; 69 $this->user_service = $user_service; 70 } 71 72 /** 73 * How should this module be identified in the control panel, etc.? 74 * 75 * @return string 76 */ 77 public function title(): string 78 { 79 /* I18N: Name of a module */ 80 return I18N::translate('Pending changes'); 81 } 82 83 public function description(): string 84 { 85 /* I18N: Description of the “Pending changes” module */ 86 return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 87 } 88 89 /** 90 * Generate the HTML content of this block. 91 * 92 * @param Tree $tree 93 * @param int $block_id 94 * @param string $context 95 * @param array<string,string> $config 96 * 97 * @return string 98 */ 99 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 100 { 101 $old_language = I18N::languageTag(); 102 103 $sendmail = (bool) $this->getBlockSetting($block_id, 'sendmail', '1'); 104 $days = (int) $this->getBlockSetting($block_id, 'days', '1'); 105 106 extract($config, EXTR_OVERWRITE); 107 108 $changes_exist = DB::table('change') 109 ->where('status', 'pending') 110 ->exists(); 111 112 if ($changes_exist && $sendmail) { 113 $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL'); 114 $next_email_timestamp = $last_email_timestamp + 86400 * $days; 115 116 // There are pending changes - tell moderators/managers/administrators about them. 117 if ($next_email_timestamp < time()) { 118 // Which users have pending changes? 119 foreach ($this->user_service->all() as $user) { 120 if ($user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== MessageService::CONTACT_METHOD_NONE) { 121 foreach ($this->tree_service->all() as $tmp_tree) { 122 if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 123 I18N::init($user->getPreference(UserInterface::PREF_LANGUAGE)); 124 125 $this->email_service->send( 126 new SiteUser(), 127 $user, 128 new TreeUser($tmp_tree), 129 I18N::translate('Pending changes'), 130 view('emails/pending-changes-text', [ 131 'tree' => $tmp_tree, 132 'user' => $user, 133 ]), 134 view('emails/pending-changes-html', [ 135 'tree' => $tmp_tree, 136 'user' => $user, 137 ]) 138 ); 139 } 140 } 141 } 142 } 143 I18N::init($old_language); 144 Site::setPreference('LAST_CHANGE_EMAIL', (string) time()); 145 } 146 } 147 if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 148 $content = ''; 149 if (Auth::isModerator($tree)) { 150 $content .= '<a href="' . e(route(PendingChanges::class, ['tree' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 151 } 152 if ($sendmail) { 153 $last_email_timestamp = Registry::timestampFactory()->make((int) Site::getPreference('LAST_CHANGE_EMAIL')); 154 $next_email_timestamp = $last_email_timestamp->addDays($days); 155 156 $content .= I18N::translate('Last email reminder was sent ') . view('components/datetime', ['timestamp' => $last_email_timestamp]) . '<br>'; 157 $content .= I18N::translate('Next email reminder will be sent after ') . view('components/datetime', ['timestamp' => $next_email_timestamp]) . '<br><br>'; 158 } 159 $content .= '<ul>'; 160 161 $changes = DB::table('change') 162 ->where('gedcom_id', '=', $tree->id()) 163 ->whereIn('change_id', static function (Builder $query) use ($tree): void { 164 $query->select([new Expression('MAX(change_id)')]) 165 ->from('change') 166 ->where('gedcom_id', '=', $tree->id()) 167 ->where('status', '=', 'pending') 168 ->groupBy(['xref']); 169 }) 170 ->get(); 171 172 foreach ($changes as $change) { 173 $record = Registry::gedcomRecordFactory()->make($change->xref, $tree, $change->new_gedcom ?: $change->old_gedcom); 174 if ($record->canShow()) { 175 $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>'; 176 } 177 } 178 $content .= '</ul>'; 179 180 if ($context !== self::CONTEXT_EMBED) { 181 return view('modules/block-template', [ 182 'block' => Str::kebab($this->name()), 183 'id' => $block_id, 184 'config_url' => $this->configUrl($tree, $context, $block_id), 185 'title' => $this->title(), 186 'content' => $content, 187 ]); 188 } 189 190 return $content; 191 } 192 193 return ''; 194 } 195 196 /** 197 * Should this block load asynchronously using AJAX? 198 * 199 * Simple blocks are faster in-line, more complex ones can be loaded later. 200 * 201 * @return bool 202 */ 203 public function loadAjax(): bool 204 { 205 return false; 206 } 207 208 /** 209 * Can this block be shown on the user’s home page? 210 * 211 * @return bool 212 */ 213 public function isUserBlock(): bool 214 { 215 return true; 216 } 217 218 /** 219 * Can this block be shown on the tree’s home page? 220 * 221 * @return bool 222 */ 223 public function isTreeBlock(): bool 224 { 225 return true; 226 } 227 228 /** 229 * Update the configuration for a block. 230 * 231 * @param ServerRequestInterface $request 232 * @param int $block_id 233 * 234 * @return void 235 */ 236 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 237 { 238 $days = Validator::parsedBody($request)->integer('days'); 239 $sendmail = Validator::parsedBody($request)->string('sendmail'); 240 241 $this->setBlockSetting($block_id, 'days', (string) $days); 242 $this->setBlockSetting($block_id, 'sendmail', $sendmail); 243 } 244 245 /** 246 * An HTML form to edit block settings 247 * 248 * @param Tree $tree 249 * @param int $block_id 250 * 251 * @return string 252 */ 253 public function editBlockConfiguration(Tree $tree, int $block_id): string 254 { 255 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 256 $days = $this->getBlockSetting($block_id, 'days', '1'); 257 258 return view('modules/review_changes/config', [ 259 'days' => $days, 260 'sendmail' => $sendmail, 261 ]); 262 } 263} 264