xref: /webtrees/app/Module/ReviewChangesModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
27a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class ReviewChangesModule
318c2e8227SGreg Roach */
32c1010edaSGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
348c2e8227SGreg Roach    /** {@inheritdoc} */
35*8f53f488SRico Sonntag    public function getTitle(): string
36c1010edaSGreg Roach    {
37bbb76c12SGreg Roach        /* I18N: Name of a module */
38bbb76c12SGreg Roach        return I18N::translate('Pending changes');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
42*8f53f488SRico Sonntag    public function getDescription(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Description of the “Pending changes” module */
45bbb76c12SGreg Roach        return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
4876692c8bSGreg Roach    /**
4976692c8bSGreg Roach     * Generate the HTML content of this block.
5076692c8bSGreg Roach     *
51e490cd80SGreg Roach     * @param Tree     $tree
5276692c8bSGreg Roach     * @param int      $block_id
5376692c8bSGreg Roach     * @param bool     $template
54727f238cSGreg Roach     * @param string[] $cfg
5576692c8bSGreg Roach     *
5676692c8bSGreg Roach     * @return string
5776692c8bSGreg Roach     */
58c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
59c1010edaSGreg Roach    {
60e490cd80SGreg Roach        global $ctype;
618c2e8227SGreg Roach
62e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
63e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
648c2e8227SGreg Roach
65c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
668c2e8227SGreg Roach
678c2e8227SGreg Roach        $changes = Database::prepare(
688c2e8227SGreg Roach            "SELECT 1" .
698c2e8227SGreg Roach            " FROM `##change`" .
708c2e8227SGreg Roach            " WHERE status='pending'" .
718c2e8227SGreg Roach            " LIMIT 1"
728c2e8227SGreg Roach        )->fetchOne();
738c2e8227SGreg Roach
748ae466efSGreg Roach        if ($changes === '1' && $sendmail === '1') {
758c2e8227SGreg Roach            // There are pending changes - tell moderators/managers/administrators about them.
7615d603e7SGreg Roach            if (WT_TIMESTAMP - (int)Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
778c2e8227SGreg Roach                // Which users have pending changes?
788c2e8227SGreg Roach                foreach (User::all() as $user) {
798c2e8227SGreg Roach                    if ($user->getPreference('contactmethod') !== 'none') {
808c2e8227SGreg Roach                        foreach (Tree::getAll() as $tree) {
818c2e8227SGreg Roach                            if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) {
828c2e8227SGreg Roach                                I18N::init($user->getPreference('language'));
838857be8bSGreg Roach
848857be8bSGreg Roach                                $sender = new User(
858857be8bSGreg Roach                                    (object)[
868857be8bSGreg Roach                                        'user_id'   => null,
878857be8bSGreg Roach                                        'user_name' => '',
88e490cd80SGreg Roach                                        'real_name' => $tree->getTitle(),
89e490cd80SGreg Roach                                        'email'     => $tree->getPreference('WEBTREES_EMAIL'),
908857be8bSGreg Roach                                    ]
918857be8bSGreg Roach                                );
928857be8bSGreg Roach
938857be8bSGreg Roach                                Mail::send(
948857be8bSGreg Roach                                    $sender,
958c2e8227SGreg Roach                                    $user,
968857be8bSGreg Roach                                    $sender,
978c2e8227SGreg Roach                                    I18N::translate('Pending changes'),
98c1010edaSGreg Roach                                    view('emails/pending-changes-text', [
99c1010edaSGreg Roach                                        'tree' => $tree,
100c1010edaSGreg Roach                                        'user' => $user,
101c1010edaSGreg Roach                                    ]),
102c1010edaSGreg Roach                                    view('emails/pending-changes-html', [
103c1010edaSGreg Roach                                        'tree' => $tree,
104c1010edaSGreg Roach                                        'user' => $user,
105c1010edaSGreg Roach                                    ])
1068c2e8227SGreg Roach                                );
1078c2e8227SGreg Roach                                I18N::init(WT_LOCALE);
1088c2e8227SGreg Roach                            }
1098c2e8227SGreg Roach                        }
1108c2e8227SGreg Roach                    }
1118c2e8227SGreg Roach                }
1128c2e8227SGreg Roach                Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP);
1138c2e8227SGreg Roach            }
1148c2e8227SGreg Roach        }
115e490cd80SGreg Roach        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
1168c2e8227SGreg Roach            $content = '';
117e490cd80SGreg Roach            if (Auth::isModerator($tree)) {
118e490cd80SGreg Roach                $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->getName()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1198c2e8227SGreg Roach            }
1208ae466efSGreg Roach            if ($sendmail === '1') {
1217a6ee1acSGreg Roach                $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . '<br>';
122f7f4b984SGreg Roach                $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp((int)Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . '<br><br>';
1238c2e8227SGreg Roach            }
1248c2e8227SGreg Roach            $content .= '<ul>';
1258c2e8227SGreg Roach            $changes = Database::prepare(
1268c2e8227SGreg Roach                "SELECT xref" .
1278c2e8227SGreg Roach                " FROM  `##change`" .
1288c2e8227SGreg Roach                " WHERE status='pending'" .
1298c2e8227SGreg Roach                " AND   gedcom_id=?" .
1308c2e8227SGreg Roach                " GROUP BY xref"
131e490cd80SGreg Roach            )->execute([$tree->getTreeId()])->fetchAll();
1328c2e8227SGreg Roach            foreach ($changes as $change) {
133e490cd80SGreg Roach                $record = GedcomRecord::getInstance($change->xref, $tree);
1348c2e8227SGreg Roach                if ($record->canShow()) {
135b1f1e4efSGreg Roach                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>';
1368c2e8227SGreg Roach                }
1378c2e8227SGreg Roach            }
1388c2e8227SGreg Roach            $content .= '</ul>';
1398c2e8227SGreg Roach
1408c2e8227SGreg Roach            if ($template) {
141e490cd80SGreg Roach                if ($ctype === 'gedcom' && Auth::isManager($tree)) {
142c1010edaSGreg Roach                    $config_url = route('tree-page-block-edit', [
143c1010edaSGreg Roach                        'block_id' => $block_id,
144c1010edaSGreg Roach                        'ged'      => $tree->getName(),
145c1010edaSGreg Roach                    ]);
146397e599aSGreg Roach                } elseif ($ctype === 'user' && Auth::check()) {
147c1010edaSGreg Roach                    $config_url = route('user-page-block-edit', [
148c1010edaSGreg Roach                        'block_id' => $block_id,
149c1010edaSGreg Roach                        'ged'      => $tree->getName(),
150c1010edaSGreg Roach                    ]);
1518cbbfdceSGreg Roach                } else {
1528cbbfdceSGreg Roach                    $config_url = '';
1538cbbfdceSGreg Roach                }
1548cbbfdceSGreg Roach
155147e99aaSGreg Roach                return view('modules/block-template', [
1569c6524dcSGreg Roach                    'block'      => str_replace('_', '-', $this->getName()),
1579c6524dcSGreg Roach                    'id'         => $block_id,
1588cbbfdceSGreg Roach                    'config_url' => $config_url,
1599c6524dcSGreg Roach                    'title'      => $this->getTitle(),
1609c6524dcSGreg Roach                    'content'    => $content,
1619c6524dcSGreg Roach                ]);
1628c2e8227SGreg Roach            } else {
1638c2e8227SGreg Roach                return $content;
1648c2e8227SGreg Roach            }
1658c2e8227SGreg Roach        }
16615d603e7SGreg Roach
16715d603e7SGreg Roach        return '';
1688c2e8227SGreg Roach    }
1698c2e8227SGreg Roach
1708c2e8227SGreg Roach    /** {@inheritdoc} */
171c1010edaSGreg Roach    public function loadAjax(): bool
172c1010edaSGreg Roach    {
1738c2e8227SGreg Roach        return false;
1748c2e8227SGreg Roach    }
1758c2e8227SGreg Roach
1768c2e8227SGreg Roach    /** {@inheritdoc} */
177c1010edaSGreg Roach    public function isUserBlock(): bool
178c1010edaSGreg Roach    {
1798c2e8227SGreg Roach        return true;
1808c2e8227SGreg Roach    }
1818c2e8227SGreg Roach
1828c2e8227SGreg Roach    /** {@inheritdoc} */
183c1010edaSGreg Roach    public function isGedcomBlock(): bool
184c1010edaSGreg Roach    {
1858c2e8227SGreg Roach        return true;
1868c2e8227SGreg Roach    }
1878c2e8227SGreg Roach
18876692c8bSGreg Roach    /**
189a45f9889SGreg Roach     * Update the configuration for a block.
190a45f9889SGreg Roach     *
191a45f9889SGreg Roach     * @param Request $request
192a45f9889SGreg Roach     * @param int     $block_id
193a45f9889SGreg Roach     *
194a45f9889SGreg Roach     * @return void
195a45f9889SGreg Roach     */
196a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
197a45f9889SGreg Roach    {
198a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'days', $request->get('num', '1'));
199a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', ''));
200a45f9889SGreg Roach    }
201a45f9889SGreg Roach
202a45f9889SGreg Roach    /**
20376692c8bSGreg Roach     * An HTML form to edit block settings
20476692c8bSGreg Roach     *
205e490cd80SGreg Roach     * @param Tree $tree
20676692c8bSGreg Roach     * @param int  $block_id
207a9430be8SGreg Roach     *
208a9430be8SGreg Roach     * @return void
20976692c8bSGreg Roach     */
210a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
211c1010edaSGreg Roach    {
212e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
213e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
2148c2e8227SGreg Roach
215147e99aaSGreg Roach        echo view('modules/review_changes/config', [
216c385536dSGreg Roach            'days'     => $days,
217c385536dSGreg Roach            'sendmail' => $sendmail,
218c385536dSGreg Roach        ]);
2198c2e8227SGreg Roach    }
2208c2e8227SGreg Roach}
221