xref: /webtrees/app/Module/SlideShowModule.php (revision 379e735bf03b2ef02d2bf328828aaefcfc3d572a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\GedcomTag;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Media;
23use Fisharebest\Webtrees\MediaFile;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26use Illuminate\Database\Query\JoinClause;
27use Illuminate\Support\Str;
28use Psr\Http\Message\ServerRequestInterface;
29use function app;
30
31/**
32 * Class SlideShowModule
33 */
34class SlideShowModule extends AbstractModule implements ModuleBlockInterface
35{
36    use ModuleBlockTrait;
37
38    /**
39     * A sentence describing what this module does.
40     *
41     * @return string
42     */
43    public function description(): string
44    {
45        /* I18N: Description of the “Slide show” module */
46        return I18N::translate('Random images from the current family tree.');
47    }
48
49    /**
50     * Generate the HTML content of this block.
51     *
52     * @param Tree     $tree
53     * @param int      $block_id
54     * @param string   $context
55     * @param string[] $config
56     *
57     * @return string
58     */
59    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
60    {
61        $request       = app(ServerRequestInterface::class);
62        $default_start = $this->getBlockSetting($block_id, 'start');
63        $filter        = $this->getBlockSetting($block_id, 'filter', 'all');
64        $controls      = $this->getBlockSetting($block_id, 'controls', '1');
65        $start         = (bool) ($request->getQueryParams()['start'] ?? $default_start);
66
67        $media_types = [
68            $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
69            $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
70            $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
71            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
72            $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
73            $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
74            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
75            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
76            $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
77            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
78            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
79            $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
80            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
81            $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
82            $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
83            $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
84            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
85            $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
86        ];
87
88        $media_types = array_filter($media_types);
89
90        // We can apply the filters using SQL
91        // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
92        $all_media = DB::table('media')
93            ->join('media_file', static function (JoinClause $join): void {
94                $join
95                    ->on('media_file.m_file', '=', 'media.m_file')
96                    ->on('media_file.m_id', '=', 'media.m_id');
97            })
98            ->where('media.m_file', '=', $tree->id())
99            ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp'])
100            ->whereIn('media_file.source_media_type', $media_types)
101            ->pluck('media.m_id')
102            ->all();
103
104        // Keep looking through the media until a suitable one is found.
105        $random_media = null;
106        while (!empty($all_media)) {
107            $n          = array_rand($all_media);
108            $media      = Media::getInstance($all_media[$n], $tree);
109            $media_file = $media->firstImageFile();
110            if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) {
111                // Check if it is linked to a suitable individual
112                foreach ($media->linkedIndividuals('OBJE') as $indi) {
113                    if (
114                        $filter === 'all' ||
115                        $filter === 'indi' && strpos($indi->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false ||
116                        $filter === 'event' && strpos($indi->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false
117                    ) {
118                        // Found one :-)
119                        $random_media = $media;
120                        break 2;
121                    }
122                }
123            }
124            unset($all_media[$n]);
125        }
126
127        if ($random_media) {
128            $content = view('modules/random_media/slide-show', [
129                'block_id'            => $block_id,
130                'media'               => $random_media,
131                'media_file'          => $random_media->firstImageFile(),
132                'show_controls'       => $controls,
133                'start_automatically' => $start,
134                'tree'                => $tree,
135            ]);
136        } else {
137            $content = I18N::translate('This family tree has no images to display.');
138        }
139
140        if ($context !== self::CONTEXT_EMBED) {
141            return view('modules/block-template', [
142                'block'      => Str::kebab($this->name()),
143                'id'         => $block_id,
144                'config_url' => $this->configUrl($tree, $context, $block_id),
145                'title'      => $this->title(),
146                'content'    => $content,
147            ]);
148        }
149
150        return $content;
151    }
152
153    /**
154     * How should this module be identified in the control panel, etc.?
155     *
156     * @return string
157     */
158    public function title(): string
159    {
160        /* I18N: Name of a module */
161        return I18N::translate('Slide show');
162    }
163
164    /**
165     * Should this block load asynchronously using AJAX?
166     *
167     * Simple blocks are faster in-line, more complex ones can be loaded later.
168     *
169     * @return bool
170     */
171    public function loadAjax(): bool
172    {
173        return true;
174    }
175
176    /**
177     * Can this block be shown on the user’s home page?
178     *
179     * @return bool
180     */
181    public function isUserBlock(): bool
182    {
183        return true;
184    }
185
186    /**
187     * Can this block be shown on the tree’s home page?
188     *
189     * @return bool
190     */
191    public function isTreeBlock(): bool
192    {
193        return true;
194    }
195
196    /**
197     * Update the configuration for a block.
198     *
199     * @param ServerRequestInterface $request
200     * @param int                    $block_id
201     *
202     * @return void
203     */
204    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
205    {
206        $params = $request->getParsedBody();
207
208        $this->setBlockSetting($block_id, 'filter', $params['filter']);
209        $this->setBlockSetting($block_id, 'controls', $params['controls']);
210        $this->setBlockSetting($block_id, 'start', $params['start']);
211        $this->setBlockSetting($block_id, 'filter_audio', $params['filter_audio'] ?? '');
212        $this->setBlockSetting($block_id, 'filter_book', $params['filter_book'] ?? '');
213        $this->setBlockSetting($block_id, 'filter_card', $params['filter_card'] ?? '');
214        $this->setBlockSetting($block_id, 'filter_certificate', $params['filter_certificate'] ?? '');
215        $this->setBlockSetting($block_id, 'filter_coat', $params['filter_coat'] ?? '');
216        $this->setBlockSetting($block_id, 'filter_document', $params['filter_document'] ?? '');
217        $this->setBlockSetting($block_id, 'filter_electronic', $params['filter_electronic'] ?? '');
218        $this->setBlockSetting($block_id, 'filter_fiche', $params['filter_fiche'] ?? '');
219        $this->setBlockSetting($block_id, 'filter_film', $params['filter_film'] ?? '');
220        $this->setBlockSetting($block_id, 'filter_magazine', $params['filter_magazine'] ?? '');
221        $this->setBlockSetting($block_id, 'filter_manuscript', $params['filter_manuscript'] ?? '');
222        $this->setBlockSetting($block_id, 'filter_map', $params['filter_map'] ?? '');
223        $this->setBlockSetting($block_id, 'filter_newspaper', $params['filter_newspaper'] ?? '');
224        $this->setBlockSetting($block_id, 'filter_other', $params['filter_other'] ?? '');
225        $this->setBlockSetting($block_id, 'filter_painting', $params['filter_painting'] ?? '');
226        $this->setBlockSetting($block_id, 'filter_photo', $params['filter_photo'] ?? '');
227        $this->setBlockSetting($block_id, 'filter_tombstone', $params['filter_tombstone'] ?? '');
228        $this->setBlockSetting($block_id, 'filter_video', $params['filter_video'] ?? '');
229    }
230
231    /**
232     * An HTML form to edit block settings
233     *
234     * @param Tree $tree
235     * @param int  $block_id
236     *
237     * @return string
238     */
239    public function editBlockConfiguration(Tree $tree, int $block_id): string
240    {
241        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
242        $controls = $this->getBlockSetting($block_id, 'controls', '1');
243        $start    = $this->getBlockSetting($block_id, 'start', '0');
244
245        $filters = [
246            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
247            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
248            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
249            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
250            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
251            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
252            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
253            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
254            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
255            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
256            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
257            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
258            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
259            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
260            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
261            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
262            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
263            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
264        ];
265
266        $formats = GedcomTag::getFileFormTypes();
267
268        return view('modules/random_media/config', [
269            'controls' => $controls,
270            'filter'   => $filter,
271            'filters'  => $filters,
272            'formats'  => $formats,
273            'start'    => $start,
274        ]);
275    }
276}
277