xref: /webtrees/app/Module/SlideShowModule.php (revision 7573bc3d78289b103f95e1640a3adf9095277886)
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 array_keys;
38use function in_array;
39use function str_contains;
40use function strtolower;
41use function var_dump;
42
43/**
44 * Class SlideShowModule
45 */
46class SlideShowModule extends AbstractModule implements ModuleBlockInterface
47{
48    use ModuleBlockTrait;
49
50    // Show media linked to events or individuals.
51    private const LINK_ALL        = 'all';
52    private const LINK_EVENT      = 'event';
53    private const LINK_INDIVIDUAL = 'indi';
54
55    // How long to show each slide (seconds)
56    private const DELAY = 6;
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 = $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         = (bool) ($request->getQueryParams()['start'] ?? $default_start);
96
97        $filter_types = [
98            $this->getBlockSetting($block_id, 'filter_audio', '0') ? SourceMediaType::TYPE_AUDIO : null,
99            $this->getBlockSetting($block_id, 'filter_book', '1') ? SourceMediaType::TYPE_BOOK : null,
100            $this->getBlockSetting($block_id, 'filter_card', '1') ? SourceMediaType::TYPE_CARD : null,
101            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? SourceMediaType::TYPE_CERTIFICATE : null,
102            $this->getBlockSetting($block_id, 'filter_coat', '1') ? SourceMediaType::TYPE_COAT : null,
103            $this->getBlockSetting($block_id, 'filter_document', '1') ? SourceMediaType::TYPE_DOCUMENT : null,
104            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? SourceMediaType::TYPE_ELECTRONIC : null,
105            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? SourceMediaType::TYPE_FICHE : null,
106            $this->getBlockSetting($block_id, 'filter_film', '1') ? SourceMediaType::TYPE_FILM : null,
107            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? SourceMediaType::TYPE_MAGAZINE : null,
108            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? SourceMediaType::TYPE_MANUSCRIPT : null,
109            $this->getBlockSetting($block_id, 'filter_map', '1') ? SourceMediaType::TYPE_MAP : null,
110            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? SourceMediaType::TYPE_NEWSPAPER : null,
111            $this->getBlockSetting($block_id, 'filter_other', '1') ? SourceMediaType::TYPE_OTHER : null,
112            $this->getBlockSetting($block_id, 'filter_painting', '1') ? SourceMediaType::TYPE_PAINTING : null,
113            $this->getBlockSetting($block_id, 'filter_photo', '1') ? SourceMediaType::TYPE_PHOTO : null,
114            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? SourceMediaType::TYPE_TOMBSTONE : null,
115            $this->getBlockSetting($block_id, 'filter_video', '0') ? SourceMediaType::TYPE_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::TYPE_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', ImageFactory::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::TYPE_AUDIO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_AUDIO, false));
254        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_BOOK), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_BOOK, false));
255        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_CARD), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_CARD, false));
256        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_CERTIFICATE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_CERTIFICATE, false));
257        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_COAT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_COAT, false));
258        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_DOCUMENT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_DOCUMENT, false));
259        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_ELECTRONIC), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_ELECTRONIC, false));
260        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_FICHE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_FICHE, false));
261        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_FILM), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_FILM, false));
262        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_MAGAZINE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_MAGAZINE, false));
263        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_MANUSCRIPT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_MANUSCRIPT, false));
264        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_MAP), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_MAP, false));
265        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_NEWSPAPER), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_NEWSPAPER, false));
266        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_OTHER), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_OTHER, false));
267        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_PAINTING), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_PAINTING, false));
268        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_PHOTO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_PHOTO, false));
269        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_TOMBSTONE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_TOMBSTONE, false));
270        $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::TYPE_VIDEO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::TYPE_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::TYPE_AUDIO       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
289            SourceMediaType::TYPE_BOOK        => $this->getBlockSetting($block_id, 'filter_book', '1'),
290            SourceMediaType::TYPE_CARD        => $this->getBlockSetting($block_id, 'filter_card', '1'),
291            SourceMediaType::TYPE_CERTIFICATE => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
292            SourceMediaType::TYPE_COAT        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
293            SourceMediaType::TYPE_DOCUMENT    => $this->getBlockSetting($block_id, 'filter_document', '1'),
294            SourceMediaType::TYPE_ELECTRONIC  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
295            SourceMediaType::TYPE_FICHE       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
296            SourceMediaType::TYPE_FILM        => $this->getBlockSetting($block_id, 'filter_film', '1'),
297            SourceMediaType::TYPE_MAGAZINE    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
298            SourceMediaType::TYPE_MANUSCRIPT  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
299            SourceMediaType::TYPE_MAP         => $this->getBlockSetting($block_id, 'filter_map', '1'),
300            SourceMediaType::TYPE_NEWSPAPER   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
301            SourceMediaType::TYPE_OTHER       => $this->getBlockSetting($block_id, 'filter_other', '1'),
302            SourceMediaType::TYPE_PAINTING    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
303            SourceMediaType::TYPE_PHOTO       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
304            SourceMediaType::TYPE_TOMBSTONE   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
305            SourceMediaType::TYPE_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