1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Functions\FunctionsDate; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Mail; 25use Fisharebest\Webtrees\Site; 26use Fisharebest\Webtrees\Tree; 27use Fisharebest\Webtrees\User; 28use Illuminate\Database\Capsule\Manager as DB; 29use Symfony\Component\HttpFoundation\Request; 30 31/** 32 * Class ReviewChangesModule 33 */ 34class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface 35{ 36 /** {@inheritdoc} */ 37 public function getTitle(): string 38 { 39 /* I18N: Name of a module */ 40 return I18N::translate('Pending changes'); 41 } 42 43 /** {@inheritdoc} */ 44 public function getDescription(): string 45 { 46 /* I18N: Description of the “Pending changes” module */ 47 return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 48 } 49 50 /** 51 * Generate the HTML content of this block. 52 * 53 * @param Tree $tree 54 * @param int $block_id 55 * @param string $ctype 56 * @param string[] $cfg 57 * 58 * @return string 59 */ 60 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 61 { 62 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 63 $days = $this->getBlockSetting($block_id, 'days', '1'); 64 65 extract($cfg, EXTR_OVERWRITE); 66 67 $changes_exist = DB::table('change') 68 ->where('status', 'pending') 69 ->exists(); 70 71 if ($changes_exist && $sendmail === '1') { 72 // There are pending changes - tell moderators/managers/administrators about them. 73 if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) { 74 // Which users have pending changes? 75 foreach (User::all() as $user) { 76 if ($user->getPreference('contactmethod') !== 'none') { 77 foreach (Tree::getAll() as $tmp_tree) { 78 if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 79 I18N::init($user->getPreference('language')); 80 81 $sender = new User( 82 (object) [ 83 'user_id' => null, 84 'user_name' => '', 85 'real_name' => $tmp_tree->title(), 86 'email' => $tmp_tree->getPreference('WEBTREES_EMAIL'), 87 ] 88 ); 89 90 Mail::send( 91 $sender, 92 $user, 93 $sender, 94 I18N::translate('Pending changes'), 95 view('emails/pending-changes-text', [ 96 'tree' => $tmp_tree, 97 'user' => $user, 98 ]), 99 view('emails/pending-changes-html', [ 100 'tree' => $tmp_tree, 101 'user' => $user, 102 ]) 103 ); 104 I18N::init(WT_LOCALE); 105 } 106 } 107 } 108 } 109 Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP); 110 } 111 } 112 if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 113 $content = ''; 114 if (Auth::isModerator($tree)) { 115 $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 116 } 117 if ($sendmail === '1') { 118 $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL'); 119 $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>'; 120 $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>'; 121 } 122 $content .= '<ul>'; 123 124 $changes = DB::table('change') 125 ->where('gedcom_id', '=', $tree->id()) 126 ->select(['xref']) 127 ->get(); 128 129 foreach ($changes as $change) { 130 $record = GedcomRecord::getInstance($change->xref, $tree); 131 if ($record->canShow()) { 132 $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>'; 133 } 134 } 135 $content .= '</ul>'; 136 137 if ($ctype !== '') { 138 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 139 $config_url = route('tree-page-block-edit', [ 140 'block_id' => $block_id, 141 'ged' => $tree->name(), 142 ]); 143 } elseif ($ctype === 'user' && Auth::check()) { 144 $config_url = route('user-page-block-edit', [ 145 'block_id' => $block_id, 146 'ged' => $tree->name(), 147 ]); 148 } else { 149 $config_url = ''; 150 } 151 152 return view('modules/block-template', [ 153 'block' => str_replace('_', '-', $this->getName()), 154 'id' => $block_id, 155 'config_url' => $config_url, 156 'title' => $this->getTitle(), 157 'content' => $content, 158 ]); 159 } 160 161 return $content; 162 } 163 164 return ''; 165 } 166 167 /** {@inheritdoc} */ 168 public function loadAjax(): bool 169 { 170 return false; 171 } 172 173 /** {@inheritdoc} */ 174 public function isUserBlock(): bool 175 { 176 return true; 177 } 178 179 /** {@inheritdoc} */ 180 public function isGedcomBlock(): bool 181 { 182 return true; 183 } 184 185 /** 186 * Update the configuration for a block. 187 * 188 * @param Request $request 189 * @param int $block_id 190 * 191 * @return void 192 */ 193 public function saveBlockConfiguration(Request $request, int $block_id) 194 { 195 $this->setBlockSetting($block_id, 'days', $request->get('num', '1')); 196 $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', '')); 197 } 198 199 /** 200 * An HTML form to edit block settings 201 * 202 * @param Tree $tree 203 * @param int $block_id 204 * 205 * @return void 206 */ 207 public function editBlockConfiguration(Tree $tree, int $block_id) 208 { 209 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 210 $days = $this->getBlockSetting($block_id, 'days', '1'); 211 212 echo view('modules/review_changes/config', [ 213 'days' => $days, 214 'sendmail' => $sendmail, 215 ]); 216 } 217} 218