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