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