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