xref: /webtrees/app/Module/ReviewChangesModule.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
51fe542e9SGreg Roach * Copyright (C) 2021 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2422e73debSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\PendingChanges;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2609482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
27e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService;
283df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
29e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
314c9729fbSGreg Roachuse Fisharebest\Webtrees\SiteUser;
320e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
33e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\TreeUser;
3477654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3546786cf3SGreg Roachuse Illuminate\Database\Query\Builder;
3646786cf3SGreg Roachuse Illuminate\Database\Query\Expression;
371e7a7a28SGreg Roachuse Illuminate\Support\Str;
386ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
398c2e8227SGreg Roach
40d97083feSGreg Roachuse function time;
41d97083feSGreg Roach
428c2e8227SGreg Roach/**
438c2e8227SGreg Roach * Class ReviewChangesModule
448c2e8227SGreg Roach */
4537eb8894SGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface
46c1010edaSGreg Roach{
4749a243cbSGreg Roach    use ModuleBlockTrait;
4849a243cbSGreg Roach
4943f2f523SGreg Roach    private EmailService $email_service;
500f97530bSGreg Roach
5143f2f523SGreg Roach    private UserService $user_service;
52e5a6b4d4SGreg Roach
5343f2f523SGreg Roach    private TreeService $tree_service;
543df1e584SGreg Roach
55e5a6b4d4SGreg Roach    /**
56e5a6b4d4SGreg Roach     * ReviewChangesModule constructor.
57e5a6b4d4SGreg Roach     *
58e381f98dSGreg Roach     * @param EmailService $email_service
593df1e584SGreg Roach     * @param TreeService  $tree_service
60e5a6b4d4SGreg Roach     * @param UserService  $user_service
61e5a6b4d4SGreg Roach     */
623df1e584SGreg Roach    public function __construct(
63e381f98dSGreg Roach        EmailService $email_service,
643df1e584SGreg Roach        TreeService $tree_service,
653df1e584SGreg Roach        UserService $user_service
663df1e584SGreg Roach    ) {
67e381f98dSGreg Roach        $this->email_service = $email_service;
683df1e584SGreg Roach        $this->tree_service  = $tree_service;
69e5a6b4d4SGreg Roach        $this->user_service  = $user_service;
70e5a6b4d4SGreg Roach    }
71e5a6b4d4SGreg Roach
72e5a6b4d4SGreg Roach    /**
730cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
74961ec755SGreg Roach     *
75961ec755SGreg Roach     * @return string
76961ec755SGreg Roach     */
7749a243cbSGreg Roach    public function title(): string
78c1010edaSGreg Roach    {
79bbb76c12SGreg Roach        /* I18N: Name of a module */
80bbb76c12SGreg Roach        return I18N::translate('Pending changes');
818c2e8227SGreg Roach    }
828c2e8227SGreg Roach
83961ec755SGreg Roach    /**
84961ec755SGreg Roach     * A sentence describing what this module does.
85961ec755SGreg Roach     *
86961ec755SGreg Roach     * @return string
87961ec755SGreg Roach     */
8849a243cbSGreg Roach    public function description(): string
89c1010edaSGreg Roach    {
90bbb76c12SGreg Roach        /* I18N: Description of the “Pending changes” module */
91bbb76c12SGreg Roach        return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.');
928c2e8227SGreg Roach    }
938c2e8227SGreg Roach
9476692c8bSGreg Roach    /**
9576692c8bSGreg Roach     * Generate the HTML content of this block.
9676692c8bSGreg Roach     *
97e490cd80SGreg Roach     * @param Tree                 $tree
9876692c8bSGreg Roach     * @param int                  $block_id
993caaa4d2SGreg Roach     * @param string               $context
100*76d39c55SGreg Roach     * @param array<string,string> $config
10176692c8bSGreg Roach     *
10276692c8bSGreg Roach     * @return string
10376692c8bSGreg Roach     */
1043caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
105c1010edaSGreg Roach    {
10665cf5706SGreg Roach        $old_language = I18N::languageTag();
10790a2f718SGreg Roach
1084459dc9aSGreg Roach        $sendmail = (bool) $this->getBlockSetting($block_id, 'sendmail', '1');
1094459dc9aSGreg Roach        $days     = (int) $this->getBlockSetting($block_id, 'days', '1');
1108c2e8227SGreg Roach
1113caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1128c2e8227SGreg Roach
11377654037SGreg Roach        $changes_exist = DB::table('change')
11477654037SGreg Roach            ->where('status', 'pending')
11577654037SGreg Roach            ->exists();
1168c2e8227SGreg Roach
1174459dc9aSGreg Roach        if ($changes_exist && $sendmail) {
118d97083feSGreg Roach            $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL');
119d97083feSGreg Roach            $next_email_timestamp = $last_email_timestamp + 86400 * $days;
120ad98d39dSGreg Roach
1218c2e8227SGreg Roach            // There are pending changes - tell moderators/managers/administrators about them.
122d97083feSGreg Roach            if ($next_email_timestamp < time()) {
1238c2e8227SGreg Roach                // Which users have pending changes?
124e5a6b4d4SGreg Roach                foreach ($this->user_service->all() as $user) {
1251fe542e9SGreg Roach                    if ($user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== 'none') {
1263df1e584SGreg Roach                        foreach ($this->tree_service->all() as $tmp_tree) {
12786bc3d8aSRico Sonntag                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) {
1281fe542e9SGreg Roach                                I18N::init($user->getPreference(UserInterface::PREF_LANGUAGE));
1298857be8bSGreg Roach
130e381f98dSGreg Roach                                $this->email_service->send(
1314c9729fbSGreg Roach                                    new SiteUser(),
1328c2e8227SGreg Roach                                    $user,
133e5a6b4d4SGreg Roach                                    new TreeUser($tmp_tree),
1348c2e8227SGreg Roach                                    I18N::translate('Pending changes'),
135c1010edaSGreg Roach                                    view('emails/pending-changes-text', [
13686bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
137c1010edaSGreg Roach                                        'user' => $user,
138c1010edaSGreg Roach                                    ]),
139c1010edaSGreg Roach                                    view('emails/pending-changes-html', [
14086bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
141c1010edaSGreg Roach                                        'user' => $user,
142c1010edaSGreg Roach                                    ])
1438c2e8227SGreg Roach                                );
1448c2e8227SGreg Roach                            }
1458c2e8227SGreg Roach                        }
1468c2e8227SGreg Roach                    }
1478c2e8227SGreg Roach                }
14865cf5706SGreg Roach                I18N::init($old_language);
149d97083feSGreg Roach                Site::setPreference('LAST_CHANGE_EMAIL', (string) time());
1508c2e8227SGreg Roach            }
1518c2e8227SGreg Roach        }
152e490cd80SGreg Roach        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
1538c2e8227SGreg Roach            $content = '';
154e490cd80SGreg Roach            if (Auth::isModerator($tree)) {
15522e73debSGreg Roach                $content .= '<a href="' . e(route(PendingChanges::class, ['tree' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1568c2e8227SGreg Roach            }
1574459dc9aSGreg Roach            if ($sendmail) {
158d97083feSGreg Roach                $last_email_timestamp = Registry::timestampFactory()->make((int) Site::getPreference('LAST_CHANGE_EMAIL'));
159d97083feSGreg Roach                $next_email_timestamp = $last_email_timestamp->addDays($days);
160ca52a408SGreg Roach
1614459dc9aSGreg Roach                $content .= I18N::translate('Last email reminder was sent ') . view('components/datetime', ['timestamp' => $last_email_timestamp]) . '<br>';
1624459dc9aSGreg Roach                $content .= I18N::translate('Next email reminder will be sent after ') . view('components/datetime', ['timestamp' => $next_email_timestamp]) . '<br><br>';
1638c2e8227SGreg Roach            }
1648c2e8227SGreg Roach            $content .= '<ul>';
16577654037SGreg Roach
16677654037SGreg Roach            $changes = DB::table('change')
16777654037SGreg Roach                ->where('gedcom_id', '=', $tree->id())
16846786cf3SGreg Roach                ->whereIn('change_id', static function (Builder $query) use ($tree): void {
16946786cf3SGreg Roach                    $query->select(new Expression('MAX(change_id)'))
17046786cf3SGreg Roach                        ->from('change')
17146786cf3SGreg Roach                        ->where('gedcom_id', '=', $tree->id())
1728cef4ae1SGreg Roach                        ->where('status', '=', 'pending')
17346786cf3SGreg Roach                        ->groupBy(['xref']);
17446786cf3SGreg Roach                })
17546786cf3SGreg Roach                //->select(['xref'])
17677654037SGreg Roach                ->get();
17777654037SGreg Roach
1788c2e8227SGreg Roach            foreach ($changes as $change) {
17946786cf3SGreg Roach                $record = Registry::gedcomRecordFactory()->make($change->xref, $tree, $change->new_gedcom ?: $change->old_gedcom);
1808c2e8227SGreg Roach                if ($record->canShow()) {
18139ca88baSGreg Roach                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>';
1828c2e8227SGreg Roach                }
1838c2e8227SGreg Roach            }
1848c2e8227SGreg Roach            $content .= '</ul>';
1858c2e8227SGreg Roach
1863caaa4d2SGreg Roach            if ($context !== self::CONTEXT_EMBED) {
187147e99aaSGreg Roach                return view('modules/block-template', [
1881e7a7a28SGreg Roach                    'block'      => Str::kebab($this->name()),
1899c6524dcSGreg Roach                    'id'         => $block_id,
1903caaa4d2SGreg Roach                    'config_url' => $this->configUrl($tree, $context, $block_id),
19149a243cbSGreg Roach                    'title'      => $this->title(),
1929c6524dcSGreg Roach                    'content'    => $content,
1939c6524dcSGreg Roach                ]);
1948c2e8227SGreg Roach            }
195b2ce94c6SRico Sonntag
196b2ce94c6SRico Sonntag            return $content;
1978c2e8227SGreg Roach        }
19815d603e7SGreg Roach
19915d603e7SGreg Roach        return '';
2008c2e8227SGreg Roach    }
2018c2e8227SGreg Roach
2023caaa4d2SGreg Roach    /**
2033caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2043caaa4d2SGreg Roach     *
2053caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2063caaa4d2SGreg Roach     *
2073caaa4d2SGreg Roach     * @return bool
2083caaa4d2SGreg Roach     */
209c1010edaSGreg Roach    public function loadAjax(): bool
210c1010edaSGreg Roach    {
2118c2e8227SGreg Roach        return false;
2128c2e8227SGreg Roach    }
2138c2e8227SGreg Roach
2143caaa4d2SGreg Roach    /**
2153caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2163caaa4d2SGreg Roach     *
2173caaa4d2SGreg Roach     * @return bool
2183caaa4d2SGreg Roach     */
219c1010edaSGreg Roach    public function isUserBlock(): bool
220c1010edaSGreg Roach    {
2218c2e8227SGreg Roach        return true;
2228c2e8227SGreg Roach    }
2238c2e8227SGreg Roach
2243caaa4d2SGreg Roach    /**
2253caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2263caaa4d2SGreg Roach     *
2273caaa4d2SGreg Roach     * @return bool
2283caaa4d2SGreg Roach     */
22963276d8fSGreg Roach    public function isTreeBlock(): bool
230c1010edaSGreg Roach    {
2318c2e8227SGreg Roach        return true;
2328c2e8227SGreg Roach    }
2338c2e8227SGreg Roach
23476692c8bSGreg Roach    /**
235a45f9889SGreg Roach     * Update the configuration for a block.
236a45f9889SGreg Roach     *
2376ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
238a45f9889SGreg Roach     * @param int     $block_id
239a45f9889SGreg Roach     *
240a45f9889SGreg Roach     * @return void
241a45f9889SGreg Roach     */
2426ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
243a45f9889SGreg Roach    {
244b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
245b6b9dcc9SGreg Roach
246b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
247b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'sendmail', $params['sendmail']);
248a45f9889SGreg Roach    }
249a45f9889SGreg Roach
250a45f9889SGreg Roach    /**
25176692c8bSGreg Roach     * An HTML form to edit block settings
25276692c8bSGreg Roach     *
253e490cd80SGreg Roach     * @param Tree $tree
25476692c8bSGreg Roach     * @param int  $block_id
255a9430be8SGreg Roach     *
2563caaa4d2SGreg Roach     * @return string
25776692c8bSGreg Roach     */
2583caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
259c1010edaSGreg Roach    {
260e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
261e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
2628c2e8227SGreg Roach
2633caaa4d2SGreg Roach        return view('modules/review_changes/config', [
264c385536dSGreg Roach            'days'     => $days,
265c385536dSGreg Roach            'sendmail' => $sendmail,
266c385536dSGreg Roach        ]);
2678c2e8227SGreg Roach    }
2688c2e8227SGreg Roach}
269