1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2017 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\FontAwesome; 23use Fisharebest\Webtrees\Functions\FunctionsDate; 24use Fisharebest\Webtrees\Functions\FunctionsEdit; 25use Fisharebest\Webtrees\GedcomRecord; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Mail; 28use Fisharebest\Webtrees\Site; 29use Fisharebest\Webtrees\Theme; 30use Fisharebest\Webtrees\Tree; 31use Fisharebest\Webtrees\User; 32use Fisharebest\Webtrees\View; 33 34/** 35 * Class ReviewChangesModule 36 */ 37class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface { 38 /** {@inheritdoc} */ 39 public function getTitle() { 40 return /* I18N: Name of a module */ I18N::translate('Pending changes'); 41 } 42 43 /** {@inheritdoc} */ 44 public function getDescription() { 45 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.'); 46 } 47 48 /** 49 * Generate the HTML content of this block. 50 * 51 * @param int $block_id 52 * @param bool $template 53 * @param string[] $cfg 54 * 55 * @return string 56 */ 57 public function getBlock($block_id, $template = true, $cfg = []) { 58 global $ctype, $WT_TREE; 59 60 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 61 $days = $this->getBlockSetting($block_id, 'days', '1'); 62 63 foreach (['days', 'sendmail'] as $name) { 64 if (array_key_exists($name, $cfg)) { 65 $$name = $cfg[$name]; 66 } 67 } 68 69 $changes = Database::prepare( 70 "SELECT 1" . 71 " FROM `##change`" . 72 " WHERE status='pending'" . 73 " LIMIT 1" 74 )->fetchOne(); 75 76 if ($changes === '1' && $sendmail === '1') { 77 // There are pending changes - tell moderators/managers/administrators about them. 78 if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) { 79 // Which users have pending changes? 80 foreach (User::all() as $user) { 81 if ($user->getPreference('contactmethod') !== 'none') { 82 foreach (Tree::getAll() as $tree) { 83 if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) { 84 I18N::init($user->getPreference('language')); 85 86 $sender = new User( 87 (object) [ 88 'user_id' => null, 89 'user_name' => '', 90 'real_name' => $WT_TREE->getTitle(), 91 'email' => $WT_TREE->getPreference('WEBTREES_EMAIL'), 92 ] 93 ); 94 95 Mail::send( 96 $sender, 97 $user, 98 $sender, 99 I18N::translate('Pending changes'), 100 View::make('emails/pending-changes-text', ['tree' => $tree, 'user' => $user]), 101 View::make('emails/pending-changes-html', ['tree' => $tree, 'user' => $user]) 102 ); 103 I18N::init(WT_LOCALE); 104 } 105 } 106 } 107 } 108 Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP); 109 } 110 } 111 if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) { 112 $id = $this->getName() . $block_id; 113 $class = $this->getName() . '_block'; 114 if ($ctype === 'user' || Auth::isManager($WT_TREE)) { 115 $title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' '; 116 } else { 117 $title = ''; 118 } 119 $title .= $this->getTitle(); 120 121 $content = ''; 122 if (Auth::isModerator($WT_TREE)) { 123 $content .= '<a href="edit_changes.php">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 124 } 125 if ($sendmail === '1') { 126 $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . '<br>'; 127 $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . '<br><br>'; 128 } 129 $content .= '<ul>'; 130 $changes = Database::prepare( 131 "SELECT xref" . 132 " FROM `##change`" . 133 " WHERE status='pending'" . 134 " AND gedcom_id=?" . 135 " GROUP BY xref" 136 )->execute([$WT_TREE->getTreeId()])->fetchAll(); 137 foreach ($changes as $change) { 138 $record = GedcomRecord::getInstance($change->xref, $WT_TREE); 139 if ($record->canShow()) { 140 $content .= '<li><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></li>'; 141 } 142 } 143 $content .= '</ul>'; 144 145 if ($template) { 146 return View::make('blocks/template', [ 147 'block' => str_replace('_', '-', $this->getName()), 148 'id' => $block_id, 149 'config_url' => '', 150 'title' => $this->getTitle(), 151 'content' => $content, 152 ]); 153 } else { 154 return $content; 155 } 156 } 157 158 return ''; 159 } 160 161 /** {@inheritdoc} */ 162 public function loadAjax() { 163 return false; 164 } 165 166 /** {@inheritdoc} */ 167 public function isUserBlock() { 168 return true; 169 } 170 171 /** {@inheritdoc} */ 172 public function isGedcomBlock() { 173 return true; 174 } 175 176 /** 177 * An HTML form to edit block settings 178 * 179 * @param int $block_id 180 */ 181 public function configureBlock($block_id) { 182 if (Filter::postBool('save') && Filter::checkCsrf()) { 183 $this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1)); 184 $this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail')); 185 } 186 187 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 188 $days = $this->getBlockSetting($block_id, 'days', '1'); 189 190 ?> 191 <p> 192 <?= 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.') ?> 193 </p> 194 195 <fieldset class="form-group"> 196 <div class="row"> 197 <legend class="col-form-legend col-sm-3"> 198 <?= /* I18N: Label for a configuration option */ I18N::translate('Send out reminder emails') ?> 199 </legend> 200 <div class="col-sm-9"> 201 <?= Bootstrap4::radioButtons('sendmail', FunctionsEdit::optionsNoYes(), $sendmail, true) ?> 202 </div> 203 </div> 204 </fieldset> 205 206 <div class="form-group row"> 207 <label class="col-sm-3 col-form-label" for="days"> 208 <?= I18N::translate('Reminder email frequency (days)') ?> 209 </label> 210 <div class="col-sm-9"> 211 <input class="form-control" type="text" name="days" id="days" value="<?= $days ?>"> 212 </div> 213 </div> 214 <?php 215 } 216} 217