xref: /webtrees/app/Module/RecentChangesModule.php (revision 01221f27a16e8d8c871feaf74c8d276e80267d5b)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
236e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
25bd77bf38SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
266e45321fSGreg Roachuse Fisharebest\Webtrees\Tree;
2777654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
28bd77bf38SGreg Roachuse Illuminate\Database\Query\Expression;
29bd77bf38SGreg Roachuse Illuminate\Support\Collection;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32bd77bf38SGreg Roachuse stdClass;
33bd77bf38SGreg Roach
34bd77bf38SGreg Roachuse function extract;
35bd77bf38SGreg Roachuse function view;
36bd77bf38SGreg Roach
37bd77bf38SGreg Roachuse const EXTR_OVERWRITE;
388c2e8227SGreg Roach
398c2e8227SGreg Roach/**
408c2e8227SGreg Roach * Class RecentChangesModule
418c2e8227SGreg Roach */
4237eb8894SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleBlockTrait;
4549a243cbSGreg Roach
4616d6367aSGreg Roach    private const DEFAULT_DAYS       = '7';
4716d6367aSGreg Roach    private const DEFAULT_SHOW_USER  = '1';
4816d6367aSGreg Roach    private const DEFAULT_SORT_STYLE = 'date_desc';
4916d6367aSGreg Roach    private const DEFAULT_INFO_STYLE = 'table';
5016d6367aSGreg Roach    private const MAX_DAYS           = 90;
518c2e8227SGreg Roach
52*01221f27SGreg Roach    // Pagination
53*01221f27SGreg Roach    private const LIMIT_LOW  = 10;
54*01221f27SGreg Roach    private const LIMIT_HIGH = 20;
55*01221f27SGreg Roach
56bd77bf38SGreg Roach    /** @var UserService */
57bd77bf38SGreg Roach    private $user_service;
58bd77bf38SGreg Roach
59bd77bf38SGreg Roach    /**
60bd77bf38SGreg Roach     * RecentChangesModule constructor.
61bd77bf38SGreg Roach     *
62bd77bf38SGreg Roach     * @param UserService $user_service
63bd77bf38SGreg Roach     */
64bd77bf38SGreg Roach    public function __construct(UserService $user_service)
65bd77bf38SGreg Roach    {
66bd77bf38SGreg Roach        $this->user_service = $user_service;
67bd77bf38SGreg Roach    }
68bd77bf38SGreg Roach
69961ec755SGreg Roach    /**
700cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
71961ec755SGreg Roach     *
72961ec755SGreg Roach     * @return string
73961ec755SGreg Roach     */
7449a243cbSGreg Roach    public function title(): string
75c1010edaSGreg Roach    {
76bbb76c12SGreg Roach        /* I18N: Name of a module */
77bbb76c12SGreg Roach        return I18N::translate('Recent changes');
788c2e8227SGreg Roach    }
798c2e8227SGreg Roach
80961ec755SGreg Roach    /**
81961ec755SGreg Roach     * A sentence describing what this module does.
82961ec755SGreg Roach     *
83961ec755SGreg Roach     * @return string
84961ec755SGreg Roach     */
8549a243cbSGreg Roach    public function description(): string
86c1010edaSGreg Roach    {
87bbb76c12SGreg Roach        /* I18N: Description of the “Recent changes” module */
88bbb76c12SGreg Roach        return I18N::translate('A list of records that have been updated recently.');
898c2e8227SGreg Roach    }
908c2e8227SGreg Roach
916e45321fSGreg Roach    /** {@inheritdoc} */
923caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
93c1010edaSGreg Roach    {
9455664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
956e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
966e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
97047cb287SGreg Roach        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
988c2e8227SGreg Roach
993caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1008c2e8227SGreg Roach
101bd77bf38SGreg Roach        $rows = $this->getRecentChanges($tree, $days);
1028c2e8227SGreg Roach
1030280f44aSGreg Roach        switch ($sortStyle) {
1040280f44aSGreg Roach            case 'name':
105bd77bf38SGreg Roach                $rows = $rows->sort(static function (stdClass $x, stdClass $y): int {
106bd77bf38SGreg Roach                    return GedcomRecord::nameComparator()($x->record, $y->record);
107bd77bf38SGreg Roach                });
1088c2e8227SGreg Roach                break;
10980e23601SGreg Roach
1100280f44aSGreg Roach            case 'date_asc':
111bd77bf38SGreg Roach                $rows = $rows->sort(static function (stdClass $x, stdClass $y): int {
112bd77bf38SGreg Roach                    return $x->time <=> $y->time;
113bd77bf38SGreg Roach                });
1148c2e8227SGreg Roach                break;
11580e23601SGreg Roach
1160280f44aSGreg Roach            case 'date_desc':
117bd77bf38SGreg Roach                $rows = $rows->sort(static function (stdClass $x, stdClass $y): int {
118bd77bf38SGreg Roach                    return $y->time <=> $x->time;
119bd77bf38SGreg Roach                });
1208c2e8227SGreg Roach        }
1210280f44aSGreg Roach
122bd77bf38SGreg Roach        if ($rows->isEmpty()) {
1230280f44aSGreg 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));
1240280f44aSGreg Roach        } elseif ($infoStyle === 'list') {
1254d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-list', [
126e24053e5SGreg Roach                'id'         => $block_id,
127*01221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
128*01221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
129e24053e5SGreg Roach                'rows'       => $rows->values(),
1304d2d7d1aSGreg Roach                'show_user'  => $show_user,
1314d2d7d1aSGreg Roach            ]);
1320280f44aSGreg Roach        } else {
1334d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-table', [
134*01221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
135*01221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
136bd77bf38SGreg Roach                'rows'       => $rows,
1370280f44aSGreg Roach                'show_user'  => $show_user,
1380280f44aSGreg Roach            ]);
1398c2e8227SGreg Roach        }
1408c2e8227SGreg Roach
1413caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
142147e99aaSGreg Roach            return view('modules/block-template', [
1431e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1449c6524dcSGreg Roach                'id'         => $block_id,
1453caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1468cbbfdceSGreg Roach                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
1479c6524dcSGreg Roach                'content'    => $content,
1489c6524dcSGreg Roach            ]);
1498c2e8227SGreg Roach        }
150b2ce94c6SRico Sonntag
151b2ce94c6SRico Sonntag        return $content;
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
1543caaa4d2SGreg Roach    /**
1553caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1563caaa4d2SGreg Roach     *
1573caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1583caaa4d2SGreg Roach     *
1593caaa4d2SGreg Roach     * @return bool
1603caaa4d2SGreg Roach     */
161c1010edaSGreg Roach    public function loadAjax(): bool
162c1010edaSGreg Roach    {
1638c2e8227SGreg Roach        return true;
1648c2e8227SGreg Roach    }
1658c2e8227SGreg Roach
1663caaa4d2SGreg Roach    /**
1673caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1683caaa4d2SGreg Roach     *
1693caaa4d2SGreg Roach     * @return bool
1703caaa4d2SGreg Roach     */
171c1010edaSGreg Roach    public function isUserBlock(): bool
172c1010edaSGreg Roach    {
1738c2e8227SGreg Roach        return true;
1748c2e8227SGreg Roach    }
1758c2e8227SGreg Roach
1763caaa4d2SGreg Roach    /**
1773caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1783caaa4d2SGreg Roach     *
1793caaa4d2SGreg Roach     * @return bool
1803caaa4d2SGreg Roach     */
18163276d8fSGreg Roach    public function isTreeBlock(): bool
182c1010edaSGreg Roach    {
1838c2e8227SGreg Roach        return true;
1848c2e8227SGreg Roach    }
1858c2e8227SGreg Roach
18620ac4041SGreg Roach    /**
187a45f9889SGreg Roach     * Update the configuration for a block.
188a45f9889SGreg Roach     *
1896ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
190a45f9889SGreg Roach     * @param int     $block_id
191a45f9889SGreg Roach     *
192a45f9889SGreg Roach     * @return void
193a45f9889SGreg Roach     */
1946ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
195a45f9889SGreg Roach    {
196b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
197af2b6902SGreg Roach
198eb235819SGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
199eb235819SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
200eb235819SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
201eb235819SGreg Roach        $this->setBlockSetting($block_id, 'show_user', $params['show_user']);
202a45f9889SGreg Roach    }
203a45f9889SGreg Roach
204a45f9889SGreg Roach    /**
20520ac4041SGreg Roach     * An HTML form to edit block settings
20620ac4041SGreg Roach     *
20720ac4041SGreg Roach     * @param Tree $tree
20820ac4041SGreg Roach     * @param int  $block_id
20920ac4041SGreg Roach     *
2103caaa4d2SGreg Roach     * @return string
21120ac4041SGreg Roach     */
2123caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
213c1010edaSGreg Roach    {
21455664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
2156e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
2166e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
2176e45321fSGreg Roach        $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
2188c2e8227SGreg Roach
219c385536dSGreg Roach        $info_styles = [
220bbb76c12SGreg Roach            /* I18N: An option in a list-box */
221bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
222bbb76c12SGreg Roach            /* I18N: An option in a list-box */
223bbb76c12SGreg Roach            'table' => I18N::translate('table'),
224c385536dSGreg Roach        ];
2258c2e8227SGreg Roach
226c385536dSGreg Roach        $sort_styles = [
227bbb76c12SGreg Roach            /* I18N: An option in a list-box */
228bbb76c12SGreg Roach            'name'      => I18N::translate('sort by name'),
229bbb76c12SGreg Roach            /* I18N: An option in a list-box */
230bbb76c12SGreg Roach            'date_asc'  => I18N::translate('sort by date, oldest first'),
231bbb76c12SGreg Roach            /* I18N: An option in a list-box */
232bbb76c12SGreg Roach            'date_desc' => I18N::translate('sort by date, newest first'),
233c385536dSGreg Roach        ];
2348c2e8227SGreg Roach
2353caaa4d2SGreg Roach        return view('modules/recent_changes/config', [
236c385536dSGreg Roach            'days'        => $days,
237c385536dSGreg Roach            'infoStyle'   => $infoStyle,
238c385536dSGreg Roach            'info_styles' => $info_styles,
239c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
240c385536dSGreg Roach            'sortStyle'   => $sortStyle,
241c385536dSGreg Roach            'sort_styles' => $sort_styles,
242c385536dSGreg Roach            'show_user'   => $show_user,
243c385536dSGreg Roach        ]);
2448c2e8227SGreg Roach    }
2458c2e8227SGreg Roach
2466e45321fSGreg Roach    /**
2476e45321fSGreg Roach     * Find records that have changed since a given julian day
2486e45321fSGreg Roach     *
2496e45321fSGreg Roach     * @param Tree $tree Changes for which tree
25024319d9dSGreg Roach     * @param int  $days Number of days
2516e45321fSGreg Roach     *
252bd77bf38SGreg Roach     * @return Collection<stdClass> List of records with changes
2536e45321fSGreg Roach     */
254bd77bf38SGreg Roach    private function getRecentChanges(Tree $tree, int $days): Collection
255c1010edaSGreg Roach    {
256bd77bf38SGreg Roach        $subquery = DB::table('change')
25777654037SGreg Roach            ->where('gedcom_id', '=', $tree->id())
25877654037SGreg Roach            ->where('status', '=', 'accepted')
25977654037SGreg Roach            ->where('new_gedcom', '<>', '')
26077654037SGreg Roach            ->where('change_time', '>', Carbon::now()->subDays($days))
2617f5c2944SGreg Roach            ->groupBy(['xref'])
262bd77bf38SGreg Roach            ->select(new Expression('MAX(change_id) AS recent_change_id'));
263bd77bf38SGreg Roach
264bd77bf38SGreg Roach        $query = DB::table('change')
265bd77bf38SGreg Roach            ->joinSub($subquery, 'recent', 'recent_change_id', '=', 'change_id')
266bd77bf38SGreg Roach            ->select(['change.*']);
267bd77bf38SGreg Roach
268bd77bf38SGreg Roach        return $query
269bd77bf38SGreg Roach            ->get()
270bd77bf38SGreg Roach            ->map(function (stdClass $row) use ($tree): stdClass {
271bd77bf38SGreg Roach                return (object) [
272*01221f27SGreg Roach                    'record' => GedcomRecord::getInstance($row->xref, $tree, $row->new_gedcom),
273bd77bf38SGreg Roach                    'time'   => Carbon::create($row->change_time)->local(),
274bd77bf38SGreg Roach                    'user'   => $this->user_service->find($row->user_id),
275bd77bf38SGreg Roach                ];
27677654037SGreg Roach            })
277bd77bf38SGreg Roach            ->filter(static function (stdClass $row): bool {
278bd77bf38SGreg Roach                return $row->record instanceof GedcomRecord && $row->record->canShow();
279bd77bf38SGreg Roach            });
2806e45321fSGreg Roach    }
2818c2e8227SGreg Roach}
282