xref: /webtrees/app/Module/ReviewChangesModule.php (revision 49a243cb5fe7c21b24e262552d556b018bfe3f41)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg 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;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Mail;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
2877654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
29a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class ReviewChangesModule
338c2e8227SGreg Roach */
34*49a243cbSGreg Roachclass ReviewChangesModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface
35c1010edaSGreg Roach{
36*49a243cbSGreg Roach    use ModuleBlockTrait;
37*49a243cbSGreg Roach
388c2e8227SGreg Roach    /** {@inheritdoc} */
39*49a243cbSGreg Roach    public function title(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Name of a module */
42bbb76c12SGreg Roach        return I18N::translate('Pending changes');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
458c2e8227SGreg Roach    /** {@inheritdoc} */
46*49a243cbSGreg Roach    public function description(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Description of the “Pending changes” module */
49bbb76c12SGreg Roach        return I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
5276692c8bSGreg Roach    /**
5376692c8bSGreg Roach     * Generate the HTML content of this block.
5476692c8bSGreg Roach     *
55e490cd80SGreg Roach     * @param Tree     $tree
5676692c8bSGreg Roach     * @param int      $block_id
575f2ae573SGreg Roach     * @param string   $ctype
58727f238cSGreg Roach     * @param string[] $cfg
5976692c8bSGreg Roach     *
6076692c8bSGreg Roach     * @return string
6176692c8bSGreg Roach     */
625f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
63c1010edaSGreg 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
6977654037SGreg Roach        $changes_exist = DB::table('change')
7077654037SGreg Roach            ->where('status', 'pending')
7177654037SGreg Roach            ->exists();
728c2e8227SGreg Roach
7377654037SGreg Roach        if ($changes_exist && $sendmail === '1') {
748c2e8227SGreg Roach            // There are pending changes - tell moderators/managers/administrators about them.
7515d603e7SGreg Roach            if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
768c2e8227SGreg Roach                // Which users have pending changes?
778c2e8227SGreg Roach                foreach (User::all() as $user) {
788c2e8227SGreg Roach                    if ($user->getPreference('contactmethod') !== 'none') {
7986bc3d8aSRico Sonntag                        foreach (Tree::getAll() as $tmp_tree) {
8086bc3d8aSRico Sonntag                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) {
818c2e8227SGreg Roach                                I18N::init($user->getPreference('language'));
828857be8bSGreg Roach
838857be8bSGreg Roach                                Mail::send(
848b67c11aSGreg Roach                                    User::userFromTree($tmp_tree),
858c2e8227SGreg Roach                                    $user,
868b67c11aSGreg Roach                                    User::userFromTree($tmp_tree),
878c2e8227SGreg Roach                                    I18N::translate('Pending changes'),
88c1010edaSGreg Roach                                    view('emails/pending-changes-text', [
8986bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
90c1010edaSGreg Roach                                        'user' => $user,
91c1010edaSGreg Roach                                    ]),
92c1010edaSGreg Roach                                    view('emails/pending-changes-html', [
9386bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
94c1010edaSGreg Roach                                        'user' => $user,
95c1010edaSGreg Roach                                    ])
968c2e8227SGreg Roach                                );
978c2e8227SGreg Roach                                I18N::init(WT_LOCALE);
988c2e8227SGreg Roach                            }
998c2e8227SGreg Roach                        }
1008c2e8227SGreg Roach                    }
1018c2e8227SGreg Roach                }
102f97e27d5SGreg Roach                Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP);
1038c2e8227SGreg Roach            }
1048c2e8227SGreg Roach        }
105e490cd80SGreg Roach        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
1068c2e8227SGreg Roach            $content = '';
107e490cd80SGreg Roach            if (Auth::isModerator($tree)) {
108aa6f03bbSGreg Roach                $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1098c2e8227SGreg Roach            }
1108ae466efSGreg Roach            if ($sendmail === '1') {
11176f666f4SGreg Roach                $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL');
11276f666f4SGreg Roach                $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>';
11376f666f4SGreg Roach                $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>';
1148c2e8227SGreg Roach            }
1158c2e8227SGreg Roach            $content .= '<ul>';
11677654037SGreg Roach
11777654037SGreg Roach            $changes = DB::table('change')
11877654037SGreg Roach                ->where('gedcom_id', '=', $tree->id())
11977654037SGreg Roach                ->select(['xref'])
12077654037SGreg Roach                ->get();
12177654037SGreg Roach
1228c2e8227SGreg Roach            foreach ($changes as $change) {
123e490cd80SGreg Roach                $record = GedcomRecord::getInstance($change->xref, $tree);
1248c2e8227SGreg Roach                if ($record->canShow()) {
125b1f1e4efSGreg Roach                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>';
1268c2e8227SGreg Roach                }
1278c2e8227SGreg Roach            }
1288c2e8227SGreg Roach            $content .= '</ul>';
1298c2e8227SGreg Roach
1306a8879feSGreg Roach            if ($ctype !== '') {
131e490cd80SGreg Roach                if ($ctype === 'gedcom' && Auth::isManager($tree)) {
132c1010edaSGreg Roach                    $config_url = route('tree-page-block-edit', [
133c1010edaSGreg Roach                        'block_id' => $block_id,
134aa6f03bbSGreg Roach                        'ged'      => $tree->name(),
135c1010edaSGreg Roach                    ]);
136397e599aSGreg Roach                } elseif ($ctype === 'user' && Auth::check()) {
137c1010edaSGreg Roach                    $config_url = route('user-page-block-edit', [
138c1010edaSGreg Roach                        'block_id' => $block_id,
139aa6f03bbSGreg Roach                        'ged'      => $tree->name(),
140c1010edaSGreg Roach                    ]);
1418cbbfdceSGreg Roach                } else {
1428cbbfdceSGreg Roach                    $config_url = '';
1438cbbfdceSGreg Roach                }
1448cbbfdceSGreg Roach
145147e99aaSGreg Roach                return view('modules/block-template', [
1469c6524dcSGreg Roach                    'block'      => str_replace('_', '-', $this->getName()),
1479c6524dcSGreg Roach                    'id'         => $block_id,
1488cbbfdceSGreg Roach                    'config_url' => $config_url,
149*49a243cbSGreg Roach                    'title'      => $this->title(),
1509c6524dcSGreg Roach                    'content'    => $content,
1519c6524dcSGreg Roach                ]);
1528c2e8227SGreg Roach            }
153b2ce94c6SRico Sonntag
154b2ce94c6SRico Sonntag            return $content;
1558c2e8227SGreg Roach        }
15615d603e7SGreg Roach
15715d603e7SGreg Roach        return '';
1588c2e8227SGreg Roach    }
1598c2e8227SGreg Roach
1608c2e8227SGreg Roach    /** {@inheritdoc} */
161c1010edaSGreg Roach    public function loadAjax(): bool
162c1010edaSGreg Roach    {
1638c2e8227SGreg Roach        return false;
1648c2e8227SGreg Roach    }
1658c2e8227SGreg Roach
1668c2e8227SGreg Roach    /** {@inheritdoc} */
167c1010edaSGreg Roach    public function isUserBlock(): bool
168c1010edaSGreg Roach    {
1698c2e8227SGreg Roach        return true;
1708c2e8227SGreg Roach    }
1718c2e8227SGreg Roach
1728c2e8227SGreg Roach    /** {@inheritdoc} */
173c1010edaSGreg Roach    public function isGedcomBlock(): bool
174c1010edaSGreg Roach    {
1758c2e8227SGreg Roach        return true;
1768c2e8227SGreg Roach    }
1778c2e8227SGreg Roach
17876692c8bSGreg Roach    /**
179a45f9889SGreg Roach     * Update the configuration for a block.
180a45f9889SGreg Roach     *
181a45f9889SGreg Roach     * @param Request $request
182a45f9889SGreg Roach     * @param int     $block_id
183a45f9889SGreg Roach     *
184a45f9889SGreg Roach     * @return void
185a45f9889SGreg Roach     */
186a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
187a45f9889SGreg Roach    {
188a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'days', $request->get('num', '1'));
189a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', ''));
190a45f9889SGreg Roach    }
191a45f9889SGreg Roach
192a45f9889SGreg Roach    /**
19376692c8bSGreg Roach     * An HTML form to edit block settings
19476692c8bSGreg Roach     *
195e490cd80SGreg Roach     * @param Tree $tree
19676692c8bSGreg Roach     * @param int  $block_id
197a9430be8SGreg Roach     *
198a9430be8SGreg Roach     * @return void
19976692c8bSGreg Roach     */
200a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
201c1010edaSGreg Roach    {
202e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
203e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
2048c2e8227SGreg Roach
205147e99aaSGreg Roach        echo view('modules/review_changes/config', [
206c385536dSGreg Roach            'days'     => $days,
207c385536dSGreg Roach            'sendmail' => $sendmail,
208c385536dSGreg Roach        ]);
2098c2e8227SGreg Roach    }
2108c2e8227SGreg Roach}
211