xref: /webtrees/app/Module/ReviewChangesModule.php (revision 8857be8be4ae0e4a5b302164a217fc463c26b735)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 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;
1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
2215d603e7SGreg Roachuse Fisharebest\Webtrees\FontAwesome;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
32530749aaSGreg Roachuse Fisharebest\Webtrees\View;
338c2e8227SGreg Roach
348c2e8227SGreg Roach/**
358c2e8227SGreg Roach * Class ReviewChangesModule
368c2e8227SGreg Roach */
37e2a378d3SGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface {
388c2e8227SGreg Roach	/** {@inheritdoc} */
398c2e8227SGreg Roach	public function getTitle() {
408c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Pending changes');
418c2e8227SGreg Roach	}
428c2e8227SGreg Roach
438c2e8227SGreg Roach	/** {@inheritdoc} */
448c2e8227SGreg Roach	public function getDescription() {
4574dc1ba0SGreg Roach		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.');
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * Generate the HTML content of this block.
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @param int      $block_id
5276692c8bSGreg Roach	 * @param bool     $template
53727f238cSGreg Roach	 * @param string[] $cfg
5476692c8bSGreg Roach	 *
5576692c8bSGreg Roach	 * @return string
5676692c8bSGreg Roach	 */
5713abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
588c2e8227SGreg Roach		global $ctype, $WT_TREE;
598c2e8227SGreg Roach
60e2a378d3SGreg Roach		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
61e2a378d3SGreg Roach		$days     = $this->getBlockSetting($block_id, 'days', '1');
628c2e8227SGreg Roach
6315d603e7SGreg Roach		foreach (['days', 'sendmail'] as $name) {
648c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
658c2e8227SGreg Roach				$$name = $cfg[$name];
668c2e8227SGreg Roach			}
678c2e8227SGreg Roach		}
688c2e8227SGreg Roach
698c2e8227SGreg Roach		$changes = Database::prepare(
708c2e8227SGreg Roach			"SELECT 1" .
718c2e8227SGreg Roach			" FROM `##change`" .
728c2e8227SGreg Roach			" WHERE status='pending'" .
738c2e8227SGreg Roach			" LIMIT 1"
748c2e8227SGreg Roach		)->fetchOne();
758c2e8227SGreg Roach
768ae466efSGreg Roach		if ($changes === '1' && $sendmail === '1') {
778c2e8227SGreg Roach			// There are pending changes - tell moderators/managers/administrators about them.
7815d603e7SGreg Roach			if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
798c2e8227SGreg Roach				// Which users have pending changes?
808c2e8227SGreg Roach				foreach (User::all() as $user) {
818c2e8227SGreg Roach					if ($user->getPreference('contactmethod') !== 'none') {
828c2e8227SGreg Roach						foreach (Tree::getAll() as $tree) {
838c2e8227SGreg Roach							if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) {
848c2e8227SGreg Roach								I18N::init($user->getPreference('language'));
85*8857be8bSGreg Roach
86*8857be8bSGreg Roach								$sender = new User(
87*8857be8bSGreg Roach									(object) [
88*8857be8bSGreg Roach										'user_id'   => null,
89*8857be8bSGreg Roach										'user_name' => '',
90*8857be8bSGreg Roach										'real_name' => $WT_TREE->getTitle(),
91*8857be8bSGreg Roach										'email'     => $WT_TREE->getPreference('WEBTREES_EMAIL'),
92*8857be8bSGreg Roach									]
93*8857be8bSGreg Roach								);
94*8857be8bSGreg Roach
95*8857be8bSGreg Roach								Mail::send(
96*8857be8bSGreg Roach									$sender,
978c2e8227SGreg Roach									$user,
98*8857be8bSGreg Roach									$sender,
998c2e8227SGreg Roach									I18N::translate('Pending changes'),
100530749aaSGreg Roach									View::make('emails/pending-changes-text', ['tree' => $tree, 'user' => $user]),
101530749aaSGreg Roach									View::make('emails/pending-changes-html', ['tree' => $tree, 'user' => $user])
1028c2e8227SGreg Roach								);
1038c2e8227SGreg Roach								I18N::init(WT_LOCALE);
1048c2e8227SGreg Roach							}
1058c2e8227SGreg Roach						}
1068c2e8227SGreg Roach					}
1078c2e8227SGreg Roach				}
1088c2e8227SGreg Roach				Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP);
1098c2e8227SGreg Roach			}
1108c2e8227SGreg Roach		}
1118c2e8227SGreg Roach		if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) {
1128c2e8227SGreg Roach			$id    = $this->getName() . $block_id;
1138c2e8227SGreg Roach			$class = $this->getName() . '_block';
1148c2e8227SGreg Roach			if ($ctype === 'user' || Auth::isManager($WT_TREE)) {
11515d603e7SGreg Roach				$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]) . ' ';
1168c2e8227SGreg Roach			} else {
1178c2e8227SGreg Roach				$title = '';
1188c2e8227SGreg Roach			}
1198c2e8227SGreg Roach			$title .= $this->getTitle();
1208c2e8227SGreg Roach
1218c2e8227SGreg Roach			$content = '';
1228c2e8227SGreg Roach			if (Auth::isModerator($WT_TREE)) {
12315d603e7SGreg Roach				$content .= '<a href="edit_changes.php">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1248c2e8227SGreg Roach			}
1258ae466efSGreg Roach			if ($sendmail === '1') {
1267a6ee1acSGreg Roach				$content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . '<br>';
1277a6ee1acSGreg Roach				$content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . '<br><br>';
1288c2e8227SGreg Roach			}
1298c2e8227SGreg Roach			$content .= '<ul>';
1308c2e8227SGreg Roach			$changes = Database::prepare(
1318c2e8227SGreg Roach				"SELECT xref" .
1328c2e8227SGreg Roach				" FROM  `##change`" .
1338c2e8227SGreg Roach				" WHERE status='pending'" .
1348c2e8227SGreg Roach				" AND   gedcom_id=?" .
1358c2e8227SGreg Roach				" GROUP BY xref"
13613abd6f3SGreg Roach			)->execute([$WT_TREE->getTreeId()])->fetchAll();
1378c2e8227SGreg Roach			foreach ($changes as $change) {
13824ec66ceSGreg Roach				$record = GedcomRecord::getInstance($change->xref, $WT_TREE);
1398c2e8227SGreg Roach				if ($record->canShow()) {
1408c2e8227SGreg Roach					$content .= '<li><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></li>';
1418c2e8227SGreg Roach				}
1428c2e8227SGreg Roach			}
1438c2e8227SGreg Roach			$content .= '</ul>';
1448c2e8227SGreg Roach
1458c2e8227SGreg Roach			if ($template) {
1468c2e8227SGreg Roach				return Theme::theme()->formatBlock($id, $title, $class, $content);
1478c2e8227SGreg Roach			} else {
1488c2e8227SGreg Roach				return $content;
1498c2e8227SGreg Roach			}
1508c2e8227SGreg Roach		}
15115d603e7SGreg Roach
15215d603e7SGreg Roach		return '';
1538c2e8227SGreg Roach	}
1548c2e8227SGreg Roach
1558c2e8227SGreg Roach	/** {@inheritdoc} */
1568c2e8227SGreg Roach	public function loadAjax() {
1578c2e8227SGreg Roach		return false;
1588c2e8227SGreg Roach	}
1598c2e8227SGreg Roach
1608c2e8227SGreg Roach	/** {@inheritdoc} */
1618c2e8227SGreg Roach	public function isUserBlock() {
1628c2e8227SGreg Roach		return true;
1638c2e8227SGreg Roach	}
1648c2e8227SGreg Roach
1658c2e8227SGreg Roach	/** {@inheritdoc} */
1668c2e8227SGreg Roach	public function isGedcomBlock() {
1678c2e8227SGreg Roach		return true;
1688c2e8227SGreg Roach	}
1698c2e8227SGreg Roach
17076692c8bSGreg Roach	/**
17176692c8bSGreg Roach	 * An HTML form to edit block settings
17276692c8bSGreg Roach	 *
17376692c8bSGreg Roach	 * @param int $block_id
17476692c8bSGreg Roach	 */
1758c2e8227SGreg Roach	public function configureBlock($block_id) {
1768c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
177e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1));
178e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail'));
1798c2e8227SGreg Roach		}
1808c2e8227SGreg Roach
181e2a378d3SGreg Roach		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
182e2a378d3SGreg Roach		$days     = $this->getBlockSetting($block_id, 'days', '1');
1838c2e8227SGreg Roach
1848c2e8227SGreg Roach	?>
18515d603e7SGreg Roach	<p>
18615d603e7SGreg Roach		<?= 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.') ?>
18715d603e7SGreg Roach	</p>
1888c2e8227SGreg Roach
18915d603e7SGreg Roach	<fieldset class="form-group">
19015d603e7SGreg Roach		<div class="row">
19115d603e7SGreg Roach			<legend class="col-form-legend col-sm-3">
19215d603e7SGreg Roach				<?= /* I18N: Label for a configuration option */ I18N::translate('Send out reminder emails') ?>
19315d603e7SGreg Roach			</legend>
19415d603e7SGreg Roach			<div class="col-sm-9">
19515d603e7SGreg Roach				<?= Bootstrap4::radioButtons('sendmail', FunctionsEdit::optionsNoYes(), $sendmail, true) ?>
19615d603e7SGreg Roach			</div>
19715d603e7SGreg Roach		</div>
19815d603e7SGreg Roach	</fieldset>
19915d603e7SGreg Roach
20015d603e7SGreg Roach	<div class="form-group row">
20115d603e7SGreg Roach		<label class="col-sm-3 col-form-label" for="days">
20215d603e7SGreg Roach			<?= I18N::translate('Reminder email frequency (days)') ?>
20315d603e7SGreg Roach		</label>
20415d603e7SGreg Roach		<div class="col-sm-9">
20515d603e7SGreg Roach			<input class="form-control" type="text" name="days" id="days" value="<?= $days ?>">
20615d603e7SGreg Roach		</div>
20715d603e7SGreg Roach	</div>
2088c2e8227SGreg Roach	<?php
2098c2e8227SGreg Roach	}
2108c2e8227SGreg Roach}
211