xref: /webtrees/app/Module/RecentChangesModule.php (revision 7f5c2944f84cdc914a20ed1e961f9b81e401f78b)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
204459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
216e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
236e45321fSGreg Roachuse Fisharebest\Webtrees\Tree;
2477654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
251e7a7a28SGreg Roachuse Illuminate\Support\Str;
266ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class RecentChangesModule
308c2e8227SGreg Roach */
3137eb8894SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
3516d6367aSGreg Roach    private const DEFAULT_DAYS       = '7';
3616d6367aSGreg Roach    private const DEFAULT_SHOW_USER  = '1';
3716d6367aSGreg Roach    private const DEFAULT_SORT_STYLE = 'date_desc';
3816d6367aSGreg Roach    private const DEFAULT_INFO_STYLE = 'table';
3916d6367aSGreg Roach    private const MAX_DAYS           = 90;
408c2e8227SGreg Roach
41961ec755SGreg Roach    /**
420cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
43961ec755SGreg Roach     *
44961ec755SGreg Roach     * @return string
45961ec755SGreg Roach     */
4649a243cbSGreg Roach    public function title(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Name of a module */
49bbb76c12SGreg Roach        return I18N::translate('Recent changes');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
52961ec755SGreg Roach    /**
53961ec755SGreg Roach     * A sentence describing what this module does.
54961ec755SGreg Roach     *
55961ec755SGreg Roach     * @return string
56961ec755SGreg Roach     */
5749a243cbSGreg Roach    public function description(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Description of the “Recent changes” module */
60bbb76c12SGreg Roach        return I18N::translate('A list of records that have been updated recently.');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
636e45321fSGreg Roach    /** {@inheritdoc} */
643caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
65c1010edaSGreg Roach    {
6655664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
676e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
686e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
69047cb287SGreg Roach        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
708c2e8227SGreg Roach
713caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
728c2e8227SGreg Roach
73e490cd80SGreg Roach        $records = $this->getRecentChanges($tree, $days);
748c2e8227SGreg Roach
750280f44aSGreg Roach        switch ($sortStyle) {
760280f44aSGreg Roach            case 'name':
77c156e8f5SGreg Roach                uasort($records, GedcomRecord::nameComparator());
788c2e8227SGreg Roach                break;
7980e23601SGreg Roach
800280f44aSGreg Roach            case 'date_asc':
81c156e8f5SGreg Roach                uasort($records, GedcomRecord::lastChangeComparator());
828c2e8227SGreg Roach                break;
8380e23601SGreg Roach
840280f44aSGreg Roach            case 'date_desc':
85c156e8f5SGreg Roach                uasort($records, GedcomRecord::lastChangeComparator(-1));
868c2e8227SGreg Roach        }
870280f44aSGreg Roach
880280f44aSGreg Roach        if (empty($records)) {
890280f44aSGreg 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));
900280f44aSGreg Roach        } elseif ($infoStyle === 'list') {
914d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-list', [
924d2d7d1aSGreg Roach                'records'   => $records,
934d2d7d1aSGreg Roach                'show_user' => $show_user,
944d2d7d1aSGreg Roach            ]);
950280f44aSGreg Roach        } else {
964d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-table', [
970280f44aSGreg Roach                'records'   => $records,
980280f44aSGreg Roach                'show_user' => $show_user,
990280f44aSGreg Roach            ]);
1008c2e8227SGreg Roach        }
1018c2e8227SGreg Roach
1023caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
103147e99aaSGreg Roach            return view('modules/block-template', [
1041e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1059c6524dcSGreg Roach                'id'         => $block_id,
1063caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1078cbbfdceSGreg Roach                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
1089c6524dcSGreg Roach                'content'    => $content,
1099c6524dcSGreg Roach            ]);
1108c2e8227SGreg Roach        }
111b2ce94c6SRico Sonntag
112b2ce94c6SRico Sonntag        return $content;
1138c2e8227SGreg Roach    }
1148c2e8227SGreg Roach
1153caaa4d2SGreg Roach    /**
1163caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1173caaa4d2SGreg Roach     *
1183caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1193caaa4d2SGreg Roach     *
1203caaa4d2SGreg Roach     * @return bool
1213caaa4d2SGreg Roach     */
122c1010edaSGreg Roach    public function loadAjax(): bool
123c1010edaSGreg Roach    {
1248c2e8227SGreg Roach        return true;
1258c2e8227SGreg Roach    }
1268c2e8227SGreg Roach
1273caaa4d2SGreg Roach    /**
1283caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1293caaa4d2SGreg Roach     *
1303caaa4d2SGreg Roach     * @return bool
1313caaa4d2SGreg Roach     */
132c1010edaSGreg Roach    public function isUserBlock(): bool
133c1010edaSGreg Roach    {
1348c2e8227SGreg Roach        return true;
1358c2e8227SGreg Roach    }
1368c2e8227SGreg Roach
1373caaa4d2SGreg Roach    /**
1383caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1393caaa4d2SGreg Roach     *
1403caaa4d2SGreg Roach     * @return bool
1413caaa4d2SGreg Roach     */
14263276d8fSGreg Roach    public function isTreeBlock(): bool
143c1010edaSGreg Roach    {
1448c2e8227SGreg Roach        return true;
1458c2e8227SGreg Roach    }
1468c2e8227SGreg Roach
14720ac4041SGreg Roach    /**
148a45f9889SGreg Roach     * Update the configuration for a block.
149a45f9889SGreg Roach     *
1506ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
151a45f9889SGreg Roach     * @param int     $block_id
152a45f9889SGreg Roach     *
153a45f9889SGreg Roach     * @return void
154a45f9889SGreg Roach     */
1556ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
156a45f9889SGreg Roach    {
157eb235819SGreg Roach        $params = $request->getParsedBody();
158af2b6902SGreg Roach
159eb235819SGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
160eb235819SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
161eb235819SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
162eb235819SGreg Roach        $this->setBlockSetting($block_id, 'show_user', $params['show_user']);
163a45f9889SGreg Roach    }
164a45f9889SGreg Roach
165a45f9889SGreg Roach    /**
16620ac4041SGreg Roach     * An HTML form to edit block settings
16720ac4041SGreg Roach     *
16820ac4041SGreg Roach     * @param Tree $tree
16920ac4041SGreg Roach     * @param int  $block_id
17020ac4041SGreg Roach     *
1713caaa4d2SGreg Roach     * @return string
17220ac4041SGreg Roach     */
1733caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
174c1010edaSGreg Roach    {
17555664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
1766e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
1776e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
1786e45321fSGreg Roach        $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
1798c2e8227SGreg Roach
180c385536dSGreg Roach        $info_styles = [
181bbb76c12SGreg Roach            /* I18N: An option in a list-box */
182bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
183bbb76c12SGreg Roach            /* I18N: An option in a list-box */
184bbb76c12SGreg Roach            'table' => I18N::translate('table'),
185c385536dSGreg Roach        ];
1868c2e8227SGreg Roach
187c385536dSGreg Roach        $sort_styles = [
188bbb76c12SGreg Roach            /* I18N: An option in a list-box */
189bbb76c12SGreg Roach            'name'      => I18N::translate('sort by name'),
190bbb76c12SGreg Roach            /* I18N: An option in a list-box */
191bbb76c12SGreg Roach            'date_asc'  => I18N::translate('sort by date, oldest first'),
192bbb76c12SGreg Roach            /* I18N: An option in a list-box */
193bbb76c12SGreg Roach            'date_desc' => I18N::translate('sort by date, newest first'),
194c385536dSGreg Roach        ];
1958c2e8227SGreg Roach
1963caaa4d2SGreg Roach        return view('modules/recent_changes/config', [
197c385536dSGreg Roach            'days'        => $days,
198c385536dSGreg Roach            'infoStyle'   => $infoStyle,
199c385536dSGreg Roach            'info_styles' => $info_styles,
200c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
201c385536dSGreg Roach            'sortStyle'   => $sortStyle,
202c385536dSGreg Roach            'sort_styles' => $sort_styles,
203c385536dSGreg Roach            'show_user'   => $show_user,
204c385536dSGreg Roach        ]);
2058c2e8227SGreg Roach    }
2068c2e8227SGreg Roach
2076e45321fSGreg Roach    /**
2086e45321fSGreg Roach     * Find records that have changed since a given julian day
2096e45321fSGreg Roach     *
2106e45321fSGreg Roach     * @param Tree $tree Changes for which tree
21124319d9dSGreg Roach     * @param int  $days Number of days
2126e45321fSGreg Roach     *
2136e45321fSGreg Roach     * @return GedcomRecord[] List of records with changes
2146e45321fSGreg Roach     */
21555664801SGreg Roach    private function getRecentChanges(Tree $tree, int $days): array
216c1010edaSGreg Roach    {
21777654037SGreg Roach        return DB::table('change')
21877654037SGreg Roach            ->where('gedcom_id', '=', $tree->id())
21977654037SGreg Roach            ->where('status', '=', 'accepted')
22077654037SGreg Roach            ->where('new_gedcom', '<>', '')
22177654037SGreg Roach            ->where('change_time', '>', Carbon::now()->subDays($days))
222*7f5c2944SGreg Roach            ->groupBy(['xref'])
22377654037SGreg Roach            ->pluck('xref')
2240b5fd0a6SGreg Roach            ->map(static function (string $xref) use ($tree): ?GedcomRecord {
22577654037SGreg Roach                return GedcomRecord::getInstance($xref, $tree);
22677654037SGreg Roach            })
2270b5fd0a6SGreg Roach            ->filter(static function (?GedcomRecord $record): bool {
22877654037SGreg Roach                return $record instanceof GedcomRecord && $record->canShow();
22977654037SGreg Roach            })
23077654037SGreg Roach            ->all();
2316e45321fSGreg Roach    }
2328c2e8227SGreg Roach}
233