xref: /webtrees/app/Module/ReviewChangesModule.php (revision 3fa66c660869af2f4c92ef37e06997aa8a0f55e1)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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 string   $ctype
56     * @param string[] $cfg
57     *
58     * @return string
59     */
60    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
61    {
62        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
63        $days     = $this->getBlockSetting($block_id, 'days', '1');
64
65        extract($cfg, EXTR_OVERWRITE);
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 - (int) 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 $tmp_tree) {
81                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) {
82                                I18N::init($user->getPreference('language'));
83
84                                $sender = new User(
85                                    (object) [
86                                        'user_id'   => null,
87                                        'user_name' => '',
88                                        'real_name' => $tmp_tree->title(),
89                                        'email'     => $tmp_tree->getPreference('WEBTREES_EMAIL'),
90                                    ]
91                                );
92
93                                Mail::send(
94                                    $sender,
95                                    $user,
96                                    $sender,
97                                    I18N::translate('Pending changes'),
98                                    view('emails/pending-changes-text', [
99                                        'tree' => $tmp_tree,
100                                        'user' => $user,
101                                    ]),
102                                    view('emails/pending-changes-html', [
103                                        'tree' => $tmp_tree,
104                                        'user' => $user,
105                                    ])
106                                );
107                                I18N::init(WT_LOCALE);
108                            }
109                        }
110                    }
111                }
112                Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP);
113            }
114        }
115        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
116            $content = '';
117            if (Auth::isModerator($tree)) {
118                $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
119            }
120            if ($sendmail === '1') {
121                $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL');
122                $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>';
123                $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>';
124            }
125            $content .= '<ul>';
126            $changes = Database::prepare(
127                "SELECT xref" .
128                " FROM  `##change`" .
129                " WHERE status='pending'" .
130                " AND   gedcom_id=?" .
131                " GROUP BY xref"
132            )->execute([$tree->id()])->fetchAll();
133            foreach ($changes as $change) {
134                $record = GedcomRecord::getInstance($change->xref, $tree);
135                if ($record->canShow()) {
136                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>';
137                }
138            }
139            $content .= '</ul>';
140
141            if ($ctype !== '') {
142                if ($ctype === 'gedcom' && Auth::isManager($tree)) {
143                    $config_url = route('tree-page-block-edit', [
144                        'block_id' => $block_id,
145                        'ged'      => $tree->name(),
146                    ]);
147                } elseif ($ctype === 'user' && Auth::check()) {
148                    $config_url = route('user-page-block-edit', [
149                        'block_id' => $block_id,
150                        'ged'      => $tree->name(),
151                    ]);
152                } else {
153                    $config_url = '';
154                }
155
156                return view('modules/block-template', [
157                    'block'      => str_replace('_', '-', $this->getName()),
158                    'id'         => $block_id,
159                    'config_url' => $config_url,
160                    'title'      => $this->getTitle(),
161                    'content'    => $content,
162                ]);
163            }
164
165            return $content;
166        }
167
168        return '';
169    }
170
171    /** {@inheritdoc} */
172    public function loadAjax(): bool
173    {
174        return false;
175    }
176
177    /** {@inheritdoc} */
178    public function isUserBlock(): bool
179    {
180        return true;
181    }
182
183    /** {@inheritdoc} */
184    public function isGedcomBlock(): bool
185    {
186        return true;
187    }
188
189    /**
190     * Update the configuration for a block.
191     *
192     * @param Request $request
193     * @param int     $block_id
194     *
195     * @return void
196     */
197    public function saveBlockConfiguration(Request $request, int $block_id)
198    {
199        $this->setBlockSetting($block_id, 'days', $request->get('num', '1'));
200        $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', ''));
201    }
202
203    /**
204     * An HTML form to edit block settings
205     *
206     * @param Tree $tree
207     * @param int  $block_id
208     *
209     * @return void
210     */
211    public function editBlockConfiguration(Tree $tree, int $block_id)
212    {
213        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
214        $days     = $this->getBlockSetting($block_id, 'days', '1');
215
216        echo view('modules/review_changes/config', [
217            'days'     => $days,
218            'sendmail' => $sendmail,
219        ]);
220    }
221}
222