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