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\Bootstrap4; 20use Fisharebest\Webtrees\Database; 21use Fisharebest\Webtrees\Filter; 22use Fisharebest\Webtrees\Functions\FunctionsDate; 23use Fisharebest\Webtrees\Functions\FunctionsEdit; 24use Fisharebest\Webtrees\GedcomRecord; 25use Fisharebest\Webtrees\Html; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Mail; 28use Fisharebest\Webtrees\Site; 29use Fisharebest\Webtrees\Tree; 30use Fisharebest\Webtrees\User; 31 32/** 33 * Class ReviewChangesModule 34 */ 35class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface { 36 /** {@inheritdoc} */ 37 public function getTitle() { 38 return /* I18N: Name of a module */ I18N::translate('Pending changes'); 39 } 40 41 /** {@inheritdoc} */ 42 public function getDescription() { 43 return /* I18N: Description of the “Pending changes” module */ I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 44 } 45 46 /** 47 * Generate the HTML content of this block. 48 * 49 * @param int $block_id 50 * @param bool $template 51 * @param string[] $cfg 52 * 53 * @return string 54 */ 55 public function getBlock($block_id, $template = true, $cfg = []): string { 56 global $ctype, $WT_TREE; 57 58 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 59 $days = $this->getBlockSetting($block_id, 'days', '1'); 60 61 foreach (['days', 'sendmail'] as $name) { 62 if (array_key_exists($name, $cfg)) { 63 $$name = $cfg[$name]; 64 } 65 } 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 $tree) { 81 if ($tree->hasPendingEdit() && Auth::isManager($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' => $WT_TREE->getTitle(), 89 'email' => $WT_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', ['tree' => $tree, 'user' => $user]), 99 view('emails/pending-changes-html', ['tree' => $tree, 'user' => $user]) 100 ); 101 I18N::init(WT_LOCALE); 102 } 103 } 104 } 105 } 106 Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP); 107 } 108 } 109 if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) { 110 $content = ''; 111 if (Auth::isModerator($WT_TREE)) { 112 $content .= '<a href="edit_changes.php">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 113 } 114 if ($sendmail === '1') { 115 $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . '<br>'; 116 $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . '<br><br>'; 117 } 118 $content .= '<ul>'; 119 $changes = Database::prepare( 120 "SELECT xref" . 121 " FROM `##change`" . 122 " WHERE status='pending'" . 123 " AND gedcom_id=?" . 124 " GROUP BY xref" 125 )->execute([$WT_TREE->getTreeId()])->fetchAll(); 126 foreach ($changes as $change) { 127 $record = GedcomRecord::getInstance($change->xref, $WT_TREE); 128 if ($record->canShow()) { 129 $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>'; 130 } 131 } 132 $content .= '</ul>'; 133 134 if ($template) { 135 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 136 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 137 } else { 138 $config_url = ''; 139 } 140 141 return view('blocks/template', [ 142 'block' => str_replace('_', '-', $this->getName()), 143 'id' => $block_id, 144 'config_url' => $config_url, 145 'title' => $this->getTitle(), 146 'content' => $content, 147 ]); 148 } else { 149 return $content; 150 } 151 } 152 153 return ''; 154 } 155 156 /** {@inheritdoc} */ 157 public function loadAjax(): bool { 158 return false; 159 } 160 161 /** {@inheritdoc} */ 162 public function isUserBlock(): bool { 163 return true; 164 } 165 166 /** {@inheritdoc} */ 167 public function isGedcomBlock(): bool { 168 return true; 169 } 170 171 /** 172 * An HTML form to edit block settings 173 * 174 * @param int $block_id 175 * 176 * @return void 177 */ 178 public function configureBlock($block_id) { 179 if (Filter::postBool('save') && Filter::checkCsrf()) { 180 $this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1)); 181 $this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail')); 182 } 183 184 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 185 $days = $this->getBlockSetting($block_id, 'days', '1'); 186 187 ?> 188 <p> 189 <?= I18N::translate('This block will show editors a list of records with pending changes that need to be reviewed by a moderator. It also generates daily emails to moderators whenever pending changes exist.') ?> 190 </p> 191 192 <fieldset class="form-group"> 193 <div class="row"> 194 <legend class="col-form-label col-sm-3"> 195 <?= /* I18N: Label for a configuration option */ I18N::translate('Send out reminder emails') ?> 196 </legend> 197 <div class="col-sm-9"> 198 <?= Bootstrap4::radioButtons('sendmail', FunctionsEdit::optionsNoYes(), $sendmail, true) ?> 199 </div> 200 </div> 201 </fieldset> 202 203 <div class="form-group row"> 204 <label class="col-sm-3 col-form-label" for="days"> 205 <?= I18N::translate('Reminder email frequency (days)') ?> 206 </label> 207 <div class="col-sm-9"> 208 <input class="form-control" type="text" name="days" id="days" value="<?= $days ?>"> 209 </div> 210 </div> 211 <?php 212 } 213} 214