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