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