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