xref: /webtrees/app/Module/ReviewChangesModule.php (revision 8898179db9ee9ab76e7912468223d7b1fdcddfbc)
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 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 identified in the control panel, 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 = (bool) $this->getBlockSetting($block_id, 'sendmail', '1');
89        $days     = (int) $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) {
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()->unix());
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) {
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 ') . view('components/datetime', ['timestamp' => $last_email_timestamp]) . '<br>';
142                $content .= I18N::translate('Next email reminder will be sent after ') . view('components/datetime', ['timestamp' => $next_email_timestamp]) . '<br><br>';
143            }
144            $content .= '<ul>';
145
146            $changes = DB::table('change')
147                ->where('gedcom_id', '=', $tree->id())
148                ->where('status', '=', 'pending')
149                ->select(['xref'])
150                ->get();
151
152            foreach ($changes as $change) {
153                $record = GedcomRecord::getInstance($change->xref, $tree);
154                if ($record->canShow()) {
155                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>';
156                }
157            }
158            $content .= '</ul>';
159
160            if ($ctype !== '') {
161                if ($ctype === 'gedcom' && Auth::isManager($tree)) {
162                    $config_url = route('tree-page-block-edit', [
163                        'block_id' => $block_id,
164                        'ged'      => $tree->name(),
165                    ]);
166                } elseif ($ctype === 'user' && Auth::check()) {
167                    $config_url = route('user-page-block-edit', [
168                        'block_id' => $block_id,
169                        'ged'      => $tree->name(),
170                    ]);
171                } else {
172                    $config_url = '';
173                }
174
175                return view('modules/block-template', [
176                    'block'      => str_replace('_', '-', $this->name()),
177                    'id'         => $block_id,
178                    'config_url' => $config_url,
179                    'title'      => $this->title(),
180                    'content'    => $content,
181                ]);
182            }
183
184            return $content;
185        }
186
187        return '';
188    }
189
190    /** {@inheritdoc} */
191    public function loadAjax(): bool
192    {
193        return false;
194    }
195
196    /** {@inheritdoc} */
197    public function isUserBlock(): bool
198    {
199        return true;
200    }
201
202    /** {@inheritdoc} */
203    public function isTreeBlock(): bool
204    {
205        return true;
206    }
207
208    /**
209     * Update the configuration for a block.
210     *
211     * @param Request $request
212     * @param int     $block_id
213     *
214     * @return void
215     */
216    public function saveBlockConfiguration(Request $request, int $block_id)
217    {
218        $this->setBlockSetting($block_id, 'days', $request->get('num', '1'));
219        $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', ''));
220    }
221
222    /**
223     * An HTML form to edit block settings
224     *
225     * @param Tree $tree
226     * @param int  $block_id
227     *
228     * @return void
229     */
230    public function editBlockConfiguration(Tree $tree, int $block_id)
231    {
232        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
233        $days     = $this->getBlockSetting($block_id, 'days', '1');
234
235        echo view('modules/review_changes/config', [
236            'days'     => $days,
237            'sendmail' => $sendmail,
238        ]);
239    }
240}
241