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