xref: /webtrees/app/Module/SlideShowModule.php (revision 4faa22d32b39010e7ad5fc52d10fd42907741acc)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\Elements\SourceMediaType;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Media;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Services\LinkedRecordService;
28use Fisharebest\Webtrees\Tree;
29use Fisharebest\Webtrees\Validator;
30use Illuminate\Database\Query\JoinClause;
31use Illuminate\Support\Str;
32use Psr\Http\Message\ServerRequestInterface;
33
34use function array_filter;
35use function in_array;
36use function str_contains;
37use function strtolower;
38
39/**
40 * Class SlideShowModule
41 */
42class SlideShowModule extends AbstractModule implements ModuleBlockInterface
43{
44    use ModuleBlockTrait;
45
46    // Show media linked to events or individuals.
47    private const LINK_ALL        = 'all';
48    private const LINK_EVENT      = 'event';
49    private const LINK_INDIVIDUAL = 'indi';
50
51    // How long to show each slide (seconds)
52    private const DELAY = 6;
53
54    // New data is normalized.  Old data may contain jpg/jpeg, tif/tiff.
55    private const SUPPORTED_FORMATS = ['bmp', 'gif', 'jpeg', 'jpg', 'png', 'tif', 'tiff', 'webp'];
56
57    private LinkedRecordService $linked_record_service;
58
59    /**
60     * @param LinkedRecordService $linked_record_service
61     */
62    public function __construct(LinkedRecordService $linked_record_service)
63    {
64        $this->linked_record_service = $linked_record_service;
65    }
66
67    /**
68     * A sentence describing what this module does.
69     *
70     * @return string
71     */
72    public function description(): string
73    {
74        /* I18N: Description of the “Slide show” module */
75        return I18N::translate('Random images from the current family tree.');
76    }
77
78    /**
79     * Generate the HTML content of this block.
80     *
81     * @param Tree                 $tree
82     * @param int                  $block_id
83     * @param string               $context
84     * @param array<string,string> $config
85     *
86     * @return string
87     */
88    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
89    {
90        $request       = Registry::container()->get(ServerRequestInterface::class);
91        $default_start = (bool) $this->getBlockSetting($block_id, 'start');
92        $filter_links  = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL);
93        $controls      = $this->getBlockSetting($block_id, 'controls', '1');
94        $start         = Validator::queryParams($request)->boolean('start', $default_start);
95
96        $filter_types = [
97            $this->getBlockSetting($block_id, 'filter_audio', '0') ? SourceMediaType::VALUE_AUDIO : null,
98            $this->getBlockSetting($block_id, 'filter_book', '1') ? SourceMediaType::VALUE_BOOK : null,
99            $this->getBlockSetting($block_id, 'filter_card', '1') ? SourceMediaType::VALUE_CARD : null,
100            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? SourceMediaType::VALUE_CERTIFICATE : null,
101            $this->getBlockSetting($block_id, 'filter_coat', '1') ? SourceMediaType::VALUE_COAT : null,
102            $this->getBlockSetting($block_id, 'filter_document', '1') ? SourceMediaType::VALUE_DOCUMENT : null,
103            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? SourceMediaType::VALUE_ELECTRONIC : null,
104            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? SourceMediaType::VALUE_FICHE : null,
105            $this->getBlockSetting($block_id, 'filter_film', '1') ? SourceMediaType::VALUE_FILM : null,
106            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? SourceMediaType::VALUE_MAGAZINE : null,
107            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? SourceMediaType::VALUE_MANUSCRIPT : null,
108            $this->getBlockSetting($block_id, 'filter_map', '1') ? SourceMediaType::VALUE_MAP : null,
109            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? SourceMediaType::VALUE_NEWSPAPER : null,
110            $this->getBlockSetting($block_id, 'filter_other', '1') ? SourceMediaType::VALUE_OTHER : null,
111            $this->getBlockSetting($block_id, 'filter_painting', '1') ? SourceMediaType::VALUE_PAINTING : null,
112            $this->getBlockSetting($block_id, 'filter_photo', '1') ? SourceMediaType::VALUE_PHOTO : null,
113            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? SourceMediaType::VALUE_TOMBSTONE : null,
114            $this->getBlockSetting($block_id, 'filter_video', '0') ? SourceMediaType::VALUE_VIDEO : null,
115        ];
116
117        $filter_types = array_filter($filter_types);
118
119        // The type "other" includes media without a type.
120        if (in_array(SourceMediaType::VALUE_OTHER, $filter_types, true)) {
121            $filter_types[] = '';
122        }
123
124        // We can apply the filters using SQL, but it is more efficient to shuffle in PHP.
125        $random_row = DB::table('media')
126            ->join('media_file', static function (JoinClause $join): void {
127                $join
128                    ->on('media_file.m_file', '=', 'media.m_file')
129                    ->on('media_file.m_id', '=', 'media.m_id');
130            })
131            ->where('media.m_file', '=', $tree->id())
132            ->whereIn('media_file.multimedia_format', self::SUPPORTED_FORMATS)
133            ->whereIn('media_file.source_media_type', $filter_types)
134            ->select(['media.*'])
135            ->get()
136            ->shuffle()
137            ->first(function (object $row) use ($filter_links, $tree): bool {
138                $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom);
139
140                if ($media === null || !$media->canShow() || $media->firstImageFile() === null) {
141                    return false;
142                }
143
144                foreach ($this->linked_record_service->linkedIndividuals($media) as $individual) {
145                    switch ($filter_links) {
146                        case self::LINK_ALL:
147                            return true;
148
149                        case self::LINK_INDIVIDUAL:
150                            return str_contains($individual->gedcom(), "\n1 OBJE @" . $media->xref() . '@');
151
152                        case self::LINK_EVENT:
153                            return str_contains($individual->gedcom(), "\n2 OBJE @" . $media->xref() . '@');
154                    }
155                }
156
157                return false;
158            });
159
160        $random_media = null;
161
162        if ($random_row !== null) {
163            $random_media = Registry::mediaFactory()->make($random_row->m_id, $tree, $random_row->m_gedcom);
164        }
165
166        if ($random_media instanceof Media) {
167            $content = view('modules/random_media/slide-show', [
168                'block_id'            => $block_id,
169                'delay'               => self::DELAY,
170                'linked_families'     => $this->linked_record_service->linkedFamilies($random_media),
171                'linked_individuals'  => $this->linked_record_service->linkedIndividuals($random_media),
172                'linked_sources'      => $this->linked_record_service->linkedSources($random_media),
173                'media'               => $random_media,
174                'media_file'          => $random_media->firstImageFile(),
175                'show_controls'       => $controls,
176                'start_automatically' => $start,
177                'tree'                => $tree,
178            ]);
179        } else {
180            $content = I18N::translate('This family tree has no images to display.');
181        }
182
183        if ($context !== self::CONTEXT_EMBED) {
184            return view('modules/block-template', [
185                'block'      => Str::kebab($this->name()),
186                'id'         => $block_id,
187                'config_url' => $this->configUrl($tree, $context, $block_id),
188                'title'      => $this->title(),
189                'content'    => $content,
190            ]);
191        }
192
193        return $content;
194    }
195
196    /**
197     * How should this module be identified in the control panel, etc.?
198     *
199     * @return string
200     */
201    public function title(): string
202    {
203        /* I18N: Name of a module */
204        return I18N::translate('Slide show');
205    }
206
207    /**
208     * Should this block load asynchronously using AJAX?
209     *
210     * Simple blocks are faster in-line, more complex ones can be loaded later.
211     *
212     * @return bool
213     */
214    public function loadAjax(): bool
215    {
216        return true;
217    }
218
219    /**
220     * Can this block be shown on the user’s home page?
221     *
222     * @return bool
223     */
224    public function isUserBlock(): bool
225    {
226        return true;
227    }
228
229    /**
230     * Can this block be shown on the tree’s home page?
231     *
232     * @return bool
233     */
234    public function isTreeBlock(): bool
235    {
236        return true;
237    }
238
239    /**
240     * Update the configuration for a block.
241     *
242     * @param ServerRequestInterface $request
243     * @param int                    $block_id
244     *
245     * @return void
246     */
247    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
248    {
249        $this->setBlockSetting($block_id, 'filter', Validator::parsedBody($request)->string('filter'));
250        $this->setBlockSetting($block_id, 'controls', Validator::parsedBody($request)->string('controls'));
251        $this->setBlockSetting($block_id, 'start', Validator::parsedBody($request)->string('start'));
252        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_AUDIO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_AUDIO, false));
253        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_BOOK), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_BOOK, false));
254        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_CARD), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_CARD, false));
255        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_CERTIFICATE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_CERTIFICATE, false));
256        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_COAT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_COAT, false));
257        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_DOCUMENT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_DOCUMENT, false));
258        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_ELECTRONIC), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_ELECTRONIC, false));
259        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_FICHE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_FICHE, false));
260        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_FILM), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_FILM, false));
261        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_MAGAZINE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_MAGAZINE, false));
262        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_MANUSCRIPT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_MANUSCRIPT, false));
263        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_MAP), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_MAP, false));
264        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_NEWSPAPER), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_NEWSPAPER, false));
265        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_OTHER), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_OTHER, false));
266        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_PAINTING), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_PAINTING, false));
267        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_PHOTO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_PHOTO, false));
268        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_TOMBSTONE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_TOMBSTONE, false));
269        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_VIDEO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_VIDEO, false));
270    }
271
272    /**
273     * An HTML form to edit block settings
274     *
275     * @param Tree $tree
276     * @param int  $block_id
277     *
278     * @return string
279     */
280    public function editBlockConfiguration(Tree $tree, int $block_id): string
281    {
282        $filter   = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL);
283        $controls = $this->getBlockSetting($block_id, 'controls', '1');
284        $start    = $this->getBlockSetting($block_id, 'start', '0');
285
286        $filters = [
287            SourceMediaType::VALUE_AUDIO       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
288            SourceMediaType::VALUE_BOOK        => $this->getBlockSetting($block_id, 'filter_book', '1'),
289            SourceMediaType::VALUE_CARD        => $this->getBlockSetting($block_id, 'filter_card', '1'),
290            SourceMediaType::VALUE_CERTIFICATE => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
291            SourceMediaType::VALUE_COAT       => $this->getBlockSetting($block_id, 'filter_coat', '1'),
292            SourceMediaType::VALUE_DOCUMENT   => $this->getBlockSetting($block_id, 'filter_document', '1'),
293            SourceMediaType::VALUE_ELECTRONIC => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
294            SourceMediaType::VALUE_FICHE      => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
295            SourceMediaType::VALUE_FILM       => $this->getBlockSetting($block_id, 'filter_film', '1'),
296            SourceMediaType::VALUE_MAGAZINE   => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
297            SourceMediaType::VALUE_MANUSCRIPT => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
298            SourceMediaType::VALUE_MAP        => $this->getBlockSetting($block_id, 'filter_map', '1'),
299            SourceMediaType::VALUE_NEWSPAPER  => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
300            SourceMediaType::VALUE_OTHER      => $this->getBlockSetting($block_id, 'filter_other', '1'),
301            SourceMediaType::VALUE_PAINTING   => $this->getBlockSetting($block_id, 'filter_painting', '1'),
302            SourceMediaType::VALUE_PHOTO      => $this->getBlockSetting($block_id, 'filter_photo', '1'),
303            SourceMediaType::VALUE_TOMBSTONE  => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
304            SourceMediaType::VALUE_VIDEO      => $this->getBlockSetting($block_id, 'filter_video', '0'),
305        ];
306
307        $formats = array_filter(Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values());
308
309        return view('modules/random_media/config', [
310            'controls' => $controls,
311            'filter'   => $filter,
312            'filters'  => $filters,
313            'formats'  => $formats,
314            'start'    => $start,
315        ]);
316    }
317}
318