xref: /webtrees/app/Module/SlideShowModule.php (revision a091ac74647eab281b25090b737835eeea14ae10)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5*a091ac74SGreg Roach * Copyright (C) 2020 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 */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
22*a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
2731e43e2fSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2831e43e2fSGreg Roachuse Illuminate\Database\Query\JoinClause;
291e7a7a28SGreg Roachuse Illuminate\Support\Str;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
317ff867dcSGreg Roachuse stdClass;
323976b470SGreg Roach
336ccdf4f0SGreg Roachuse function app;
347ff867dcSGreg Roachuse function assert;
359e8577e7SGreg Roachuse function in_array;
365136fb17SGreg Roachuse function strpos;
378c2e8227SGreg Roach
388c2e8227SGreg Roach/**
398c2e8227SGreg Roach * Class SlideShowModule
408c2e8227SGreg Roach */
4137eb8894SGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface
42c1010edaSGreg Roach{
4349a243cbSGreg Roach    use ModuleBlockTrait;
4449a243cbSGreg Roach
457ff867dcSGreg Roach    // Show media linked to events or individuals.
467ff867dcSGreg Roach    private const LINK_ALL        = 'all';
477ff867dcSGreg Roach    private const LINK_EVENT      = 'event';
487ff867dcSGreg Roach    private const LINK_INDIVIDUAL = 'indi';
497ff867dcSGreg Roach
507ff867dcSGreg Roach    // How long to show each slide (seconds)
517ff867dcSGreg Roach    private const DELAY = 6;
527ff867dcSGreg Roach
53961ec755SGreg Roach    /**
54961ec755SGreg Roach     * A sentence describing what this module does.
55961ec755SGreg Roach     *
56961ec755SGreg Roach     * @return string
57961ec755SGreg Roach     */
5849a243cbSGreg Roach    public function description(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Description of the “Slide show” module */
61bbb76c12SGreg Roach        return I18N::translate('Random images from the current family tree.');
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
6476692c8bSGreg Roach    /**
6576692c8bSGreg Roach     * Generate the HTML content of this block.
6676692c8bSGreg Roach     *
67e490cd80SGreg Roach     * @param Tree     $tree
6876692c8bSGreg Roach     * @param int      $block_id
693caaa4d2SGreg Roach     * @param string   $context
703caaa4d2SGreg Roach     * @param string[] $config
7176692c8bSGreg Roach     *
7276692c8bSGreg Roach     * @return string
7376692c8bSGreg Roach     */
743caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
75c1010edaSGreg Roach    {
766ccdf4f0SGreg Roach        $request       = app(ServerRequestInterface::class);
77b6b9dcc9SGreg Roach        $default_start = $this->getBlockSetting($block_id, 'start');
787ff867dcSGreg Roach        $filter_links  = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL);
79e2a378d3SGreg Roach        $controls      = $this->getBlockSetting($block_id, 'controls', '1');
80b6b9dcc9SGreg Roach        $start         = (bool) ($request->getQueryParams()['start'] ?? $default_start);
818c2e8227SGreg Roach
827ff867dcSGreg Roach        $filter_types = [
83e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
84e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
85e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
86e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
87e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
88e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
89e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
90e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
91e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
92e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
93e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
94e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
95e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
96e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
97e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
98e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
99e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
100e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
10131e43e2fSGreg Roach        ];
10231e43e2fSGreg Roach
1037ff867dcSGreg Roach        $filter_types = array_filter($filter_types);
10431e43e2fSGreg Roach
1059e8577e7SGreg Roach        // The type "other" includes media without a type.
1067ff867dcSGreg Roach        if (in_array('other', $filter_types, true)) {
1077ff867dcSGreg Roach            $filter_types[] = '';
1089e8577e7SGreg Roach        }
1099e8577e7SGreg Roach
1105136fb17SGreg Roach        // We can apply the filters using SQL, but it is more efficient to shuffle in PHP.
1115136fb17SGreg Roach        $random_row = DB::table('media')
1120b5fd0a6SGreg Roach            ->join('media_file', static function (JoinClause $join): void {
11331e43e2fSGreg Roach                $join
11431e43e2fSGreg Roach                    ->on('media_file.m_file', '=', 'media.m_file')
11531e43e2fSGreg Roach                    ->on('media_file.m_id', '=', 'media.m_id');
11631e43e2fSGreg Roach            })
11731e43e2fSGreg Roach            ->where('media.m_file', '=', $tree->id())
11831e43e2fSGreg Roach            ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp'])
1197ff867dcSGreg Roach            ->whereIn('media_file.source_media_type', $filter_types)
1205136fb17SGreg Roach            ->select('media.*')
1215136fb17SGreg Roach            ->get()
1225136fb17SGreg Roach            ->shuffle()
1237ff867dcSGreg Roach            ->first(static function (stdClass $row) use ($filter_links, $tree): bool {
124*a091ac74SGreg Roach                $media = Factory::media()->make($row->m_id, $tree, $row->m_gedcom);
1257ff867dcSGreg Roach                assert($media instanceof Media);
1267ff867dcSGreg Roach
127615ea171SGreg Roach                if (!$media->canShow() || $media->firstImageFile() === null) {
1287ff867dcSGreg Roach                    return false;
1297ff867dcSGreg Roach                }
1308c2e8227SGreg Roach
1315136fb17SGreg Roach                foreach ($media->linkedIndividuals('OBJE') as $individual) {
1327ff867dcSGreg Roach                    switch ($filter_links) {
1337ff867dcSGreg Roach                        case self::LINK_ALL:
1345136fb17SGreg Roach                            return true;
1357ff867dcSGreg Roach
1367ff867dcSGreg Roach                        case self::LINK_INDIVIDUAL:
1375136fb17SGreg Roach                            return strpos($individual->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false;
1387ff867dcSGreg Roach
1397ff867dcSGreg Roach                        case self::LINK_EVENT:
1405136fb17SGreg Roach                            return strpos($individual->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false;
1418c2e8227SGreg Roach                    }
1428c2e8227SGreg Roach                }
1438c2e8227SGreg Roach
1445136fb17SGreg Roach                return false;
1455136fb17SGreg Roach            });
1465136fb17SGreg Roach
147ae7e1e94SGreg Roach        $random_media = null;
148ae7e1e94SGreg Roach
149ae7e1e94SGreg Roach        if ($random_row !== null) {
150*a091ac74SGreg Roach            $random_media = Factory::media()->make($random_row->m_id, $tree, $random_row->m_gedcom);
151ae7e1e94SGreg Roach        }
1525136fb17SGreg Roach
1535136fb17SGreg Roach        if ($random_media instanceof Media) {
154147e99aaSGreg Roach            $content = view('modules/random_media/slide-show', [
155a1fe7073SGreg Roach                'block_id'            => $block_id,
1567ff867dcSGreg Roach                'delay'               => self::DELAY,
157a1fe7073SGreg Roach                'media'               => $random_media,
158495cefeaSGreg Roach                'media_file'          => $random_media->firstImageFile(),
159a1fe7073SGreg Roach                'show_controls'       => $controls,
160a1fe7073SGreg Roach                'start_automatically' => $start,
161911f5683SGreg Roach                'tree'                => $tree,
162a1fe7073SGreg Roach            ]);
1638c2e8227SGreg Roach        } else {
1648c2e8227SGreg Roach            $content = I18N::translate('This family tree has no images to display.');
1658c2e8227SGreg Roach        }
166a1fe7073SGreg Roach
1673caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
168147e99aaSGreg Roach            return view('modules/block-template', [
1691e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1709c6524dcSGreg Roach                'id'         => $block_id,
1713caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
17249a243cbSGreg Roach                'title'      => $this->title(),
1739c6524dcSGreg Roach                'content'    => $content,
1749c6524dcSGreg Roach            ]);
1758c2e8227SGreg Roach        }
176b2ce94c6SRico Sonntag
177b2ce94c6SRico Sonntag        return $content;
1788c2e8227SGreg Roach    }
1798c2e8227SGreg Roach
1806ccdf4f0SGreg Roach    /**
1816ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1826ccdf4f0SGreg Roach     *
1836ccdf4f0SGreg Roach     * @return string
1846ccdf4f0SGreg Roach     */
1856ccdf4f0SGreg Roach    public function title(): string
1866ccdf4f0SGreg Roach    {
1876ccdf4f0SGreg Roach        /* I18N: Name of a module */
1886ccdf4f0SGreg Roach        return I18N::translate('Slide show');
1896ccdf4f0SGreg Roach    }
1906ccdf4f0SGreg Roach
1913caaa4d2SGreg Roach    /**
1923caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1933caaa4d2SGreg Roach     *
1943caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1953caaa4d2SGreg Roach     *
1963caaa4d2SGreg Roach     * @return bool
1973caaa4d2SGreg Roach     */
198c1010edaSGreg Roach    public function loadAjax(): bool
199c1010edaSGreg Roach    {
2008c2e8227SGreg Roach        return true;
2018c2e8227SGreg Roach    }
2028c2e8227SGreg Roach
2033caaa4d2SGreg Roach    /**
2043caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2053caaa4d2SGreg Roach     *
2063caaa4d2SGreg Roach     * @return bool
2073caaa4d2SGreg Roach     */
208c1010edaSGreg Roach    public function isUserBlock(): bool
209c1010edaSGreg Roach    {
2108c2e8227SGreg Roach        return true;
2118c2e8227SGreg Roach    }
2128c2e8227SGreg Roach
2133caaa4d2SGreg Roach    /**
2143caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2153caaa4d2SGreg Roach     *
2163caaa4d2SGreg Roach     * @return bool
2173caaa4d2SGreg Roach     */
21863276d8fSGreg Roach    public function isTreeBlock(): bool
219c1010edaSGreg Roach    {
2208c2e8227SGreg Roach        return true;
2218c2e8227SGreg Roach    }
2228c2e8227SGreg Roach
22376692c8bSGreg Roach    /**
224a45f9889SGreg Roach     * Update the configuration for a block.
225a45f9889SGreg Roach     *
2266ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
227a45f9889SGreg Roach     * @param int                    $block_id
228a45f9889SGreg Roach     *
229a45f9889SGreg Roach     * @return void
230a45f9889SGreg Roach     */
2316ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
232a45f9889SGreg Roach    {
233b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
234b6b9dcc9SGreg Roach
235b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter', $params['filter']);
236b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'controls', $params['controls']);
237b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'start', $params['start']);
238b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_audio', $params['filter_audio'] ?? '');
239b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_book', $params['filter_book'] ?? '');
240b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_card', $params['filter_card'] ?? '');
241b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_certificate', $params['filter_certificate'] ?? '');
242b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_coat', $params['filter_coat'] ?? '');
243b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_document', $params['filter_document'] ?? '');
244b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_electronic', $params['filter_electronic'] ?? '');
245b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_fiche', $params['filter_fiche'] ?? '');
246b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_film', $params['filter_film'] ?? '');
247b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_magazine', $params['filter_magazine'] ?? '');
248b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_manuscript', $params['filter_manuscript'] ?? '');
249b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_map', $params['filter_map'] ?? '');
250b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_newspaper', $params['filter_newspaper'] ?? '');
251b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_other', $params['filter_other'] ?? '');
252b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_painting', $params['filter_painting'] ?? '');
253b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_photo', $params['filter_photo'] ?? '');
254b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_tombstone', $params['filter_tombstone'] ?? '');
255b6b9dcc9SGreg Roach        $this->setBlockSetting($block_id, 'filter_video', $params['filter_video'] ?? '');
256a45f9889SGreg Roach    }
257a45f9889SGreg Roach
258a45f9889SGreg Roach    /**
25976692c8bSGreg Roach     * An HTML form to edit block settings
26076692c8bSGreg Roach     *
261e490cd80SGreg Roach     * @param Tree $tree
26276692c8bSGreg Roach     * @param int  $block_id
263a9430be8SGreg Roach     *
2643caaa4d2SGreg Roach     * @return string
26576692c8bSGreg Roach     */
2663caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
267c1010edaSGreg Roach    {
2687ff867dcSGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL);
269e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
27015d603e7SGreg Roach        $start    = $this->getBlockSetting($block_id, 'start', '0');
2718c2e8227SGreg Roach
27213abd6f3SGreg Roach        $filters = [
273e2a378d3SGreg Roach            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
274e2a378d3SGreg Roach            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
275e2a378d3SGreg Roach            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
276e2a378d3SGreg Roach            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
277e2a378d3SGreg Roach            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
278e2a378d3SGreg Roach            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
279e2a378d3SGreg Roach            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
280e2a378d3SGreg Roach            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
281e2a378d3SGreg Roach            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
282e2a378d3SGreg Roach            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
283e2a378d3SGreg Roach            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
284e2a378d3SGreg Roach            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
285e2a378d3SGreg Roach            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
286e2a378d3SGreg Roach            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
287e2a378d3SGreg Roach            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
288e2a378d3SGreg Roach            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
289e2a378d3SGreg Roach            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
290e2a378d3SGreg Roach            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
29113abd6f3SGreg Roach        ];
2928c2e8227SGreg Roach
293c385536dSGreg Roach        $formats = GedcomTag::getFileFormTypes();
29415d603e7SGreg Roach
2953caaa4d2SGreg Roach        return view('modules/random_media/config', [
296c385536dSGreg Roach            'controls' => $controls,
297c385536dSGreg Roach            'filter'   => $filter,
298c385536dSGreg Roach            'filters'  => $filters,
299c385536dSGreg Roach            'formats'  => $formats,
300c385536dSGreg Roach            'start'    => $start,
301c385536dSGreg Roach        ]);
3028c2e8227SGreg Roach    }
3038c2e8227SGreg Roach}
304