xref: /webtrees/app/Module/ReviewChangesModule.php (revision 3caaa4d28e5aa6cc52c831b60066ebdc346fcd50)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail;
25e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
28e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\TreeUser;
2977654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
328c2e8227SGreg Roach
338c2e8227SGreg Roach/**
348c2e8227SGreg Roach * Class ReviewChangesModule
358c2e8227SGreg Roach */
3637eb8894SGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleBlockTrait;
3949a243cbSGreg Roach
40961ec755SGreg Roach    /**
41e5a6b4d4SGreg Roach     * @var UserService
42e5a6b4d4SGreg Roach     */
43e5a6b4d4SGreg Roach    private $user_service;
44e5a6b4d4SGreg Roach
45e5a6b4d4SGreg Roach    /**
46e5a6b4d4SGreg Roach     * ReviewChangesModule constructor.
47e5a6b4d4SGreg Roach     *
48e5a6b4d4SGreg Roach     * @param UserService $user_service
49e5a6b4d4SGreg Roach     */
50e5a6b4d4SGreg Roach    public function __construct(UserService $user_service)
51e5a6b4d4SGreg Roach    {
52e5a6b4d4SGreg Roach        $this->user_service = $user_service;
53e5a6b4d4SGreg Roach    }
54e5a6b4d4SGreg Roach
55e5a6b4d4SGreg Roach    /**
560cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
57961ec755SGreg Roach     *
58961ec755SGreg Roach     * @return string
59961ec755SGreg Roach     */
6049a243cbSGreg Roach    public function title(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Name of a module */
63bbb76c12SGreg Roach        return I18N::translate('Pending changes');
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
66961ec755SGreg Roach    /**
67961ec755SGreg Roach     * A sentence describing what this module does.
68961ec755SGreg Roach     *
69961ec755SGreg Roach     * @return string
70961ec755SGreg Roach     */
7149a243cbSGreg Roach    public function description(): string
72c1010edaSGreg Roach    {
73bbb76c12SGreg Roach        /* I18N: Description of the “Pending changes” module */
74bbb76c12SGreg Roach        return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.');
758c2e8227SGreg Roach    }
768c2e8227SGreg Roach
7776692c8bSGreg Roach    /**
7876692c8bSGreg Roach     * Generate the HTML content of this block.
7976692c8bSGreg Roach     *
80e490cd80SGreg Roach     * @param Tree     $tree
8176692c8bSGreg Roach     * @param int      $block_id
82*3caaa4d2SGreg Roach     * @param string   $context
83*3caaa4d2SGreg Roach     * @param string[] $config
8476692c8bSGreg Roach     *
8576692c8bSGreg Roach     * @return string
8676692c8bSGreg Roach     */
87*3caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
88c1010edaSGreg Roach    {
894459dc9aSGreg Roach        $sendmail = (bool) $this->getBlockSetting($block_id, 'sendmail', '1');
904459dc9aSGreg Roach        $days     = (int) $this->getBlockSetting($block_id, 'days', '1');
918c2e8227SGreg Roach
92*3caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
938c2e8227SGreg Roach
9477654037SGreg Roach        $changes_exist = DB::table('change')
9577654037SGreg Roach            ->where('status', 'pending')
9677654037SGreg Roach            ->exists();
978c2e8227SGreg Roach
984459dc9aSGreg Roach        if ($changes_exist && $sendmail) {
99ad98d39dSGreg Roach            $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL'));
100ad98d39dSGreg Roach            $next_email_timestamp = $last_email_timestamp->addDays($days);
101ad98d39dSGreg Roach
1028c2e8227SGreg Roach            // There are pending changes - tell moderators/managers/administrators about them.
103ad98d39dSGreg Roach            if ($next_email_timestamp < Carbon::now()) {
1048c2e8227SGreg Roach                // Which users have pending changes?
105e5a6b4d4SGreg Roach                foreach ($this->user_service->all() as $user) {
1068c2e8227SGreg Roach                    if ($user->getPreference('contactmethod') !== 'none') {
10786bc3d8aSRico Sonntag                        foreach (Tree::getAll() as $tmp_tree) {
10886bc3d8aSRico Sonntag                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) {
1098c2e8227SGreg Roach                                I18N::init($user->getPreference('language'));
1108857be8bSGreg Roach
1118857be8bSGreg Roach                                Mail::send(
112e5a6b4d4SGreg Roach                                    new TreeUser($tmp_tree),
1138c2e8227SGreg Roach                                    $user,
114e5a6b4d4SGreg Roach                                    new TreeUser($tmp_tree),
1158c2e8227SGreg Roach                                    I18N::translate('Pending changes'),
116c1010edaSGreg Roach                                    view('emails/pending-changes-text', [
11786bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
118c1010edaSGreg Roach                                        'user' => $user,
119c1010edaSGreg Roach                                    ]),
120c1010edaSGreg Roach                                    view('emails/pending-changes-html', [
12186bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
122c1010edaSGreg Roach                                        'user' => $user,
123c1010edaSGreg Roach                                    ])
1248c2e8227SGreg Roach                                );
1258c2e8227SGreg Roach                                I18N::init(WT_LOCALE);
1268c2e8227SGreg Roach                            }
1278c2e8227SGreg Roach                        }
1288c2e8227SGreg Roach                    }
1298c2e8227SGreg Roach                }
1304459dc9aSGreg Roach                Site::setPreference('LAST_CHANGE_EMAIL', (string) Carbon::now()->unix());
1318c2e8227SGreg Roach            }
1328c2e8227SGreg Roach        }
133e490cd80SGreg Roach        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
1348c2e8227SGreg Roach            $content = '';
135e490cd80SGreg Roach            if (Auth::isModerator($tree)) {
136aa6f03bbSGreg Roach                $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1378c2e8227SGreg Roach            }
1384459dc9aSGreg Roach            if ($sendmail) {
139ca52a408SGreg Roach                $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL'));
140ca52a408SGreg Roach                $next_email_timestamp = $last_email_timestamp->copy()->addDays($days);
141ca52a408SGreg Roach
1424459dc9aSGreg Roach                $content .= I18N::translate('Last email reminder was sent ') . view('components/datetime', ['timestamp' => $last_email_timestamp]) . '<br>';
1434459dc9aSGreg Roach                $content .= I18N::translate('Next email reminder will be sent after ') . view('components/datetime', ['timestamp' => $next_email_timestamp]) . '<br><br>';
1448c2e8227SGreg Roach            }
1458c2e8227SGreg Roach            $content .= '<ul>';
14677654037SGreg Roach
14777654037SGreg Roach            $changes = DB::table('change')
14877654037SGreg Roach                ->where('gedcom_id', '=', $tree->id())
1498cef4ae1SGreg Roach                ->where('status', '=', 'pending')
15077654037SGreg Roach                ->select(['xref'])
15177654037SGreg Roach                ->get();
15277654037SGreg Roach
1538c2e8227SGreg Roach            foreach ($changes as $change) {
154e490cd80SGreg Roach                $record = GedcomRecord::getInstance($change->xref, $tree);
1558c2e8227SGreg Roach                if ($record->canShow()) {
15639ca88baSGreg Roach                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>';
1578c2e8227SGreg Roach                }
1588c2e8227SGreg Roach            }
1598c2e8227SGreg Roach            $content .= '</ul>';
1608c2e8227SGreg Roach
161*3caaa4d2SGreg Roach            if ($context !== self::CONTEXT_EMBED) {
162147e99aaSGreg Roach                return view('modules/block-template', [
1631e7a7a28SGreg Roach                    'block'      => Str::kebab($this->name()),
1649c6524dcSGreg Roach                    'id'         => $block_id,
165*3caaa4d2SGreg Roach                    'config_url' => $this->configUrl($tree, $context, $block_id),
16649a243cbSGreg Roach                    'title'      => $this->title(),
1679c6524dcSGreg Roach                    'content'    => $content,
1689c6524dcSGreg Roach                ]);
1698c2e8227SGreg Roach            }
170b2ce94c6SRico Sonntag
171b2ce94c6SRico Sonntag            return $content;
1728c2e8227SGreg Roach        }
17315d603e7SGreg Roach
17415d603e7SGreg Roach        return '';
1758c2e8227SGreg Roach    }
1768c2e8227SGreg Roach
177*3caaa4d2SGreg Roach    /**
178*3caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
179*3caaa4d2SGreg Roach     *
180*3caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
181*3caaa4d2SGreg Roach     *
182*3caaa4d2SGreg Roach     * @return bool
183*3caaa4d2SGreg Roach     */
184c1010edaSGreg Roach    public function loadAjax(): bool
185c1010edaSGreg Roach    {
1868c2e8227SGreg Roach        return false;
1878c2e8227SGreg Roach    }
1888c2e8227SGreg Roach
189*3caaa4d2SGreg Roach    /**
190*3caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
191*3caaa4d2SGreg Roach     *
192*3caaa4d2SGreg Roach     * @return bool
193*3caaa4d2SGreg Roach     */
194c1010edaSGreg Roach    public function isUserBlock(): bool
195c1010edaSGreg Roach    {
1968c2e8227SGreg Roach        return true;
1978c2e8227SGreg Roach    }
1988c2e8227SGreg Roach
199*3caaa4d2SGreg Roach    /**
200*3caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
201*3caaa4d2SGreg Roach     *
202*3caaa4d2SGreg Roach     * @return bool
203*3caaa4d2SGreg Roach     */
20463276d8fSGreg Roach    public function isTreeBlock(): bool
205c1010edaSGreg Roach    {
2068c2e8227SGreg Roach        return true;
2078c2e8227SGreg Roach    }
2088c2e8227SGreg Roach
20976692c8bSGreg Roach    /**
210a45f9889SGreg Roach     * Update the configuration for a block.
211a45f9889SGreg Roach     *
2126ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
213a45f9889SGreg Roach     * @param int     $block_id
214a45f9889SGreg Roach     *
215a45f9889SGreg Roach     * @return void
216a45f9889SGreg Roach     */
2176ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
218a45f9889SGreg Roach    {
219b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
220b6b9dcc9SGreg Roach
221b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
222b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'sendmail', $params['sendmail']);
223a45f9889SGreg Roach    }
224a45f9889SGreg Roach
225a45f9889SGreg Roach    /**
22676692c8bSGreg Roach     * An HTML form to edit block settings
22776692c8bSGreg Roach     *
228e490cd80SGreg Roach     * @param Tree $tree
22976692c8bSGreg Roach     * @param int  $block_id
230a9430be8SGreg Roach     *
231*3caaa4d2SGreg Roach     * @return string
23276692c8bSGreg Roach     */
233*3caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
234c1010edaSGreg Roach    {
235e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
236e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
2378c2e8227SGreg Roach
238*3caaa4d2SGreg Roach        return view('modules/review_changes/config', [
239c385536dSGreg Roach            'days'     => $days,
240c385536dSGreg Roach            'sendmail' => $sendmail,
241c385536dSGreg Roach        ]);
2428c2e8227SGreg Roach    }
2438c2e8227SGreg Roach}
244