xref: /webtrees/app/Module/SlideShowModule.php (revision 6dee9a8c9b87a4759535afc29fe1e81073a98265)
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', '0');
64        $filter        = $this->getBlockSetting($block_id, 'filter', 'all');
65        $controls      = $this->getBlockSetting($block_id, 'controls', '1');
66        $start         = (bool) $request->get('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        $this->setBlockSetting($block_id, 'filter', $request->get('filter', 'all'));
208        $this->setBlockSetting($block_id, 'controls', $request->get('controls', ''));
209        $this->setBlockSetting($block_id, 'start', $request->get('start', ''));
210        $this->setBlockSetting($block_id, 'filter_audio', $request->get('filter_audio', ''));
211        $this->setBlockSetting($block_id, 'filter_book', $request->get('filter_book', ''));
212        $this->setBlockSetting($block_id, 'filter_card', $request->get('filter_card', ''));
213        $this->setBlockSetting($block_id, 'filter_certificate', $request->get('filter_certificate', ''));
214        $this->setBlockSetting($block_id, 'filter_coat', $request->get('filter_coat', ''));
215        $this->setBlockSetting($block_id, 'filter_document', $request->get('filter_document', ''));
216        $this->setBlockSetting($block_id, 'filter_electronic', $request->get('filter_electronic', ''));
217        $this->setBlockSetting($block_id, 'filter_fiche', $request->get('filter_fiche', ''));
218        $this->setBlockSetting($block_id, 'filter_film', $request->get('filter_film', ''));
219        $this->setBlockSetting($block_id, 'filter_magazine', $request->get('filter_magazine', ''));
220        $this->setBlockSetting($block_id, 'filter_manuscript', $request->get('filter_manuscript', ''));
221        $this->setBlockSetting($block_id, 'filter_map', $request->get('filter_map', ''));
222        $this->setBlockSetting($block_id, 'filter_newspaper', $request->get('filter_newspaper', ''));
223        $this->setBlockSetting($block_id, 'filter_other', $request->get('filter_other', ''));
224        $this->setBlockSetting($block_id, 'filter_painting', $request->get('filter_painting', ''));
225        $this->setBlockSetting($block_id, 'filter_photo', $request->get('filter_photo', ''));
226        $this->setBlockSetting($block_id, 'filter_tombstone', $request->get('filter_tombstone', ''));
227        $this->setBlockSetting($block_id, 'filter_video', $request->get('filter_video', ''));
228    }
229
230    /**
231     * An HTML form to edit block settings
232     *
233     * @param Tree $tree
234     * @param int  $block_id
235     *
236     * @return void
237     */
238    public function editBlockConfiguration(Tree $tree, int $block_id): void
239    {
240        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
241        $controls = $this->getBlockSetting($block_id, 'controls', '1');
242        $start    = $this->getBlockSetting($block_id, 'start', '0');
243
244        $filters = [
245            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
246            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
247            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
248            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
249            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
250            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
251            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
252            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
253            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
254            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
255            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
256            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
257            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
258            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
259            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
260            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
261            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
262            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
263        ];
264
265        $formats = GedcomTag::getFileFormTypes();
266
267        echo view('modules/random_media/config', [
268            'controls' => $controls,
269            'filter'   => $filter,
270            'filters'  => $filters,
271            'formats'  => $formats,
272            'start'    => $start,
273        ]);
274    }
275}
276