1<?php 2namespace Fisharebest\Webtrees\Module; 3 4/** 5 * webtrees: online genealogy 6 * Copyright (C) 2015 webtrees development team 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\Functions\FunctionsDate; 22use Fisharebest\Webtrees\Functions\FunctionsEdit; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Mail; 26use Fisharebest\Webtrees\Site; 27use Fisharebest\Webtrees\Theme; 28use Fisharebest\Webtrees\Tree; 29use Fisharebest\Webtrees\User; 30 31/** 32 * Class ReviewChangesModule 33 */ 34class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface { 35 /** {@inheritdoc} */ 36 public function getTitle() { 37 return /* I18N: Name of a module */ I18N::translate('Pending changes'); 38 } 39 40 /** {@inheritdoc} */ 41 public function getDescription() { 42 return /* I18N: Description of the “Pending changes” module */ I18N::translate('A list of changes that need moderator approval, and email notifications.'); 43 } 44 45 /** {@inheritdoc} */ 46 public function getBlock($block_id, $template = true, $cfg = null) { 47 global $ctype, $WT_TREE; 48 49 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 50 $days = $this->getBlockSetting($block_id, 'days', '1'); 51 $block = $this->getBlockSetting($block_id, 'block', '1'); 52 53 if ($cfg) { 54 foreach (array('days', 'sendmail', 'block') as $name) { 55 if (array_key_exists($name, $cfg)) { 56 $$name = $cfg[$name]; 57 } 58 } 59 } 60 61 $changes = Database::prepare( 62 "SELECT 1" . 63 " FROM `##change`" . 64 " WHERE status='pending'" . 65 " LIMIT 1" 66 )->fetchOne(); 67 68 if ($changes && $sendmail == 'yes') { 69 // There are pending changes - tell moderators/managers/administrators about them. 70 if (WT_TIMESTAMP - Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) { 71 // Which users have pending changes? 72 foreach (User::all() as $user) { 73 if ($user->getPreference('contactmethod') !== 'none') { 74 foreach (Tree::getAll() as $tree) { 75 if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) { 76 I18N::init($user->getPreference('language')); 77 Mail::systemMessage( 78 $tree, 79 $user, 80 I18N::translate('Pending changes'), 81 I18N::translate('There are pending changes for you to moderate.') . 82 Mail::EOL . Mail::EOL . 83 '<a href="' . WT_BASE_URL . 'index.php?ged=' . $WT_TREE->getNameUrl() . '">' . WT_BASE_URL . 'index.php?ged=' . $WT_TREE->getNameUrl() . '</a>' 84 ); 85 I18N::init(WT_LOCALE); 86 } 87 } 88 } 89 } 90 Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP); 91 } 92 } 93 if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) { 94 $id = $this->getName() . $block_id; 95 $class = $this->getName() . '_block'; 96 if ($ctype === 'user' || Auth::isManager($WT_TREE)) { 97 $title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; 98 } else { 99 $title = ''; 100 } 101 $title .= $this->getTitle(); 102 103 $content = ''; 104 if (Auth::isModerator($WT_TREE)) { 105 $content .= "<a href=\"#\" onclick=\"window.open('edit_changes.php','_blank', chan_window_specs); return false;\">" . I18N::translate('There are pending changes for you to moderate.') . "</a><br>"; 106 } 107 if ($sendmail == "yes") { 108 $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . "<br>"; 109 $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . "<br><br>"; 110 } 111 $content .= '<ul>'; 112 $changes = Database::prepare( 113 "SELECT xref" . 114 " FROM `##change`" . 115 " WHERE status='pending'" . 116 " AND gedcom_id=?" . 117 " GROUP BY xref" 118 )->execute(array($WT_TREE->getTreeId()))->fetchAll(); 119 foreach ($changes as $change) { 120 $record = GedcomRecord::getInstance($change->xref, $WT_TREE); 121 if ($record->canShow()) { 122 $content .= '<li><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></li>'; 123 } 124 } 125 $content .= '</ul>'; 126 127 if ($template) { 128 if ($block) { 129 $class .= ' small_inner_block'; 130 } 131 132 return Theme::theme()->formatBlock($id, $title, $class, $content); 133 } else { 134 return $content; 135 } 136 } 137 } 138 139 /** {@inheritdoc} */ 140 public function loadAjax() { 141 return false; 142 } 143 144 /** {@inheritdoc} */ 145 public function isUserBlock() { 146 return true; 147 } 148 149 /** {@inheritdoc} */ 150 public function isGedcomBlock() { 151 return true; 152 } 153 154 /** {@inheritdoc} */ 155 public function configureBlock($block_id) { 156 if (Filter::postBool('save') && Filter::checkCsrf()) { 157 $this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1)); 158 $this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail')); 159 $this->setBlockSetting($block_id, 'block', Filter::postBool('block')); 160 } 161 162 $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 163 $days = $this->getBlockSetting($block_id, 'days', '1'); 164 $block = $this->getBlockSetting($block_id, 'block', '1'); 165 166 ?> 167 <tr> 168 <td colspan="2"> 169 <?php echo I18N::translate('This block will show editors a list of records with pending changes that need to be approved by a moderator. It also generates daily emails to moderators whenever pending changes exist.'); ?> 170 </td> 171 </tr> 172 173 <?php 174 echo '<tr><td class="descriptionbox wrap width33">'; 175 echo I18N::translate('Send out reminder emails?'); 176 echo '</td><td class="optionbox">'; 177 echo FunctionsEdit::editFieldYesNo('sendmail', $sendmail); 178 echo '<br>'; 179 echo I18N::translate('Reminder email frequency (days)') . " <input type='text' name='days' value='" . $days . "' size='2'>"; 180 echo '</td></tr>'; 181 182 echo '<tr><td class="descriptionbox wrap width33">'; 183 echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 184 echo '</td><td class="optionbox">'; 185 echo FunctionsEdit::editFieldYesNo('block', $block); 186 echo '</td></tr>'; 187 } 188} 189