xref: /webtrees/app/Module/MediaTabModule.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17*fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Fact;
238d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
25225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
262adcbd9aSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
278eaf8709SGreg Roachuse Illuminate\Support\Collection;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class MediaTabModule
318c2e8227SGreg Roach */
3237eb8894SGreg Roachclass MediaTabModule extends AbstractModule implements ModuleTabInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleTabTrait;
3549a243cbSGreg Roach
368af3e5c1SGreg Roach    /** @var  Collection A list of facts with media objects. */
378c2e8227SGreg Roach    private $facts;
388c2e8227SGreg Roach
392adcbd9aSGreg Roach    /** @var ClipboardService */
402adcbd9aSGreg Roach    private $clipboard_service;
412adcbd9aSGreg Roach
422adcbd9aSGreg Roach    /**
432adcbd9aSGreg Roach     * NotesTabModule constructor.
442adcbd9aSGreg Roach     *
452adcbd9aSGreg Roach     * @param ClipboardService $clipboard_service
462adcbd9aSGreg Roach     */
47406c2a36SGreg Roach    public function __construct(ClipboardService $clipboard_service)
48406c2a36SGreg Roach    {
492adcbd9aSGreg Roach        $this->clipboard_service = $clipboard_service;
502adcbd9aSGreg Roach    }
512adcbd9aSGreg Roach
52961ec755SGreg Roach    /**
530cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
54961ec755SGreg Roach     *
55961ec755SGreg Roach     * @return string
56961ec755SGreg Roach     */
5749a243cbSGreg Roach    public function title(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Name of a module */
60bbb76c12SGreg Roach        return I18N::translate('Media');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
63961ec755SGreg Roach    /**
64961ec755SGreg Roach     * A sentence describing what this module does.
65961ec755SGreg Roach     *
66961ec755SGreg Roach     * @return string
67961ec755SGreg Roach     */
6849a243cbSGreg Roach    public function description(): string
69c1010edaSGreg Roach    {
70bbb76c12SGreg Roach        /* I18N: Description of the “Media” module */
71bbb76c12SGreg Roach        return I18N::translate('A tab showing the media objects linked to an individual.');
728c2e8227SGreg Roach    }
738c2e8227SGreg Roach
7449a243cbSGreg Roach    /**
7549a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
7649a243cbSGreg Roach     *
7749a243cbSGreg Roach     * @return int
7849a243cbSGreg Roach     */
79cbf4b7faSGreg Roach    public function defaultTabOrder(): int
80cbf4b7faSGreg Roach    {
81fb7a0427SGreg Roach        return 5;
828c2e8227SGreg Roach    }
838c2e8227SGreg Roach
843caaa4d2SGreg Roach    /**
853caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
863caaa4d2SGreg Roach     *
873caaa4d2SGreg Roach     * @param Individual $individual
883caaa4d2SGreg Roach     *
893caaa4d2SGreg Roach     * @return bool
903caaa4d2SGreg Roach     */
918f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
92c1010edaSGreg Roach    {
938af3e5c1SGreg Roach        return $individual->canEdit() || $this->getFactsWithMedia($individual)->isNotEmpty();
948c2e8227SGreg Roach    }
958c2e8227SGreg Roach
963caaa4d2SGreg Roach    /**
973caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
983caaa4d2SGreg Roach     * options to create content.
993caaa4d2SGreg Roach     *
1003caaa4d2SGreg Roach     * @param Individual $individual
1013caaa4d2SGreg Roach     *
1023caaa4d2SGreg Roach     * @return bool
1033caaa4d2SGreg Roach     */
1048f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
105c1010edaSGreg Roach    {
106225e381fSGreg Roach        return !$this->getFactsWithMedia($individual);
1078c2e8227SGreg Roach    }
1088c2e8227SGreg Roach
1093caaa4d2SGreg Roach    /**
1103caaa4d2SGreg Roach     * Generate the HTML content of this tab.
1113caaa4d2SGreg Roach     *
1123caaa4d2SGreg Roach     * @param Individual $individual
1133caaa4d2SGreg Roach     *
1143caaa4d2SGreg Roach     * @return string
1153caaa4d2SGreg Roach     */
1169b34404bSGreg Roach    public function getTabContent(Individual $individual): string
117c1010edaSGreg Roach    {
118a8cd57e1SGreg Roach        return view('modules/media/tab', [
119225e381fSGreg Roach            'can_edit'   => $individual->canEdit(),
1202adcbd9aSGreg Roach            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
121225e381fSGreg Roach            'individual' => $individual,
122225e381fSGreg Roach            'facts'      => $this->getFactsWithMedia($individual),
123225e381fSGreg Roach        ]);
1248c2e8227SGreg Roach    }
1258c2e8227SGreg Roach
1268c2e8227SGreg Roach    /**
1278c2e8227SGreg Roach     * Get all the facts for an individual which contain media objects.
1288c2e8227SGreg Roach     *
129225e381fSGreg Roach     * @param Individual $individual
130225e381fSGreg Roach     *
1318af3e5c1SGreg Roach     * @return Collection
1328c2e8227SGreg Roach     */
1338af3e5c1SGreg Roach    private function getFactsWithMedia(Individual $individual): Collection
134c1010edaSGreg Roach    {
1358c2e8227SGreg Roach        if ($this->facts === null) {
13630158ae7SGreg Roach            $facts = $individual->facts();
13739ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
1388c2e8227SGreg Roach                if ($family->canShow()) {
13930158ae7SGreg Roach                    foreach ($family->facts() as $fact) {
14039ca88baSGreg Roach                        $facts->push($fact);
1418c2e8227SGreg Roach                    }
1428c2e8227SGreg Roach                }
1438c2e8227SGreg Roach            }
1448af3e5c1SGreg Roach
1458af3e5c1SGreg Roach            $this->facts = new Collection();
1468af3e5c1SGreg Roach
1478c2e8227SGreg Roach            foreach ($facts as $fact) {
1488d0ebef0SGreg Roach                if (preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom())) {
1498af3e5c1SGreg Roach                    $this->facts->push($fact);
1508c2e8227SGreg Roach                }
1518c2e8227SGreg Roach            }
1528af3e5c1SGreg Roach            $this->facts = Fact::sortFacts($this->facts);
1538c2e8227SGreg Roach        }
1548c2e8227SGreg Roach
1558c2e8227SGreg Roach        return $this->facts;
1568c2e8227SGreg Roach    }
1578c2e8227SGreg Roach
1583caaa4d2SGreg Roach    /**
1593caaa4d2SGreg Roach     * Can this tab load asynchronously?
1603caaa4d2SGreg Roach     *
1613caaa4d2SGreg Roach     * @return bool
1623caaa4d2SGreg Roach     */
1638f53f488SRico Sonntag    public function canLoadAjax(): bool
164c1010edaSGreg Roach    {
16515d603e7SGreg Roach        return false;
1668c2e8227SGreg Roach    }
1678eaf8709SGreg Roach
1688eaf8709SGreg Roach    /**
1698eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1708eaf8709SGreg Roach     *
17154c7f8dfSGreg Roach     * @return Collection
1728eaf8709SGreg Roach     */
1738eaf8709SGreg Roach    public function supportedFacts(): Collection
1748eaf8709SGreg Roach    {
1758eaf8709SGreg Roach        return new Collection(['OBJE']);
1768eaf8709SGreg Roach    }
1778c2e8227SGreg Roach}
178