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