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