1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\Functions\FunctionsDate; 21use Fisharebest\Webtrees\GedcomRecord; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Mail; 24use Fisharebest\Webtrees\Site; 25use Fisharebest\Webtrees\Tree; 26use Fisharebest\Webtrees\User; 27use Symfony\Component\HttpFoundation\Request; 28 29/** 30 * Class ReviewChangesModule 31 */ 32class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface 33{ 34 /** {@inheritdoc} */ 35 public function getTitle(): string 36 { 37 /* I18N: Name of a module */ 38 return I18N::translate('Pending changes'); 39 } 40 41 /** {@inheritdoc} */ 42 public function getDescription(): string 43 { 44 /* I18N: Description of the “Pending changes” module */ 45 return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 46 } 47 48 /** 49 * Generate the HTML content of this block. 50 * 51 * @param Tree $tree 52 * @param int $block_id 53 * @param bool $template 54 * @param string[] $cfg 55 * 56 * @return string 57 */ 58 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 59 { 60 global $ctype; 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 = Database::prepare( 68 "SELECT 1" . 69 " FROM `##change`" . 70 " WHERE status='pending'" . 71 " LIMIT 1" 72 )->fetchOne(); 73 74 if ($changes === '1' && $sendmail === '1') { 75 // There are pending changes - tell moderators/managers/administrators about them. 76 if (WT_TIMESTAMP - (int)Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) { 77 // Which users have pending changes? 78 foreach (User::all() as $user) { 79 if ($user->getPreference('contactmethod') !== 'none') { 80 foreach (Tree::getAll() as $tmp_tree) { 81 if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 82 I18N::init($user->getPreference('language')); 83 84 $sender = new User( 85 (object)[ 86 'user_id' => null, 87 'user_name' => '', 88 'real_name' => $tmp_tree->getTitle(), 89 'email' => $tmp_tree->getPreference('WEBTREES_EMAIL'), 90 ] 91 ); 92 93 Mail::send( 94 $sender, 95 $user, 96 $sender, 97 I18N::translate('Pending changes'), 98 view('emails/pending-changes-text', [ 99 'tree' => $tmp_tree, 100 'user' => $user, 101 ]), 102 view('emails/pending-changes-html', [ 103 'tree' => $tmp_tree, 104 'user' => $user, 105 ]) 106 ); 107 I18N::init(WT_LOCALE); 108 } 109 } 110 } 111 } 112 Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP); 113 } 114 } 115 if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 116 $content = ''; 117 if (Auth::isModerator($tree)) { 118 $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->getName()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 119 } 120 if ($sendmail === '1') { 121 $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . '<br>'; 122 $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp((int)Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . '<br><br>'; 123 } 124 $content .= '<ul>'; 125 $changes = Database::prepare( 126 "SELECT xref" . 127 " FROM `##change`" . 128 " WHERE status='pending'" . 129 " AND gedcom_id=?" . 130 " GROUP BY xref" 131 )->execute([$tree->getTreeId()])->fetchAll(); 132 foreach ($changes as $change) { 133 $record = GedcomRecord::getInstance($change->xref, $tree); 134 if ($record->canShow()) { 135 $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>'; 136 } 137 } 138 $content .= '</ul>'; 139 140 if ($template) { 141 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 142 $config_url = route('tree-page-block-edit', [ 143 'block_id' => $block_id, 144 'ged' => $tree->getName(), 145 ]); 146 } elseif ($ctype === 'user' && Auth::check()) { 147 $config_url = route('user-page-block-edit', [ 148 'block_id' => $block_id, 149 'ged' => $tree->getName(), 150 ]); 151 } else { 152 $config_url = ''; 153 } 154 155 return view('modules/block-template', [ 156 'block' => str_replace('_', '-', $this->getName()), 157 'id' => $block_id, 158 'config_url' => $config_url, 159 'title' => $this->getTitle(), 160 'content' => $content, 161 ]); 162 } else { 163 return $content; 164 } 165 } 166 167 return ''; 168 } 169 170 /** {@inheritdoc} */ 171 public function loadAjax(): bool 172 { 173 return false; 174 } 175 176 /** {@inheritdoc} */ 177 public function isUserBlock(): bool 178 { 179 return true; 180 } 181 182 /** {@inheritdoc} */ 183 public function isGedcomBlock(): bool 184 { 185 return true; 186 } 187 188 /** 189 * Update the configuration for a block. 190 * 191 * @param Request $request 192 * @param int $block_id 193 * 194 * @return void 195 */ 196 public function saveBlockConfiguration(Request $request, int $block_id) 197 { 198 $this->setBlockSetting($block_id, 'days', $request->get('num', '1')); 199 $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', '')); 200 } 201 202 /** 203 * An HTML form to edit block settings 204 * 205 * @param Tree $tree 206 * @param int $block_id 207 * 208 * @return void 209 */ 210 public function editBlockConfiguration(Tree $tree, int $block_id) 211 { 212 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 213 $days = $this->getBlockSetting($block_id, 'days', '1'); 214 215 echo view('modules/review_changes/config', [ 216 'days' => $days, 217 'sendmail' => $sendmail, 218 ]); 219 } 220} 221