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