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