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