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