xref: /webtrees/app/Module/RecentChangesModule.php (revision d97083fe315dad9b7d0a150d4fb5f563e57d1869)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
226b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
23501bc70dSGreg Roachuse Fisharebest\Webtrees\Family;
246e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
26501bc70dSGreg Roachuse Fisharebest\Webtrees\Individual;
27bd77bf38SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
286e45321fSGreg Roachuse Fisharebest\Webtrees\Tree;
29e10e1dc9SGreg Roachuse Fisharebest\Webtrees\User;
3077654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
31bd77bf38SGreg Roachuse Illuminate\Database\Query\Expression;
32501bc70dSGreg Roachuse Illuminate\Database\Query\JoinClause;
33bd77bf38SGreg Roachuse Illuminate\Support\Collection;
341e7a7a28SGreg Roachuse Illuminate\Support\Str;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
36bd77bf38SGreg Roach
37bd77bf38SGreg Roachuse function extract;
38bd77bf38SGreg Roachuse function view;
39bd77bf38SGreg Roach
40bd77bf38SGreg Roachuse const EXTR_OVERWRITE;
418c2e8227SGreg Roach
428c2e8227SGreg Roach/**
438c2e8227SGreg Roach * Class RecentChangesModule
448c2e8227SGreg Roach */
4537eb8894SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface
46c1010edaSGreg Roach{
4749a243cbSGreg Roach    use ModuleBlockTrait;
4849a243cbSGreg Roach
49501bc70dSGreg Roach    // Where do we look for change information
50501bc70dSGreg Roach    private const SOURCE_DATABASE = 'database';
51501bc70dSGreg Roach    private const SOURCE_GEDCOM   = 'gedcom';
52501bc70dSGreg Roach
5316d6367aSGreg Roach    private const DEFAULT_DAYS       = '7';
5416d6367aSGreg Roach    private const DEFAULT_SHOW_USER  = '1';
55501bc70dSGreg Roach    private const DEFAULT_SHOW_DATE  = '1';
5616d6367aSGreg Roach    private const DEFAULT_SORT_STYLE = 'date_desc';
5716d6367aSGreg Roach    private const DEFAULT_INFO_STYLE = 'table';
58501bc70dSGreg Roach    private const DEFAULT_SOURCE     = self::SOURCE_DATABASE;
5916d6367aSGreg Roach    private const MAX_DAYS           = 90;
608c2e8227SGreg Roach
6101221f27SGreg Roach    // Pagination
6201221f27SGreg Roach    private const LIMIT_LOW  = 10;
6301221f27SGreg Roach    private const LIMIT_HIGH = 20;
6401221f27SGreg Roach
6543f2f523SGreg Roach    private UserService $user_service;
66bd77bf38SGreg Roach
67bd77bf38SGreg Roach    /**
68bd77bf38SGreg Roach     * RecentChangesModule constructor.
69bd77bf38SGreg Roach     *
70bd77bf38SGreg Roach     * @param UserService $user_service
71bd77bf38SGreg Roach     */
72bd77bf38SGreg Roach    public function __construct(UserService $user_service)
73bd77bf38SGreg Roach    {
74bd77bf38SGreg Roach        $this->user_service = $user_service;
75bd77bf38SGreg Roach    }
76bd77bf38SGreg Roach
77961ec755SGreg Roach    /**
780cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
79961ec755SGreg Roach     *
80961ec755SGreg Roach     * @return string
81961ec755SGreg Roach     */
8249a243cbSGreg Roach    public function title(): string
83c1010edaSGreg Roach    {
84bbb76c12SGreg Roach        /* I18N: Name of a module */
85bbb76c12SGreg Roach        return I18N::translate('Recent changes');
868c2e8227SGreg Roach    }
878c2e8227SGreg Roach
88961ec755SGreg Roach    /**
89961ec755SGreg Roach     * A sentence describing what this module does.
90961ec755SGreg Roach     *
91961ec755SGreg Roach     * @return string
92961ec755SGreg Roach     */
9349a243cbSGreg Roach    public function description(): string
94c1010edaSGreg Roach    {
95bbb76c12SGreg Roach        /* I18N: Description of the “Recent changes” module */
96bbb76c12SGreg Roach        return I18N::translate('A list of records that have been updated recently.');
978c2e8227SGreg Roach    }
988c2e8227SGreg Roach
990dcd9387SGreg Roach    /**
1000dcd9387SGreg Roach     * @param Tree   $tree
1010dcd9387SGreg Roach     * @param int    $block_id
1020dcd9387SGreg Roach     * @param string $context
1030dcd9387SGreg Roach     * @param array  $config
1040dcd9387SGreg Roach     *
1050dcd9387SGreg Roach     * @return string
1060dcd9387SGreg Roach     */
1073caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
108c1010edaSGreg Roach    {
10955664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
1106e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
1116e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
112047cb287SGreg Roach        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
113501bc70dSGreg Roach        $show_date = (bool) $this->getBlockSetting($block_id, 'show_date', self::DEFAULT_SHOW_DATE);
114501bc70dSGreg Roach        $source    = $this->getBlockSetting($block_id, 'source', self::DEFAULT_SOURCE);
1158c2e8227SGreg Roach
1163caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1178c2e8227SGreg Roach
118501bc70dSGreg Roach        if ($source === self::SOURCE_DATABASE) {
119501bc70dSGreg Roach            $rows = $this->getRecentChangesFromDatabase($tree, $days);
120501bc70dSGreg Roach        } else {
121501bc70dSGreg Roach            $rows = $this->getRecentChangesFromGenealogy($tree, $days);
122501bc70dSGreg Roach        }
1238c2e8227SGreg Roach
1240280f44aSGreg Roach        switch ($sortStyle) {
1250280f44aSGreg Roach            case 'name':
126f70bcff5SGreg Roach                $rows  = $rows->sort(static function (object $x, object $y): int {
127bd77bf38SGreg Roach                    return GedcomRecord::nameComparator()($x->record, $y->record);
128bd77bf38SGreg Roach                });
129fbd9d3f8SGreg Roach                $order = [[1, 'asc']];
1308c2e8227SGreg Roach                break;
13180e23601SGreg Roach
1320280f44aSGreg Roach            case 'date_asc':
133f70bcff5SGreg Roach                $rows  = $rows->sort(static function (object $x, object $y): int {
134bd77bf38SGreg Roach                    return $x->time <=> $y->time;
135bd77bf38SGreg Roach                });
136fbd9d3f8SGreg Roach                $order = [[2, 'asc']];
1378c2e8227SGreg Roach                break;
13880e23601SGreg Roach
139fbd9d3f8SGreg Roach            default:
1400280f44aSGreg Roach            case 'date_desc':
141f70bcff5SGreg Roach                $rows  = $rows->sort(static function (object $x, object $y): int {
142bd77bf38SGreg Roach                    return $y->time <=> $x->time;
143bd77bf38SGreg Roach                });
1441be6d38bSGreg Roach                $order = [[2, 'desc']];
145fbd9d3f8SGreg Roach                break;
1468c2e8227SGreg Roach        }
1470280f44aSGreg Roach
148bd77bf38SGreg Roach        if ($rows->isEmpty()) {
1490280f44aSGreg Roach            $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));
1500280f44aSGreg Roach        } elseif ($infoStyle === 'list') {
1514d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-list', [
152e24053e5SGreg Roach                'id'         => $block_id,
15301221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
15401221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
155e24053e5SGreg Roach                'rows'       => $rows->values(),
156501bc70dSGreg Roach                'show_date'  => $show_date,
1574d2d7d1aSGreg Roach                'show_user'  => $show_user,
1584d2d7d1aSGreg Roach            ]);
1590280f44aSGreg Roach        } else {
1604d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-table', [
16101221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
16201221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
163bd77bf38SGreg Roach                'rows'       => $rows,
164501bc70dSGreg Roach                'show_date'  => $show_date,
1650280f44aSGreg Roach                'show_user'  => $show_user,
166fbd9d3f8SGreg Roach                'order'      => $order,
1670280f44aSGreg Roach            ]);
1688c2e8227SGreg Roach        }
1698c2e8227SGreg Roach
1703caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
171147e99aaSGreg Roach            return view('modules/block-template', [
1721e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1739c6524dcSGreg Roach                'id'         => $block_id,
1743caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1758cbbfdceSGreg Roach                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
1769c6524dcSGreg Roach                'content'    => $content,
1779c6524dcSGreg Roach            ]);
1788c2e8227SGreg Roach        }
179b2ce94c6SRico Sonntag
180b2ce94c6SRico Sonntag        return $content;
1818c2e8227SGreg Roach    }
1828c2e8227SGreg Roach
1833caaa4d2SGreg Roach    /**
1843caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1853caaa4d2SGreg Roach     *
1863caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1873caaa4d2SGreg Roach     *
1883caaa4d2SGreg Roach     * @return bool
1893caaa4d2SGreg Roach     */
190c1010edaSGreg Roach    public function loadAjax(): bool
191c1010edaSGreg Roach    {
1928c2e8227SGreg Roach        return true;
1938c2e8227SGreg Roach    }
1948c2e8227SGreg Roach
1953caaa4d2SGreg Roach    /**
1963caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1973caaa4d2SGreg Roach     *
1983caaa4d2SGreg Roach     * @return bool
1993caaa4d2SGreg Roach     */
200c1010edaSGreg Roach    public function isUserBlock(): bool
201c1010edaSGreg Roach    {
2028c2e8227SGreg Roach        return true;
2038c2e8227SGreg Roach    }
2048c2e8227SGreg Roach
2053caaa4d2SGreg Roach    /**
2063caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2073caaa4d2SGreg Roach     *
2083caaa4d2SGreg Roach     * @return bool
2093caaa4d2SGreg Roach     */
21063276d8fSGreg Roach    public function isTreeBlock(): bool
211c1010edaSGreg Roach    {
2128c2e8227SGreg Roach        return true;
2138c2e8227SGreg Roach    }
2148c2e8227SGreg Roach
21520ac4041SGreg Roach    /**
216a45f9889SGreg Roach     * Update the configuration for a block.
217a45f9889SGreg Roach     *
2186ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
219a45f9889SGreg Roach     * @param int                    $block_id
220a45f9889SGreg Roach     *
221a45f9889SGreg Roach     * @return void
222a45f9889SGreg Roach     */
2236ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
224a45f9889SGreg Roach    {
225b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
226af2b6902SGreg Roach
227eb235819SGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
228eb235819SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
229eb235819SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
230501bc70dSGreg Roach        $this->setBlockSetting($block_id, 'show_date', $params['show_date']);
231eb235819SGreg Roach        $this->setBlockSetting($block_id, 'show_user', $params['show_user']);
232501bc70dSGreg Roach        $this->setBlockSetting($block_id, 'source', $params['source']);
233a45f9889SGreg Roach    }
234a45f9889SGreg Roach
235a45f9889SGreg Roach    /**
23620ac4041SGreg Roach     * An HTML form to edit block settings
23720ac4041SGreg Roach     *
23820ac4041SGreg Roach     * @param Tree $tree
23920ac4041SGreg Roach     * @param int  $block_id
24020ac4041SGreg Roach     *
2413caaa4d2SGreg Roach     * @return string
24220ac4041SGreg Roach     */
2433caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
244c1010edaSGreg Roach    {
24555664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
2466e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
2476e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
248501bc70dSGreg Roach        $show_date = $this->getBlockSetting($block_id, 'show_date', self::DEFAULT_SHOW_DATE);
2496e45321fSGreg Roach        $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
250501bc70dSGreg Roach        $source    = $this->getBlockSetting($block_id, 'source', self::DEFAULT_SOURCE);
2518c2e8227SGreg Roach
252c385536dSGreg Roach        $info_styles = [
253bbb76c12SGreg Roach            /* I18N: An option in a list-box */
254bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
255bbb76c12SGreg Roach            /* I18N: An option in a list-box */
256bbb76c12SGreg Roach            'table' => I18N::translate('table'),
257c385536dSGreg Roach        ];
2588c2e8227SGreg Roach
259c385536dSGreg Roach        $sort_styles = [
260bbb76c12SGreg Roach            /* I18N: An option in a list-box */
261bbb76c12SGreg Roach            'name'      => I18N::translate('sort by name'),
262bbb76c12SGreg Roach            /* I18N: An option in a list-box */
263bbb76c12SGreg Roach            'date_asc'  => I18N::translate('sort by date, oldest first'),
264bbb76c12SGreg Roach            /* I18N: An option in a list-box */
265bbb76c12SGreg Roach            'date_desc' => I18N::translate('sort by date, newest first'),
266c385536dSGreg Roach        ];
2678c2e8227SGreg Roach
268501bc70dSGreg Roach        $sources = [
269501bc70dSGreg Roach            /* I18N: An option in a list-box */
270501bc70dSGreg Roach            self::SOURCE_DATABASE => I18N::translate('show changes made in webtrees'),
271501bc70dSGreg Roach            /* I18N: An option in a list-box */
272501bc70dSGreg Roach            self::SOURCE_GEDCOM   => I18N::translate('show changes recorded in the genealogy data'),
273501bc70dSGreg Roach        ];
274501bc70dSGreg Roach
2753caaa4d2SGreg Roach        return view('modules/recent_changes/config', [
276c385536dSGreg Roach            'days'        => $days,
277c385536dSGreg Roach            'infoStyle'   => $infoStyle,
278c385536dSGreg Roach            'info_styles' => $info_styles,
279c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
280c385536dSGreg Roach            'sortStyle'   => $sortStyle,
281c385536dSGreg Roach            'sort_styles' => $sort_styles,
282501bc70dSGreg Roach            'source'      => $source,
283501bc70dSGreg Roach            'sources'     => $sources,
284501bc70dSGreg Roach            'show_date'   => $show_date,
285c385536dSGreg Roach            'show_user'   => $show_user,
286c385536dSGreg Roach        ]);
2878c2e8227SGreg Roach    }
2888c2e8227SGreg Roach
2896e45321fSGreg Roach    /**
2906e45321fSGreg Roach     * Find records that have changed since a given julian day
2916e45321fSGreg Roach     *
2926e45321fSGreg Roach     * @param Tree $tree Changes for which tree
29324319d9dSGreg Roach     * @param int  $days Number of days
2946e45321fSGreg Roach     *
29536779af1SGreg Roach     * @return Collection<int,object> List of records with changes
2966e45321fSGreg Roach     */
297501bc70dSGreg Roach    private function getRecentChangesFromDatabase(Tree $tree, int $days): Collection
298c1010edaSGreg Roach    {
299bd77bf38SGreg Roach        $subquery = DB::table('change')
30077654037SGreg Roach            ->where('gedcom_id', '=', $tree->id())
30177654037SGreg Roach            ->where('status', '=', 'accepted')
30277654037SGreg Roach            ->where('new_gedcom', '<>', '')
303*d97083feSGreg Roach            ->where('change_time', '>', Registry::timestampFactory()->now()->subtractDays($days)->toDateTimeString())
3047f5c2944SGreg Roach            ->groupBy(['xref'])
305bd77bf38SGreg Roach            ->select(new Expression('MAX(change_id) AS recent_change_id'));
306bd77bf38SGreg Roach
307bd77bf38SGreg Roach        $query = DB::table('change')
308bd77bf38SGreg Roach            ->joinSub($subquery, 'recent', 'recent_change_id', '=', 'change_id')
309bd77bf38SGreg Roach            ->select(['change.*']);
310bd77bf38SGreg Roach
311bd77bf38SGreg Roach        return $query
312bd77bf38SGreg Roach            ->get()
313f70bcff5SGreg Roach            ->map(function (object $row) use ($tree): object {
314bd77bf38SGreg Roach                return (object) [
3156b9cb339SGreg Roach                    'record' => Registry::gedcomRecordFactory()->make($row->xref, $tree, $row->new_gedcom),
316*d97083feSGreg Roach                    'time'   => Registry::timestampFactory()->fromString($row->change_time),
317d608cecbSGreg Roach                    'user'   => $this->user_service->find((int) $row->user_id),
318bd77bf38SGreg Roach                ];
31977654037SGreg Roach            })
320f70bcff5SGreg Roach            ->filter(static function (object $row): bool {
321bd77bf38SGreg Roach                return $row->record instanceof GedcomRecord && $row->record->canShow();
322bd77bf38SGreg Roach            });
3236e45321fSGreg Roach    }
324501bc70dSGreg Roach
325501bc70dSGreg Roach    /**
326501bc70dSGreg Roach     * Find records that have changed since a given julian day
327501bc70dSGreg Roach     *
328501bc70dSGreg Roach     * @param Tree $tree Changes for which tree
329501bc70dSGreg Roach     * @param int  $days Number of days
330501bc70dSGreg Roach     *
33136779af1SGreg Roach     * @return Collection<int,object> List of records with changes
332501bc70dSGreg Roach     */
333501bc70dSGreg Roach    private function getRecentChangesFromGenealogy(Tree $tree, int $days): Collection
334501bc70dSGreg Roach    {
335*d97083feSGreg Roach        $julian_day = Registry::timestampFactory()->now()->subtractDays($days)->julianDay();
336501bc70dSGreg Roach
337501bc70dSGreg Roach        $individuals = DB::table('dates')
338501bc70dSGreg Roach            ->where('d_file', '=', $tree->id())
339501bc70dSGreg Roach            ->where('d_julianday1', '>=', $julian_day)
340501bc70dSGreg Roach            ->where('d_fact', '=', 'CHAN')
341501bc70dSGreg Roach            ->join('individuals', static function (JoinClause $join): void {
342501bc70dSGreg Roach                $join
343501bc70dSGreg Roach                    ->on('d_file', '=', 'i_file')
344501bc70dSGreg Roach                    ->on('d_gid', '=', 'i_id');
345501bc70dSGreg Roach            })
346501bc70dSGreg Roach            ->select(['individuals.*'])
347501bc70dSGreg Roach            ->get()
3486b9cb339SGreg Roach            ->map(Registry::individualFactory()->mapper($tree))
349501bc70dSGreg Roach            ->filter(Individual::accessFilter());
350501bc70dSGreg Roach
351501bc70dSGreg Roach        $families = DB::table('dates')
352501bc70dSGreg Roach            ->where('d_file', '=', $tree->id())
353501bc70dSGreg Roach            ->where('d_julianday1', '>=', $julian_day)
354501bc70dSGreg Roach            ->where('d_fact', '=', 'CHAN')
355501bc70dSGreg Roach            ->join('families', static function (JoinClause $join): void {
356501bc70dSGreg Roach                $join
357501bc70dSGreg Roach                    ->on('d_file', '=', 'f_file')
358501bc70dSGreg Roach                    ->on('d_gid', '=', 'f_id');
359501bc70dSGreg Roach            })
360501bc70dSGreg Roach            ->select(['families.*'])
361501bc70dSGreg Roach            ->get()
3626b9cb339SGreg Roach            ->map(Registry::familyFactory()->mapper($tree))
363501bc70dSGreg Roach            ->filter(Family::accessFilter());
364501bc70dSGreg Roach
365501bc70dSGreg Roach        return $individuals->merge($families)
366f70bcff5SGreg Roach            ->map(function (GedcomRecord $record): object {
367e10e1dc9SGreg Roach                $user = $this->user_service->findByUserName($record->lastChangeUser());
368e10e1dc9SGreg Roach
369501bc70dSGreg Roach                return (object) [
370501bc70dSGreg Roach                    'record' => $record,
371501bc70dSGreg Roach                    'time'   => $record->lastChangeTimestamp(),
372e10e1dc9SGreg Roach                    'user'   => $user ?? new User(0, '…', '…', ''),
373501bc70dSGreg Roach                ];
374501bc70dSGreg Roach            });
375501bc70dSGreg Roach    }
3768c2e8227SGreg Roach}
377