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