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