xref: /webtrees/app/Module/ReviewChangesModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg 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;
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
555f2ae573SGreg Roach     * @param string   $ctype
56727f238cSGreg Roach     * @param string[] $cfg
5776692c8bSGreg Roach     *
5876692c8bSGreg Roach     * @return string
5976692c8bSGreg Roach     */
605f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
61c1010edaSGreg 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') {
8086bc3d8aSRico Sonntag                        foreach (Tree::getAll() as $tmp_tree) {
8186bc3d8aSRico Sonntag                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_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' => '',
88cc13d6d8SGreg Roach                                        'real_name' => $tmp_tree->title(),
8986bc3d8aSRico Sonntag                                        'email'     => $tmp_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', [
9986bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
100c1010edaSGreg Roach                                        'user' => $user,
101c1010edaSGreg Roach                                    ]),
102c1010edaSGreg Roach                                    view('emails/pending-changes-html', [
10386bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
104c1010edaSGreg Roach                                        'user' => $user,
105c1010edaSGreg Roach                                    ])
1068c2e8227SGreg Roach                                );
1078c2e8227SGreg Roach                                I18N::init(WT_LOCALE);
1088c2e8227SGreg Roach                            }
1098c2e8227SGreg Roach                        }
1108c2e8227SGreg Roach                    }
1118c2e8227SGreg Roach                }
112f97e27d5SGreg Roach                Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP);
1138c2e8227SGreg Roach            }
1148c2e8227SGreg Roach        }
115e490cd80SGreg Roach        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
1168c2e8227SGreg Roach            $content = '';
117e490cd80SGreg Roach            if (Auth::isModerator($tree)) {
118aa6f03bbSGreg Roach                $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1198c2e8227SGreg Roach            }
1208ae466efSGreg Roach            if ($sendmail === '1') {
12176f666f4SGreg Roach                $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL');
12276f666f4SGreg Roach                $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>';
12376f666f4SGreg Roach                $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>';
1248c2e8227SGreg Roach            }
1258c2e8227SGreg Roach            $content .= '<ul>';
1268c2e8227SGreg Roach            $changes = Database::prepare(
1278c2e8227SGreg Roach                "SELECT xref" .
1288c2e8227SGreg Roach                " FROM  `##change`" .
1298c2e8227SGreg Roach                " WHERE status='pending'" .
1308c2e8227SGreg Roach                " AND   gedcom_id=?" .
1318c2e8227SGreg Roach                " GROUP BY xref"
13272cf66d4SGreg Roach            )->execute([$tree->id()])->fetchAll();
1338c2e8227SGreg Roach            foreach ($changes as $change) {
134e490cd80SGreg Roach                $record = GedcomRecord::getInstance($change->xref, $tree);
1358c2e8227SGreg Roach                if ($record->canShow()) {
136b1f1e4efSGreg Roach                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>';
1378c2e8227SGreg Roach                }
1388c2e8227SGreg Roach            }
1398c2e8227SGreg Roach            $content .= '</ul>';
1408c2e8227SGreg Roach
1416a8879feSGreg Roach            if ($ctype !== '') {
142e490cd80SGreg Roach                if ($ctype === 'gedcom' && Auth::isManager($tree)) {
143c1010edaSGreg Roach                    $config_url = route('tree-page-block-edit', [
144c1010edaSGreg Roach                        'block_id' => $block_id,
145aa6f03bbSGreg Roach                        'ged'      => $tree->name(),
146c1010edaSGreg Roach                    ]);
147397e599aSGreg Roach                } elseif ($ctype === 'user' && Auth::check()) {
148c1010edaSGreg Roach                    $config_url = route('user-page-block-edit', [
149c1010edaSGreg Roach                        'block_id' => $block_id,
150aa6f03bbSGreg Roach                        'ged'      => $tree->name(),
151c1010edaSGreg Roach                    ]);
1528cbbfdceSGreg Roach                } else {
1538cbbfdceSGreg Roach                    $config_url = '';
1548cbbfdceSGreg Roach                }
1558cbbfdceSGreg Roach
156147e99aaSGreg Roach                return view('modules/block-template', [
1579c6524dcSGreg Roach                    'block'      => str_replace('_', '-', $this->getName()),
1589c6524dcSGreg Roach                    'id'         => $block_id,
1598cbbfdceSGreg Roach                    'config_url' => $config_url,
1609c6524dcSGreg Roach                    'title'      => $this->getTitle(),
1619c6524dcSGreg Roach                    'content'    => $content,
1629c6524dcSGreg Roach                ]);
1638c2e8227SGreg Roach            }
164b2ce94c6SRico Sonntag
165b2ce94c6SRico Sonntag            return $content;
1668c2e8227SGreg Roach        }
16715d603e7SGreg Roach
16815d603e7SGreg Roach        return '';
1698c2e8227SGreg Roach    }
1708c2e8227SGreg Roach
1718c2e8227SGreg Roach    /** {@inheritdoc} */
172c1010edaSGreg Roach    public function loadAjax(): bool
173c1010edaSGreg Roach    {
1748c2e8227SGreg Roach        return false;
1758c2e8227SGreg Roach    }
1768c2e8227SGreg Roach
1778c2e8227SGreg Roach    /** {@inheritdoc} */
178c1010edaSGreg Roach    public function isUserBlock(): bool
179c1010edaSGreg Roach    {
1808c2e8227SGreg Roach        return true;
1818c2e8227SGreg Roach    }
1828c2e8227SGreg Roach
1838c2e8227SGreg Roach    /** {@inheritdoc} */
184c1010edaSGreg Roach    public function isGedcomBlock(): bool
185c1010edaSGreg Roach    {
1868c2e8227SGreg Roach        return true;
1878c2e8227SGreg Roach    }
1888c2e8227SGreg Roach
18976692c8bSGreg Roach    /**
190a45f9889SGreg Roach     * Update the configuration for a block.
191a45f9889SGreg Roach     *
192a45f9889SGreg Roach     * @param Request $request
193a45f9889SGreg Roach     * @param int     $block_id
194a45f9889SGreg Roach     *
195a45f9889SGreg Roach     * @return void
196a45f9889SGreg Roach     */
197a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
198a45f9889SGreg Roach    {
199a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'days', $request->get('num', '1'));
200a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', ''));
201a45f9889SGreg Roach    }
202a45f9889SGreg Roach
203a45f9889SGreg Roach    /**
20476692c8bSGreg Roach     * An HTML form to edit block settings
20576692c8bSGreg Roach     *
206e490cd80SGreg Roach     * @param Tree $tree
20776692c8bSGreg Roach     * @param int  $block_id
208a9430be8SGreg Roach     *
209a9430be8SGreg Roach     * @return void
21076692c8bSGreg Roach     */
211a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
212c1010edaSGreg Roach    {
213e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
214e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
2158c2e8227SGreg Roach
216147e99aaSGreg Roach        echo view('modules/review_changes/config', [
217c385536dSGreg Roach            'days'     => $days,
218c385536dSGreg Roach            'sendmail' => $sendmail,
219c385536dSGreg Roach        ]);
2208c2e8227SGreg Roach    }
2218c2e8227SGreg Roach}
222