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