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