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