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