1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsDate;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Mail;
26use Fisharebest\Webtrees\Site;
27use Fisharebest\Webtrees\Theme;
28use Fisharebest\Webtrees\Tree;
29use Fisharebest\Webtrees\User;
30
31/**
32 * Class ReviewChangesModule
33 */
34class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface
35{
36    /** {@inheritdoc} */
37    public function getTitle()
38    {
39        return /* I18N: Name of a module */ I18N::translate('Pending changes');
40    }
41
42    /** {@inheritdoc} */
43    public function getDescription()
44    {
45        return /* I18N: Description of the “Pending changes” module */ I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.');
46    }
47
48    /**
49     * Generate the HTML content of this block.
50     *
51     * @param int      $block_id
52     * @param bool     $template
53     * @param string[] $cfg
54     *
55     * @return string
56     */
57    public function getBlock($block_id, $template = true, $cfg = array())
58    {
59        global $ctype, $WT_TREE;
60
61        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
62        $days     = $this->getBlockSetting($block_id, 'days', '1');
63        $block    = $this->getBlockSetting($block_id, 'block', '1');
64
65        foreach (array('days', 'sendmail', 'block') as $name) {
66            if (array_key_exists($name, $cfg)) {
67                $$name = $cfg[$name];
68            }
69        }
70
71        $changes = Database::prepare(
72            "SELECT 1" .
73            " FROM `##change`" .
74            " WHERE status='pending'" .
75            " LIMIT 1"
76        )->fetchOne();
77
78        if ($changes === '1' && $sendmail === '1') {
79            // There are pending changes - tell moderators/managers/administrators about them.
80            if (WT_TIMESTAMP - Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
81                // Which users have pending changes?
82                foreach (User::all() as $user) {
83                    if ($user->getPreference('contactmethod') !== 'none') {
84                        foreach (Tree::getAll() as $tree) {
85                            if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) {
86                                I18N::init($user->getPreference('language'));
87                                Mail::systemMessage(
88                                    $tree,
89                                    $user,
90                                    I18N::translate('Pending changes'),
91                                    I18N::translate('There are pending changes for you to moderate.') .
92                                    Mail::EOL . Mail::EOL .
93                                    '<a href="' . WT_BASE_URL . 'index.php?ged=' . $WT_TREE->getNameUrl() . '">' . WT_BASE_URL . 'index.php?ged=' . $WT_TREE->getNameUrl() . '</a>'
94                                );
95                                I18N::init(WT_LOCALE);
96                            }
97                        }
98                    }
99                }
100                Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP);
101            }
102        }
103        if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) {
104            $id    = $this->getName() . $block_id;
105            $class = $this->getName() . '_block';
106            if ($ctype === 'user' || Auth::isManager($WT_TREE)) {
107                $title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
108            } else {
109                $title = '';
110            }
111            $title .= $this->getTitle();
112
113            $content = '';
114            if (Auth::isModerator($WT_TREE)) {
115                $content .= "<a href=\"#\" onclick=\"window.open('edit_changes.php','_blank', chan_window_specs); return false;\">" . I18N::translate('There are pending changes for you to moderate.') . "</a><br>";
116            }
117            if ($sendmail === '1') {
118                $content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . "<br>";
119                $content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . "<br><br>";
120            }
121            $content .= '<ul>';
122            $changes = Database::prepare(
123                "SELECT xref" .
124                " FROM  `##change`" .
125                " WHERE status='pending'" .
126                " AND   gedcom_id=?" .
127                " GROUP BY xref"
128            )->execute(array($WT_TREE->getTreeId()))->fetchAll();
129            foreach ($changes as $change) {
130                $record = GedcomRecord::getInstance($change->xref, $WT_TREE);
131                if ($record->canShow()) {
132                    $content .= '<li><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></li>';
133                }
134            }
135            $content .= '</ul>';
136
137            if ($template) {
138                if ($block) {
139                    $class .= ' small_inner_block';
140                }
141
142                return Theme::theme()->formatBlock($id, $title, $class, $content);
143            } else {
144                return $content;
145            }
146        }
147    }
148
149    /** {@inheritdoc} */
150    public function loadAjax()
151    {
152        return false;
153    }
154
155    /** {@inheritdoc} */
156    public function isUserBlock()
157    {
158        return true;
159    }
160
161    /** {@inheritdoc} */
162    public function isGedcomBlock()
163    {
164        return true;
165    }
166
167    /**
168     * An HTML form to edit block settings
169     *
170     * @param int $block_id
171     */
172    public function configureBlock($block_id)
173    {
174        if (Filter::postBool('save') && Filter::checkCsrf()) {
175            $this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1));
176            $this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail'));
177            $this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
178        }
179
180        $sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
181        $days     = $this->getBlockSetting($block_id, 'days', '1');
182        $block    = $this->getBlockSetting($block_id, 'block', '1');
183
184        ?>
185    <tr>
186        <td colspan="2">
187            <?php echo I18N::translate('This block will show editors a list of records with pending changes that need to be reviewed by a moderator. It also generates daily emails to moderators whenever pending changes exist.'); ?>
188        </td>
189    </tr>
190
191        <?php
192        echo '<tr><td class="descriptionbox wrap width33">';
193        echo /* I18N: Label for a configuration option */ I18N::translate('Send out reminder emails');
194        echo '</td><td class="optionbox">';
195        echo FunctionsEdit::editFieldYesNo('sendmail', $sendmail);
196        echo '<br>';
197        echo I18N::translate('Reminder email frequency (days)') . "&nbsp;<input type='text' name='days' value='" . $days . "' size='2'>";
198        echo '</td></tr>';
199
200        echo '<tr><td class="descriptionbox wrap width33">';
201        echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
202        echo '</td><td class="optionbox">';
203        echo FunctionsEdit::editFieldYesNo('block', $block);
204        echo '</td></tr>';
205    }
206}
207