xref: /webtrees/app/Module/SlideShowModule.php (revision bed27cedea507c28d3a20c0fce49bfb8580f832f)
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\Auth;
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;
30use function app;
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   $ctype
56     * @param string[] $cfg
57     *
58     * @return string
59     */
60    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): 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        // We can apply the filters using SQL
92        // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
93        $all_media = DB::table('media')
94            ->join('media_file', static function (JoinClause $join): void {
95                $join
96                    ->on('media_file.m_file', '=', 'media.m_file')
97                    ->on('media_file.m_id', '=', 'media.m_id');
98            })
99            ->where('media.m_file', '=', $tree->id())
100            ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp'])
101            ->whereIn('media_file.source_media_type', $media_types)
102            ->pluck('media.m_id')
103            ->all();
104
105        // Keep looking through the media until a suitable one is found.
106        $random_media = null;
107        while (!empty($all_media)) {
108            $n          = array_rand($all_media);
109            $media      = Media::getInstance($all_media[$n], $tree);
110            $media_file = $media->firstImageFile();
111            if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) {
112                // Check if it is linked to a suitable individual
113                foreach ($media->linkedIndividuals('OBJE') as $indi) {
114                    if (
115                        $filter === 'all' ||
116                        $filter === 'indi' && strpos($indi->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false ||
117                        $filter === 'event' && strpos($indi->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false
118                    ) {
119                        // Found one :-)
120                        $random_media = $media;
121                        break 2;
122                    }
123                }
124            }
125            unset($all_media[$n]);
126        }
127
128        if ($random_media) {
129            $content = view('modules/random_media/slide-show', [
130                'block_id'            => $block_id,
131                'media'               => $random_media,
132                'media_file'          => $random_media->firstImageFile(),
133                'show_controls'       => $controls,
134                'start_automatically' => $start,
135                'tree'                => $tree,
136            ]);
137        } else {
138            $content = I18N::translate('This family tree has no images to display.');
139        }
140
141        if ($ctype !== '') {
142            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
143                $config_url = route('tree-page-block-edit', [
144                    'block_id' => $block_id,
145                    'ged'      => $tree->name(),
146                ]);
147            } elseif ($ctype === 'user' && Auth::check()) {
148                $config_url = route('user-page-block-edit', [
149                    'block_id' => $block_id,
150                    'ged'      => $tree->name(),
151                ]);
152            } else {
153                $config_url = '';
154            }
155
156            return view('modules/block-template', [
157                'block'      => Str::kebab($this->name()),
158                'id'         => $block_id,
159                'config_url' => $config_url,
160                'title'      => $this->title(),
161                'content'    => $content,
162            ]);
163        }
164
165        return $content;
166    }
167
168    /**
169     * How should this module be identified in the control panel, etc.?
170     *
171     * @return string
172     */
173    public function title(): string
174    {
175        /* I18N: Name of a module */
176        return I18N::translate('Slide show');
177    }
178
179    /** {@inheritdoc} */
180    public function loadAjax(): bool
181    {
182        return true;
183    }
184
185    /** {@inheritdoc} */
186    public function isUserBlock(): bool
187    {
188        return true;
189    }
190
191    /** {@inheritdoc} */
192    public function isTreeBlock(): bool
193    {
194        return true;
195    }
196
197    /**
198     * Update the configuration for a block.
199     *
200     * @param ServerRequestInterface $request
201     * @param int                    $block_id
202     *
203     * @return void
204     */
205    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
206    {
207        $params = $request->getParsedBody();
208
209        $this->setBlockSetting($block_id, 'filter', $params['filter']);
210        $this->setBlockSetting($block_id, 'controls', $params['controls']);
211        $this->setBlockSetting($block_id, 'start', $params['start']);
212        $this->setBlockSetting($block_id, 'filter_audio', $params['filter_audio'] ?? '');
213        $this->setBlockSetting($block_id, 'filter_book', $params['filter_book'] ?? '');
214        $this->setBlockSetting($block_id, 'filter_card', $params['filter_card'] ?? '');
215        $this->setBlockSetting($block_id, 'filter_certificate', $params['filter_certificate'] ?? '');
216        $this->setBlockSetting($block_id, 'filter_coat', $params['filter_coat'] ?? '');
217        $this->setBlockSetting($block_id, 'filter_document', $params['filter_document'] ?? '');
218        $this->setBlockSetting($block_id, 'filter_electronic', $params['filter_electronic'] ?? '');
219        $this->setBlockSetting($block_id, 'filter_fiche', $params['filter_fiche'] ?? '');
220        $this->setBlockSetting($block_id, 'filter_film', $params['filter_film'] ?? '');
221        $this->setBlockSetting($block_id, 'filter_magazine', $params['filter_magazine'] ?? '');
222        $this->setBlockSetting($block_id, 'filter_manuscript', $params['filter_manuscript'] ?? '');
223        $this->setBlockSetting($block_id, 'filter_map', $params['filter_map'] ?? '');
224        $this->setBlockSetting($block_id, 'filter_newspaper', $params['filter_newspaper'] ?? '');
225        $this->setBlockSetting($block_id, 'filter_other', $params['filter_other'] ?? '');
226        $this->setBlockSetting($block_id, 'filter_painting', $params['filter_painting'] ?? '');
227        $this->setBlockSetting($block_id, 'filter_photo', $params['filter_photo'] ?? '');
228        $this->setBlockSetting($block_id, 'filter_tombstone', $params['filter_tombstone'] ?? '');
229        $this->setBlockSetting($block_id, 'filter_video', $params['filter_video'] ?? '');
230    }
231
232    /**
233     * An HTML form to edit block settings
234     *
235     * @param Tree $tree
236     * @param int  $block_id
237     *
238     * @return void
239     */
240    public function editBlockConfiguration(Tree $tree, int $block_id): void
241    {
242        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
243        $controls = $this->getBlockSetting($block_id, 'controls', '1');
244        $start    = $this->getBlockSetting($block_id, 'start', '0');
245
246        $filters = [
247            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
248            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
249            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
250            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
251            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
252            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
253            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
254            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
255            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
256            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
257            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
258            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
259            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
260            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
261            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
262            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
263            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
264            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
265        ];
266
267        $formats = GedcomTag::getFileFormTypes();
268
269        echo view('modules/random_media/config', [
270            'controls' => $controls,
271            'filter'   => $filter,
272            'filters'  => $filters,
273            'formats'  => $formats,
274            'start'    => $start,
275        ]);
276    }
277}
278