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