xref: /webtrees/app/Module/SlideShowModule.php (revision 6ebd34c53b50e06ff296dfc6dfab9e827e3ae38f)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
24a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class SlideShowModule
288c2e8227SGreg Roach */
29c1010edaSGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface
30c1010edaSGreg Roach{
318c2e8227SGreg Roach    /** {@inheritdoc} */
32c1010edaSGreg Roach    public function getTitle()
33c1010edaSGreg Roach    {
34bbb76c12SGreg Roach        /* I18N: Name of a module */
35bbb76c12SGreg Roach        return I18N::translate('Slide show');
368c2e8227SGreg Roach    }
378c2e8227SGreg Roach
388c2e8227SGreg Roach    /** {@inheritdoc} */
39c1010edaSGreg Roach    public function getDescription()
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Description of the “Slide show” module */
42bbb76c12SGreg Roach        return I18N::translate('Random images from the current family tree.');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
4576692c8bSGreg Roach    /**
4676692c8bSGreg Roach     * Generate the HTML content of this block.
4776692c8bSGreg Roach     *
48e490cd80SGreg Roach     * @param Tree     $tree
4976692c8bSGreg Roach     * @param int      $block_id
5076692c8bSGreg Roach     * @param bool     $template
51727f238cSGreg Roach     * @param string[] $cfg
5276692c8bSGreg Roach     *
5376692c8bSGreg Roach     * @return string
5476692c8bSGreg Roach     */
55c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
56c1010edaSGreg Roach    {
57e490cd80SGreg Roach        global $ctype;
588c2e8227SGreg Roach
59e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
60e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
61*6ebd34c5SGreg Roach        $start    = (bool) $this->getBlockSetting($block_id, 'start', '0');
628c2e8227SGreg Roach
638c2e8227SGreg Roach        // We can apply the filters using SQL
648c2e8227SGreg Roach        // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
658c2e8227SGreg Roach        $all_media = Database::prepare(
668c2e8227SGreg Roach            "SELECT m_id FROM `##media`" .
678f5f5da8SGreg Roach            " JOIN `##media_file` USING (m_file, m_id)" .
688c2e8227SGreg Roach            " WHERE m_file = ?" .
698f5f5da8SGreg Roach            " AND multimedia_format  IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" .
708f5f5da8SGreg Roach            " AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')"
7113abd6f3SGreg Roach        )->execute([
72e490cd80SGreg Roach            $tree->getTreeId(),
73e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
74e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
75e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
76e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
77e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
78e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
79e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
80e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
81e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
82e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
83e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
84e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
85e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
86e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
87e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
88e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
89e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
90e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
9113abd6f3SGreg Roach        ])->fetchOneColumn();
928c2e8227SGreg Roach
938c2e8227SGreg Roach        // Keep looking through the media until a suitable one is found.
948c2e8227SGreg Roach        $random_media = null;
958c2e8227SGreg Roach        while ($all_media) {
968c2e8227SGreg Roach            $n          = array_rand($all_media);
97e490cd80SGreg Roach            $media      = Media::getInstance($all_media[$n], $tree);
98572a608bSGreg Roach            $media_file = $media->firstImageFile();
99bdf55d8bSGreg Roach            if ($media->canShow() && $media_file !== null && !$media_file->isExternal()) {
1008c2e8227SGreg Roach                // Check if it is linked to a suitable individual
1018c2e8227SGreg Roach                foreach ($media->linkedIndividuals('OBJE') as $indi) {
1028c2e8227SGreg Roach                    if (
1038c2e8227SGreg Roach                        $filter === 'all' ||
1048c2e8227SGreg Roach                        $filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false ||
1058c2e8227SGreg Roach                        $filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false
1068c2e8227SGreg Roach                    ) {
1078c2e8227SGreg Roach                        // Found one :-)
1088c2e8227SGreg Roach                        $random_media = $media;
1098c2e8227SGreg Roach                        break 2;
1108c2e8227SGreg Roach                    }
1118c2e8227SGreg Roach                }
1128c2e8227SGreg Roach            }
1138c2e8227SGreg Roach            unset($all_media[$n]);
1143c12f3e5SGreg Roach        }
1158c2e8227SGreg Roach
1168c2e8227SGreg Roach        if ($random_media) {
117147e99aaSGreg Roach            $content = view('modules/random_media/slide-show', [
118a1fe7073SGreg Roach                'block_id'            => $block_id,
119a1fe7073SGreg Roach                'media'               => $random_media,
120572a608bSGreg Roach                'media_file'          => $media_file,
121a1fe7073SGreg Roach                'show_controls'       => $controls,
122a1fe7073SGreg Roach                'start_automatically' => $start,
123911f5683SGreg Roach                'tree'                => $tree,
124a1fe7073SGreg Roach            ]);
1258c2e8227SGreg Roach        } else {
1268c2e8227SGreg Roach            $content = I18N::translate('This family tree has no images to display.');
1278c2e8227SGreg Roach        }
128a1fe7073SGreg Roach
1298c2e8227SGreg Roach        if ($template) {
130e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
131c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
132c1010edaSGreg Roach                    'block_id' => $block_id,
133c1010edaSGreg Roach                    'ged'      => $tree->getName(),
134c1010edaSGreg Roach                ]);
135397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
136c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
137c1010edaSGreg Roach                    'block_id' => $block_id,
138c1010edaSGreg Roach                    'ged'      => $tree->getName(),
139c1010edaSGreg Roach                ]);
1408cbbfdceSGreg Roach            } else {
1418cbbfdceSGreg Roach                $config_url = '';
1428cbbfdceSGreg Roach            }
1438cbbfdceSGreg Roach
144147e99aaSGreg Roach            return view('modules/block-template', [
1459c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1469c6524dcSGreg Roach                'id'         => $block_id,
1478cbbfdceSGreg Roach                'config_url' => $config_url,
1489c6524dcSGreg Roach                'title'      => $this->getTitle(),
1499c6524dcSGreg Roach                'content'    => $content,
1509c6524dcSGreg Roach            ]);
1518c2e8227SGreg Roach        } else {
1528c2e8227SGreg Roach            return $content;
1538c2e8227SGreg Roach        }
1548c2e8227SGreg Roach    }
1558c2e8227SGreg Roach
1568c2e8227SGreg Roach    /** {@inheritdoc} */
157c1010edaSGreg Roach    public function loadAjax(): bool
158c1010edaSGreg Roach    {
1598c2e8227SGreg Roach        return true;
1608c2e8227SGreg Roach    }
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach    /** {@inheritdoc} */
163c1010edaSGreg Roach    public function isUserBlock(): bool
164c1010edaSGreg Roach    {
1658c2e8227SGreg Roach        return true;
1668c2e8227SGreg Roach    }
1678c2e8227SGreg Roach
1688c2e8227SGreg Roach    /** {@inheritdoc} */
169c1010edaSGreg Roach    public function isGedcomBlock(): bool
170c1010edaSGreg Roach    {
1718c2e8227SGreg Roach        return true;
1728c2e8227SGreg Roach    }
1738c2e8227SGreg Roach
17476692c8bSGreg Roach    /**
175a45f9889SGreg Roach     * Update the configuration for a block.
176a45f9889SGreg Roach     *
177a45f9889SGreg Roach     * @param Request $request
178a45f9889SGreg Roach     * @param int     $block_id
179a45f9889SGreg Roach     *
180a45f9889SGreg Roach     * @return void
181a45f9889SGreg Roach     */
182a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
183a45f9889SGreg Roach    {
184a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter', $request->get('filter', 'all'));
185a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'controls', $request->get('controls', ''));
186a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'start', $request->get('start', ''));
187a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_audio', $request->get('filter_audio', ''));
188a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_book', $request->get('filter_book', ''));
189a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_card', $request->get('filter_card', ''));
190a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_certificate', $request->get('filter_certificate', ''));
191a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_coat', $request->get('filter_coat', ''));
192a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_document', $request->get('filter_document', ''));
193a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_electronic', $request->get('filter_electronic', ''));
194a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_fiche', $request->get('filter_fiche', ''));
195a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_film', $request->get('filter_film', ''));
196a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_magazine', $request->get('filter_magazine', ''));
197a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_manuscript', $request->get('filter_manuscript', ''));
198a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_map', $request->get('filter_map', ''));
199a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_newspaper', $request->get('filter_newspaper', ''));
200a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_other', $request->get('filter_other', ''));
201a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_painting', $request->get('filter_painting', ''));
202a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_photo', $request->get('filter_photo', ''));
203a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_tombstone', $request->get('filter_tombstone', ''));
204a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_video', $request->get('filter_video', ''));
205a45f9889SGreg Roach    }
206a45f9889SGreg Roach
207a45f9889SGreg Roach    /**
20876692c8bSGreg Roach     * An HTML form to edit block settings
20976692c8bSGreg Roach     *
210e490cd80SGreg Roach     * @param Tree $tree
21176692c8bSGreg Roach     * @param int  $block_id
212a9430be8SGreg Roach     *
213a9430be8SGreg Roach     * @return void
21476692c8bSGreg Roach     */
215a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
216c1010edaSGreg Roach    {
217e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
218e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
21915d603e7SGreg Roach        $start    = $this->getBlockSetting($block_id, 'start', '0');
2208c2e8227SGreg Roach
22113abd6f3SGreg Roach        $filters = [
222e2a378d3SGreg Roach            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
223e2a378d3SGreg Roach            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
224e2a378d3SGreg Roach            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
225e2a378d3SGreg Roach            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
226e2a378d3SGreg Roach            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
227e2a378d3SGreg Roach            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
228e2a378d3SGreg Roach            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
229e2a378d3SGreg Roach            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
230e2a378d3SGreg Roach            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
231e2a378d3SGreg Roach            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
232e2a378d3SGreg Roach            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
233e2a378d3SGreg Roach            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
234e2a378d3SGreg Roach            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
235e2a378d3SGreg Roach            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
236e2a378d3SGreg Roach            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
237e2a378d3SGreg Roach            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
238e2a378d3SGreg Roach            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
239e2a378d3SGreg Roach            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
24013abd6f3SGreg Roach        ];
2418c2e8227SGreg Roach
242c385536dSGreg Roach        $formats = GedcomTag::getFileFormTypes();
24315d603e7SGreg Roach
244147e99aaSGreg Roach        echo view('modules/random_media/config', [
245c385536dSGreg Roach            'controls' => $controls,
246c385536dSGreg Roach            'filter'   => $filter,
247c385536dSGreg Roach            'filters'  => $filters,
248c385536dSGreg Roach            'formats'  => $formats,
249c385536dSGreg Roach            'start'    => $start,
250c385536dSGreg Roach        ]);
2518c2e8227SGreg Roach    }
2528c2e8227SGreg Roach}
253