xref: /webtrees/app/Module/SlideShowModule.php (revision 5f2ae57322ecd2d23660294256ca8376fd889348)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
25495cefeaSGreg Roachuse Fisharebest\Webtrees\MediaFile;
26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
27a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class SlideShowModule
318c2e8227SGreg Roach */
32c1010edaSGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
348c2e8227SGreg Roach    /** {@inheritdoc} */
358f53f488SRico Sonntag    public function getTitle(): string
36c1010edaSGreg Roach    {
37bbb76c12SGreg Roach        /* I18N: Name of a module */
38bbb76c12SGreg Roach        return I18N::translate('Slide show');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
428f53f488SRico Sonntag    public function getDescription(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Description of the “Slide show” module */
45bbb76c12SGreg Roach        return I18N::translate('Random images from the current family tree.');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
4876692c8bSGreg Roach    /**
4976692c8bSGreg Roach     * Generate the HTML content of this block.
5076692c8bSGreg Roach     *
51e490cd80SGreg Roach     * @param Tree     $tree
5276692c8bSGreg Roach     * @param int      $block_id
53*5f2ae573SGreg Roach     * @param string   $ctype
54727f238cSGreg Roach     * @param string[] $cfg
5576692c8bSGreg Roach     *
5676692c8bSGreg Roach     * @return string
5776692c8bSGreg Roach     */
58*5f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
59c1010edaSGreg Roach    {
60e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
61e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
626ebd34c5SGreg Roach        $start    = (bool) $this->getBlockSetting($block_id, 'start', '0');
638c2e8227SGreg Roach
648c2e8227SGreg Roach        // We can apply the filters using SQL
658c2e8227SGreg Roach        // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
668c2e8227SGreg Roach        $all_media = Database::prepare(
678c2e8227SGreg Roach            "SELECT m_id FROM `##media`" .
688f5f5da8SGreg Roach            " JOIN `##media_file` USING (m_file, m_id)" .
698c2e8227SGreg Roach            " WHERE m_file = ?" .
708f5f5da8SGreg Roach            " AND multimedia_format  IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" .
718f5f5da8SGreg Roach            " AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')"
7213abd6f3SGreg Roach        )->execute([
7372cf66d4SGreg Roach            $tree->id(),
74e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
75e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
76e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
77e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
78e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
79e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
80e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
81e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
82e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
83e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
84e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
85e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
86e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
87e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
88e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
89e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
90e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
91e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
9213abd6f3SGreg Roach        ])->fetchOneColumn();
938c2e8227SGreg Roach
948c2e8227SGreg Roach        // Keep looking through the media until a suitable one is found.
958c2e8227SGreg Roach        $random_media = null;
96495cefeaSGreg Roach        while (!empty($all_media)) {
978c2e8227SGreg Roach            $n          = array_rand($all_media);
98e490cd80SGreg Roach            $media      = Media::getInstance($all_media[$n], $tree);
99572a608bSGreg Roach            $media_file = $media->firstImageFile();
100495cefeaSGreg Roach            if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) {
1018c2e8227SGreg Roach                // Check if it is linked to a suitable individual
1028c2e8227SGreg Roach                foreach ($media->linkedIndividuals('OBJE') as $indi) {
1038c2e8227SGreg Roach                    if (
1048c2e8227SGreg Roach                        $filter === 'all' ||
1057d0db648SGreg Roach                        $filter === 'indi' && strpos($indi->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false ||
1067d0db648SGreg Roach                        $filter === 'event' && strpos($indi->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false
1078c2e8227SGreg Roach                    ) {
1088c2e8227SGreg Roach                        // Found one :-)
1098c2e8227SGreg Roach                        $random_media = $media;
1108c2e8227SGreg Roach                        break 2;
1118c2e8227SGreg Roach                    }
1128c2e8227SGreg Roach                }
1138c2e8227SGreg Roach            }
1148c2e8227SGreg Roach            unset($all_media[$n]);
1153c12f3e5SGreg Roach        }
1168c2e8227SGreg Roach
1178c2e8227SGreg Roach        if ($random_media) {
118147e99aaSGreg Roach            $content = view('modules/random_media/slide-show', [
119a1fe7073SGreg Roach                'block_id'            => $block_id,
120a1fe7073SGreg Roach                'media'               => $random_media,
121495cefeaSGreg Roach                'media_file'          => $random_media->firstImageFile(),
122a1fe7073SGreg Roach                'show_controls'       => $controls,
123a1fe7073SGreg Roach                'start_automatically' => $start,
124911f5683SGreg Roach                'tree'                => $tree,
125a1fe7073SGreg Roach            ]);
1268c2e8227SGreg Roach        } else {
1278c2e8227SGreg Roach            $content = I18N::translate('This family tree has no images to display.');
1288c2e8227SGreg Roach        }
129a1fe7073SGreg Roach
130*5f2ae573SGreg Roach        if ($ctype) {
131e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
132c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
133c1010edaSGreg Roach                    'block_id' => $block_id,
134aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
135c1010edaSGreg Roach                ]);
136397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
137c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
138c1010edaSGreg Roach                    'block_id' => $block_id,
139aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
140c1010edaSGreg Roach                ]);
1418cbbfdceSGreg Roach            } else {
1428cbbfdceSGreg Roach                $config_url = '';
1438cbbfdceSGreg Roach            }
1448cbbfdceSGreg Roach
145147e99aaSGreg Roach            return view('modules/block-template', [
1469c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1479c6524dcSGreg Roach                'id'         => $block_id,
1488cbbfdceSGreg Roach                'config_url' => $config_url,
1499c6524dcSGreg Roach                'title'      => $this->getTitle(),
1509c6524dcSGreg Roach                'content'    => $content,
1519c6524dcSGreg Roach            ]);
1528c2e8227SGreg Roach        }
153b2ce94c6SRico Sonntag
154b2ce94c6SRico Sonntag        return $content;
1558c2e8227SGreg Roach    }
1568c2e8227SGreg Roach
1578c2e8227SGreg Roach    /** {@inheritdoc} */
158c1010edaSGreg Roach    public function loadAjax(): bool
159c1010edaSGreg Roach    {
1608c2e8227SGreg Roach        return true;
1618c2e8227SGreg Roach    }
1628c2e8227SGreg Roach
1638c2e8227SGreg Roach    /** {@inheritdoc} */
164c1010edaSGreg Roach    public function isUserBlock(): bool
165c1010edaSGreg Roach    {
1668c2e8227SGreg Roach        return true;
1678c2e8227SGreg Roach    }
1688c2e8227SGreg Roach
1698c2e8227SGreg Roach    /** {@inheritdoc} */
170c1010edaSGreg Roach    public function isGedcomBlock(): bool
171c1010edaSGreg Roach    {
1728c2e8227SGreg Roach        return true;
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach
17576692c8bSGreg Roach    /**
176a45f9889SGreg Roach     * Update the configuration for a block.
177a45f9889SGreg Roach     *
178a45f9889SGreg Roach     * @param Request $request
179a45f9889SGreg Roach     * @param int     $block_id
180a45f9889SGreg Roach     *
181a45f9889SGreg Roach     * @return void
182a45f9889SGreg Roach     */
183a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
184a45f9889SGreg Roach    {
185a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter', $request->get('filter', 'all'));
186a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'controls', $request->get('controls', ''));
187a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'start', $request->get('start', ''));
188a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_audio', $request->get('filter_audio', ''));
189a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_book', $request->get('filter_book', ''));
190a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_card', $request->get('filter_card', ''));
191a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_certificate', $request->get('filter_certificate', ''));
192a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_coat', $request->get('filter_coat', ''));
193a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_document', $request->get('filter_document', ''));
194a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_electronic', $request->get('filter_electronic', ''));
195a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_fiche', $request->get('filter_fiche', ''));
196a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_film', $request->get('filter_film', ''));
197a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_magazine', $request->get('filter_magazine', ''));
198a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_manuscript', $request->get('filter_manuscript', ''));
199a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_map', $request->get('filter_map', ''));
200a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_newspaper', $request->get('filter_newspaper', ''));
201a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_other', $request->get('filter_other', ''));
202a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_painting', $request->get('filter_painting', ''));
203a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_photo', $request->get('filter_photo', ''));
204a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_tombstone', $request->get('filter_tombstone', ''));
205a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_video', $request->get('filter_video', ''));
206a45f9889SGreg Roach    }
207a45f9889SGreg Roach
208a45f9889SGreg Roach    /**
20976692c8bSGreg Roach     * An HTML form to edit block settings
21076692c8bSGreg Roach     *
211e490cd80SGreg Roach     * @param Tree $tree
21276692c8bSGreg Roach     * @param int  $block_id
213a9430be8SGreg Roach     *
214a9430be8SGreg Roach     * @return void
21576692c8bSGreg Roach     */
216a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
217c1010edaSGreg Roach    {
218e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
219e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
22015d603e7SGreg Roach        $start    = $this->getBlockSetting($block_id, 'start', '0');
2218c2e8227SGreg Roach
22213abd6f3SGreg Roach        $filters = [
223e2a378d3SGreg Roach            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
224e2a378d3SGreg Roach            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
225e2a378d3SGreg Roach            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
226e2a378d3SGreg Roach            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
227e2a378d3SGreg Roach            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
228e2a378d3SGreg Roach            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
229e2a378d3SGreg Roach            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
230e2a378d3SGreg Roach            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
231e2a378d3SGreg Roach            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
232e2a378d3SGreg Roach            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
233e2a378d3SGreg Roach            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
234e2a378d3SGreg Roach            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
235e2a378d3SGreg Roach            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
236e2a378d3SGreg Roach            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
237e2a378d3SGreg Roach            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
238e2a378d3SGreg Roach            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
239e2a378d3SGreg Roach            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
240e2a378d3SGreg Roach            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
24113abd6f3SGreg Roach        ];
2428c2e8227SGreg Roach
243c385536dSGreg Roach        $formats = GedcomTag::getFileFormTypes();
24415d603e7SGreg Roach
245147e99aaSGreg Roach        echo view('modules/random_media/config', [
246c385536dSGreg Roach            'controls' => $controls,
247c385536dSGreg Roach            'filter'   => $filter,
248c385536dSGreg Roach            'filters'  => $filters,
249c385536dSGreg Roach            'formats'  => $formats,
250c385536dSGreg Roach            'start'    => $start,
251c385536dSGreg Roach        ]);
2528c2e8227SGreg Roach    }
2538c2e8227SGreg Roach}
254