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