xref: /webtrees/app/Module/NotesTabModule.php (revision a45f98897789fc9ff88705eb09ae5f037bf49c10)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Fact;
19use Fisharebest\Webtrees\Functions\Functions;
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Individual;
22
23/**
24 * Class NotesTabModule
25 */
26class NotesTabModule extends AbstractModule implements ModuleTabInterface
27{
28    /** @var Fact[] A list facts for this note. */
29    private $facts;
30
31    /** {@inheritdoc} */
32    public function getTitle()
33    {
34        /* I18N: Name of a module */
35        return I18N::translate('Notes');
36    }
37
38    /** {@inheritdoc} */
39    public function getDescription()
40    {
41        /* I18N: Description of the “Notes” module */
42        return I18N::translate('A tab showing the notes attached to an individual.');
43    }
44
45    /** {@inheritdoc} */
46    public function defaultTabOrder()
47    {
48        return 40;
49    }
50
51    /** {@inheritdoc} */
52    public function hasTabContent(Individual $individual)
53    {
54        return $individual->canEdit() || $this->getFactsWithNotes($individual);
55    }
56
57    /** {@inheritdoc} */
58    public function isGrayedOut(Individual $individual)
59    {
60        return !$this->getFactsWithNotes($individual);
61    }
62
63    /** {@inheritdoc} */
64    public function getTabContent(Individual $individual)
65    {
66        return view('modules/notes/tab', [
67            'can_edit'   => $individual->canEdit(),
68            'individual' => $individual,
69            'facts'      => $this->getFactsWithNotes($individual),
70        ]);
71    }
72
73    /**
74     * Get all the facts for an individual which contain notes.
75     *
76     * @param Individual $individual
77     *
78     * @return Fact[]
79     */
80    private function getFactsWithNotes(Individual $individual)
81    {
82        if ($this->facts === null) {
83            $facts = $individual->getFacts();
84            foreach ($individual->getSpouseFamilies() as $family) {
85                if ($family->canShow()) {
86                    foreach ($family->getFacts() as $fact) {
87                        $facts[] = $fact;
88                    }
89                }
90            }
91            $this->facts = [];
92            foreach ($facts as $fact) {
93                if (preg_match('/(?:^1|\n\d) NOTE/', $fact->getGedcom())) {
94                    $this->facts[] = $fact;
95                }
96            }
97            Functions::sortFacts($this->facts);
98        }
99
100        return $this->facts;
101    }
102
103    /** {@inheritdoc} */
104    public function canLoadAjax()
105    {
106        return false;
107    }
108}
109