xref: /webtrees/app/Module/SlideShowModule.php (revision d32a32162970e6bfef5abe5397602a75311438d3)
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\Bootstrap4;
20use Fisharebest\Webtrees\Database;
21use Fisharebest\Webtrees\Filter;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\GedcomTag;
24use Fisharebest\Webtrees\Html;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Media;
27
28/**
29 * Class SlideShowModule
30 */
31class SlideShowModule extends AbstractModule implements ModuleBlockInterface {
32	/** {@inheritdoc} */
33	public function getTitle() {
34		return /* I18N: Name of a module */ I18N::translate('Slide show');
35	}
36
37	/** {@inheritdoc} */
38	public function getDescription() {
39		return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.');
40	}
41
42	/**
43	 * Generate the HTML content of this block.
44	 *
45	 * @param int      $block_id
46	 * @param bool     $template
47	 * @param string[] $cfg
48	 *
49	 * @return string
50	 */
51	public function getBlock($block_id, $template = true, $cfg = []): string {
52		global $ctype, $WT_TREE;
53
54		$filter   = $this->getBlockSetting($block_id, 'filter', 'all');
55		$controls = $this->getBlockSetting($block_id, 'controls', '1');
56		$start    = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start');
57
58		// We can apply the filters using SQL
59		// Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
60		$all_media = Database::prepare(
61			"SELECT m_id FROM `##media`" .
62			" JOIN `##media_file` USING (m_file, m_id)" .
63			" WHERE m_file = ?" .
64			" AND multimedia_format  IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" .
65			" AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')"
66		)->execute([
67			$WT_TREE->getTreeId(),
68			$this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
69			$this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
70			$this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
71			$this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
72			$this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
73			$this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
74			$this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
75			$this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
76			$this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
77			$this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
78			$this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
79			$this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
80			$this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
81			$this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
82			$this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
83			$this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
84			$this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
85			$this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
86		])->fetchOneColumn();
87
88		// Keep looking through the media until a suitable one is found.
89		$random_media = null;
90		while ($all_media) {
91			$n     = array_rand($all_media);
92			$media = Media::getInstance($all_media[$n], $WT_TREE);
93			$media_file = $media->firstImageFile();
94			if ($media->canShow() && $media_file !== null && !$media_file->isExternal()) {
95				// Check if it is linked to a suitable individual
96				foreach ($media->linkedIndividuals('OBJE') as $indi) {
97					if (
98						$filter === 'all' ||
99						$filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false ||
100						$filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false
101					) {
102						// Found one :-)
103						$random_media = $media;
104						break 2;
105					}
106				}
107			}
108			unset($all_media[$n]);
109		}
110
111		if ($random_media) {
112			$content = view('blocks/slide-show', [
113				'block_id'            => $block_id,
114				'media'               => $random_media,
115				'media_file'          => $media_file,
116				'show_controls'       => $controls,
117				'start_automatically' => $start,
118			]);
119		} else {
120			$content = I18N::translate('This family tree has no images to display.');
121		}
122
123		if ($template) {
124			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
125				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
126			} elseif ($ctype === 'user' && Auth::check()) {
127				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
128			} else {
129				$config_url = '';
130			}
131
132			return view('blocks/template', [
133				'block'      => str_replace('_', '-', $this->getName()),
134				'id'         => $block_id,
135				'config_url' => $config_url,
136				'title'      => $this->getTitle(),
137				'content'    => $content,
138			]);
139		} else {
140			return $content;
141		}
142	}
143
144	/** {@inheritdoc} */
145	public function loadAjax(): bool {
146		return true;
147	}
148
149	/** {@inheritdoc} */
150	public function isUserBlock(): bool {
151		return true;
152	}
153
154	/** {@inheritdoc} */
155	public function isGedcomBlock(): bool {
156		return true;
157	}
158
159	/**
160	 * An HTML form to edit block settings
161	 *
162	 * @param int $block_id
163	 *
164	 * @return void
165	 */
166	public function configureBlock($block_id) {
167		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
168			$this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all'));
169			$this->setBlockSetting($block_id, 'controls', Filter::postBool('controls'));
170			$this->setBlockSetting($block_id, 'start', Filter::postBool('start'));
171			$this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio'));
172			$this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book'));
173			$this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card'));
174			$this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate'));
175			$this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat'));
176			$this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document'));
177			$this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic'));
178			$this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche'));
179			$this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film'));
180			$this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine'));
181			$this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript'));
182			$this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map'));
183			$this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper'));
184			$this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other'));
185			$this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting'));
186			$this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo'));
187			$this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone'));
188			$this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video'));
189
190			return;
191		}
192
193		$filter   = $this->getBlockSetting($block_id, 'filter', 'all');
194		$controls = $this->getBlockSetting($block_id, 'controls', '1');
195		$start    = $this->getBlockSetting($block_id, 'start', '0');
196
197		$filters = [
198			'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
199			'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
200			'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
201			'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
202			'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
203			'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
204			'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
205			'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
206			'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
207			'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
208			'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
209			'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
210			'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
211			'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
212			'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
213			'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
214			'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
215			'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
216		];
217
218		$formats = GedcomTag::getFileFormTypes();
219
220		echo view('blocks/slide-show-config', [
221			'controls' => $controls,
222			'filter'   => $filter,
223			'filters'  => $filters,
224			'formats'  => $formats,
225			'start'    => $start,
226		]);
227	}
228}
229