xref: /webtrees/app/Module/ReviewChangesModule.php (revision 8b67c11a1199191915b4af08a3841e7ce9d528b6)
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 */
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
6777654037SGreg Roach        $changes_exist = DB::table('change')
6877654037SGreg Roach            ->where('status', 'pending')
6977654037SGreg Roach            ->exists();
708c2e8227SGreg Roach
7177654037SGreg Roach        if ($changes_exist && $sendmail === '1') {
728c2e8227SGreg Roach            // There are pending changes - tell moderators/managers/administrators about them.
7315d603e7SGreg Roach            if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
748c2e8227SGreg Roach                // Which users have pending changes?
758c2e8227SGreg Roach                foreach (User::all() as $user) {
768c2e8227SGreg Roach                    if ($user->getPreference('contactmethod') !== 'none') {
7786bc3d8aSRico Sonntag                        foreach (Tree::getAll() as $tmp_tree) {
7886bc3d8aSRico Sonntag                            if ($tmp_tree->hasPendingEdit() && Auth::isManager($tmp_tree, $user)) {
798c2e8227SGreg Roach                                I18N::init($user->getPreference('language'));
808857be8bSGreg Roach
818857be8bSGreg Roach                                Mail::send(
82*8b67c11aSGreg Roach                                    User::userFromTree($tmp_tree),
838c2e8227SGreg Roach                                    $user,
84*8b67c11aSGreg Roach                                    User::userFromTree($tmp_tree),
858c2e8227SGreg Roach                                    I18N::translate('Pending changes'),
86c1010edaSGreg Roach                                    view('emails/pending-changes-text', [
8786bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
88c1010edaSGreg Roach                                        'user' => $user,
89c1010edaSGreg Roach                                    ]),
90c1010edaSGreg Roach                                    view('emails/pending-changes-html', [
9186bc3d8aSRico Sonntag                                        'tree' => $tmp_tree,
92c1010edaSGreg Roach                                        'user' => $user,
93c1010edaSGreg Roach                                    ])
948c2e8227SGreg Roach                                );
958c2e8227SGreg Roach                                I18N::init(WT_LOCALE);
968c2e8227SGreg Roach                            }
978c2e8227SGreg Roach                        }
988c2e8227SGreg Roach                    }
998c2e8227SGreg Roach                }
100f97e27d5SGreg Roach                Site::setPreference('LAST_CHANGE_EMAIL', (string) WT_TIMESTAMP);
1018c2e8227SGreg Roach            }
1028c2e8227SGreg Roach        }
103e490cd80SGreg Roach        if (Auth::isEditor($tree) && $tree->hasPendingEdit()) {
1048c2e8227SGreg Roach            $content = '';
105e490cd80SGreg Roach            if (Auth::isModerator($tree)) {
106aa6f03bbSGreg Roach                $content .= '<a href="' . e(route('show-pending', ['ged' => $tree->name()])) . '">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
1078c2e8227SGreg Roach            }
1088ae466efSGreg Roach            if ($sendmail === '1') {
10976f666f4SGreg Roach                $last_email_timestamp = (int) Site::getPreference('LAST_CHANGE_EMAIL');
11076f666f4SGreg Roach                $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp($last_email_timestamp) . '<br>';
11176f666f4SGreg Roach                $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp($last_email_timestamp + 60 * 60 * 24 * $days) . '<br><br>';
1128c2e8227SGreg Roach            }
1138c2e8227SGreg Roach            $content .= '<ul>';
11477654037SGreg Roach
11577654037SGreg Roach            $changes = DB::table('change')
11677654037SGreg Roach                ->where('gedcom_id', '=', $tree->id())
11777654037SGreg Roach                ->select(['xref'])
11877654037SGreg Roach                ->get();
11977654037SGreg Roach
1208c2e8227SGreg Roach            foreach ($changes as $change) {
121e490cd80SGreg Roach                $record = GedcomRecord::getInstance($change->xref, $tree);
1228c2e8227SGreg Roach                if ($record->canShow()) {
123b1f1e4efSGreg Roach                    $content .= '<li><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></li>';
1248c2e8227SGreg Roach                }
1258c2e8227SGreg Roach            }
1268c2e8227SGreg Roach            $content .= '</ul>';
1278c2e8227SGreg Roach
1286a8879feSGreg Roach            if ($ctype !== '') {
129e490cd80SGreg Roach                if ($ctype === 'gedcom' && Auth::isManager($tree)) {
130c1010edaSGreg Roach                    $config_url = route('tree-page-block-edit', [
131c1010edaSGreg Roach                        'block_id' => $block_id,
132aa6f03bbSGreg Roach                        'ged'      => $tree->name(),
133c1010edaSGreg Roach                    ]);
134397e599aSGreg Roach                } elseif ($ctype === 'user' && Auth::check()) {
135c1010edaSGreg Roach                    $config_url = route('user-page-block-edit', [
136c1010edaSGreg Roach                        'block_id' => $block_id,
137aa6f03bbSGreg Roach                        'ged'      => $tree->name(),
138c1010edaSGreg Roach                    ]);
1398cbbfdceSGreg Roach                } else {
1408cbbfdceSGreg Roach                    $config_url = '';
1418cbbfdceSGreg Roach                }
1428cbbfdceSGreg Roach
143147e99aaSGreg Roach                return view('modules/block-template', [
1449c6524dcSGreg Roach                    'block'      => str_replace('_', '-', $this->getName()),
1459c6524dcSGreg Roach                    'id'         => $block_id,
1468cbbfdceSGreg Roach                    'config_url' => $config_url,
1479c6524dcSGreg Roach                    'title'      => $this->getTitle(),
1489c6524dcSGreg Roach                    'content'    => $content,
1499c6524dcSGreg Roach                ]);
1508c2e8227SGreg Roach            }
151b2ce94c6SRico Sonntag
152b2ce94c6SRico Sonntag            return $content;
1538c2e8227SGreg Roach        }
15415d603e7SGreg Roach
15515d603e7SGreg Roach        return '';
1568c2e8227SGreg Roach    }
1578c2e8227SGreg Roach
1588c2e8227SGreg Roach    /** {@inheritdoc} */
159c1010edaSGreg Roach    public function loadAjax(): bool
160c1010edaSGreg Roach    {
1618c2e8227SGreg Roach        return false;
1628c2e8227SGreg Roach    }
1638c2e8227SGreg Roach
1648c2e8227SGreg Roach    /** {@inheritdoc} */
165c1010edaSGreg Roach    public function isUserBlock(): bool
166c1010edaSGreg Roach    {
1678c2e8227SGreg Roach        return true;
1688c2e8227SGreg Roach    }
1698c2e8227SGreg Roach
1708c2e8227SGreg Roach    /** {@inheritdoc} */
171c1010edaSGreg Roach    public function isGedcomBlock(): bool
172c1010edaSGreg Roach    {
1738c2e8227SGreg Roach        return true;
1748c2e8227SGreg Roach    }
1758c2e8227SGreg Roach
17676692c8bSGreg Roach    /**
177a45f9889SGreg Roach     * Update the configuration for a block.
178a45f9889SGreg Roach     *
179a45f9889SGreg Roach     * @param Request $request
180a45f9889SGreg Roach     * @param int     $block_id
181a45f9889SGreg Roach     *
182a45f9889SGreg Roach     * @return void
183a45f9889SGreg Roach     */
184a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
185a45f9889SGreg Roach    {
186a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'days', $request->get('num', '1'));
187a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'sendmail', $request->get('sendmail', ''));
188a45f9889SGreg Roach    }
189a45f9889SGreg Roach
190a45f9889SGreg Roach    /**
19176692c8bSGreg Roach     * An HTML form to edit block settings
19276692c8bSGreg Roach     *
193e490cd80SGreg Roach     * @param Tree $tree
19476692c8bSGreg Roach     * @param int  $block_id
195a9430be8SGreg Roach     *
196a9430be8SGreg Roach     * @return void
19776692c8bSGreg Roach     */
198a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
199c1010edaSGreg Roach    {
200e2a378d3SGreg Roach        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
201e2a378d3SGreg Roach        $days     = $this->getBlockSetting($block_id, 'days', '1');
2028c2e8227SGreg Roach
203147e99aaSGreg Roach        echo view('modules/review_changes/config', [
204c385536dSGreg Roach            'days'     => $days,
205c385536dSGreg Roach            'sendmail' => $sendmail,
206c385536dSGreg Roach        ]);
2078c2e8227SGreg Roach    }
2088c2e8227SGreg Roach}
209