xref: /webtrees/app/Module/ReviewChangesModule.php (revision f7f4b984688fda35ae0a943c461ab9e8bb98e19b)
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;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
25*f7f4b984SGreg Roachuse Fisharebest\Webtrees\Html;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
31530749aaSGreg Roachuse Fisharebest\Webtrees\View;
328c2e8227SGreg Roach
338c2e8227SGreg Roach/**
348c2e8227SGreg Roach * Class ReviewChangesModule
358c2e8227SGreg Roach */
36e2a378d3SGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface {
378c2e8227SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function getTitle() {
398c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Pending changes');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
428c2e8227SGreg Roach	/** {@inheritdoc} */
438c2e8227SGreg Roach	public function getDescription() {
4474dc1ba0SGreg 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.');
458c2e8227SGreg Roach	}
468c2e8227SGreg Roach
4776692c8bSGreg Roach	/**
4876692c8bSGreg Roach	 * Generate the HTML content of this block.
4976692c8bSGreg Roach	 *
5076692c8bSGreg Roach	 * @param int      $block_id
5176692c8bSGreg Roach	 * @param bool     $template
52727f238cSGreg Roach	 * @param string[] $cfg
5376692c8bSGreg Roach	 *
5476692c8bSGreg Roach	 * @return string
5576692c8bSGreg Roach	 */
5613abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
578c2e8227SGreg Roach		global $ctype, $WT_TREE;
588c2e8227SGreg Roach
59e2a378d3SGreg Roach		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
60e2a378d3SGreg Roach		$days     = $this->getBlockSetting($block_id, 'days', '1');
618c2e8227SGreg Roach
6215d603e7SGreg Roach		foreach (['days', 'sendmail'] as $name) {
638c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
648c2e8227SGreg Roach				$$name = $cfg[$name];
658c2e8227SGreg Roach			}
668c2e8227SGreg Roach		}
678c2e8227SGreg Roach
688c2e8227SGreg Roach		$changes = Database::prepare(
698c2e8227SGreg Roach			"SELECT 1" .
708c2e8227SGreg Roach			" FROM `##change`" .
718c2e8227SGreg Roach			" WHERE status='pending'" .
728c2e8227SGreg Roach			" LIMIT 1"
738c2e8227SGreg Roach		)->fetchOne();
748c2e8227SGreg Roach
758ae466efSGreg Roach		if ($changes === '1' && $sendmail === '1') {
768c2e8227SGreg Roach			// There are pending changes - tell moderators/managers/administrators about them.
7715d603e7SGreg Roach			if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
788c2e8227SGreg Roach				// Which users have pending changes?
798c2e8227SGreg Roach				foreach (User::all() as $user) {
808c2e8227SGreg Roach					if ($user->getPreference('contactmethod') !== 'none') {
818c2e8227SGreg Roach						foreach (Tree::getAll() as $tree) {
828c2e8227SGreg Roach							if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) {
838c2e8227SGreg Roach								I18N::init($user->getPreference('language'));
848857be8bSGreg Roach
858857be8bSGreg Roach								$sender = new User(
868857be8bSGreg Roach									(object) [
878857be8bSGreg Roach										'user_id'   => null,
888857be8bSGreg Roach										'user_name' => '',
898857be8bSGreg Roach										'real_name' => $WT_TREE->getTitle(),
908857be8bSGreg Roach										'email'     => $WT_TREE->getPreference('WEBTREES_EMAIL'),
918857be8bSGreg Roach									]
928857be8bSGreg Roach								);
938857be8bSGreg Roach
948857be8bSGreg Roach								Mail::send(
958857be8bSGreg Roach									$sender,
968c2e8227SGreg Roach									$user,
978857be8bSGreg Roach									$sender,
988c2e8227SGreg Roach									I18N::translate('Pending changes'),
99530749aaSGreg Roach									View::make('emails/pending-changes-text', ['tree' => $tree, 'user' => $user]),
100530749aaSGreg Roach									View::make('emails/pending-changes-html', ['tree' => $tree, 'user' => $user])
1018c2e8227SGreg Roach								);
1028c2e8227SGreg Roach								I18N::init(WT_LOCALE);
1038c2e8227SGreg Roach							}
1048c2e8227SGreg Roach						}
1058c2e8227SGreg Roach					}
1068c2e8227SGreg Roach				}
1078c2e8227SGreg Roach				Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP);
1088c2e8227SGreg Roach			}
1098c2e8227SGreg Roach		}
1108c2e8227SGreg Roach		if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) {
1118c2e8227SGreg Roach			$content = '';
1128c2e8227SGreg Roach			if (Auth::isModerator($WT_TREE)) {
11315d603e7SGreg Roach				$content .= '<a href="edit_changes.php">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1148c2e8227SGreg Roach			}
1158ae466efSGreg Roach			if ($sendmail === '1') {
1167a6ee1acSGreg Roach				$content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . '<br>';
117*f7f4b984SGreg Roach				$content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . '<br><br>';
1188c2e8227SGreg Roach			}
1198c2e8227SGreg Roach			$content .= '<ul>';
1208c2e8227SGreg Roach			$changes = Database::prepare(
1218c2e8227SGreg Roach				"SELECT xref" .
1228c2e8227SGreg Roach				" FROM  `##change`" .
1238c2e8227SGreg Roach				" WHERE status='pending'" .
1248c2e8227SGreg Roach				" AND   gedcom_id=?" .
1258c2e8227SGreg Roach				" GROUP BY xref"
12613abd6f3SGreg Roach			)->execute([$WT_TREE->getTreeId()])->fetchAll();
1278c2e8227SGreg Roach			foreach ($changes as $change) {
12824ec66ceSGreg Roach				$record = GedcomRecord::getInstance($change->xref, $WT_TREE);
1298c2e8227SGreg Roach				if ($record->canShow()) {
1308c2e8227SGreg Roach					$content .= '<li><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></li>';
1318c2e8227SGreg Roach				}
1328c2e8227SGreg Roach			}
1338c2e8227SGreg Roach			$content .= '</ul>';
1348c2e8227SGreg Roach
1358c2e8227SGreg Roach			if ($template) {
1368cbbfdceSGreg Roach				if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
1378cbbfdceSGreg Roach					$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1388cbbfdceSGreg Roach				} else {
1398cbbfdceSGreg Roach					$config_url = '';
1408cbbfdceSGreg Roach				}
1418cbbfdceSGreg Roach
1429c6524dcSGreg Roach				return View::make('blocks/template', [
1439c6524dcSGreg Roach					'block'      => str_replace('_', '-', $this->getName()),
1449c6524dcSGreg Roach					'id'         => $block_id,
1458cbbfdceSGreg Roach					'config_url' => $config_url,
1469c6524dcSGreg Roach					'title'      => $this->getTitle(),
1479c6524dcSGreg Roach					'content'    => $content,
1489c6524dcSGreg Roach				]);
1498c2e8227SGreg Roach			} else {
1508c2e8227SGreg Roach				return $content;
1518c2e8227SGreg Roach			}
1528c2e8227SGreg Roach		}
15315d603e7SGreg Roach
15415d603e7SGreg Roach		return '';
1558c2e8227SGreg Roach	}
1568c2e8227SGreg Roach
1578c2e8227SGreg Roach	/** {@inheritdoc} */
1588c2e8227SGreg Roach	public function loadAjax() {
1598c2e8227SGreg Roach		return false;
1608c2e8227SGreg Roach	}
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach	/** {@inheritdoc} */
1638c2e8227SGreg Roach	public function isUserBlock() {
1648c2e8227SGreg Roach		return true;
1658c2e8227SGreg Roach	}
1668c2e8227SGreg Roach
1678c2e8227SGreg Roach	/** {@inheritdoc} */
1688c2e8227SGreg Roach	public function isGedcomBlock() {
1698c2e8227SGreg Roach		return true;
1708c2e8227SGreg Roach	}
1718c2e8227SGreg Roach
17276692c8bSGreg Roach	/**
17376692c8bSGreg Roach	 * An HTML form to edit block settings
17476692c8bSGreg Roach	 *
17576692c8bSGreg Roach	 * @param int $block_id
17676692c8bSGreg Roach	 */
1778c2e8227SGreg Roach	public function configureBlock($block_id) {
1788c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
179e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1));
180e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail'));
1818c2e8227SGreg Roach		}
1828c2e8227SGreg Roach
183e2a378d3SGreg Roach		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
184e2a378d3SGreg Roach		$days     = $this->getBlockSetting($block_id, 'days', '1');
1858c2e8227SGreg Roach
1868c2e8227SGreg Roach	?>
18715d603e7SGreg Roach	<p>
18815d603e7SGreg 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.') ?>
18915d603e7SGreg Roach	</p>
1908c2e8227SGreg Roach
19115d603e7SGreg Roach	<fieldset class="form-group">
19215d603e7SGreg Roach		<div class="row">
19315d603e7SGreg Roach			<legend class="col-form-legend col-sm-3">
19415d603e7SGreg Roach				<?= /* I18N: Label for a configuration option */ I18N::translate('Send out reminder emails') ?>
19515d603e7SGreg Roach			</legend>
19615d603e7SGreg Roach			<div class="col-sm-9">
19715d603e7SGreg Roach				<?= Bootstrap4::radioButtons('sendmail', FunctionsEdit::optionsNoYes(), $sendmail, true) ?>
19815d603e7SGreg Roach			</div>
19915d603e7SGreg Roach		</div>
20015d603e7SGreg Roach	</fieldset>
20115d603e7SGreg Roach
20215d603e7SGreg Roach	<div class="form-group row">
20315d603e7SGreg Roach		<label class="col-sm-3 col-form-label" for="days">
20415d603e7SGreg Roach			<?= I18N::translate('Reminder email frequency (days)') ?>
20515d603e7SGreg Roach		</label>
20615d603e7SGreg Roach		<div class="col-sm-9">
20715d603e7SGreg Roach			<input class="form-control" type="text" name="days" id="days" value="<?= $days ?>">
20815d603e7SGreg Roach		</div>
20915d603e7SGreg Roach	</div>
2108c2e8227SGreg Roach	<?php
2118c2e8227SGreg Roach	}
2128c2e8227SGreg Roach}
213