xref: /webtrees/app/Module/ReviewChangesModule.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17*fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
260f97530bSGreg Roachuse Fisharebest\Webtrees\Services\MailService;
27e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
294c9729fbSGreg Roachuse Fisharebest\Webtrees\SiteUser;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
31e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\TreeUser;
3277654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
331e7a7a28SGreg Roachuse Illuminate\Support\Str;
346ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
358c2e8227SGreg Roach
368c2e8227SGreg Roach/**
378c2e8227SGreg Roach * Class ReviewChangesModule
388c2e8227SGreg Roach */
3937eb8894SGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface
40c1010edaSGreg Roach{
4149a243cbSGreg Roach    use ModuleBlockTrait;
4249a243cbSGreg Roach
43961ec755SGreg Roach    /**
440f97530bSGreg Roach     * @var MailService
450f97530bSGreg Roach     */
460f97530bSGreg Roach    private $mail_service;
470f97530bSGreg Roach
480f97530bSGreg Roach    /**
49e5a6b4d4SGreg Roach     * @var UserService
50e5a6b4d4SGreg Roach     */
51e5a6b4d4SGreg Roach    private $user_service;
52e5a6b4d4SGreg Roach
53e5a6b4d4SGreg Roach    /**
54e5a6b4d4SGreg Roach     * ReviewChangesModule constructor.
55e5a6b4d4SGreg Roach     *
560f97530bSGreg Roach     * @param MailService $mail_service
57e5a6b4d4SGreg Roach     * @param UserService $user_service
58e5a6b4d4SGreg Roach     */
590f97530bSGreg Roach    public function __construct(MailService $mail_service, UserService $user_service)
60e5a6b4d4SGreg Roach    {
610f97530bSGreg Roach        $this->mail_service = $mail_service;
62e5a6b4d4SGreg Roach        $this->user_service = $user_service;
63e5a6b4d4SGreg Roach    }
64e5a6b4d4SGreg Roach
65e5a6b4d4SGreg Roach    /**
660cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
67961ec755SGreg Roach     *
68961ec755SGreg Roach     * @return string
69961ec755SGreg Roach     */
7049a243cbSGreg Roach    public function title(): string
71c1010edaSGreg Roach    {
72bbb76c12SGreg Roach        /* I18N: Name of a module */
73bbb76c12SGreg Roach        return I18N::translate('Pending changes');
748c2e8227SGreg Roach    }
758c2e8227SGreg Roach
76961ec755SGreg Roach    /**
77961ec755SGreg Roach     * A sentence describing what this module does.
78961ec755SGreg Roach     *
79961ec755SGreg Roach     * @return string
80961ec755SGreg Roach     */
8149a243cbSGreg Roach    public function description(): string
82c1010edaSGreg Roach    {
83bbb76c12SGreg Roach        /* I18N: Description of the “Pending changes” module */
84bbb76c12SGreg Roach        return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.');
858c2e8227SGreg Roach    }
868c2e8227SGreg Roach
8776692c8bSGreg Roach    /**
8876692c8bSGreg Roach     * Generate the HTML content of this block.
8976692c8bSGreg Roach     *
90e490cd80SGreg Roach     * @param Tree     $tree
9176692c8bSGreg Roach     * @param int      $block_id
923caaa4d2SGreg Roach     * @param string   $context
933caaa4d2SGreg Roach     * @param string[] $config
9476692c8bSGreg Roach     *
9576692c8bSGreg Roach     * @return string
9676692c8bSGreg Roach     */
973caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
98c1010edaSGreg Roach    {
994459dc9aSGreg Roach        $sendmail = (bool) $this->getBlockSetting($block_id, 'sendmail', '1');
1004459dc9aSGreg Roach        $days     = (int) $this->getBlockSetting($block_id, 'days', '1');
1018c2e8227SGreg Roach
1023caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1038c2e8227SGreg Roach
10477654037SGreg Roach        $changes_exist = DB::table('change')
10577654037SGreg Roach            ->where('status', 'pending')
10677654037SGreg Roach            ->exists();
1078c2e8227SGreg Roach
1084459dc9aSGreg Roach        if ($changes_exist && $sendmail) {
109ad98d39dSGreg Roach            $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL'));
110ad98d39dSGreg Roach            $next_email_timestamp = $last_email_timestamp->addDays($days);
111ad98d39dSGreg Roach
1128c2e8227SGreg Roach            // There are pending changes - tell moderators/managers/administrators about them.
113ad98d39dSGreg Roach            if ($next_email_timestamp < Carbon::now()) {
1148c2e8227SGreg Roach                // Which users have pending changes?
115e5a6b4d4SGreg Roach                foreach ($this->user_service->all() as $user) {
1168c2e8227SGreg Roach                    if ($user->getPreference('contactmethod') !== 'none') {
11786bc3d8aSRico Sonntag                        foreach (Tree::getAll() as $tmp_tree) {
11886bc3d8aSRico Sonntag                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) {
1198c2e8227SGreg Roach                                I18N::init($user->getPreference('language'));
1208857be8bSGreg Roach
1210f97530bSGreg Roach                                $this->mail_service->send(
1224c9729fbSGreg Roach                                    new SiteUser(),
1238c2e8227SGreg Roach                                    $user,
124e5a6b4d4SGreg Roach                                    new TreeUser($tmp_tree),
1258c2e8227SGreg Roach                                    I18N::translate('Pending changes'),
126c1010edaSGreg Roach                                    view('emails/pending-changes-text', [
12786bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
128c1010edaSGreg Roach                                        'user' => $user,
129c1010edaSGreg Roach                                    ]),
130c1010edaSGreg Roach                                    view('emails/pending-changes-html', [
13186bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
132c1010edaSGreg Roach                                        'user' => $user,
133c1010edaSGreg Roach                                    ])
1348c2e8227SGreg Roach                                );
1358c2e8227SGreg Roach                                I18N::init(WT_LOCALE);
1368c2e8227SGreg Roach                            }
1378c2e8227SGreg Roach                        }
1388c2e8227SGreg Roach                    }
1398c2e8227SGreg Roach                }
1404459dc9aSGreg Roach                Site::setPreference('LAST_CHANGE_EMAIL', (string) Carbon::now()->unix());
1418c2e8227SGreg Roach            }
1428c2e8227SGreg Roach        }
143e490cd80SGreg Roach        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
1448c2e8227SGreg Roach            $content = '';
145e490cd80SGreg Roach            if (Auth::isModerator($tree)) {
146d72b284aSGreg Roach                $content .= '<a href="' . e(route('show-pending', ['tree' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1478c2e8227SGreg Roach            }
1484459dc9aSGreg Roach            if ($sendmail) {
149ca52a408SGreg Roach                $last_email_timestamp = Carbon::createFromTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL'));
150ca52a408SGreg Roach                $next_email_timestamp = $last_email_timestamp->copy()->addDays($days);
151ca52a408SGreg Roach
1524459dc9aSGreg Roach                $content .= I18N::translate('Last email reminder was sent ') . view('components/datetime', ['timestamp' => $last_email_timestamp]) . '<br>';
1534459dc9aSGreg Roach                $content .= I18N::translate('Next email reminder will be sent after ') . view('components/datetime', ['timestamp' => $next_email_timestamp]) . '<br><br>';
1548c2e8227SGreg Roach            }
1558c2e8227SGreg Roach            $content .= '<ul>';
15677654037SGreg Roach
15777654037SGreg Roach            $changes = DB::table('change')
15877654037SGreg Roach                ->where('gedcom_id', '=', $tree->id())
1598cef4ae1SGreg Roach                ->where('status', '=', 'pending')
16077654037SGreg Roach                ->select(['xref'])
16177654037SGreg Roach                ->get();
16277654037SGreg Roach
1638c2e8227SGreg Roach            foreach ($changes as $change) {
164e490cd80SGreg Roach                $record = GedcomRecord::getInstance($change->xref, $tree);
1658c2e8227SGreg Roach                if ($record->canShow()) {
16639ca88baSGreg Roach                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></li>';
1678c2e8227SGreg Roach                }
1688c2e8227SGreg Roach            }
1698c2e8227SGreg Roach            $content .= '</ul>';
1708c2e8227SGreg Roach
1713caaa4d2SGreg Roach            if ($context !== self::CONTEXT_EMBED) {
172147e99aaSGreg Roach                return view('modules/block-template', [
1731e7a7a28SGreg Roach                    'block'      => Str::kebab($this->name()),
1749c6524dcSGreg Roach                    'id'         => $block_id,
1753caaa4d2SGreg Roach                    'config_url' => $this->configUrl($tree, $context, $block_id),
17649a243cbSGreg Roach                    'title'      => $this->title(),
1779c6524dcSGreg Roach                    'content'    => $content,
1789c6524dcSGreg Roach                ]);
1798c2e8227SGreg Roach            }
180b2ce94c6SRico Sonntag
181b2ce94c6SRico Sonntag            return $content;
1828c2e8227SGreg Roach        }
18315d603e7SGreg Roach
18415d603e7SGreg Roach        return '';
1858c2e8227SGreg Roach    }
1868c2e8227SGreg Roach
1873caaa4d2SGreg Roach    /**
1883caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1893caaa4d2SGreg Roach     *
1903caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1913caaa4d2SGreg Roach     *
1923caaa4d2SGreg Roach     * @return bool
1933caaa4d2SGreg Roach     */
194c1010edaSGreg Roach    public function loadAjax(): bool
195c1010edaSGreg Roach    {
1968c2e8227SGreg Roach        return false;
1978c2e8227SGreg Roach    }
1988c2e8227SGreg Roach
1993caaa4d2SGreg Roach    /**
2003caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2013caaa4d2SGreg Roach     *
2023caaa4d2SGreg Roach     * @return bool
2033caaa4d2SGreg Roach     */
204c1010edaSGreg Roach    public function isUserBlock(): bool
205c1010edaSGreg Roach    {
2068c2e8227SGreg Roach        return true;
2078c2e8227SGreg Roach    }
2088c2e8227SGreg Roach
2093caaa4d2SGreg Roach    /**
2103caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2113caaa4d2SGreg Roach     *
2123caaa4d2SGreg Roach     * @return bool
2133caaa4d2SGreg Roach     */
21463276d8fSGreg Roach    public function isTreeBlock(): bool
215c1010edaSGreg Roach    {
2168c2e8227SGreg Roach        return true;
2178c2e8227SGreg Roach    }
2188c2e8227SGreg Roach
21976692c8bSGreg Roach    /**
220a45f9889SGreg Roach     * Update the configuration for a block.
221a45f9889SGreg Roach     *
2226ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
223a45f9889SGreg Roach     * @param int     $block_id
224a45f9889SGreg Roach     *
225a45f9889SGreg Roach     * @return void
226a45f9889SGreg Roach     */
2276ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
228a45f9889SGreg Roach    {
229b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
230b6b9dcc9SGreg Roach
231b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
232b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'sendmail', $params['sendmail']);
233a45f9889SGreg Roach    }
234a45f9889SGreg Roach
235a45f9889SGreg Roach    /**
23676692c8bSGreg Roach     * An HTML form to edit block settings
23776692c8bSGreg Roach     *
238e490cd80SGreg Roach     * @param Tree $tree
23976692c8bSGreg Roach     * @param int  $block_id
240a9430be8SGreg Roach     *
2413caaa4d2SGreg Roach     * @return string
24276692c8bSGreg Roach     */
2433caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
244c1010edaSGreg Roach    {
245e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
246e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
2478c2e8227SGreg Roach
2483caaa4d2SGreg Roach        return view('modules/review_changes/config', [
249c385536dSGreg Roach            'days'     => $days,
250c385536dSGreg Roach            'sendmail' => $sendmail,
251c385536dSGreg Roach        ]);
2528c2e8227SGreg Roach    }
2538c2e8227SGreg Roach}
254