xref: /webtrees/app/Module/RecentChangesModule.php (revision 575707d10f8c6929c3e1baa59b4fc51d1be11ae1)
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 Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Carbon;
22use Fisharebest\Webtrees\GedcomRecord;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26use Illuminate\Support\Str;
27use Psr\Http\Message\ServerRequestInterface;
28
29/**
30 * Class RecentChangesModule
31 */
32class RecentChangesModule extends AbstractModule implements ModuleBlockInterface
33{
34    use ModuleBlockTrait;
35
36    private const DEFAULT_DAYS       = '7';
37    private const DEFAULT_SHOW_USER  = '1';
38    private const DEFAULT_SORT_STYLE = 'date_desc';
39    private const DEFAULT_INFO_STYLE = 'table';
40    private const MAX_DAYS           = 90;
41
42    /**
43     * How should this module be identified in the control panel, etc.?
44     *
45     * @return string
46     */
47    public function title(): string
48    {
49        /* I18N: Name of a module */
50        return I18N::translate('Recent changes');
51    }
52
53    /**
54     * A sentence describing what this module does.
55     *
56     * @return string
57     */
58    public function description(): string
59    {
60        /* I18N: Description of the “Recent changes” module */
61        return I18N::translate('A list of records that have been updated recently.');
62    }
63
64    /** {@inheritdoc} */
65    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
66    {
67        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
68        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
69        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
70        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
71
72        extract($cfg, EXTR_OVERWRITE);
73
74        $records = $this->getRecentChanges($tree, $days);
75
76        switch ($sortStyle) {
77            case 'name':
78                uasort($records, GedcomRecord::nameComparator());
79                break;
80
81            case 'date_asc':
82                uasort($records, GedcomRecord::lastChangeComparator());
83                break;
84
85            case 'date_desc':
86                uasort($records, GedcomRecord::lastChangeComparator(-1));
87        }
88
89        if (empty($records)) {
90            $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));
91        } elseif ($infoStyle === 'list') {
92            $content = view('modules/recent_changes/changes-list', [
93                'records'   => $records,
94                'show_user' => $show_user,
95            ]);
96        } else {
97            $content = view('modules/recent_changes/changes-table', [
98                'records'   => $records,
99                'show_user' => $show_user,
100            ]);
101        }
102
103        if ($ctype !== '') {
104            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
105                $config_url = route('tree-page-block-edit', [
106                    'block_id' => $block_id,
107                    'ged'      => $tree->name(),
108                ]);
109            } elseif ($ctype === 'user' && Auth::check()) {
110                $config_url = route('user-page-block-edit', [
111                    'block_id' => $block_id,
112                    'ged'      => $tree->name(),
113                ]);
114            } else {
115                $config_url = '';
116            }
117
118            return view('modules/block-template', [
119                'block'      => Str::kebab($this->name()),
120                'id'         => $block_id,
121                'config_url' => $config_url,
122                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
123                'content'    => $content,
124            ]);
125        }
126
127        return $content;
128    }
129
130    /** {@inheritdoc} */
131    public function loadAjax(): bool
132    {
133        return true;
134    }
135
136    /** {@inheritdoc} */
137    public function isUserBlock(): bool
138    {
139        return true;
140    }
141
142    /** {@inheritdoc} */
143    public function isTreeBlock(): bool
144    {
145        return true;
146    }
147
148    /**
149     * Update the configuration for a block.
150     *
151     * @param ServerRequestInterface $request
152     * @param int     $block_id
153     *
154     * @return void
155     */
156    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
157    {
158        $params = $request->getParsedBody();
159
160        $this->setBlockSetting($block_id, 'days', $params['days']);
161        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
162        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
163        $this->setBlockSetting($block_id, 'show_user', $params['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): void
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        return DB::table('change')
219            ->where('gedcom_id', '=', $tree->id())
220            ->where('status', '=', 'accepted')
221            ->where('new_gedcom', '<>', '')
222            ->where('change_time', '>', Carbon::now()->subDays($days))
223            ->groupBy('xref')
224            ->pluck('xref')
225            ->map(static function (string $xref) use ($tree): ?GedcomRecord {
226                return GedcomRecord::getInstance($xref, $tree);
227            })
228            ->filter(static function (?GedcomRecord $record): bool {
229                return $record instanceof GedcomRecord && $record->canShow();
230            })
231            ->all();
232    }
233}
234