xref: /webtrees/app/Module/RecentChangesModule.php (revision b46c87bda4b592cf9252f1db48552a820b1e3d97)
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;
256e45321fSGreg Roachuse Fisharebest\Webtrees\Tree;
2677654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class RecentChangesModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
3716d6367aSGreg Roach    private const DEFAULT_DAYS       = '7';
3816d6367aSGreg Roach    private const DEFAULT_SHOW_USER  = '1';
3916d6367aSGreg Roach    private const DEFAULT_SORT_STYLE = 'date_desc';
4016d6367aSGreg Roach    private const DEFAULT_INFO_STYLE = 'table';
4116d6367aSGreg Roach    private const MAX_DAYS           = 90;
428c2e8227SGreg Roach
43961ec755SGreg Roach    /**
440cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
45961ec755SGreg Roach     *
46961ec755SGreg Roach     * @return string
47961ec755SGreg Roach     */
4849a243cbSGreg Roach    public function title(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Name of a module */
51bbb76c12SGreg Roach        return I18N::translate('Recent changes');
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
54961ec755SGreg Roach    /**
55961ec755SGreg Roach     * A sentence describing what this module does.
56961ec755SGreg Roach     *
57961ec755SGreg Roach     * @return string
58961ec755SGreg Roach     */
5949a243cbSGreg Roach    public function description(): string
60c1010edaSGreg Roach    {
61bbb76c12SGreg Roach        /* I18N: Description of the “Recent changes” module */
62bbb76c12SGreg Roach        return I18N::translate('A list of records that have been updated recently.');
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
656e45321fSGreg Roach    /** {@inheritdoc} */
663caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
67c1010edaSGreg Roach    {
6855664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
696e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
706e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
71047cb287SGreg Roach        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
728c2e8227SGreg Roach
733caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
748c2e8227SGreg Roach
75e490cd80SGreg Roach        $records = $this->getRecentChanges($tree, $days);
768c2e8227SGreg Roach
770280f44aSGreg Roach        switch ($sortStyle) {
780280f44aSGreg Roach            case 'name':
79c156e8f5SGreg Roach                uasort($records, GedcomRecord::nameComparator());
808c2e8227SGreg Roach                break;
8180e23601SGreg Roach
820280f44aSGreg Roach            case 'date_asc':
83c156e8f5SGreg Roach                uasort($records, GedcomRecord::lastChangeComparator());
848c2e8227SGreg Roach                break;
8580e23601SGreg Roach
860280f44aSGreg Roach            case 'date_desc':
87c156e8f5SGreg Roach                uasort($records, GedcomRecord::lastChangeComparator(-1));
888c2e8227SGreg Roach        }
890280f44aSGreg Roach
90b2448a1bSGreg Roach        if ($records === []) {
910280f44aSGreg 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));
920280f44aSGreg Roach        } elseif ($infoStyle === 'list') {
934d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-list', [
944d2d7d1aSGreg Roach                'records'   => $records,
954d2d7d1aSGreg Roach                'show_user' => $show_user,
964d2d7d1aSGreg Roach            ]);
970280f44aSGreg Roach        } else {
984d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-table', [
990280f44aSGreg Roach                'records'   => $records,
1000280f44aSGreg Roach                'show_user' => $show_user,
1010280f44aSGreg Roach            ]);
1028c2e8227SGreg Roach        }
1038c2e8227SGreg Roach
1043caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
105147e99aaSGreg Roach            return view('modules/block-template', [
1061e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1079c6524dcSGreg Roach                'id'         => $block_id,
1083caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1098cbbfdceSGreg Roach                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
1109c6524dcSGreg Roach                'content'    => $content,
1119c6524dcSGreg Roach            ]);
1128c2e8227SGreg Roach        }
113b2ce94c6SRico Sonntag
114b2ce94c6SRico Sonntag        return $content;
1158c2e8227SGreg Roach    }
1168c2e8227SGreg Roach
1173caaa4d2SGreg Roach    /**
1183caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1193caaa4d2SGreg Roach     *
1203caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1213caaa4d2SGreg Roach     *
1223caaa4d2SGreg Roach     * @return bool
1233caaa4d2SGreg Roach     */
124c1010edaSGreg Roach    public function loadAjax(): bool
125c1010edaSGreg Roach    {
1268c2e8227SGreg Roach        return true;
1278c2e8227SGreg Roach    }
1288c2e8227SGreg Roach
1293caaa4d2SGreg Roach    /**
1303caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1313caaa4d2SGreg Roach     *
1323caaa4d2SGreg Roach     * @return bool
1333caaa4d2SGreg Roach     */
134c1010edaSGreg Roach    public function isUserBlock(): bool
135c1010edaSGreg Roach    {
1368c2e8227SGreg Roach        return true;
1378c2e8227SGreg Roach    }
1388c2e8227SGreg Roach
1393caaa4d2SGreg Roach    /**
1403caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1413caaa4d2SGreg Roach     *
1423caaa4d2SGreg Roach     * @return bool
1433caaa4d2SGreg Roach     */
14463276d8fSGreg Roach    public function isTreeBlock(): bool
145c1010edaSGreg Roach    {
1468c2e8227SGreg Roach        return true;
1478c2e8227SGreg Roach    }
1488c2e8227SGreg Roach
14920ac4041SGreg Roach    /**
150a45f9889SGreg Roach     * Update the configuration for a block.
151a45f9889SGreg Roach     *
1526ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
153a45f9889SGreg Roach     * @param int     $block_id
154a45f9889SGreg Roach     *
155a45f9889SGreg Roach     * @return void
156a45f9889SGreg Roach     */
1576ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
158a45f9889SGreg Roach    {
159*b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
160af2b6902SGreg Roach
161eb235819SGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
162eb235819SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
163eb235819SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
164eb235819SGreg Roach        $this->setBlockSetting($block_id, 'show_user', $params['show_user']);
165a45f9889SGreg Roach    }
166a45f9889SGreg Roach
167a45f9889SGreg Roach    /**
16820ac4041SGreg Roach     * An HTML form to edit block settings
16920ac4041SGreg Roach     *
17020ac4041SGreg Roach     * @param Tree $tree
17120ac4041SGreg Roach     * @param int  $block_id
17220ac4041SGreg Roach     *
1733caaa4d2SGreg Roach     * @return string
17420ac4041SGreg Roach     */
1753caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
176c1010edaSGreg Roach    {
17755664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
1786e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
1796e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
1806e45321fSGreg Roach        $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
1818c2e8227SGreg Roach
182c385536dSGreg Roach        $info_styles = [
183bbb76c12SGreg Roach            /* I18N: An option in a list-box */
184bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
185bbb76c12SGreg Roach            /* I18N: An option in a list-box */
186bbb76c12SGreg Roach            'table' => I18N::translate('table'),
187c385536dSGreg Roach        ];
1888c2e8227SGreg Roach
189c385536dSGreg Roach        $sort_styles = [
190bbb76c12SGreg Roach            /* I18N: An option in a list-box */
191bbb76c12SGreg Roach            'name'      => I18N::translate('sort by name'),
192bbb76c12SGreg Roach            /* I18N: An option in a list-box */
193bbb76c12SGreg Roach            'date_asc'  => I18N::translate('sort by date, oldest first'),
194bbb76c12SGreg Roach            /* I18N: An option in a list-box */
195bbb76c12SGreg Roach            'date_desc' => I18N::translate('sort by date, newest first'),
196c385536dSGreg Roach        ];
1978c2e8227SGreg Roach
1983caaa4d2SGreg Roach        return view('modules/recent_changes/config', [
199c385536dSGreg Roach            'days'        => $days,
200c385536dSGreg Roach            'infoStyle'   => $infoStyle,
201c385536dSGreg Roach            'info_styles' => $info_styles,
202c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
203c385536dSGreg Roach            'sortStyle'   => $sortStyle,
204c385536dSGreg Roach            'sort_styles' => $sort_styles,
205c385536dSGreg Roach            'show_user'   => $show_user,
206c385536dSGreg Roach        ]);
2078c2e8227SGreg Roach    }
2088c2e8227SGreg Roach
2096e45321fSGreg Roach    /**
2106e45321fSGreg Roach     * Find records that have changed since a given julian day
2116e45321fSGreg Roach     *
2126e45321fSGreg Roach     * @param Tree $tree Changes for which tree
21324319d9dSGreg Roach     * @param int  $days Number of days
2146e45321fSGreg Roach     *
2156e45321fSGreg Roach     * @return GedcomRecord[] List of records with changes
2166e45321fSGreg Roach     */
21755664801SGreg Roach    private function getRecentChanges(Tree $tree, int $days): array
218c1010edaSGreg Roach    {
21977654037SGreg Roach        return DB::table('change')
22077654037SGreg Roach            ->where('gedcom_id', '=', $tree->id())
22177654037SGreg Roach            ->where('status', '=', 'accepted')
22277654037SGreg Roach            ->where('new_gedcom', '<>', '')
22377654037SGreg Roach            ->where('change_time', '>', Carbon::now()->subDays($days))
2247f5c2944SGreg Roach            ->groupBy(['xref'])
22577654037SGreg Roach            ->pluck('xref')
2260b5fd0a6SGreg Roach            ->map(static function (string $xref) use ($tree): ?GedcomRecord {
22777654037SGreg Roach                return GedcomRecord::getInstance($xref, $tree);
22877654037SGreg Roach            })
2290b5fd0a6SGreg Roach            ->filter(static function (?GedcomRecord $record): bool {
23077654037SGreg Roach                return $record instanceof GedcomRecord && $record->canShow();
23177654037SGreg Roach            })
23277654037SGreg Roach            ->all();
2336e45321fSGreg Roach    }
2348c2e8227SGreg Roach}
235