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