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