xref: /webtrees/app/Module/ReviewChangesModule.php (revision cc13d6d8ff36fa70d85a3019126ce6f85d0071ad)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
29a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class ReviewChangesModule
338c2e8227SGreg Roach */
34c1010edaSGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
368c2e8227SGreg Roach    /** {@inheritdoc} */
378f53f488SRico Sonntag    public function getTitle(): string
38c1010edaSGreg Roach    {
39bbb76c12SGreg Roach        /* I18N: Name of a module */
40bbb76c12SGreg Roach        return I18N::translate('Pending changes');
418c2e8227SGreg Roach    }
428c2e8227SGreg Roach
438c2e8227SGreg Roach    /** {@inheritdoc} */
448f53f488SRico Sonntag    public function getDescription(): string
45c1010edaSGreg Roach    {
46bbb76c12SGreg Roach        /* I18N: Description of the “Pending changes” module */
47bbb76c12SGreg Roach        return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.');
488c2e8227SGreg Roach    }
498c2e8227SGreg Roach
5076692c8bSGreg Roach    /**
5176692c8bSGreg Roach     * Generate the HTML content of this block.
5276692c8bSGreg Roach     *
53e490cd80SGreg Roach     * @param Tree     $tree
5476692c8bSGreg Roach     * @param int      $block_id
5576692c8bSGreg Roach     * @param bool     $template
56727f238cSGreg Roach     * @param string[] $cfg
5776692c8bSGreg Roach     *
5876692c8bSGreg Roach     * @return string
5976692c8bSGreg Roach     */
60c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
61c1010edaSGreg Roach    {
62e490cd80SGreg Roach        global $ctype;
638c2e8227SGreg Roach
64e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
65e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
668c2e8227SGreg Roach
67c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
688c2e8227SGreg Roach
698c2e8227SGreg Roach        $changes = Database::prepare(
708c2e8227SGreg Roach            "SELECT 1" .
718c2e8227SGreg Roach            " FROM `##change`" .
728c2e8227SGreg Roach            " WHERE status='pending'" .
738c2e8227SGreg Roach            " LIMIT 1"
748c2e8227SGreg Roach        )->fetchOne();
758c2e8227SGreg Roach
768ae466efSGreg Roach        if ($changes === '1' && $sendmail === '1') {
778c2e8227SGreg Roach            // There are pending changes - tell moderators/managers/administrators about them.
7815d603e7SGreg Roach            if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
798c2e8227SGreg Roach                // Which users have pending changes?
808c2e8227SGreg Roach                foreach (User::all() as $user) {
818c2e8227SGreg Roach                    if ($user->getPreference('contactmethod') !== 'none') {
8286bc3d8aSRico Sonntag                        foreach (Tree::getAll() as $tmp_tree) {
8386bc3d8aSRico Sonntag                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) {
848c2e8227SGreg Roach                                I18N::init($user->getPreference('language'));
858857be8bSGreg Roach
868857be8bSGreg Roach                                $sender = new User(
878857be8bSGreg Roach                                    (object) [
888857be8bSGreg Roach                                        'user_id'   => null,
898857be8bSGreg Roach                                        'user_name' => '',
90*cc13d6d8SGreg Roach                                        'real_name' => $tmp_tree->title(),
9186bc3d8aSRico Sonntag                                        'email'     => $tmp_tree->getPreference('WEBTREES_EMAIL'),
928857be8bSGreg Roach                                    ]
938857be8bSGreg Roach                                );
948857be8bSGreg Roach
958857be8bSGreg Roach                                Mail::send(
968857be8bSGreg Roach                                    $sender,
978c2e8227SGreg Roach                                    $user,
988857be8bSGreg Roach                                    $sender,
998c2e8227SGreg Roach                                    I18N::translate('Pending changes'),
100c1010edaSGreg Roach                                    view('emails/pending-changes-text', [
10186bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
102c1010edaSGreg Roach                                        'user' => $user,
103c1010edaSGreg Roach                                    ]),
104c1010edaSGreg Roach                                    view('emails/pending-changes-html', [
10586bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
106c1010edaSGreg Roach                                        'user' => $user,
107c1010edaSGreg Roach                                    ])
1088c2e8227SGreg Roach                                );
1098c2e8227SGreg Roach                                I18N::init(WT_LOCALE);
1108c2e8227SGreg Roach                            }
1118c2e8227SGreg Roach                        }
1128c2e8227SGreg Roach                    }
1138c2e8227SGreg Roach                }
114f97e27d5SGreg Roach                Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP);
1158c2e8227SGreg Roach            }
1168c2e8227SGreg Roach        }
117e490cd80SGreg Roach        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
1188c2e8227SGreg Roach            $content = '';
119e490cd80SGreg Roach            if (Auth::isModerator($tree)) {
120aa6f03bbSGreg Roach                $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1218c2e8227SGreg Roach            }
1228ae466efSGreg Roach            if ($sendmail === '1') {
12376f666f4SGreg Roach                $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL');
12476f666f4SGreg Roach                $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>';
12576f666f4SGreg Roach                $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>';
1268c2e8227SGreg Roach            }
1278c2e8227SGreg Roach            $content .= '<ul>';
1288c2e8227SGreg Roach            $changes = Database::prepare(
1298c2e8227SGreg Roach                "SELECT xref" .
1308c2e8227SGreg Roach                " FROM  `##change`" .
1318c2e8227SGreg Roach                " WHERE status='pending'" .
1328c2e8227SGreg Roach                " AND   gedcom_id=?" .
1338c2e8227SGreg Roach                " GROUP BY xref"
13472cf66d4SGreg Roach            )->execute([$tree->id()])->fetchAll();
1358c2e8227SGreg Roach            foreach ($changes as $change) {
136e490cd80SGreg Roach                $record = GedcomRecord::getInstance($change->xref, $tree);
1378c2e8227SGreg Roach                if ($record->canShow()) {
138b1f1e4efSGreg Roach                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>';
1398c2e8227SGreg Roach                }
1408c2e8227SGreg Roach            }
1418c2e8227SGreg Roach            $content .= '</ul>';
1428c2e8227SGreg Roach
1438c2e8227SGreg Roach            if ($template) {
144e490cd80SGreg Roach                if ($ctype === 'gedcom' && Auth::isManager($tree)) {
145c1010edaSGreg Roach                    $config_url = route('tree-page-block-edit', [
146c1010edaSGreg Roach                        'block_id' => $block_id,
147aa6f03bbSGreg Roach                        'ged'      => $tree->name(),
148c1010edaSGreg Roach                    ]);
149397e599aSGreg Roach                } elseif ($ctype === 'user' && Auth::check()) {
150c1010edaSGreg Roach                    $config_url = route('user-page-block-edit', [
151c1010edaSGreg Roach                        'block_id' => $block_id,
152aa6f03bbSGreg Roach                        'ged'      => $tree->name(),
153c1010edaSGreg Roach                    ]);
1548cbbfdceSGreg Roach                } else {
1558cbbfdceSGreg Roach                    $config_url = '';
1568cbbfdceSGreg Roach                }
1578cbbfdceSGreg Roach
158147e99aaSGreg Roach                return view('modules/block-template', [
1599c6524dcSGreg Roach                    'block'      => str_replace('_', '-', $this->getName()),
1609c6524dcSGreg Roach                    'id'         => $block_id,
1618cbbfdceSGreg Roach                    'config_url' => $config_url,
1629c6524dcSGreg Roach                    'title'      => $this->getTitle(),
1639c6524dcSGreg Roach                    'content'    => $content,
1649c6524dcSGreg Roach                ]);
1658c2e8227SGreg Roach            }
166b2ce94c6SRico Sonntag
167b2ce94c6SRico Sonntag            return $content;
1688c2e8227SGreg Roach        }
16915d603e7SGreg Roach
17015d603e7SGreg Roach        return '';
1718c2e8227SGreg Roach    }
1728c2e8227SGreg Roach
1738c2e8227SGreg Roach    /** {@inheritdoc} */
174c1010edaSGreg Roach    public function loadAjax(): bool
175c1010edaSGreg Roach    {
1768c2e8227SGreg Roach        return false;
1778c2e8227SGreg Roach    }
1788c2e8227SGreg Roach
1798c2e8227SGreg Roach    /** {@inheritdoc} */
180c1010edaSGreg Roach    public function isUserBlock(): bool
181c1010edaSGreg Roach    {
1828c2e8227SGreg Roach        return true;
1838c2e8227SGreg Roach    }
1848c2e8227SGreg Roach
1858c2e8227SGreg Roach    /** {@inheritdoc} */
186c1010edaSGreg Roach    public function isGedcomBlock(): bool
187c1010edaSGreg Roach    {
1888c2e8227SGreg Roach        return true;
1898c2e8227SGreg Roach    }
1908c2e8227SGreg Roach
19176692c8bSGreg Roach    /**
192a45f9889SGreg Roach     * Update the configuration for a block.
193a45f9889SGreg Roach     *
194a45f9889SGreg Roach     * @param Request $request
195a45f9889SGreg Roach     * @param int     $block_id
196a45f9889SGreg Roach     *
197a45f9889SGreg Roach     * @return void
198a45f9889SGreg Roach     */
199a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
200a45f9889SGreg Roach    {
201a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'days', $request->get('num', '1'));
202a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', ''));
203a45f9889SGreg Roach    }
204a45f9889SGreg Roach
205a45f9889SGreg Roach    /**
20676692c8bSGreg Roach     * An HTML form to edit block settings
20776692c8bSGreg Roach     *
208e490cd80SGreg Roach     * @param Tree $tree
20976692c8bSGreg Roach     * @param int  $block_id
210a9430be8SGreg Roach     *
211a9430be8SGreg Roach     * @return void
21276692c8bSGreg Roach     */
213a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
214c1010edaSGreg Roach    {
215e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
216e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
2178c2e8227SGreg Roach
218147e99aaSGreg Roach        echo view('modules/review_changes/config', [
219c385536dSGreg Roach            'days'     => $days,
220c385536dSGreg Roach            'sendmail' => $sendmail,
221c385536dSGreg Roach        ]);
2228c2e8227SGreg Roach    }
2238c2e8227SGreg Roach}
224