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