xref: /webtrees/app/Module/SlideShowModule.php (revision 7d0db6487d7b511ca7b3b66dc7ed8174893963ca)
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
5376692c8bSGreg Roach     * @param bool     $template
54727f238cSGreg Roach     * @param string[] $cfg
5576692c8bSGreg Roach     *
5676692c8bSGreg Roach     * @return string
5776692c8bSGreg Roach     */
58c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
59c1010edaSGreg Roach    {
60e490cd80SGreg Roach        global $ctype;
618c2e8227SGreg Roach
62e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
63e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
646ebd34c5SGreg Roach        $start    = (bool) $this->getBlockSetting($block_id, 'start', '0');
658c2e8227SGreg Roach
668c2e8227SGreg Roach        // We can apply the filters using SQL
678c2e8227SGreg Roach        // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
688c2e8227SGreg Roach        $all_media = Database::prepare(
698c2e8227SGreg Roach            "SELECT m_id FROM `##media`" .
708f5f5da8SGreg Roach            " JOIN `##media_file` USING (m_file, m_id)" .
718c2e8227SGreg Roach            " WHERE m_file = ?" .
728f5f5da8SGreg Roach            " AND multimedia_format  IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" .
738f5f5da8SGreg Roach            " AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')"
7413abd6f3SGreg Roach        )->execute([
7572cf66d4SGreg Roach            $tree->id(),
76e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
77e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
78e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
79e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
80e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
81e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
82e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
83e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
84e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
85e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
86e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
87e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
88e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
89e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
90e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
91e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
92e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
93e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
9413abd6f3SGreg Roach        ])->fetchOneColumn();
958c2e8227SGreg Roach
968c2e8227SGreg Roach        // Keep looking through the media until a suitable one is found.
978c2e8227SGreg Roach        $random_media = null;
98495cefeaSGreg Roach        while (!empty($all_media)) {
998c2e8227SGreg Roach            $n          = array_rand($all_media);
100e490cd80SGreg Roach            $media      = Media::getInstance($all_media[$n], $tree);
101572a608bSGreg Roach            $media_file = $media->firstImageFile();
102495cefeaSGreg Roach            if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) {
1038c2e8227SGreg Roach                // Check if it is linked to a suitable individual
1048c2e8227SGreg Roach                foreach ($media->linkedIndividuals('OBJE') as $indi) {
1058c2e8227SGreg Roach                    if (
1068c2e8227SGreg Roach                        $filter === 'all' ||
107*7d0db648SGreg Roach                        $filter === 'indi' && strpos($indi->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false ||
108*7d0db648SGreg Roach                        $filter === 'event' && strpos($indi->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false
1098c2e8227SGreg Roach                    ) {
1108c2e8227SGreg Roach                        // Found one :-)
1118c2e8227SGreg Roach                        $random_media = $media;
1128c2e8227SGreg Roach                        break 2;
1138c2e8227SGreg Roach                    }
1148c2e8227SGreg Roach                }
1158c2e8227SGreg Roach            }
1168c2e8227SGreg Roach            unset($all_media[$n]);
1173c12f3e5SGreg Roach        }
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach        if ($random_media) {
120147e99aaSGreg Roach            $content = view('modules/random_media/slide-show', [
121a1fe7073SGreg Roach                'block_id'            => $block_id,
122a1fe7073SGreg Roach                'media'               => $random_media,
123495cefeaSGreg Roach                'media_file'          => $random_media->firstImageFile(),
124a1fe7073SGreg Roach                'show_controls'       => $controls,
125a1fe7073SGreg Roach                'start_automatically' => $start,
126911f5683SGreg Roach                'tree'                => $tree,
127a1fe7073SGreg Roach            ]);
1288c2e8227SGreg Roach        } else {
1298c2e8227SGreg Roach            $content = I18N::translate('This family tree has no images to display.');
1308c2e8227SGreg Roach        }
131a1fe7073SGreg Roach
1328c2e8227SGreg Roach        if ($template) {
133e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
134c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
135c1010edaSGreg Roach                    'block_id' => $block_id,
136aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
137c1010edaSGreg Roach                ]);
138397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
139c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
140c1010edaSGreg Roach                    'block_id' => $block_id,
141aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
142c1010edaSGreg Roach                ]);
1438cbbfdceSGreg Roach            } else {
1448cbbfdceSGreg Roach                $config_url = '';
1458cbbfdceSGreg Roach            }
1468cbbfdceSGreg Roach
147147e99aaSGreg Roach            return view('modules/block-template', [
1489c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1499c6524dcSGreg Roach                'id'         => $block_id,
1508cbbfdceSGreg Roach                'config_url' => $config_url,
1519c6524dcSGreg Roach                'title'      => $this->getTitle(),
1529c6524dcSGreg Roach                'content'    => $content,
1539c6524dcSGreg Roach            ]);
1548c2e8227SGreg Roach        }
155b2ce94c6SRico Sonntag
156b2ce94c6SRico Sonntag        return $content;
1578c2e8227SGreg Roach    }
1588c2e8227SGreg Roach
1598c2e8227SGreg Roach    /** {@inheritdoc} */
160c1010edaSGreg Roach    public function loadAjax(): bool
161c1010edaSGreg Roach    {
1628c2e8227SGreg Roach        return true;
1638c2e8227SGreg Roach    }
1648c2e8227SGreg Roach
1658c2e8227SGreg Roach    /** {@inheritdoc} */
166c1010edaSGreg Roach    public function isUserBlock(): bool
167c1010edaSGreg Roach    {
1688c2e8227SGreg Roach        return true;
1698c2e8227SGreg Roach    }
1708c2e8227SGreg Roach
1718c2e8227SGreg Roach    /** {@inheritdoc} */
172c1010edaSGreg Roach    public function isGedcomBlock(): bool
173c1010edaSGreg Roach    {
1748c2e8227SGreg Roach        return true;
1758c2e8227SGreg Roach    }
1768c2e8227SGreg Roach
17776692c8bSGreg Roach    /**
178a45f9889SGreg Roach     * Update the configuration for a block.
179a45f9889SGreg Roach     *
180a45f9889SGreg Roach     * @param Request $request
181a45f9889SGreg Roach     * @param int     $block_id
182a45f9889SGreg Roach     *
183a45f9889SGreg Roach     * @return void
184a45f9889SGreg Roach     */
185a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
186a45f9889SGreg Roach    {
187a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter', $request->get('filter', 'all'));
188a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'controls', $request->get('controls', ''));
189a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'start', $request->get('start', ''));
190a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_audio', $request->get('filter_audio', ''));
191a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_book', $request->get('filter_book', ''));
192a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_card', $request->get('filter_card', ''));
193a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_certificate', $request->get('filter_certificate', ''));
194a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_coat', $request->get('filter_coat', ''));
195a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_document', $request->get('filter_document', ''));
196a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_electronic', $request->get('filter_electronic', ''));
197a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_fiche', $request->get('filter_fiche', ''));
198a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_film', $request->get('filter_film', ''));
199a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_magazine', $request->get('filter_magazine', ''));
200a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_manuscript', $request->get('filter_manuscript', ''));
201a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_map', $request->get('filter_map', ''));
202a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_newspaper', $request->get('filter_newspaper', ''));
203a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_other', $request->get('filter_other', ''));
204a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_painting', $request->get('filter_painting', ''));
205a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_photo', $request->get('filter_photo', ''));
206a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_tombstone', $request->get('filter_tombstone', ''));
207a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_video', $request->get('filter_video', ''));
208a45f9889SGreg Roach    }
209a45f9889SGreg Roach
210a45f9889SGreg Roach    /**
21176692c8bSGreg Roach     * An HTML form to edit block settings
21276692c8bSGreg Roach     *
213e490cd80SGreg Roach     * @param Tree $tree
21476692c8bSGreg Roach     * @param int  $block_id
215a9430be8SGreg Roach     *
216a9430be8SGreg Roach     * @return void
21776692c8bSGreg Roach     */
218a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
219c1010edaSGreg Roach    {
220e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
221e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
22215d603e7SGreg Roach        $start    = $this->getBlockSetting($block_id, 'start', '0');
2238c2e8227SGreg Roach
22413abd6f3SGreg Roach        $filters = [
225e2a378d3SGreg Roach            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
226e2a378d3SGreg Roach            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
227e2a378d3SGreg Roach            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
228e2a378d3SGreg Roach            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
229e2a378d3SGreg Roach            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
230e2a378d3SGreg Roach            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
231e2a378d3SGreg Roach            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
232e2a378d3SGreg Roach            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
233e2a378d3SGreg Roach            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
234e2a378d3SGreg Roach            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
235e2a378d3SGreg Roach            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
236e2a378d3SGreg Roach            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
237e2a378d3SGreg Roach            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
238e2a378d3SGreg Roach            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
239e2a378d3SGreg Roach            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
240e2a378d3SGreg Roach            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
241e2a378d3SGreg Roach            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
242e2a378d3SGreg Roach            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
24313abd6f3SGreg Roach        ];
2448c2e8227SGreg Roach
245c385536dSGreg Roach        $formats = GedcomTag::getFileFormTypes();
24615d603e7SGreg Roach
247147e99aaSGreg Roach        echo view('modules/random_media/config', [
248c385536dSGreg Roach            'controls' => $controls,
249c385536dSGreg Roach            'filter'   => $filter,
250c385536dSGreg Roach            'filters'  => $filters,
251c385536dSGreg Roach            'formats'  => $formats,
252c385536dSGreg Roach            'start'    => $start,
253c385536dSGreg Roach        ]);
2548c2e8227SGreg Roach    }
2558c2e8227SGreg Roach}
256