xref: /webtrees/app/Module/ReviewChangesModule.php (revision ad2f0e13109a624a158c7532c312bc08f1d45af8)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2015 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\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsDate;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Mail;
26use Fisharebest\Webtrees\Site;
27use Fisharebest\Webtrees\Theme;
28use Fisharebest\Webtrees\Tree;
29use Fisharebest\Webtrees\User;
30
31/**
32 * Class ReviewChangesModule
33 */
34class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface {
35	/** {@inheritdoc} */
36	public function getTitle() {
37		return /* I18N: Name of a module */ I18N::translate('Pending changes');
38	}
39
40	/** {@inheritdoc} */
41	public function getDescription() {
42		return /* I18N: Description of the “Pending changes” module */ I18N::translate('A list of changes that need moderator approval, and email notifications.');
43	}
44
45	/**
46	 * Generate the HTML content of this block.
47	 *
48	 * @param int      $block_id
49	 * @param bool     $template
50	 * @param string[] $cfg
51	 *
52	 * @return string
53	 */
54	public function getBlock($block_id, $template = true, $cfg = array()) {
55		global $ctype, $WT_TREE;
56
57		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
58		$days     = $this->getBlockSetting($block_id, 'days', '1');
59		$block    = $this->getBlockSetting($block_id, 'block', '1');
60
61		foreach (array('days', 'sendmail', 'block') as $name) {
62			if (array_key_exists($name, $cfg)) {
63				$$name = $cfg[$name];
64			}
65		}
66
67		$changes = Database::prepare(
68			"SELECT 1" .
69			" FROM `##change`" .
70			" WHERE status='pending'" .
71			" LIMIT 1"
72		)->fetchOne();
73
74		if ($changes === '1' && $sendmail === '1') {
75			// There are pending changes - tell moderators/managers/administrators about them.
76			if (WT_TIMESTAMP - Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
77				// Which users have pending changes?
78				foreach (User::all() as $user) {
79					if ($user->getPreference('contactmethod') !== 'none') {
80						foreach (Tree::getAll() as $tree) {
81							if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) {
82								I18N::init($user->getPreference('language'));
83								Mail::systemMessage(
84									$tree,
85									$user,
86									I18N::translate('Pending changes'),
87									I18N::translate('There are pending changes for you to moderate.') .
88									Mail::EOL . Mail::EOL .
89									'<a href="' . WT_BASE_URL . 'index.php?ged=' . $WT_TREE->getNameUrl() . '">' . WT_BASE_URL . 'index.php?ged=' . $WT_TREE->getNameUrl() . '</a>'
90								);
91								I18N::init(WT_LOCALE);
92							}
93						}
94					}
95				}
96				Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP);
97			}
98		}
99		if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) {
100			$id    = $this->getName() . $block_id;
101			$class = $this->getName() . '_block';
102			if ($ctype === 'user' || Auth::isManager($WT_TREE)) {
103				$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
104			} else {
105				$title = '';
106			}
107			$title .= $this->getTitle();
108
109			$content = '';
110			if (Auth::isModerator($WT_TREE)) {
111				$content .= "<a href=\"#\" onclick=\"window.open('edit_changes.php','_blank', chan_window_specs); return false;\">" . I18N::translate('There are pending changes for you to moderate.') . "</a><br>";
112			}
113			if ($sendmail === '1') {
114				$content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . "<br>";
115				$content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . "<br><br>";
116			}
117			$content .= '<ul>';
118			$changes = Database::prepare(
119				"SELECT xref" .
120				" FROM  `##change`" .
121				" WHERE status='pending'" .
122				" AND   gedcom_id=?" .
123				" GROUP BY xref"
124			)->execute(array($WT_TREE->getTreeId()))->fetchAll();
125			foreach ($changes as $change) {
126				$record = GedcomRecord::getInstance($change->xref, $WT_TREE);
127				if ($record->canShow()) {
128					$content .= '<li><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></li>';
129				}
130			}
131			$content .= '</ul>';
132
133			if ($template) {
134				if ($block) {
135					$class .= ' small_inner_block';
136				}
137
138				return Theme::theme()->formatBlock($id, $title, $class, $content);
139			} else {
140				return $content;
141			}
142		}
143	}
144
145	/** {@inheritdoc} */
146	public function loadAjax() {
147		return false;
148	}
149
150	/** {@inheritdoc} */
151	public function isUserBlock() {
152		return true;
153	}
154
155	/** {@inheritdoc} */
156	public function isGedcomBlock() {
157		return true;
158	}
159
160	/**
161	 * An HTML form to edit block settings
162	 *
163	 * @param int $block_id
164	 */
165	public function configureBlock($block_id) {
166		if (Filter::postBool('save') && Filter::checkCsrf()) {
167			$this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1));
168			$this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail'));
169			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
170		}
171
172		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
173		$days     = $this->getBlockSetting($block_id, 'days', '1');
174		$block    = $this->getBlockSetting($block_id, 'block', '1');
175
176	?>
177	<tr>
178		<td colspan="2">
179			<?php echo I18N::translate('This block will show editors a list of records with pending changes that need to be approved by a moderator. It also generates daily emails to moderators whenever pending changes exist.'); ?>
180		</td>
181	</tr>
182
183	<?php
184		echo '<tr><td class="descriptionbox wrap width33">';
185		echo I18N::translate('Send out reminder emails?');
186		echo '</td><td class="optionbox">';
187		echo FunctionsEdit::editFieldYesNo('sendmail', $sendmail);
188		echo '<br>';
189		echo I18N::translate('Reminder email frequency (days)') . "&nbsp;<input type='text' name='days' value='" . $days . "' size='2'>";
190		echo '</td></tr>';
191
192		echo '<tr><td class="descriptionbox wrap width33">';
193		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
194		echo '</td><td class="optionbox">';
195		echo FunctionsEdit::editFieldYesNo('block', $block);
196		echo '</td></tr>';
197	}
198}
199