1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\Carbon; 24use Fisharebest\Webtrees\Contracts\UserInterface; 25use Fisharebest\Webtrees\Http\RequestHandlers\PendingChanges; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Services\EmailService; 29use Fisharebest\Webtrees\Services\TreeService; 30use Fisharebest\Webtrees\Services\UserService; 31use Fisharebest\Webtrees\Site; 32use Fisharebest\Webtrees\SiteUser; 33use Fisharebest\Webtrees\Tree; 34use Fisharebest\Webtrees\TreeUser; 35use Illuminate\Database\Capsule\Manager as DB; 36use Illuminate\Support\Str; 37use Psr\Http\Message\ServerRequestInterface; 38 39/** 40 * Class ReviewChangesModule 41 */ 42class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface 43{ 44 use ModuleBlockTrait; 45 46 private EmailService $email_service; 47 48 private UserService $user_service; 49 50 private TreeService $tree_service; 51 52 /** 53 * ReviewChangesModule constructor. 54 * 55 * @param EmailService $email_service 56 * @param TreeService $tree_service 57 * @param UserService $user_service 58 */ 59 public function __construct( 60 EmailService $email_service, 61 TreeService $tree_service, 62 UserService $user_service 63 ) { 64 $this->email_service = $email_service; 65 $this->tree_service = $tree_service; 66 $this->user_service = $user_service; 67 } 68 69 /** 70 * How should this module be identified in the control panel, etc.? 71 * 72 * @return string 73 */ 74 public function title(): string 75 { 76 /* I18N: Name of a module */ 77 return I18N::translate('Pending changes'); 78 } 79 80 /** 81 * A sentence describing what this module does. 82 * 83 * @return string 84 */ 85 public function description(): string 86 { 87 /* I18N: Description of the “Pending changes” module */ 88 return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 89 } 90 91 /** 92 * Generate the HTML content of this block. 93 * 94 * @param Tree $tree 95 * @param int $block_id 96 * @param string $context 97 * @param array<string> $config 98 * 99 * @return string 100 */ 101 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 102 { 103 $old_language = I18N::languageTag(); 104 105 $sendmail = (bool) $this->getBlockSetting($block_id, 'sendmail', '1'); 106 $days = (int) $this->getBlockSetting($block_id, 'days', '1'); 107 108 extract($config, EXTR_OVERWRITE); 109 110 $changes_exist = DB::table('change') 111 ->where('status', 'pending') 112 ->exists(); 113 114 if ($changes_exist && $sendmail) { 115 $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL')); 116 $next_email_timestamp = $last_email_timestamp->addDays($days); 117 118 // There are pending changes - tell moderators/managers/administrators about them. 119 if ($next_email_timestamp < Carbon::now()) { 120 // Which users have pending changes? 121 foreach ($this->user_service->all() as $user) { 122 if ($user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== 'none') { 123 foreach ($this->tree_service->all() as $tmp_tree) { 124 if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 125 I18N::init($user->getPreference(UserInterface::PREF_LANGUAGE)); 126 127 $this->email_service->send( 128 new SiteUser(), 129 $user, 130 new TreeUser($tmp_tree), 131 I18N::translate('Pending changes'), 132 view('emails/pending-changes-text', [ 133 'tree' => $tmp_tree, 134 'user' => $user, 135 ]), 136 view('emails/pending-changes-html', [ 137 'tree' => $tmp_tree, 138 'user' => $user, 139 ]) 140 ); 141 } 142 } 143 } 144 } 145 I18N::init($old_language); 146 Site::setPreference('LAST_CHANGE_EMAIL', (string) Carbon::now()->unix()); 147 } 148 } 149 if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 150 $content = ''; 151 if (Auth::isModerator($tree)) { 152 $content .= '<a href="' . e(route(PendingChanges::class, ['tree' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 153 } 154 if ($sendmail) { 155 $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL')); 156 $next_email_timestamp = $last_email_timestamp->copy()->addDays($days); 157 158 $content .= I18N::translate('Last email reminder was sent ') . view('components/datetime', ['timestamp' => $last_email_timestamp]) . '<br>'; 159 $content .= I18N::translate('Next email reminder will be sent after ') . view('components/datetime', ['timestamp' => $next_email_timestamp]) . '<br><br>'; 160 } 161 $content .= '<ul>'; 162 163 $changes = DB::table('change') 164 ->where('gedcom_id', '=', $tree->id()) 165 ->where('status', '=', 'pending') 166 ->select(['xref']) 167 ->get(); 168 169 foreach ($changes as $change) { 170 $record = Registry::gedcomRecordFactory()->make($change->xref, $tree); 171 if ($record->canShow()) { 172 $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>'; 173 } 174 } 175 $content .= '</ul>'; 176 177 if ($context !== self::CONTEXT_EMBED) { 178 return view('modules/block-template', [ 179 'block' => Str::kebab($this->name()), 180 'id' => $block_id, 181 'config_url' => $this->configUrl($tree, $context, $block_id), 182 'title' => $this->title(), 183 'content' => $content, 184 ]); 185 } 186 187 return $content; 188 } 189 190 return ''; 191 } 192 193 /** 194 * Should this block load asynchronously using AJAX? 195 * 196 * Simple blocks are faster in-line, more complex ones can be loaded later. 197 * 198 * @return bool 199 */ 200 public function loadAjax(): bool 201 { 202 return false; 203 } 204 205 /** 206 * Can this block be shown on the user’s home page? 207 * 208 * @return bool 209 */ 210 public function isUserBlock(): bool 211 { 212 return true; 213 } 214 215 /** 216 * Can this block be shown on the tree’s home page? 217 * 218 * @return bool 219 */ 220 public function isTreeBlock(): bool 221 { 222 return true; 223 } 224 225 /** 226 * Update the configuration for a block. 227 * 228 * @param ServerRequestInterface $request 229 * @param int $block_id 230 * 231 * @return void 232 */ 233 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 234 { 235 $params = (array) $request->getParsedBody(); 236 237 $this->setBlockSetting($block_id, 'days', $params['days']); 238 $this->setBlockSetting($block_id, 'sendmail', $params['sendmail']); 239 } 240 241 /** 242 * An HTML form to edit block settings 243 * 244 * @param Tree $tree 245 * @param int $block_id 246 * 247 * @return string 248 */ 249 public function editBlockConfiguration(Tree $tree, int $block_id): string 250 { 251 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 252 $days = $this->getBlockSetting($block_id, 'days', '1'); 253 254 return view('modules/review_changes/config', [ 255 'days' => $days, 256 'sendmail' => $sendmail, 257 ]); 258 } 259} 260