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