xref: /webtrees/app/Module/NotesTabModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Fact;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
252adcbd9aSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
268eaf8709SGreg Roachuse Illuminate\Support\Collection;
278c2e8227SGreg Roach
28b315f3e1SGreg Roachuse function preg_match;
29b315f3e1SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class NotesTabModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass NotesTabModule extends AbstractModule implements ModuleTabInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleTabTrait;
3649a243cbSGreg Roach
3776d39c55SGreg Roach    /** @var Collection<array-key,Fact>|null  */
38*1ff45046SGreg Roach    private Collection|null $facts = null;
398c2e8227SGreg Roach
4043f2f523SGreg Roach    private ClipboardService $clipboard_service;
412adcbd9aSGreg Roach
422adcbd9aSGreg Roach    /**
432adcbd9aSGreg Roach     * @param ClipboardService $clipboard_service
442adcbd9aSGreg Roach     */
45713a37fbSGreg Roach    public function __construct(ClipboardService $clipboard_service)
46713a37fbSGreg Roach    {
472adcbd9aSGreg Roach        $this->clipboard_service = $clipboard_service;
482adcbd9aSGreg Roach    }
492adcbd9aSGreg Roach
50961ec755SGreg Roach    /**
510cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
52961ec755SGreg Roach     *
53961ec755SGreg Roach     * @return string
54961ec755SGreg Roach     */
5549a243cbSGreg Roach    public function title(): string
56c1010edaSGreg Roach    {
57bbb76c12SGreg Roach        /* I18N: Name of a module */
58bbb76c12SGreg Roach        return I18N::translate('Notes');
598c2e8227SGreg Roach    }
608c2e8227SGreg Roach
6149a243cbSGreg Roach    public function description(): string
62c1010edaSGreg Roach    {
63bbb76c12SGreg Roach        /* I18N: Description of the “Notes” module */
64bbb76c12SGreg Roach        return I18N::translate('A tab showing the notes attached to an individual.');
658c2e8227SGreg Roach    }
668c2e8227SGreg Roach
6749a243cbSGreg Roach    /**
6849a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
6949a243cbSGreg Roach     *
7049a243cbSGreg Roach     * @return int
7149a243cbSGreg Roach     */
72cbf4b7faSGreg Roach    public function defaultTabOrder(): int
73cbf4b7faSGreg Roach    {
74fb7a0427SGreg Roach        return 4;
758c2e8227SGreg Roach    }
768c2e8227SGreg Roach
773caaa4d2SGreg Roach    /**
783caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
793caaa4d2SGreg Roach     *
803caaa4d2SGreg Roach     * @param Individual $individual
813caaa4d2SGreg Roach     *
823caaa4d2SGreg Roach     * @return bool
833caaa4d2SGreg Roach     */
848f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
85c1010edaSGreg Roach    {
868af3e5c1SGreg Roach        return $individual->canEdit() || $this->getFactsWithNotes($individual)->isNotEmpty();
878c2e8227SGreg Roach    }
888c2e8227SGreg Roach
893caaa4d2SGreg Roach    /**
903caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
913caaa4d2SGreg Roach     * options to create content.
923caaa4d2SGreg Roach     *
933caaa4d2SGreg Roach     * @param Individual $individual
943caaa4d2SGreg Roach     *
953caaa4d2SGreg Roach     * @return bool
963caaa4d2SGreg Roach     */
978f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
98c1010edaSGreg Roach    {
998af3e5c1SGreg Roach        return $this->getFactsWithNotes($individual)->isEmpty();
1008c2e8227SGreg Roach    }
1018c2e8227SGreg Roach
1023caaa4d2SGreg Roach    /**
1033caaa4d2SGreg Roach     * Generate the HTML content of this tab.
1043caaa4d2SGreg Roach     *
1053caaa4d2SGreg Roach     * @param Individual $individual
1063caaa4d2SGreg Roach     *
1073caaa4d2SGreg Roach     * @return string
1083caaa4d2SGreg Roach     */
1099b34404bSGreg Roach    public function getTabContent(Individual $individual): string
110c1010edaSGreg Roach    {
111a8cd57e1SGreg Roach        return view('modules/notes/tab', [
112225e381fSGreg Roach            'can_edit'        => $individual->canEdit(),
1132adcbd9aSGreg Roach            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
114225e381fSGreg Roach            'individual'      => $individual,
115225e381fSGreg Roach            'facts'           => $this->getFactsWithNotes($individual),
116225e381fSGreg Roach        ]);
1178c2e8227SGreg Roach    }
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach    /**
1208c2e8227SGreg Roach     * Get all the facts for an individual which contain notes.
1218c2e8227SGreg Roach     *
122225e381fSGreg Roach     * @param Individual $individual
123225e381fSGreg Roach     *
12436779af1SGreg Roach     * @return Collection<int,Fact>
1258c2e8227SGreg Roach     */
1268af3e5c1SGreg Roach    private function getFactsWithNotes(Individual $individual): Collection
127c1010edaSGreg Roach    {
1288c2e8227SGreg Roach        if ($this->facts === null) {
12930158ae7SGreg Roach            $facts = $individual->facts();
1308af3e5c1SGreg Roach
13139ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
1328c2e8227SGreg Roach                if ($family->canShow()) {
13330158ae7SGreg Roach                    foreach ($family->facts() as $fact) {
13439ca88baSGreg Roach                        $facts->push($fact);
1358c2e8227SGreg Roach                    }
1368c2e8227SGreg Roach                }
1378c2e8227SGreg Roach            }
1388af3e5c1SGreg Roach
139b315f3e1SGreg Roach            $callback = static fn (Fact $fact): bool => preg_match('/(?:^1|\n\d) NOTE/', $fact->gedcom()) === 1;
1408af3e5c1SGreg Roach
141b315f3e1SGreg Roach            $this->facts = $facts->filter($callback);
142b315f3e1SGreg Roach
1438af3e5c1SGreg Roach            $this->facts = Fact::sortFacts($this->facts);
1448c2e8227SGreg Roach        }
1458c2e8227SGreg Roach
1468c2e8227SGreg Roach        return $this->facts;
1478c2e8227SGreg Roach    }
1488c2e8227SGreg Roach
1493caaa4d2SGreg Roach    /**
1503caaa4d2SGreg Roach     * Can this tab load asynchronously?
1513caaa4d2SGreg Roach     *
1523caaa4d2SGreg Roach     * @return bool
1533caaa4d2SGreg Roach     */
1548f53f488SRico Sonntag    public function canLoadAjax(): bool
155c1010edaSGreg Roach    {
15615d603e7SGreg Roach        return false;
1578c2e8227SGreg Roach    }
1588eaf8709SGreg Roach
1598eaf8709SGreg Roach    /**
1608eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1618eaf8709SGreg Roach     *
16236779af1SGreg Roach     * @return Collection<int,string>
1638eaf8709SGreg Roach     */
1648eaf8709SGreg Roach    public function supportedFacts(): Collection
1658eaf8709SGreg Roach    {
166d0889c63SGreg Roach        return new Collection(['INDI:NOTE', 'FAM:NOTE']);
1678eaf8709SGreg Roach    }
1688c2e8227SGreg Roach}
169