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