xref: /webtrees/app/Module/SlideShowModule.php (revision b6b9dcc9af05c3f136144fb4f6f9b69a8abd1995)
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\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
24495cefeaSGreg Roachuse Fisharebest\Webtrees\MediaFile;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
2631e43e2fSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2731e43e2fSGreg Roachuse Illuminate\Database\Query\JoinClause;
281e7a7a28SGreg Roachuse Illuminate\Support\Str;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
306ccdf4f0SGreg Roachuse function app;
318c2e8227SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class SlideShowModule
348c2e8227SGreg Roach */
3537eb8894SGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface
36c1010edaSGreg Roach{
3749a243cbSGreg Roach    use ModuleBlockTrait;
3849a243cbSGreg Roach
39961ec755SGreg Roach    /**
40961ec755SGreg Roach     * A sentence describing what this module does.
41961ec755SGreg Roach     *
42961ec755SGreg Roach     * @return string
43961ec755SGreg Roach     */
4449a243cbSGreg Roach    public function description(): string
45c1010edaSGreg Roach    {
46bbb76c12SGreg Roach        /* I18N: Description of the “Slide show” module */
47bbb76c12SGreg Roach        return I18N::translate('Random images from the current family tree.');
488c2e8227SGreg Roach    }
498c2e8227SGreg Roach
5076692c8bSGreg Roach    /**
5176692c8bSGreg Roach     * Generate the HTML content of this block.
5276692c8bSGreg Roach     *
53e490cd80SGreg Roach     * @param Tree     $tree
5476692c8bSGreg Roach     * @param int      $block_id
555f2ae573SGreg Roach     * @param string   $ctype
56727f238cSGreg Roach     * @param string[] $cfg
5776692c8bSGreg Roach     *
5876692c8bSGreg Roach     * @return string
5976692c8bSGreg Roach     */
605f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
61c1010edaSGreg Roach    {
626ccdf4f0SGreg Roach        $request       = app(ServerRequestInterface::class);
63*b6b9dcc9SGreg Roach        $default_start = $this->getBlockSetting($block_id, 'start');
64e2a378d3SGreg Roach        $filter        = $this->getBlockSetting($block_id, 'filter', 'all');
65e2a378d3SGreg Roach        $controls      = $this->getBlockSetting($block_id, 'controls', '1');
66*b6b9dcc9SGreg Roach        $start         = (bool) ($request->getQueryParams()['start'] ?? $default_start);
678c2e8227SGreg Roach
6831e43e2fSGreg Roach        $media_types = [
69e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
70e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
71e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
72e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
73e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
74e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
75e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
76e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
77e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
78e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
79e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
80e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
81e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
82e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
83e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
84e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
85e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
86e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
8731e43e2fSGreg Roach        ];
8831e43e2fSGreg Roach
8931e43e2fSGreg Roach        $media_types = array_filter($media_types);
9031e43e2fSGreg Roach
9131e43e2fSGreg Roach        // We can apply the filters using SQL
9231e43e2fSGreg Roach        // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
9331e43e2fSGreg Roach        $all_media = DB::table('media')
940b5fd0a6SGreg Roach            ->join('media_file', static function (JoinClause $join): void {
9531e43e2fSGreg Roach                $join
9631e43e2fSGreg Roach                    ->on('media_file.m_file', '=', 'media.m_file')
9731e43e2fSGreg Roach                    ->on('media_file.m_id', '=', 'media.m_id');
9831e43e2fSGreg Roach            })
9931e43e2fSGreg Roach            ->where('media.m_file', '=', $tree->id())
10031e43e2fSGreg Roach            ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp'])
10131e43e2fSGreg Roach            ->whereIn('media_file.source_media_type', $media_types)
10231e43e2fSGreg Roach            ->pluck('media.m_id')
10331e43e2fSGreg Roach            ->all();
1048c2e8227SGreg Roach
1058c2e8227SGreg Roach        // Keep looking through the media until a suitable one is found.
1068c2e8227SGreg Roach        $random_media = null;
107495cefeaSGreg Roach        while (!empty($all_media)) {
1088c2e8227SGreg Roach            $n          = array_rand($all_media);
109e490cd80SGreg Roach            $media      = Media::getInstance($all_media[$n], $tree);
110572a608bSGreg Roach            $media_file = $media->firstImageFile();
111495cefeaSGreg Roach            if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) {
1128c2e8227SGreg Roach                // Check if it is linked to a suitable individual
1138c2e8227SGreg Roach                foreach ($media->linkedIndividuals('OBJE') as $indi) {
1148c2e8227SGreg Roach                    if (
1158c2e8227SGreg Roach                        $filter === 'all' ||
1167d0db648SGreg Roach                        $filter === 'indi' && strpos($indi->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false ||
1177d0db648SGreg Roach                        $filter === 'event' && strpos($indi->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false
1188c2e8227SGreg Roach                    ) {
1198c2e8227SGreg Roach                        // Found one :-)
1208c2e8227SGreg Roach                        $random_media = $media;
1218c2e8227SGreg Roach                        break 2;
1228c2e8227SGreg Roach                    }
1238c2e8227SGreg Roach                }
1248c2e8227SGreg Roach            }
1258c2e8227SGreg Roach            unset($all_media[$n]);
1263c12f3e5SGreg Roach        }
1278c2e8227SGreg Roach
1288c2e8227SGreg Roach        if ($random_media) {
129147e99aaSGreg Roach            $content = view('modules/random_media/slide-show', [
130a1fe7073SGreg Roach                'block_id'            => $block_id,
131a1fe7073SGreg Roach                'media'               => $random_media,
132495cefeaSGreg Roach                'media_file'          => $random_media->firstImageFile(),
133a1fe7073SGreg Roach                'show_controls'       => $controls,
134a1fe7073SGreg Roach                'start_automatically' => $start,
135911f5683SGreg Roach                'tree'                => $tree,
136a1fe7073SGreg Roach            ]);
1378c2e8227SGreg Roach        } else {
1388c2e8227SGreg Roach            $content = I18N::translate('This family tree has no images to display.');
1398c2e8227SGreg Roach        }
140a1fe7073SGreg Roach
1416a8879feSGreg Roach        if ($ctype !== '') {
142e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
143c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
144c1010edaSGreg Roach                    'block_id' => $block_id,
145aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
146c1010edaSGreg Roach                ]);
147397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
148c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
149c1010edaSGreg Roach                    'block_id' => $block_id,
150aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
151c1010edaSGreg Roach                ]);
1528cbbfdceSGreg Roach            } else {
1538cbbfdceSGreg Roach                $config_url = '';
1548cbbfdceSGreg Roach            }
1558cbbfdceSGreg Roach
156147e99aaSGreg Roach            return view('modules/block-template', [
1571e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1589c6524dcSGreg Roach                'id'         => $block_id,
1598cbbfdceSGreg Roach                'config_url' => $config_url,
16049a243cbSGreg Roach                'title'      => $this->title(),
1619c6524dcSGreg Roach                'content'    => $content,
1629c6524dcSGreg Roach            ]);
1638c2e8227SGreg Roach        }
164b2ce94c6SRico Sonntag
165b2ce94c6SRico Sonntag        return $content;
1668c2e8227SGreg Roach    }
1678c2e8227SGreg Roach
1686ccdf4f0SGreg Roach    /**
1696ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1706ccdf4f0SGreg Roach     *
1716ccdf4f0SGreg Roach     * @return string
1726ccdf4f0SGreg Roach     */
1736ccdf4f0SGreg Roach    public function title(): string
1746ccdf4f0SGreg Roach    {
1756ccdf4f0SGreg Roach        /* I18N: Name of a module */
1766ccdf4f0SGreg Roach        return I18N::translate('Slide show');
1776ccdf4f0SGreg Roach    }
1786ccdf4f0SGreg Roach
1798c2e8227SGreg Roach    /** {@inheritdoc} */
180c1010edaSGreg Roach    public function loadAjax(): bool
181c1010edaSGreg Roach    {
1828c2e8227SGreg Roach        return true;
1838c2e8227SGreg Roach    }
1848c2e8227SGreg Roach
1858c2e8227SGreg Roach    /** {@inheritdoc} */
186c1010edaSGreg Roach    public function isUserBlock(): bool
187c1010edaSGreg Roach    {
1888c2e8227SGreg Roach        return true;
1898c2e8227SGreg Roach    }
1908c2e8227SGreg Roach
1918c2e8227SGreg Roach    /** {@inheritdoc} */
19263276d8fSGreg Roach    public function isTreeBlock(): bool
193c1010edaSGreg Roach    {
1948c2e8227SGreg Roach        return true;
1958c2e8227SGreg Roach    }
1968c2e8227SGreg Roach
19776692c8bSGreg Roach    /**
198a45f9889SGreg Roach     * Update the configuration for a block.
199a45f9889SGreg Roach     *
2006ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
201a45f9889SGreg Roach     * @param int                    $block_id
202a45f9889SGreg Roach     *
203a45f9889SGreg Roach     * @return void
204a45f9889SGreg Roach     */
2056ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
206a45f9889SGreg Roach    {
207*b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
208*b6b9dcc9SGreg Roach
209*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter', $params['filter']);
210*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'controls', $params['controls']);
211*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'start', $params['start']);
212*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_audio', $params['filter_audio'] ?? '');
213*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_book', $params['filter_book'] ?? '');
214*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_card', $params['filter_card'] ?? '');
215*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_certificate', $params['filter_certificate'] ?? '');
216*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_coat', $params['filter_coat'] ?? '');
217*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_document', $params['filter_document'] ?? '');
218*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_electronic', $params['filter_electronic'] ?? '');
219*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_fiche', $params['filter_fiche'] ?? '');
220*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_film', $params['filter_film'] ?? '');
221*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_magazine', $params['filter_magazine'] ?? '');
222*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_manuscript', $params['filter_manuscript'] ?? '');
223*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_map', $params['filter_map'] ?? '');
224*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_newspaper', $params['filter_newspaper'] ?? '');
225*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_other', $params['filter_other'] ?? '');
226*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_painting', $params['filter_painting'] ?? '');
227*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_photo', $params['filter_photo'] ?? '');
228*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_tombstone', $params['filter_tombstone'] ?? '');
229*b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_video', $params['filter_video'] ?? '');
230a45f9889SGreg Roach    }
231a45f9889SGreg Roach
232a45f9889SGreg Roach    /**
23376692c8bSGreg Roach     * An HTML form to edit block settings
23476692c8bSGreg Roach     *
235e490cd80SGreg Roach     * @param Tree $tree
23676692c8bSGreg Roach     * @param int  $block_id
237a9430be8SGreg Roach     *
238a9430be8SGreg Roach     * @return void
23976692c8bSGreg Roach     */
240e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
241c1010edaSGreg Roach    {
242e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
243e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
24415d603e7SGreg Roach        $start    = $this->getBlockSetting($block_id, 'start', '0');
2458c2e8227SGreg Roach
24613abd6f3SGreg Roach        $filters = [
247e2a378d3SGreg Roach            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
248e2a378d3SGreg Roach            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
249e2a378d3SGreg Roach            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
250e2a378d3SGreg Roach            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
251e2a378d3SGreg Roach            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
252e2a378d3SGreg Roach            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
253e2a378d3SGreg Roach            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
254e2a378d3SGreg Roach            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
255e2a378d3SGreg Roach            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
256e2a378d3SGreg Roach            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
257e2a378d3SGreg Roach            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
258e2a378d3SGreg Roach            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
259e2a378d3SGreg Roach            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
260e2a378d3SGreg Roach            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
261e2a378d3SGreg Roach            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
262e2a378d3SGreg Roach            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
263e2a378d3SGreg Roach            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
264e2a378d3SGreg Roach            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
26513abd6f3SGreg Roach        ];
2668c2e8227SGreg Roach
267c385536dSGreg Roach        $formats = GedcomTag::getFileFormTypes();
26815d603e7SGreg Roach
269147e99aaSGreg Roach        echo view('modules/random_media/config', [
270c385536dSGreg Roach            'controls' => $controls,
271c385536dSGreg Roach            'filter'   => $filter,
272c385536dSGreg Roach            'filters'  => $filters,
273c385536dSGreg Roach            'formats'  => $formats,
274c385536dSGreg Roach            'start'    => $start,
275c385536dSGreg Roach        ]);
2768c2e8227SGreg Roach    }
2778c2e8227SGreg Roach}
278