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 Mail::send( 82 User::userFromTree($tmp_tree), 83 $user, 84 User::userFromTree($tmp_tree), 85 I18N::translate('Pending changes'), 86 view('emails/pending-changes-text', [ 87 'tree' => $tmp_tree, 88 'user' => $user, 89 ]), 90 view('emails/pending-changes-html', [ 91 'tree' => $tmp_tree, 92 'user' => $user, 93 ]) 94 ); 95 I18N::init(WT_LOCALE); 96 } 97 } 98 } 99 } 100 Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP); 101 } 102 } 103 if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 104 $content = ''; 105 if (Auth::isModerator($tree)) { 106 $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 107 } 108 if ($sendmail === '1') { 109 $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL'); 110 $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>'; 111 $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>'; 112 } 113 $content .= '<ul>'; 114 115 $changes = DB::table('change') 116 ->where('gedcom_id', '=', $tree->id()) 117 ->select(['xref']) 118 ->get(); 119 120 foreach ($changes as $change) { 121 $record = GedcomRecord::getInstance($change->xref, $tree); 122 if ($record->canShow()) { 123 $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>'; 124 } 125 } 126 $content .= '</ul>'; 127 128 if ($ctype !== '') { 129 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 130 $config_url = route('tree-page-block-edit', [ 131 'block_id' => $block_id, 132 'ged' => $tree->name(), 133 ]); 134 } elseif ($ctype === 'user' && Auth::check()) { 135 $config_url = route('user-page-block-edit', [ 136 'block_id' => $block_id, 137 'ged' => $tree->name(), 138 ]); 139 } else { 140 $config_url = ''; 141 } 142 143 return view('modules/block-template', [ 144 'block' => str_replace('_', '-', $this->getName()), 145 'id' => $block_id, 146 'config_url' => $config_url, 147 'title' => $this->getTitle(), 148 'content' => $content, 149 ]); 150 } 151 152 return $content; 153 } 154 155 return ''; 156 } 157 158 /** {@inheritdoc} */ 159 public function loadAjax(): bool 160 { 161 return false; 162 } 163 164 /** {@inheritdoc} */ 165 public function isUserBlock(): bool 166 { 167 return true; 168 } 169 170 /** {@inheritdoc} */ 171 public function isGedcomBlock(): bool 172 { 173 return true; 174 } 175 176 /** 177 * Update the configuration for a block. 178 * 179 * @param Request $request 180 * @param int $block_id 181 * 182 * @return void 183 */ 184 public function saveBlockConfiguration(Request $request, int $block_id) 185 { 186 $this->setBlockSetting($block_id, 'days', $request->get('num', '1')); 187 $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', '')); 188 } 189 190 /** 191 * An HTML form to edit block settings 192 * 193 * @param Tree $tree 194 * @param int $block_id 195 * 196 * @return void 197 */ 198 public function editBlockConfiguration(Tree $tree, int $block_id) 199 { 200 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 201 $days = $this->getBlockSetting($block_id, 'days', '1'); 202 203 echo view('modules/review_changes/config', [ 204 'days' => $days, 205 'sendmail' => $sendmail, 206 ]); 207 } 208} 209