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