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