xref: /webtrees/app/Module/MediaTabModule.php (revision ad7270802ba57a28d195fc58fcc55f26458a779a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Fact;
21use Fisharebest\Webtrees\Functions\Functions;
22use Fisharebest\Webtrees\Gedcom;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Services\ClipboardService;
26use Illuminate\Support\Collection;
27
28/**
29 * Class MediaTabModule
30 */
31class MediaTabModule extends AbstractModule implements ModuleTabInterface
32{
33    use ModuleTabTrait;
34
35    /** @var  Fact[] A list of facts with media objects. */
36    private $facts;
37
38    /** @var ClipboardService */
39    private $clipboard_service;
40
41    /**
42     * NotesTabModule constructor.
43     *
44     * @param ClipboardService $clipboard_service
45     */
46    public function __construct(ClipboardService $clipboard_service)
47    {
48        $this->clipboard_service = $clipboard_service;
49    }
50
51    /**
52     * How should this module be labelled on tabs, menus, etc.?
53     *
54     * @return string
55     */
56    public function title(): string
57    {
58        /* I18N: Name of a module */
59        return I18N::translate('Media');
60    }
61
62    /**
63     * A sentence describing what this module does.
64     *
65     * @return string
66     */
67    public function description(): string
68    {
69        /* I18N: Description of the “Media” module */
70        return I18N::translate('A tab showing the media objects linked to an individual.');
71    }
72
73    /**
74     * The default position for this tab.  It can be changed in the control panel.
75     *
76     * @return int
77     */
78    public function defaultTabOrder(): int
79    {
80        return 6;
81    }
82
83    /** {@inheritdoc} */
84    public function hasTabContent(Individual $individual): bool
85    {
86        return $individual->canEdit() || $this->getFactsWithMedia($individual);
87    }
88
89    /** {@inheritdoc} */
90    public function isGrayedOut(Individual $individual): bool
91    {
92        return !$this->getFactsWithMedia($individual);
93    }
94
95    /** {@inheritdoc} */
96    public function getTabContent(Individual $individual): string
97    {
98        return view('modules/media/tab', [
99            'can_edit'   => $individual->canEdit(),
100            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
101            'individual' => $individual,
102            'facts'      => $this->getFactsWithMedia($individual),
103        ]);
104    }
105
106    /**
107     * Get all the facts for an individual which contain media objects.
108     *
109     * @param Individual $individual
110     *
111     * @return Fact[]
112     */
113    private function getFactsWithMedia(Individual $individual): array
114    {
115        if ($this->facts === null) {
116            $facts = $individual->facts();
117            foreach ($individual->getSpouseFamilies() as $family) {
118                if ($family->canShow()) {
119                    foreach ($family->facts() as $fact) {
120                        $facts[] = $fact;
121                    }
122                }
123            }
124            $this->facts = [];
125            foreach ($facts as $fact) {
126                if (preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom())) {
127                    $this->facts[] = $fact;
128                }
129            }
130            Functions::sortFacts($this->facts);
131        }
132
133        return $this->facts;
134    }
135
136    /** {@inheritdoc} */
137    public function canLoadAjax(): bool
138    {
139        return false;
140    }
141
142    /**
143     * This module handles the following facts - so don't show them on the "Facts and events" tab.
144     *
145     * @return Collection|string[]
146     */
147    public function supportedFacts(): Collection
148    {
149        return new Collection(['OBJE']);
150    }
151}
152