xref: /webtrees/app/Module/NotesTabModule.php (revision 49a243cb5fe7c21b24e262552d556b018bfe3f41)
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\Fact;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class NotesTabModule
278c2e8227SGreg Roach */
28*49a243cbSGreg Roachclass NotesTabModule extends AbstractModule implements ModuleInterface, ModuleTabInterface
29c1010edaSGreg Roach{
30*49a243cbSGreg Roach    use ModuleTabTrait;
31*49a243cbSGreg Roach
3276692c8bSGreg Roach    /** @var Fact[] A list facts for this note. */
338c2e8227SGreg Roach    private $facts;
348c2e8227SGreg Roach
358c2e8227SGreg Roach    /** {@inheritdoc} */
36*49a243cbSGreg Roach    public function title(): string
37c1010edaSGreg Roach    {
38bbb76c12SGreg Roach        /* I18N: Name of a module */
39bbb76c12SGreg Roach        return I18N::translate('Notes');
408c2e8227SGreg Roach    }
418c2e8227SGreg Roach
428c2e8227SGreg Roach    /** {@inheritdoc} */
43*49a243cbSGreg Roach    public function description(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Description of the “Notes” module */
46bbb76c12SGreg Roach        return I18N::translate('A tab showing the notes attached to an individual.');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
49*49a243cbSGreg Roach    /**
50*49a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
51*49a243cbSGreg Roach     *
52*49a243cbSGreg Roach     * @return int
53*49a243cbSGreg Roach     */
54*49a243cbSGreg Roach    function defaultTabOrder(): int {
55*49a243cbSGreg Roach        return 50;
568c2e8227SGreg Roach    }
578c2e8227SGreg Roach
588c2e8227SGreg Roach    /** {@inheritdoc} */
598f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
60c1010edaSGreg Roach    {
61225e381fSGreg Roach        return $individual->canEdit() || $this->getFactsWithNotes($individual);
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
648c2e8227SGreg Roach    /** {@inheritdoc} */
658f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
66c1010edaSGreg Roach    {
67225e381fSGreg Roach        return !$this->getFactsWithNotes($individual);
688c2e8227SGreg Roach    }
698c2e8227SGreg Roach
708c2e8227SGreg Roach    /** {@inheritdoc} */
719b34404bSGreg Roach    public function getTabContent(Individual $individual): string
72c1010edaSGreg Roach    {
73a8cd57e1SGreg Roach        return view('modules/notes/tab', [
74225e381fSGreg Roach            'can_edit'   => $individual->canEdit(),
75225e381fSGreg Roach            'individual' => $individual,
76225e381fSGreg Roach            'facts'      => $this->getFactsWithNotes($individual),
77225e381fSGreg Roach        ]);
788c2e8227SGreg Roach    }
798c2e8227SGreg Roach
808c2e8227SGreg Roach    /**
818c2e8227SGreg Roach     * Get all the facts for an individual which contain notes.
828c2e8227SGreg Roach     *
83225e381fSGreg Roach     * @param Individual $individual
84225e381fSGreg Roach     *
858c2e8227SGreg Roach     * @return Fact[]
868c2e8227SGreg Roach     */
878f53f488SRico Sonntag    private function getFactsWithNotes(Individual $individual): array
88c1010edaSGreg Roach    {
898c2e8227SGreg Roach        if ($this->facts === null) {
9030158ae7SGreg Roach            $facts = $individual->facts();
91225e381fSGreg Roach            foreach ($individual->getSpouseFamilies() as $family) {
928c2e8227SGreg Roach                if ($family->canShow()) {
9330158ae7SGreg Roach                    foreach ($family->facts() as $fact) {
948c2e8227SGreg Roach                        $facts[] = $fact;
958c2e8227SGreg Roach                    }
968c2e8227SGreg Roach                }
978c2e8227SGreg Roach            }
9813abd6f3SGreg Roach            $this->facts = [];
998c2e8227SGreg Roach            foreach ($facts as $fact) {
100138ca96cSGreg Roach                if (preg_match('/(?:^1|\n\d) NOTE/', $fact->gedcom())) {
1018c2e8227SGreg Roach                    $this->facts[] = $fact;
1028c2e8227SGreg Roach                }
1038c2e8227SGreg Roach            }
1043d7a8a4cSGreg Roach            Functions::sortFacts($this->facts);
1058c2e8227SGreg Roach        }
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach        return $this->facts;
1088c2e8227SGreg Roach    }
1098c2e8227SGreg Roach
1108c2e8227SGreg Roach    /** {@inheritdoc} */
1118f53f488SRico Sonntag    public function canLoadAjax(): bool
112c1010edaSGreg Roach    {
11315d603e7SGreg Roach        return false;
1148c2e8227SGreg Roach    }
1158c2e8227SGreg Roach}
116