xref: /webtrees/app/Module/SlideShowModule.php (revision 1e7a7a28dc1ca3b4463e0c60d00ce3163e72010d)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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\GedcomTag;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
24495cefeaSGreg Roachuse Fisharebest\Webtrees\MediaFile;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
2631e43e2fSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2731e43e2fSGreg Roachuse Illuminate\Database\Query\JoinClause;
28*1e7a7a28SGreg Roachuse Illuminate\Support\Str;
29a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class SlideShowModule
338c2e8227SGreg Roach */
3437eb8894SGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleBlockTrait;
3749a243cbSGreg Roach
38961ec755SGreg Roach    /**
390cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
40961ec755SGreg Roach     *
41961ec755SGreg Roach     * @return string
42961ec755SGreg Roach     */
4349a243cbSGreg Roach    public function title(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Name of a module */
46bbb76c12SGreg Roach        return I18N::translate('Slide show');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
49961ec755SGreg Roach    /**
50961ec755SGreg Roach     * A sentence describing what this module does.
51961ec755SGreg Roach     *
52961ec755SGreg Roach     * @return string
53961ec755SGreg Roach     */
5449a243cbSGreg Roach    public function description(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Description of the “Slide show” module */
57bbb76c12SGreg Roach        return I18N::translate('Random images from the current family tree.');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
6076692c8bSGreg Roach    /**
6176692c8bSGreg Roach     * Generate the HTML content of this block.
6276692c8bSGreg Roach     *
63e490cd80SGreg Roach     * @param Tree     $tree
6476692c8bSGreg Roach     * @param int      $block_id
655f2ae573SGreg Roach     * @param string   $ctype
66727f238cSGreg Roach     * @param string[] $cfg
6776692c8bSGreg Roach     *
6876692c8bSGreg Roach     * @return string
6976692c8bSGreg Roach     */
705f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
71c1010edaSGreg Roach    {
72e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
73e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
746ebd34c5SGreg Roach        $start    = (bool) $this->getBlockSetting($block_id, 'start', '0');
758c2e8227SGreg Roach
7631e43e2fSGreg Roach        $media_types = [
77e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
78e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
79e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
80e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
81e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
82e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
83e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
84e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
85e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
86e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
87e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
88e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
89e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
90e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
91e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
92e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
93e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
94e2a378d3SGreg Roach            $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
9531e43e2fSGreg Roach        ];
9631e43e2fSGreg Roach
9731e43e2fSGreg Roach        $media_types = array_filter($media_types);
9831e43e2fSGreg Roach
9931e43e2fSGreg Roach        // We can apply the filters using SQL
10031e43e2fSGreg Roach        // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
10131e43e2fSGreg Roach        $all_media = DB::table('media')
10231e43e2fSGreg Roach            ->join('media_file', function (JoinClause $join): void {
10331e43e2fSGreg Roach                $join
10431e43e2fSGreg Roach                    ->on('media_file.m_file', '=', 'media.m_file')
10531e43e2fSGreg Roach                    ->on('media_file.m_id', '=', 'media.m_id');
10631e43e2fSGreg Roach            })
10731e43e2fSGreg Roach            ->where('media.m_file', '=', $tree->id())
10831e43e2fSGreg Roach            ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp'])
10931e43e2fSGreg Roach            ->whereIn('media_file.source_media_type', $media_types)
11031e43e2fSGreg Roach            ->pluck('media.m_id')
11131e43e2fSGreg Roach            ->all();
1128c2e8227SGreg Roach
1138c2e8227SGreg Roach        // Keep looking through the media until a suitable one is found.
1148c2e8227SGreg Roach        $random_media = null;
115495cefeaSGreg Roach        while (!empty($all_media)) {
1168c2e8227SGreg Roach            $n          = array_rand($all_media);
117e490cd80SGreg Roach            $media      = Media::getInstance($all_media[$n], $tree);
118572a608bSGreg Roach            $media_file = $media->firstImageFile();
119495cefeaSGreg Roach            if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) {
1208c2e8227SGreg Roach                // Check if it is linked to a suitable individual
1218c2e8227SGreg Roach                foreach ($media->linkedIndividuals('OBJE') as $indi) {
1228c2e8227SGreg Roach                    if (
1238c2e8227SGreg Roach                        $filter === 'all' ||
1247d0db648SGreg Roach                        $filter === 'indi' && strpos($indi->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false ||
1257d0db648SGreg Roach                        $filter === 'event' && strpos($indi->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false
1268c2e8227SGreg Roach                    ) {
1278c2e8227SGreg Roach                        // Found one :-)
1288c2e8227SGreg Roach                        $random_media = $media;
1298c2e8227SGreg Roach                        break 2;
1308c2e8227SGreg Roach                    }
1318c2e8227SGreg Roach                }
1328c2e8227SGreg Roach            }
1338c2e8227SGreg Roach            unset($all_media[$n]);
1343c12f3e5SGreg Roach        }
1358c2e8227SGreg Roach
1368c2e8227SGreg Roach        if ($random_media) {
137147e99aaSGreg Roach            $content = view('modules/random_media/slide-show', [
138a1fe7073SGreg Roach                'block_id'            => $block_id,
139a1fe7073SGreg Roach                'media'               => $random_media,
140495cefeaSGreg Roach                'media_file'          => $random_media->firstImageFile(),
141a1fe7073SGreg Roach                'show_controls'       => $controls,
142a1fe7073SGreg Roach                'start_automatically' => $start,
143911f5683SGreg Roach                'tree'                => $tree,
144a1fe7073SGreg Roach            ]);
1458c2e8227SGreg Roach        } else {
1468c2e8227SGreg Roach            $content = I18N::translate('This family tree has no images to display.');
1478c2e8227SGreg Roach        }
148a1fe7073SGreg Roach
1496a8879feSGreg Roach        if ($ctype !== '') {
150e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
151c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
152c1010edaSGreg Roach                    'block_id' => $block_id,
153aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
154c1010edaSGreg Roach                ]);
155397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
156c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
157c1010edaSGreg Roach                    'block_id' => $block_id,
158aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
159c1010edaSGreg Roach                ]);
1608cbbfdceSGreg Roach            } else {
1618cbbfdceSGreg Roach                $config_url = '';
1628cbbfdceSGreg Roach            }
1638cbbfdceSGreg Roach
164147e99aaSGreg Roach            return view('modules/block-template', [
165*1e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1669c6524dcSGreg Roach                'id'         => $block_id,
1678cbbfdceSGreg Roach                'config_url' => $config_url,
16849a243cbSGreg Roach                'title'      => $this->title(),
1699c6524dcSGreg Roach                'content'    => $content,
1709c6524dcSGreg Roach            ]);
1718c2e8227SGreg Roach        }
172b2ce94c6SRico Sonntag
173b2ce94c6SRico Sonntag        return $content;
1748c2e8227SGreg Roach    }
1758c2e8227SGreg Roach
1768c2e8227SGreg Roach    /** {@inheritdoc} */
177c1010edaSGreg Roach    public function loadAjax(): bool
178c1010edaSGreg Roach    {
1798c2e8227SGreg Roach        return true;
1808c2e8227SGreg Roach    }
1818c2e8227SGreg Roach
1828c2e8227SGreg Roach    /** {@inheritdoc} */
183c1010edaSGreg Roach    public function isUserBlock(): bool
184c1010edaSGreg Roach    {
1858c2e8227SGreg Roach        return true;
1868c2e8227SGreg Roach    }
1878c2e8227SGreg Roach
1888c2e8227SGreg Roach    /** {@inheritdoc} */
18963276d8fSGreg Roach    public function isTreeBlock(): bool
190c1010edaSGreg Roach    {
1918c2e8227SGreg Roach        return true;
1928c2e8227SGreg Roach    }
1938c2e8227SGreg Roach
19476692c8bSGreg Roach    /**
195a45f9889SGreg Roach     * Update the configuration for a block.
196a45f9889SGreg Roach     *
197a45f9889SGreg Roach     * @param Request $request
198a45f9889SGreg Roach     * @param int     $block_id
199a45f9889SGreg Roach     *
200a45f9889SGreg Roach     * @return void
201a45f9889SGreg Roach     */
202e364afe4SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id): void
203a45f9889SGreg Roach    {
204a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter', $request->get('filter', 'all'));
205a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'controls', $request->get('controls', ''));
206a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'start', $request->get('start', ''));
207a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_audio', $request->get('filter_audio', ''));
208a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_book', $request->get('filter_book', ''));
209a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_card', $request->get('filter_card', ''));
210a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_certificate', $request->get('filter_certificate', ''));
211a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_coat', $request->get('filter_coat', ''));
212a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_document', $request->get('filter_document', ''));
213a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_electronic', $request->get('filter_electronic', ''));
214a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_fiche', $request->get('filter_fiche', ''));
215a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_film', $request->get('filter_film', ''));
216a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_magazine', $request->get('filter_magazine', ''));
217a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_manuscript', $request->get('filter_manuscript', ''));
218a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_map', $request->get('filter_map', ''));
219a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_newspaper', $request->get('filter_newspaper', ''));
220a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_other', $request->get('filter_other', ''));
221a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_painting', $request->get('filter_painting', ''));
222a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_photo', $request->get('filter_photo', ''));
223a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_tombstone', $request->get('filter_tombstone', ''));
224a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter_video', $request->get('filter_video', ''));
225a45f9889SGreg Roach    }
226a45f9889SGreg Roach
227a45f9889SGreg Roach    /**
22876692c8bSGreg Roach     * An HTML form to edit block settings
22976692c8bSGreg Roach     *
230e490cd80SGreg Roach     * @param Tree $tree
23176692c8bSGreg Roach     * @param int  $block_id
232a9430be8SGreg Roach     *
233a9430be8SGreg Roach     * @return void
23476692c8bSGreg Roach     */
235e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
236c1010edaSGreg Roach    {
237e2a378d3SGreg Roach        $filter   = $this->getBlockSetting($block_id, 'filter', 'all');
238e2a378d3SGreg Roach        $controls = $this->getBlockSetting($block_id, 'controls', '1');
23915d603e7SGreg Roach        $start    = $this->getBlockSetting($block_id, 'start', '0');
2408c2e8227SGreg Roach
24113abd6f3SGreg Roach        $filters = [
242e2a378d3SGreg Roach            'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
243e2a378d3SGreg Roach            'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
244e2a378d3SGreg Roach            'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
245e2a378d3SGreg Roach            'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
246e2a378d3SGreg Roach            'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
247e2a378d3SGreg Roach            'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
248e2a378d3SGreg Roach            'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
249e2a378d3SGreg Roach            'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
250e2a378d3SGreg Roach            'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
251e2a378d3SGreg Roach            'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
252e2a378d3SGreg Roach            'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
253e2a378d3SGreg Roach            'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
254e2a378d3SGreg Roach            'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
255e2a378d3SGreg Roach            'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
256e2a378d3SGreg Roach            'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
257e2a378d3SGreg Roach            'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
258e2a378d3SGreg Roach            'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
259e2a378d3SGreg Roach            'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
26013abd6f3SGreg Roach        ];
2618c2e8227SGreg Roach
262c385536dSGreg Roach        $formats = GedcomTag::getFileFormTypes();
26315d603e7SGreg Roach
264147e99aaSGreg Roach        echo view('modules/random_media/config', [
265c385536dSGreg Roach            'controls' => $controls,
266c385536dSGreg Roach            'filter'   => $filter,
267c385536dSGreg Roach            'filters'  => $filters,
268c385536dSGreg Roach            'formats'  => $formats,
269c385536dSGreg Roach            'start'    => $start,
270c385536dSGreg Roach        ]);
2718c2e8227SGreg Roach    }
2728c2e8227SGreg Roach}
273