18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database; 203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\User; 27a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 288c2e8227SGreg Roach 298c2e8227SGreg Roach/** 308c2e8227SGreg Roach * Class ReviewChangesModule 318c2e8227SGreg Roach */ 32c1010edaSGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface 33c1010edaSGreg Roach{ 348c2e8227SGreg Roach /** {@inheritdoc} */ 358f53f488SRico Sonntag public function getTitle(): string 36c1010edaSGreg Roach { 37bbb76c12SGreg Roach /* I18N: Name of a module */ 38bbb76c12SGreg Roach return I18N::translate('Pending changes'); 398c2e8227SGreg Roach } 408c2e8227SGreg Roach 418c2e8227SGreg Roach /** {@inheritdoc} */ 428f53f488SRico Sonntag public function getDescription(): string 43c1010edaSGreg Roach { 44bbb76c12SGreg Roach /* I18N: Description of the “Pending changes” module */ 45bbb76c12SGreg Roach return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 468c2e8227SGreg Roach } 478c2e8227SGreg Roach 4876692c8bSGreg Roach /** 4976692c8bSGreg Roach * Generate the HTML content of this block. 5076692c8bSGreg Roach * 51e490cd80SGreg Roach * @param Tree $tree 5276692c8bSGreg Roach * @param int $block_id 5376692c8bSGreg Roach * @param bool $template 54727f238cSGreg Roach * @param string[] $cfg 5576692c8bSGreg Roach * 5676692c8bSGreg Roach * @return string 5776692c8bSGreg Roach */ 58c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 59c1010edaSGreg Roach { 60e490cd80SGreg Roach global $ctype; 618c2e8227SGreg Roach 62e2a378d3SGreg Roach $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 63e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', '1'); 648c2e8227SGreg Roach 65c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 668c2e8227SGreg Roach 678c2e8227SGreg Roach $changes = Database::prepare( 688c2e8227SGreg Roach "SELECT 1" . 698c2e8227SGreg Roach " FROM `##change`" . 708c2e8227SGreg Roach " WHERE status='pending'" . 718c2e8227SGreg Roach " LIMIT 1" 728c2e8227SGreg Roach )->fetchOne(); 738c2e8227SGreg Roach 748ae466efSGreg Roach if ($changes === '1' && $sendmail === '1') { 758c2e8227SGreg Roach // There are pending changes - tell moderators/managers/administrators about them. 7615d603e7SGreg Roach if (WT_TIMESTAMP - (int)Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) { 778c2e8227SGreg Roach // Which users have pending changes? 788c2e8227SGreg Roach foreach (User::all() as $user) { 798c2e8227SGreg Roach if ($user->getPreference('contactmethod') !== 'none') { 8086bc3d8aSRico Sonntag foreach (Tree::getAll() as $tmp_tree) { 8186bc3d8aSRico Sonntag if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 828c2e8227SGreg Roach I18N::init($user->getPreference('language')); 838857be8bSGreg Roach 848857be8bSGreg Roach $sender = new User( 858857be8bSGreg Roach (object)[ 868857be8bSGreg Roach 'user_id' => null, 878857be8bSGreg Roach 'user_name' => '', 8886bc3d8aSRico Sonntag 'real_name' => $tmp_tree->getTitle(), 8986bc3d8aSRico Sonntag 'email' => $tmp_tree->getPreference('WEBTREES_EMAIL'), 908857be8bSGreg Roach ] 918857be8bSGreg Roach ); 928857be8bSGreg Roach 938857be8bSGreg Roach Mail::send( 948857be8bSGreg Roach $sender, 958c2e8227SGreg Roach $user, 968857be8bSGreg Roach $sender, 978c2e8227SGreg Roach I18N::translate('Pending changes'), 98c1010edaSGreg Roach view('emails/pending-changes-text', [ 9986bc3d8aSRico Sonntag 'tree' => $tmp_tree, 100c1010edaSGreg Roach 'user' => $user, 101c1010edaSGreg Roach ]), 102c1010edaSGreg Roach view('emails/pending-changes-html', [ 10386bc3d8aSRico Sonntag 'tree' => $tmp_tree, 104c1010edaSGreg Roach 'user' => $user, 105c1010edaSGreg Roach ]) 1068c2e8227SGreg Roach ); 1078c2e8227SGreg Roach I18N::init(WT_LOCALE); 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach } 1108c2e8227SGreg Roach } 1118c2e8227SGreg Roach } 1128c2e8227SGreg Roach Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP); 1138c2e8227SGreg Roach } 1148c2e8227SGreg Roach } 115e490cd80SGreg Roach if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 1168c2e8227SGreg Roach $content = ''; 117e490cd80SGreg Roach if (Auth::isModerator($tree)) { 118e490cd80SGreg Roach $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->getName()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 1198c2e8227SGreg Roach } 1208ae466efSGreg Roach if ($sendmail === '1') { 121*76f666f4SGreg Roach $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL'); 122*76f666f4SGreg Roach $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>'; 123*76f666f4SGreg Roach $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>'; 1248c2e8227SGreg Roach } 1258c2e8227SGreg Roach $content .= '<ul>'; 1268c2e8227SGreg Roach $changes = Database::prepare( 1278c2e8227SGreg Roach "SELECT xref" . 1288c2e8227SGreg Roach " FROM `##change`" . 1298c2e8227SGreg Roach " WHERE status='pending'" . 1308c2e8227SGreg Roach " AND gedcom_id=?" . 1318c2e8227SGreg Roach " GROUP BY xref" 132e490cd80SGreg Roach )->execute([$tree->getTreeId()])->fetchAll(); 1338c2e8227SGreg Roach foreach ($changes as $change) { 134e490cd80SGreg Roach $record = GedcomRecord::getInstance($change->xref, $tree); 1358c2e8227SGreg Roach if ($record->canShow()) { 136b1f1e4efSGreg Roach $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>'; 1378c2e8227SGreg Roach } 1388c2e8227SGreg Roach } 1398c2e8227SGreg Roach $content .= '</ul>'; 1408c2e8227SGreg Roach 1418c2e8227SGreg Roach if ($template) { 142e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 143c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 144c1010edaSGreg Roach 'block_id' => $block_id, 145c1010edaSGreg Roach 'ged' => $tree->getName(), 146c1010edaSGreg Roach ]); 147397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 148c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 149c1010edaSGreg Roach 'block_id' => $block_id, 150c1010edaSGreg Roach 'ged' => $tree->getName(), 151c1010edaSGreg Roach ]); 1528cbbfdceSGreg Roach } else { 1538cbbfdceSGreg Roach $config_url = ''; 1548cbbfdceSGreg Roach } 1558cbbfdceSGreg Roach 156147e99aaSGreg Roach return view('modules/block-template', [ 1579c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1589c6524dcSGreg Roach 'id' => $block_id, 1598cbbfdceSGreg Roach 'config_url' => $config_url, 1609c6524dcSGreg Roach 'title' => $this->getTitle(), 1619c6524dcSGreg Roach 'content' => $content, 1629c6524dcSGreg Roach ]); 1638c2e8227SGreg Roach } else { 1648c2e8227SGreg Roach return $content; 1658c2e8227SGreg Roach } 1668c2e8227SGreg Roach } 16715d603e7SGreg Roach 16815d603e7SGreg Roach return ''; 1698c2e8227SGreg Roach } 1708c2e8227SGreg Roach 1718c2e8227SGreg Roach /** {@inheritdoc} */ 172c1010edaSGreg Roach public function loadAjax(): bool 173c1010edaSGreg Roach { 1748c2e8227SGreg Roach return false; 1758c2e8227SGreg Roach } 1768c2e8227SGreg Roach 1778c2e8227SGreg Roach /** {@inheritdoc} */ 178c1010edaSGreg Roach public function isUserBlock(): bool 179c1010edaSGreg Roach { 1808c2e8227SGreg Roach return true; 1818c2e8227SGreg Roach } 1828c2e8227SGreg Roach 1838c2e8227SGreg Roach /** {@inheritdoc} */ 184c1010edaSGreg Roach public function isGedcomBlock(): bool 185c1010edaSGreg Roach { 1868c2e8227SGreg Roach return true; 1878c2e8227SGreg Roach } 1888c2e8227SGreg Roach 18976692c8bSGreg Roach /** 190a45f9889SGreg Roach * Update the configuration for a block. 191a45f9889SGreg Roach * 192a45f9889SGreg Roach * @param Request $request 193a45f9889SGreg Roach * @param int $block_id 194a45f9889SGreg Roach * 195a45f9889SGreg Roach * @return void 196a45f9889SGreg Roach */ 197a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 198a45f9889SGreg Roach { 199a45f9889SGreg Roach $this->setBlockSetting($block_id, 'days', $request->get('num', '1')); 200a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', '')); 201a45f9889SGreg Roach } 202a45f9889SGreg Roach 203a45f9889SGreg Roach /** 20476692c8bSGreg Roach * An HTML form to edit block settings 20576692c8bSGreg Roach * 206e490cd80SGreg Roach * @param Tree $tree 20776692c8bSGreg Roach * @param int $block_id 208a9430be8SGreg Roach * 209a9430be8SGreg Roach * @return void 21076692c8bSGreg Roach */ 211a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 212c1010edaSGreg Roach { 213e2a378d3SGreg Roach $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 214e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', '1'); 2158c2e8227SGreg Roach 216147e99aaSGreg Roach echo view('modules/review_changes/config', [ 217c385536dSGreg Roach 'days' => $days, 218c385536dSGreg Roach 'sendmail' => $sendmail, 219c385536dSGreg Roach ]); 2208c2e8227SGreg Roach } 2218c2e8227SGreg Roach} 222