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