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