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