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