xref: /webtrees/app/Module/ReviewChangesModule.php (revision 67992b6a3e78399bd33189954a5f08bb23b02503)
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            // There are pending changes - tell moderators/managers/administrators about them.
99            if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
100                // Which users have pending changes?
101                foreach ($this->user_service->all() as $user) {
102                    if ($user->getPreference('contactmethod') !== 'none') {
103                        foreach (Tree::getAll() as $tmp_tree) {
104                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) {
105                                I18N::init($user->getPreference('language'));
106
107                                Mail::send(
108                                    new TreeUser($tmp_tree),
109                                    $user,
110                                    new TreeUser($tmp_tree),
111                                    I18N::translate('Pending changes'),
112                                    view('emails/pending-changes-text', [
113                                        'tree' => $tmp_tree,
114                                        'user' => $user,
115                                    ]),
116                                    view('emails/pending-changes-html', [
117                                        'tree' => $tmp_tree,
118                                        'user' => $user,
119                                    ])
120                                );
121                                I18N::init(WT_LOCALE);
122                            }
123                        }
124                    }
125                }
126                Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP);
127            }
128        }
129        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
130            $content = '';
131            if (Auth::isModerator($tree)) {
132                $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
133            }
134            if ($sendmail === '1') {
135                $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL'));
136                $next_email_timestamp = $last_email_timestamp->copy()->addDays($days);
137
138                $content .= I18N::translate('Last email reminder was sent ') . I18N::localTime($last_email_timestamp) . '<br>';
139                $content .= I18N::translate('Next email reminder will be sent after ') . I18N::localTime($next_email_timestamp) . '<br><br>';
140            }
141            $content .= '<ul>';
142
143            $changes = DB::table('change')
144                ->where('gedcom_id', '=', $tree->id())
145                ->select(['xref'])
146                ->get();
147
148            foreach ($changes as $change) {
149                $record = GedcomRecord::getInstance($change->xref, $tree);
150                if ($record->canShow()) {
151                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>';
152                }
153            }
154            $content .= '</ul>';
155
156            if ($ctype !== '') {
157                if ($ctype === 'gedcom' && Auth::isManager($tree)) {
158                    $config_url = route('tree-page-block-edit', [
159                        'block_id' => $block_id,
160                        'ged'      => $tree->name(),
161                    ]);
162                } elseif ($ctype === 'user' && Auth::check()) {
163                    $config_url = route('user-page-block-edit', [
164                        'block_id' => $block_id,
165                        'ged'      => $tree->name(),
166                    ]);
167                } else {
168                    $config_url = '';
169                }
170
171                return view('modules/block-template', [
172                    'block'      => str_replace('_', '-', $this->name()),
173                    'id'         => $block_id,
174                    'config_url' => $config_url,
175                    'title'      => $this->title(),
176                    'content'    => $content,
177                ]);
178            }
179
180            return $content;
181        }
182
183        return '';
184    }
185
186    /** {@inheritdoc} */
187    public function loadAjax(): bool
188    {
189        return false;
190    }
191
192    /** {@inheritdoc} */
193    public function isUserBlock(): bool
194    {
195        return true;
196    }
197
198    /** {@inheritdoc} */
199    public function isTreeBlock(): bool
200    {
201        return true;
202    }
203
204    /**
205     * Update the configuration for a block.
206     *
207     * @param Request $request
208     * @param int     $block_id
209     *
210     * @return void
211     */
212    public function saveBlockConfiguration(Request $request, int $block_id)
213    {
214        $this->setBlockSetting($block_id, 'days', $request->get('num', '1'));
215        $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', ''));
216    }
217
218    /**
219     * An HTML form to edit block settings
220     *
221     * @param Tree $tree
222     * @param int  $block_id
223     *
224     * @return void
225     */
226    public function editBlockConfiguration(Tree $tree, int $block_id)
227    {
228        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
229        $days     = $this->getBlockSetting($block_id, 'days', '1');
230
231        echo view('modules/review_changes/config', [
232            'days'     => $days,
233            'sendmail' => $sendmail,
234        ]);
235    }
236}
237