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