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