xref: /webtrees/app/Module/ReviewChangesModule.php (revision c385536d61a9b9bed37314ecc08afa8429a269ef)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\Functions\FunctionsDate;
23use Fisharebest\Webtrees\Functions\FunctionsEdit;
24use Fisharebest\Webtrees\GedcomRecord;
25use Fisharebest\Webtrees\Html;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Mail;
28use Fisharebest\Webtrees\Site;
29use Fisharebest\Webtrees\Tree;
30use Fisharebest\Webtrees\User;
31
32/**
33 * Class ReviewChangesModule
34 */
35class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface {
36	/** {@inheritdoc} */
37	public function getTitle() {
38		return /* I18N: Name of a module */ I18N::translate('Pending changes');
39	}
40
41	/** {@inheritdoc} */
42	public function getDescription() {
43		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.');
44	}
45
46	/**
47	 * Generate the HTML content of this block.
48	 *
49	 * @param int      $block_id
50	 * @param bool     $template
51	 * @param string[] $cfg
52	 *
53	 * @return string
54	 */
55	public function getBlock($block_id, $template = true, $cfg = []): string {
56		global $ctype, $WT_TREE;
57
58		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
59		$days     = $this->getBlockSetting($block_id, 'days', '1');
60
61		extract($cfg, EXTR_OVERWRITE);
62
63		$changes = Database::prepare(
64			"SELECT 1" .
65			" FROM `##change`" .
66			" WHERE status='pending'" .
67			" LIMIT 1"
68		)->fetchOne();
69
70		if ($changes === '1' && $sendmail === '1') {
71			// There are pending changes - tell moderators/managers/administrators about them.
72			if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
73				// Which users have pending changes?
74				foreach (User::all() as $user) {
75					if ($user->getPreference('contactmethod') !== 'none') {
76						foreach (Tree::getAll() as $tree) {
77							if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) {
78								I18N::init($user->getPreference('language'));
79
80								$sender = new User(
81									(object) [
82										'user_id'   => null,
83										'user_name' => '',
84										'real_name' => $WT_TREE->getTitle(),
85										'email'     => $WT_TREE->getPreference('WEBTREES_EMAIL'),
86									]
87								);
88
89								Mail::send(
90									$sender,
91									$user,
92									$sender,
93									I18N::translate('Pending changes'),
94									view('emails/pending-changes-text', ['tree' => $tree, 'user' => $user]),
95									view('emails/pending-changes-html', ['tree' => $tree, 'user' => $user])
96								);
97								I18N::init(WT_LOCALE);
98							}
99						}
100					}
101				}
102				Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP);
103			}
104		}
105		if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) {
106			$content = '';
107			if (Auth::isModerator($WT_TREE)) {
108				$content .= '<a href="edit_changes.php">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
109			}
110			if ($sendmail === '1') {
111				$content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . '<br>';
112				$content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . '<br><br>';
113			}
114			$content .= '<ul>';
115			$changes = Database::prepare(
116				"SELECT xref" .
117				" FROM  `##change`" .
118				" WHERE status='pending'" .
119				" AND   gedcom_id=?" .
120				" GROUP BY xref"
121			)->execute([$WT_TREE->getTreeId()])->fetchAll();
122			foreach ($changes as $change) {
123				$record = GedcomRecord::getInstance($change->xref, $WT_TREE);
124				if ($record->canShow()) {
125					$content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>';
126				}
127			}
128			$content .= '</ul>';
129
130			if ($template) {
131				if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
132					$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
133				} elseif ($ctype === 'user' && Auth::check()) {
134					$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
135				} else {
136					$config_url = '';
137				}
138
139				return view('blocks/template', [
140					'block'      => str_replace('_', '-', $this->getName()),
141					'id'         => $block_id,
142					'config_url' => $config_url,
143					'title'      => $this->getTitle(),
144					'content'    => $content,
145				]);
146			} else {
147				return $content;
148			}
149		}
150
151		return '';
152	}
153
154	/** {@inheritdoc} */
155	public function loadAjax(): bool {
156		return false;
157	}
158
159	/** {@inheritdoc} */
160	public function isUserBlock(): bool {
161		return true;
162	}
163
164	/** {@inheritdoc} */
165	public function isGedcomBlock(): bool {
166		return true;
167	}
168
169	/**
170	 * An HTML form to edit block settings
171	 *
172	 * @param int $block_id
173	 *
174	 * @return void
175	 */
176	public function configureBlock($block_id) {
177		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
178			$this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1));
179			$this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail'));
180
181			return;
182		}
183
184		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
185		$days     = $this->getBlockSetting($block_id, 'days', '1');
186
187		echo view('blocks/review-changes-config', [
188			'days'     => $days,
189			'sendmail' => $sendmail,
190		]);
191	}
192}
193