xref: /webtrees/app/Module/RecentChangesModule.php (revision 84586c024c68dbfa3b15df636ed2b74037428442)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Database;
22use Fisharebest\Webtrees\GedcomRecord;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Tree;
25use Symfony\Component\HttpFoundation\Request;
26
27/**
28 * Class RecentChangesModule
29 */
30class RecentChangesModule extends AbstractModule implements ModuleBlockInterface
31{
32    const DEFAULT_BLOCK      = '1';
33    const DEFAULT_DAYS       = '7';
34    const DEFAULT_HIDE_EMPTY = '0';
35    const DEFAULT_SHOW_USER  = '1';
36    const DEFAULT_SORT_STYLE = 'date_desc';
37    const DEFAULT_INFO_STYLE = 'table';
38    const MAX_DAYS           = 90;
39
40    /** {@inheritdoc} */
41    public function getTitle(): string
42    {
43        /* I18N: Name of a module */
44        return I18N::translate('Recent changes');
45    }
46
47    /** {@inheritdoc} */
48    public function getDescription(): string
49    {
50        /* I18N: Description of the “Recent changes” module */
51        return I18N::translate('A list of records that have been updated recently.');
52    }
53
54    /** {@inheritdoc} */
55    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
56    {
57        global $ctype;
58
59        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
60        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
61        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
62        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
63
64        extract($cfg, EXTR_OVERWRITE);
65
66        $records = $this->getRecentChanges($tree, $days);
67
68        switch ($sortStyle) {
69            case 'name':
70                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
71                    return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true);
72                });
73                break;
74
75            case 'date_asc':
76                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
77                    return $a->lastChangeTimestamp(true) - $b->lastChangeTimestamp(true) ?: GedcomRecord::compare($b, $a);
78                });
79                break;
80
81            case 'date_desc':
82                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
83                    return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b);
84                });
85        }
86
87        if (empty($records)) {
88            $content = I18N::plural('There have been no changes within the last %s day.', 'There have been no changes within the last %s days.', $days, I18N::number($days));
89        } elseif ($infoStyle === 'list') {
90            $content = view('modules/recent_changes/changes-list', [
91                'records'   => $records,
92                'show_user' => $show_user,
93            ]);
94        } else {
95            $content = view('modules/recent_changes/changes-table', [
96                'records'   => $records,
97                'show_user' => $show_user,
98            ]);
99        }
100
101        if ($template) {
102            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
103                $config_url = route('tree-page-block-edit', [
104                    'block_id' => $block_id,
105                    'ged'      => $tree->getName(),
106                ]);
107            } elseif ($ctype === 'user' && Auth::check()) {
108                $config_url = route('user-page-block-edit', [
109                    'block_id' => $block_id,
110                    'ged'      => $tree->getName(),
111                ]);
112            } else {
113                $config_url = '';
114            }
115
116            return view('modules/block-template', [
117                'block'      => str_replace('_', '-', $this->getName()),
118                'id'         => $block_id,
119                'config_url' => $config_url,
120                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
121                'content'    => $content,
122            ]);
123        }
124
125        return $content;
126    }
127
128    /** {@inheritdoc} */
129    public function loadAjax(): bool
130    {
131        return true;
132    }
133
134    /** {@inheritdoc} */
135    public function isUserBlock(): bool
136    {
137        return true;
138    }
139
140    /** {@inheritdoc} */
141    public function isGedcomBlock(): bool
142    {
143        return true;
144    }
145
146    /**
147     * Update the configuration for a block.
148     *
149     * @param Request $request
150     * @param int     $block_id
151     *
152     * @return void
153     */
154    public function saveBlockConfiguration(Request $request, int $block_id)
155    {
156        $days = $request->get('days', self::DEFAULT_DAYS);
157
158        if ((int) $days > self::MAX_DAYS || (int) $days < 1) {
159            $days = self::DEFAULT_DAYS;
160        }
161
162        $this->setBlockSetting($block_id, 'days', $days);
163        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_INFO_STYLE));
164        $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT_STYLE));
165        $this->setBlockSetting($block_id, 'show_user', $request->get('show_user', self::DEFAULT_SHOW_USER));
166    }
167
168    /**
169     * An HTML form to edit block settings
170     *
171     * @param Tree $tree
172     * @param int  $block_id
173     *
174     * @return void
175     */
176    public function editBlockConfiguration(Tree $tree, int $block_id)
177    {
178        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
179        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
180        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
181        $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
182
183        $info_styles = [
184            /* I18N: An option in a list-box */
185            'list'  => I18N::translate('list'),
186            /* I18N: An option in a list-box */
187            'table' => I18N::translate('table'),
188        ];
189
190        $sort_styles = [
191            /* I18N: An option in a list-box */
192            'name'      => I18N::translate('sort by name'),
193            /* I18N: An option in a list-box */
194            'date_asc'  => I18N::translate('sort by date, oldest first'),
195            /* I18N: An option in a list-box */
196            'date_desc' => I18N::translate('sort by date, newest first'),
197        ];
198
199        echo view('modules/recent_changes/config', [
200            'days'        => $days,
201            'infoStyle'   => $infoStyle,
202            'info_styles' => $info_styles,
203            'max_days'    => self::MAX_DAYS,
204            'sortStyle'   => $sortStyle,
205            'sort_styles' => $sort_styles,
206            'show_user'   => $show_user,
207        ]);
208    }
209
210    /**
211     * Find records that have changed since a given julian day
212     *
213     * @param Tree $tree Changes for which tree
214     * @param int  $days Number of days
215     *
216     * @return GedcomRecord[] List of records with changes
217     */
218    private function getRecentChanges(Tree $tree, int $days): array
219    {
220        $sql =
221            "SELECT xref FROM `##change`" .
222            " WHERE new_gedcom != '' AND change_time > DATE_SUB(NOW(), INTERVAL :days DAY) AND gedcom_id = :tree_id" .
223            " GROUP BY xref" .
224            " ORDER BY MAX(change_id) DESC";
225
226        $vars = [
227            'days'    => $days,
228            'tree_id' => $tree->getTreeId(),
229        ];
230
231        $xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn();
232
233        $records = [];
234        foreach ($xrefs as $xref) {
235            $record = GedcomRecord::getInstance($xref, $tree);
236            if ($record && $record->canShow()) {
237                $records[] = $record;
238            }
239        }
240
241        return $records;
242    }
243}
244