xref: /webtrees/app/Module/RecentChangesModule.php (revision eb235819d2dcd26d677ffd48caefaff86620ce99)
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
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
226e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
246e45321fSGreg Roachuse Fisharebest\Webtrees\Tree;
2577654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
261e7a7a28SGreg Roachuse Illuminate\Support\Str;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class RecentChangesModule
318c2e8227SGreg Roach */
3237eb8894SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleBlockTrait;
3549a243cbSGreg Roach
3616d6367aSGreg Roach    private const DEFAULT_DAYS       = '7';
3716d6367aSGreg Roach    private const DEFAULT_SHOW_USER  = '1';
3816d6367aSGreg Roach    private const DEFAULT_SORT_STYLE = 'date_desc';
3916d6367aSGreg Roach    private const DEFAULT_INFO_STYLE = 'table';
4016d6367aSGreg Roach    private const MAX_DAYS           = 90;
418c2e8227SGreg Roach
42961ec755SGreg Roach    /**
430cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
44961ec755SGreg Roach     *
45961ec755SGreg Roach     * @return string
46961ec755SGreg Roach     */
4749a243cbSGreg Roach    public function title(): string
48c1010edaSGreg Roach    {
49bbb76c12SGreg Roach        /* I18N: Name of a module */
50bbb76c12SGreg Roach        return I18N::translate('Recent changes');
518c2e8227SGreg Roach    }
528c2e8227SGreg Roach
53961ec755SGreg Roach    /**
54961ec755SGreg Roach     * A sentence describing what this module does.
55961ec755SGreg Roach     *
56961ec755SGreg Roach     * @return string
57961ec755SGreg Roach     */
5849a243cbSGreg Roach    public function description(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Description of the “Recent changes” module */
61bbb76c12SGreg Roach        return I18N::translate('A list of records that have been updated recently.');
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
646e45321fSGreg Roach    /** {@inheritdoc} */
655f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
66c1010edaSGreg Roach    {
6755664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
686e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
696e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
70047cb287SGreg Roach        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
718c2e8227SGreg Roach
72c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
738c2e8227SGreg Roach
74e490cd80SGreg Roach        $records = $this->getRecentChanges($tree, $days);
758c2e8227SGreg Roach
760280f44aSGreg Roach        switch ($sortStyle) {
770280f44aSGreg Roach            case 'name':
78c156e8f5SGreg Roach                uasort($records, GedcomRecord::nameComparator());
798c2e8227SGreg Roach                break;
8080e23601SGreg Roach
810280f44aSGreg Roach            case 'date_asc':
82c156e8f5SGreg Roach                uasort($records, GedcomRecord::lastChangeComparator());
838c2e8227SGreg Roach                break;
8480e23601SGreg Roach
850280f44aSGreg Roach            case 'date_desc':
86c156e8f5SGreg Roach                uasort($records, GedcomRecord::lastChangeComparator(-1));
878c2e8227SGreg Roach        }
880280f44aSGreg Roach
890280f44aSGreg Roach        if (empty($records)) {
900280f44aSGreg 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));
910280f44aSGreg Roach        } elseif ($infoStyle === 'list') {
924d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-list', [
934d2d7d1aSGreg Roach                'records'   => $records,
944d2d7d1aSGreg Roach                'show_user' => $show_user,
954d2d7d1aSGreg Roach            ]);
960280f44aSGreg Roach        } else {
974d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-table', [
980280f44aSGreg Roach                'records'   => $records,
990280f44aSGreg Roach                'show_user' => $show_user,
1000280f44aSGreg Roach            ]);
1018c2e8227SGreg Roach        }
1028c2e8227SGreg Roach
1036a8879feSGreg Roach        if ($ctype !== '') {
104e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
105c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
106c1010edaSGreg Roach                    'block_id' => $block_id,
107aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
108c1010edaSGreg Roach                ]);
109397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
110c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
111c1010edaSGreg Roach                    'block_id' => $block_id,
112aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
113c1010edaSGreg Roach                ]);
1148cbbfdceSGreg Roach            } else {
1158cbbfdceSGreg Roach                $config_url = '';
1168cbbfdceSGreg Roach            }
1178cbbfdceSGreg Roach
118147e99aaSGreg Roach            return view('modules/block-template', [
1191e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1209c6524dcSGreg Roach                'id'         => $block_id,
1218cbbfdceSGreg Roach                'config_url' => $config_url,
1228cbbfdceSGreg Roach                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
1239c6524dcSGreg Roach                'content'    => $content,
1249c6524dcSGreg Roach            ]);
1258c2e8227SGreg Roach        }
126b2ce94c6SRico Sonntag
127b2ce94c6SRico Sonntag        return $content;
1288c2e8227SGreg Roach    }
1298c2e8227SGreg Roach
1308c2e8227SGreg Roach    /** {@inheritdoc} */
131c1010edaSGreg Roach    public function loadAjax(): bool
132c1010edaSGreg Roach    {
1338c2e8227SGreg Roach        return true;
1348c2e8227SGreg Roach    }
1358c2e8227SGreg Roach
1368c2e8227SGreg Roach    /** {@inheritdoc} */
137c1010edaSGreg Roach    public function isUserBlock(): bool
138c1010edaSGreg Roach    {
1398c2e8227SGreg Roach        return true;
1408c2e8227SGreg Roach    }
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach    /** {@inheritdoc} */
14363276d8fSGreg Roach    public function isTreeBlock(): bool
144c1010edaSGreg Roach    {
1458c2e8227SGreg Roach        return true;
1468c2e8227SGreg Roach    }
1478c2e8227SGreg Roach
14820ac4041SGreg Roach    /**
149a45f9889SGreg Roach     * Update the configuration for a block.
150a45f9889SGreg Roach     *
1516ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
152a45f9889SGreg Roach     * @param int     $block_id
153a45f9889SGreg Roach     *
154a45f9889SGreg Roach     * @return void
155a45f9889SGreg Roach     */
1566ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
157a45f9889SGreg Roach    {
158*eb235819SGreg Roach        $params = $request->getParsedBody();
159af2b6902SGreg Roach
160*eb235819SGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
161*eb235819SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
162*eb235819SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
163*eb235819SGreg Roach        $this->setBlockSetting($block_id, 'show_user', $params['show_user']);
164a45f9889SGreg Roach    }
165a45f9889SGreg Roach
166a45f9889SGreg Roach    /**
16720ac4041SGreg Roach     * An HTML form to edit block settings
16820ac4041SGreg Roach     *
16920ac4041SGreg Roach     * @param Tree $tree
17020ac4041SGreg Roach     * @param int  $block_id
17120ac4041SGreg Roach     *
17220ac4041SGreg Roach     * @return void
17320ac4041SGreg Roach     */
174e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
175c1010edaSGreg Roach    {
17655664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
1776e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
1786e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
1796e45321fSGreg Roach        $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
1808c2e8227SGreg Roach
181c385536dSGreg Roach        $info_styles = [
182bbb76c12SGreg Roach            /* I18N: An option in a list-box */
183bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
184bbb76c12SGreg Roach            /* I18N: An option in a list-box */
185bbb76c12SGreg Roach            'table' => I18N::translate('table'),
186c385536dSGreg Roach        ];
1878c2e8227SGreg Roach
188c385536dSGreg Roach        $sort_styles = [
189bbb76c12SGreg Roach            /* I18N: An option in a list-box */
190bbb76c12SGreg Roach            'name'      => I18N::translate('sort by name'),
191bbb76c12SGreg Roach            /* I18N: An option in a list-box */
192bbb76c12SGreg Roach            'date_asc'  => I18N::translate('sort by date, oldest first'),
193bbb76c12SGreg Roach            /* I18N: An option in a list-box */
194bbb76c12SGreg Roach            'date_desc' => I18N::translate('sort by date, newest first'),
195c385536dSGreg Roach        ];
1968c2e8227SGreg Roach
197147e99aaSGreg Roach        echo view('modules/recent_changes/config', [
198c385536dSGreg Roach            'days'        => $days,
199c385536dSGreg Roach            'infoStyle'   => $infoStyle,
200c385536dSGreg Roach            'info_styles' => $info_styles,
201c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
202c385536dSGreg Roach            'sortStyle'   => $sortStyle,
203c385536dSGreg Roach            'sort_styles' => $sort_styles,
204c385536dSGreg Roach            'show_user'   => $show_user,
205c385536dSGreg Roach        ]);
2068c2e8227SGreg Roach    }
2078c2e8227SGreg Roach
2086e45321fSGreg Roach    /**
2096e45321fSGreg Roach     * Find records that have changed since a given julian day
2106e45321fSGreg Roach     *
2116e45321fSGreg Roach     * @param Tree $tree Changes for which tree
21224319d9dSGreg Roach     * @param int  $days Number of days
2136e45321fSGreg Roach     *
2146e45321fSGreg Roach     * @return GedcomRecord[] List of records with changes
2156e45321fSGreg Roach     */
21655664801SGreg Roach    private function getRecentChanges(Tree $tree, int $days): array
217c1010edaSGreg Roach    {
21877654037SGreg Roach        return DB::table('change')
21977654037SGreg Roach            ->where('gedcom_id', '=', $tree->id())
22077654037SGreg Roach            ->where('status', '=', 'accepted')
22177654037SGreg Roach            ->where('new_gedcom', '<>', '')
22277654037SGreg Roach            ->where('change_time', '>', Carbon::now()->subDays($days))
22377654037SGreg Roach            ->groupBy('xref')
22477654037SGreg Roach            ->pluck('xref')
2250b5fd0a6SGreg Roach            ->map(static function (string $xref) use ($tree): ?GedcomRecord {
22677654037SGreg Roach                return GedcomRecord::getInstance($xref, $tree);
22777654037SGreg Roach            })
2280b5fd0a6SGreg Roach            ->filter(static function (?GedcomRecord $record): bool {
22977654037SGreg Roach                return $record instanceof GedcomRecord && $record->canShow();
23077654037SGreg Roach            })
23177654037SGreg Roach            ->all();
2326e45321fSGreg Roach    }
2338c2e8227SGreg Roach}
234