xref: /webtrees/app/Module/MediaTabModule.php (revision 8af3e5c1dbbe831c0125b68edb9f9b9d290d1974)
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;
218d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
242adcbd9aSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
258eaf8709SGreg Roachuse Illuminate\Support\Collection;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class MediaTabModule
298c2e8227SGreg Roach */
3037eb8894SGreg Roachclass MediaTabModule extends AbstractModule implements ModuleTabInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleTabTrait;
3349a243cbSGreg Roach
34*8af3e5c1SGreg Roach    /** @var  Collection A list of facts with media objects. */
358c2e8227SGreg Roach    private $facts;
368c2e8227SGreg Roach
372adcbd9aSGreg Roach    /** @var ClipboardService */
382adcbd9aSGreg Roach    private $clipboard_service;
392adcbd9aSGreg Roach
402adcbd9aSGreg Roach    /**
412adcbd9aSGreg Roach     * NotesTabModule constructor.
422adcbd9aSGreg Roach     *
432adcbd9aSGreg Roach     * @param ClipboardService $clipboard_service
442adcbd9aSGreg Roach     */
45406c2a36SGreg Roach    public function __construct(ClipboardService $clipboard_service)
46406c2a36SGreg 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('Media');
598c2e8227SGreg Roach    }
608c2e8227SGreg Roach
61961ec755SGreg Roach    /**
62961ec755SGreg Roach     * A sentence describing what this module does.
63961ec755SGreg Roach     *
64961ec755SGreg Roach     * @return string
65961ec755SGreg Roach     */
6649a243cbSGreg Roach    public function description(): string
67c1010edaSGreg Roach    {
68bbb76c12SGreg Roach        /* I18N: Description of the “Media” module */
69bbb76c12SGreg Roach        return I18N::translate('A tab showing the media objects linked to an individual.');
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
7249a243cbSGreg Roach    /**
7349a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
7449a243cbSGreg Roach     *
7549a243cbSGreg Roach     * @return int
7649a243cbSGreg Roach     */
77cbf4b7faSGreg Roach    public function defaultTabOrder(): int
78cbf4b7faSGreg Roach    {
79fb7a0427SGreg Roach        return 5;
808c2e8227SGreg Roach    }
818c2e8227SGreg Roach
828c2e8227SGreg Roach    /** {@inheritdoc} */
838f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
84c1010edaSGreg Roach    {
85*8af3e5c1SGreg Roach        return $individual->canEdit() || $this->getFactsWithMedia($individual)->isNotEmpty();
868c2e8227SGreg Roach    }
878c2e8227SGreg Roach
888c2e8227SGreg Roach    /** {@inheritdoc} */
898f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
90c1010edaSGreg Roach    {
91225e381fSGreg Roach        return !$this->getFactsWithMedia($individual);
928c2e8227SGreg Roach    }
938c2e8227SGreg Roach
948c2e8227SGreg Roach    /** {@inheritdoc} */
959b34404bSGreg Roach    public function getTabContent(Individual $individual): string
96c1010edaSGreg Roach    {
97a8cd57e1SGreg Roach        return view('modules/media/tab', [
98225e381fSGreg Roach            'can_edit'   => $individual->canEdit(),
992adcbd9aSGreg Roach            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
100225e381fSGreg Roach            'individual' => $individual,
101225e381fSGreg Roach            'facts'      => $this->getFactsWithMedia($individual),
102225e381fSGreg Roach        ]);
1038c2e8227SGreg Roach    }
1048c2e8227SGreg Roach
1058c2e8227SGreg Roach    /**
1068c2e8227SGreg Roach     * Get all the facts for an individual which contain media objects.
1078c2e8227SGreg Roach     *
108225e381fSGreg Roach     * @param Individual $individual
109225e381fSGreg Roach     *
110*8af3e5c1SGreg Roach     * @return Collection
1118c2e8227SGreg Roach     * @return Fact[]
1128c2e8227SGreg Roach     */
113*8af3e5c1SGreg Roach    private function getFactsWithMedia(Individual $individual): Collection
114c1010edaSGreg Roach    {
1158c2e8227SGreg Roach        if ($this->facts === null) {
11630158ae7SGreg Roach            $facts = $individual->facts();
11739ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
1188c2e8227SGreg Roach                if ($family->canShow()) {
11930158ae7SGreg Roach                    foreach ($family->facts() as $fact) {
12039ca88baSGreg Roach                        $facts->push($fact);
1218c2e8227SGreg Roach                    }
1228c2e8227SGreg Roach                }
1238c2e8227SGreg Roach            }
124*8af3e5c1SGreg Roach
125*8af3e5c1SGreg Roach            $this->facts = new Collection();
126*8af3e5c1SGreg Roach
1278c2e8227SGreg Roach            foreach ($facts as $fact) {
1288d0ebef0SGreg Roach                if (preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom())) {
129*8af3e5c1SGreg Roach                    $this->facts->push($fact);
1308c2e8227SGreg Roach                }
1318c2e8227SGreg Roach            }
132*8af3e5c1SGreg Roach            $this->facts = Fact::sortFacts($this->facts);
1338c2e8227SGreg Roach        }
1348c2e8227SGreg Roach
1358c2e8227SGreg Roach        return $this->facts;
1368c2e8227SGreg Roach    }
1378c2e8227SGreg Roach
1388c2e8227SGreg Roach    /** {@inheritdoc} */
1398f53f488SRico Sonntag    public function canLoadAjax(): bool
140c1010edaSGreg Roach    {
14115d603e7SGreg Roach        return false;
1428c2e8227SGreg Roach    }
1438eaf8709SGreg Roach
1448eaf8709SGreg Roach    /**
1458eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1468eaf8709SGreg Roach     *
14754c7f8dfSGreg Roach     * @return Collection
14854c7f8dfSGreg Roach     * @return string[]
1498eaf8709SGreg Roach     */
1508eaf8709SGreg Roach    public function supportedFacts(): Collection
1518eaf8709SGreg Roach    {
1528eaf8709SGreg Roach        return new Collection(['OBJE']);
1538eaf8709SGreg Roach    }
1548c2e8227SGreg Roach}
155