18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 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 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\User; 2877654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 29a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 308c2e8227SGreg Roach 318c2e8227SGreg Roach/** 328c2e8227SGreg Roach * Class ReviewChangesModule 338c2e8227SGreg Roach */ 3449a243cbSGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 35c1010edaSGreg Roach{ 3649a243cbSGreg Roach use ModuleBlockTrait; 3749a243cbSGreg Roach 38961ec755SGreg Roach /** 39961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 40961ec755SGreg Roach * 41961ec755SGreg Roach * @return string 42961ec755SGreg Roach */ 4349a243cbSGreg Roach public function title(): string 44c1010edaSGreg Roach { 45bbb76c12SGreg Roach /* I18N: Name of a module */ 46bbb76c12SGreg Roach return I18N::translate('Pending changes'); 478c2e8227SGreg Roach } 488c2e8227SGreg Roach 49961ec755SGreg Roach /** 50961ec755SGreg Roach * A sentence describing what this module does. 51961ec755SGreg Roach * 52961ec755SGreg Roach * @return string 53961ec755SGreg Roach */ 5449a243cbSGreg Roach public function description(): string 55c1010edaSGreg Roach { 56bbb76c12SGreg Roach /* I18N: Description of the “Pending changes” module */ 57bbb76c12SGreg Roach return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.'); 588c2e8227SGreg Roach } 598c2e8227SGreg Roach 6076692c8bSGreg Roach /** 6176692c8bSGreg Roach * Generate the HTML content of this block. 6276692c8bSGreg Roach * 63e490cd80SGreg Roach * @param Tree $tree 6476692c8bSGreg Roach * @param int $block_id 655f2ae573SGreg Roach * @param string $ctype 66727f238cSGreg Roach * @param string[] $cfg 6776692c8bSGreg Roach * 6876692c8bSGreg Roach * @return string 6976692c8bSGreg Roach */ 705f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 71c1010edaSGreg Roach { 72e2a378d3SGreg Roach $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 73e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', '1'); 748c2e8227SGreg Roach 75c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 768c2e8227SGreg Roach 7777654037SGreg Roach $changes_exist = DB::table('change') 7877654037SGreg Roach ->where('status', 'pending') 7977654037SGreg Roach ->exists(); 808c2e8227SGreg Roach 8177654037SGreg Roach if ($changes_exist && $sendmail === '1') { 828c2e8227SGreg Roach // There are pending changes - tell moderators/managers/administrators about them. 8315d603e7SGreg Roach if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) { 848c2e8227SGreg Roach // Which users have pending changes? 858c2e8227SGreg Roach foreach (User::all() as $user) { 868c2e8227SGreg Roach if ($user->getPreference('contactmethod') !== 'none') { 8786bc3d8aSRico Sonntag foreach (Tree::getAll() as $tmp_tree) { 8886bc3d8aSRico Sonntag if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) { 898c2e8227SGreg Roach I18N::init($user->getPreference('language')); 908857be8bSGreg Roach 918857be8bSGreg Roach Mail::send( 928b67c11aSGreg Roach User::userFromTree($tmp_tree), 938c2e8227SGreg Roach $user, 948b67c11aSGreg Roach User::userFromTree($tmp_tree), 958c2e8227SGreg Roach I18N::translate('Pending changes'), 96c1010edaSGreg Roach view('emails/pending-changes-text', [ 9786bc3d8aSRico Sonntag 'tree' => $tmp_tree, 98c1010edaSGreg Roach 'user' => $user, 99c1010edaSGreg Roach ]), 100c1010edaSGreg Roach view('emails/pending-changes-html', [ 10186bc3d8aSRico Sonntag 'tree' => $tmp_tree, 102c1010edaSGreg Roach 'user' => $user, 103c1010edaSGreg Roach ]) 1048c2e8227SGreg Roach ); 1058c2e8227SGreg Roach I18N::init(WT_LOCALE); 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach } 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach } 110f97e27d5SGreg Roach Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP); 1118c2e8227SGreg Roach } 1128c2e8227SGreg Roach } 113e490cd80SGreg Roach if (Auth::isEditor($tree) && $tree->hasPendingEdit()) { 1148c2e8227SGreg Roach $content = ''; 115e490cd80SGreg Roach if (Auth::isModerator($tree)) { 116aa6f03bbSGreg Roach $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>'; 1178c2e8227SGreg Roach } 1188ae466efSGreg Roach if ($sendmail === '1') { 11976f666f4SGreg Roach $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL'); 12076f666f4SGreg Roach $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>'; 12176f666f4SGreg Roach $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>'; 1228c2e8227SGreg Roach } 1238c2e8227SGreg Roach $content .= '<ul>'; 12477654037SGreg Roach 12577654037SGreg Roach $changes = DB::table('change') 12677654037SGreg Roach ->where('gedcom_id', '=', $tree->id()) 12777654037SGreg Roach ->select(['xref']) 12877654037SGreg Roach ->get(); 12977654037SGreg Roach 1308c2e8227SGreg Roach foreach ($changes as $change) { 131e490cd80SGreg Roach $record = GedcomRecord::getInstance($change->xref, $tree); 1328c2e8227SGreg Roach if ($record->canShow()) { 133b1f1e4efSGreg Roach $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>'; 1348c2e8227SGreg Roach } 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach $content .= '</ul>'; 1378c2e8227SGreg Roach 1386a8879feSGreg Roach if ($ctype !== '') { 139e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 140c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 141c1010edaSGreg Roach 'block_id' => $block_id, 142aa6f03bbSGreg Roach 'ged' => $tree->name(), 143c1010edaSGreg Roach ]); 144397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 145c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 146c1010edaSGreg Roach 'block_id' => $block_id, 147aa6f03bbSGreg Roach 'ged' => $tree->name(), 148c1010edaSGreg Roach ]); 1498cbbfdceSGreg Roach } else { 1508cbbfdceSGreg Roach $config_url = ''; 1518cbbfdceSGreg Roach } 1528cbbfdceSGreg Roach 153147e99aaSGreg Roach return view('modules/block-template', [ 154*26684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1559c6524dcSGreg Roach 'id' => $block_id, 1568cbbfdceSGreg Roach 'config_url' => $config_url, 15749a243cbSGreg Roach 'title' => $this->title(), 1589c6524dcSGreg Roach 'content' => $content, 1599c6524dcSGreg Roach ]); 1608c2e8227SGreg Roach } 161b2ce94c6SRico Sonntag 162b2ce94c6SRico Sonntag return $content; 1638c2e8227SGreg Roach } 16415d603e7SGreg Roach 16515d603e7SGreg Roach return ''; 1668c2e8227SGreg Roach } 1678c2e8227SGreg Roach 1688c2e8227SGreg Roach /** {@inheritdoc} */ 169c1010edaSGreg Roach public function loadAjax(): bool 170c1010edaSGreg Roach { 1718c2e8227SGreg Roach return false; 1728c2e8227SGreg Roach } 1738c2e8227SGreg Roach 1748c2e8227SGreg Roach /** {@inheritdoc} */ 175c1010edaSGreg Roach public function isUserBlock(): bool 176c1010edaSGreg Roach { 1778c2e8227SGreg Roach return true; 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 1808c2e8227SGreg Roach /** {@inheritdoc} */ 181c1010edaSGreg Roach public function isGedcomBlock(): bool 182c1010edaSGreg Roach { 1838c2e8227SGreg Roach return true; 1848c2e8227SGreg Roach } 1858c2e8227SGreg Roach 18676692c8bSGreg Roach /** 187a45f9889SGreg Roach * Update the configuration for a block. 188a45f9889SGreg Roach * 189a45f9889SGreg Roach * @param Request $request 190a45f9889SGreg Roach * @param int $block_id 191a45f9889SGreg Roach * 192a45f9889SGreg Roach * @return void 193a45f9889SGreg Roach */ 194a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 195a45f9889SGreg Roach { 196a45f9889SGreg Roach $this->setBlockSetting($block_id, 'days', $request->get('num', '1')); 197a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', '')); 198a45f9889SGreg Roach } 199a45f9889SGreg Roach 200a45f9889SGreg Roach /** 20176692c8bSGreg Roach * An HTML form to edit block settings 20276692c8bSGreg Roach * 203e490cd80SGreg Roach * @param Tree $tree 20476692c8bSGreg Roach * @param int $block_id 205a9430be8SGreg Roach * 206a9430be8SGreg Roach * @return void 20776692c8bSGreg Roach */ 208a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 209c1010edaSGreg Roach { 210e2a378d3SGreg Roach $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1'); 211e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', '1'); 2128c2e8227SGreg Roach 213147e99aaSGreg Roach echo view('modules/review_changes/config', [ 214c385536dSGreg Roach 'days' => $days, 215c385536dSGreg Roach 'sendmail' => $sendmail, 216c385536dSGreg Roach ]); 2178c2e8227SGreg Roach } 2188c2e8227SGreg Roach} 219