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