xref: /webtrees/resources/views/modules/GEDFact_assistant/select-census.phtml (revision d1da5ba430762bd82a5d57bc40552492431d11ee)
1<?php
2
3use Fisharebest\Webtrees\Census\CensusPlaceInterface;
4use Fisharebest\Webtrees\Date;
5use Fisharebest\Webtrees\I18N;
6
7/**
8 * @var array<CensusPlaceInterface> $census_places
9 */
10
11?>
12
13<select class="form-control census-selector" aria-label="<?= I18N::translate('Census date and place') ?>">
14    <option value="" disabled selected hidden><?= I18N::translate('Census date') ?></option>
15
16    <?php foreach ($census_places as $census_place) : ?>
17        <optgroup label="<?= $census_place->censusPlace() ?>">
18          <?php foreach ($census_place->allCensusDates() as $census) : ?>
19              <option data-date="<?= $census->censusDate() ?>" data-place="<?= $census->censusPlace() ?>" data-census="<?= get_class($census) ?>">
20                  <?= (new Date($census->censusDate()))->minimumDate()->format('%Y') ?>
2122                  <?= $census->censusPlace() ?>
23              </option>
24          <?php endforeach ?>
25        </optgroup>
26    <?php endforeach ?>
27</select>
28
29<script>
30  document.querySelectorAll('.census-selector').forEach(function (el) {
31    el.addEventListener('change', function (el) {
32      const select = el.target;
33      const option = select.options[select.selectedIndex];
34      const form = select.closest('form');
35
36      form.querySelector('[id$=CENS\\3a DATE]').value = option.dataset.date;
37      form.querySelector('[id$=CENS\\3a PLAC]').value = option.dataset.place;
38    });
39  });
40</script>
41