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