xref: /webtrees/app/Module/SlideShowModule.php (revision b1f1e4ef9803ff38b1a05784d23abe6119b8d697)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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\View;
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 int      $block_id
47	 * @param bool     $template
48	 * @param string[] $cfg
49	 *
50	 * @return string
51	 */
52	public function getBlock($block_id, $template = true, $cfg = []): string {
53		global $ctype, $WT_TREE;
54
55		$filter   = $this->getBlockSetting($block_id, 'filter', 'all');
56		$controls = $this->getBlockSetting($block_id, 'controls', '1');
57		$start    = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start');
58
59		// We can apply the filters using SQL
60		// Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead.
61		$all_media = Database::prepare(
62			"SELECT m_id FROM `##media`" .
63			" JOIN `##media_file` USING (m_file, m_id)" .
64			" WHERE m_file = ?" .
65			" AND multimedia_format  IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" .
66			" AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')"
67		)->execute([
68			$WT_TREE->getTreeId(),
69			$this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null,
70			$this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null,
71			$this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null,
72			$this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
73			$this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null,
74			$this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null,
75			$this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
76			$this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
77			$this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null,
78			$this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
79			$this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
80			$this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null,
81			$this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
82			$this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null,
83			$this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null,
84			$this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null,
85			$this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
86			$this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null,
87		])->fetchOneColumn();
88
89		// Keep looking through the media until a suitable one is found.
90		$random_media = null;
91		while ($all_media) {
92			$n     = array_rand($all_media);
93			$media = Media::getInstance($all_media[$n], $WT_TREE);
94			$media_file = $media->firstImageFile();
95			if ($media->canShow() && $media !== null && !$media_file->isExternal()) {
96				// Check if it is linked to a suitable individual
97				foreach ($media->linkedIndividuals('OBJE') as $indi) {
98					if (
99						$filter === 'all' ||
100						$filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false ||
101						$filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false
102					) {
103						// Found one :-)
104						$random_media = $media;
105						break 2;
106					}
107				}
108			}
109			unset($all_media[$n]);
110		}
111
112		if ($random_media) {
113			$content = View::make('blocks/slide-show', [
114				'block_id'            => $block_id,
115				'media'               => $random_media,
116				'media_file'          => $media_file,
117				'show_controls'       => $controls,
118				'start_automatically' => $start,
119			]);
120		} else {
121			$content = I18N::translate('This family tree has no images to display.');
122		}
123
124		if ($template) {
125			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
126				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
127			} else {
128				$config_url = '';
129			}
130
131			return View::make('blocks/template', [
132				'block'      => str_replace('_', '-', $this->getName()),
133				'id'         => $block_id,
134				'config_url' => $config_url,
135				'title'      => $this->getTitle(),
136				'content'    => $content,
137			]);
138		} else {
139			return $content;
140		}
141	}
142
143	/** {@inheritdoc} */
144	public function loadAjax(): bool {
145		return true;
146	}
147
148	/** {@inheritdoc} */
149	public function isUserBlock(): bool {
150		return true;
151	}
152
153	/** {@inheritdoc} */
154	public function isGedcomBlock(): bool {
155		return true;
156	}
157
158	/**
159	 * An HTML form to edit block settings
160	 *
161	 * @param int $block_id
162	 *
163	 * @return void
164	 */
165	public function configureBlock($block_id) {
166		if (Filter::postBool('save') && Filter::checkCsrf()) {
167			$this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all'));
168			$this->setBlockSetting($block_id, 'controls', Filter::postBool('controls'));
169			$this->setBlockSetting($block_id, 'start', Filter::postBool('start'));
170			$this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio'));
171			$this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book'));
172			$this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card'));
173			$this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate'));
174			$this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat'));
175			$this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document'));
176			$this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic'));
177			$this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche'));
178			$this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film'));
179			$this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine'));
180			$this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript'));
181			$this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map'));
182			$this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper'));
183			$this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other'));
184			$this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting'));
185			$this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo'));
186			$this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone'));
187			$this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video'));
188		}
189
190		$filter   = $this->getBlockSetting($block_id, 'filter', 'all');
191		$controls = $this->getBlockSetting($block_id, 'controls', '1');
192		$start    = $this->getBlockSetting($block_id, 'start', '0');
193
194		$filters = [
195			'audio'       => $this->getBlockSetting($block_id, 'filter_audio', '0'),
196			'book'        => $this->getBlockSetting($block_id, 'filter_book', '1'),
197			'card'        => $this->getBlockSetting($block_id, 'filter_card', '1'),
198			'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'),
199			'coat'        => $this->getBlockSetting($block_id, 'filter_coat', '1'),
200			'document'    => $this->getBlockSetting($block_id, 'filter_document', '1'),
201			'electronic'  => $this->getBlockSetting($block_id, 'filter_electronic', '1'),
202			'fiche'       => $this->getBlockSetting($block_id, 'filter_fiche', '1'),
203			'film'        => $this->getBlockSetting($block_id, 'filter_film', '1'),
204			'magazine'    => $this->getBlockSetting($block_id, 'filter_magazine', '1'),
205			'manuscript'  => $this->getBlockSetting($block_id, 'filter_manuscript', '1'),
206			'map'         => $this->getBlockSetting($block_id, 'filter_map', '1'),
207			'newspaper'   => $this->getBlockSetting($block_id, 'filter_newspaper', '1'),
208			'other'       => $this->getBlockSetting($block_id, 'filter_other', '1'),
209			'painting'    => $this->getBlockSetting($block_id, 'filter_painting', '1'),
210			'photo'       => $this->getBlockSetting($block_id, 'filter_photo', '1'),
211			'tombstone'   => $this->getBlockSetting($block_id, 'filter_tombstone', '1'),
212			'video'       => $this->getBlockSetting($block_id, 'filter_video', '0'),
213		];
214
215		?>
216		<div class="form-control row">
217			<label class="col-sm-3 col-form-label" for="filter">
218				<?= I18N::translate('Show only individuals, events, or all') ?>
219			</label>
220			<div class="col-sm-9">
221				<?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?>
222			</div>
223		</div>
224
225		<fieldset class="form-group">
226			<div class="row">
227				<legend class="col-form-label col-sm-3">
228					<?= GedcomTag::getLabel('TYPE') ?>
229				</legend>
230				<div class="col-sm-9">
231					<?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?>
232						<?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?>
233					<?php endforeach ?>
234				</div>
235			</div>
236		</fieldset>
237
238		<div class="form-group row">
239			<label class="col-sm-3 col-form-label" for="controls">
240				<?= I18N::translate('Show slide show controls') ?>
241			</label>
242			<div class="col-sm-9">
243				<?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?>
244			</div>
245		</div>
246
247		<div class="form-group row">
248			<label class="col-sm-3 col-form-label" for="start">
249				<?= I18N::translate('Start slide show on page load') ?>
250			</label>
251			<div class="col-sm-9">
252				<?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?>
253			</div>
254		</div>
255		<?php
256	}
257}
258