xref: /webtrees/app/Module/SlideShowModule.php (revision 59f2f229057fe08ec1b09bd435699190641eed6a)
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\Filter;
21use Fisharebest\Webtrees\GedcomTag;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Media;
24use Fisharebest\Webtrees\Tree;
25
26/**
27 * Class SlideShowModule
28 */
29class SlideShowModule extends AbstractModule implements ModuleBlockInterface
30{
31    /** {@inheritdoc} */
32    public function getTitle()
33    {
34        /* I18N: Name of a module */
35        return I18N::translate('Slide show');
36    }
37
38    /** {@inheritdoc} */
39    public function getDescription()
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    = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start');
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        } else {
152            return $content;
153        }
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     * An HTML form to edit block settings
176     *
177     * @param Tree $tree
178     * @param int  $block_id
179     *
180     * @return void
181     */
182    public function configureBlock(Tree $tree, int $block_id)
183    {
184        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
185            $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all'));
186            $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls'));
187            $this->setBlockSetting($block_id, 'start', Filter::postBool('start'));
188            $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio'));
189            $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book'));
190            $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card'));
191            $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate'));
192            $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat'));
193            $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document'));
194            $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic'));
195            $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche'));
196            $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film'));
197            $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine'));
198            $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript'));
199            $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map'));
200            $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper'));
201            $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other'));
202            $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting'));
203            $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo'));
204            $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone'));
205            $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video'));
206
207            return;
208        }
209
210        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
211        $controls = $this->getBlockSetting($block_id, 'controls', '1');
212        $start    = $this->getBlockSetting($block_id, 'start', '0');
213
214        $filters = [
215            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
216            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
217            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
218            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
219            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
220            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
221            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
222            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
223            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
224            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
225            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
226            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
227            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
228            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
229            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
230            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
231            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
232            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
233        ];
234
235        $formats = GedcomTag::getFileFormTypes();
236
237        echo view('modules/random_media/config', [
238            'controls' => $controls,
239            'filter'   => $filter,
240            'filters'  => $filters,
241            'formats'  => $formats,
242            'start'    => $start,
243        ]);
244    }
245}
246