xref: /webtrees/resources/views/modules/census-assistant.phtml (revision 10e0649788c8d7d4974d81c048ca2b225df8f22e)
171378461SGreg Roach<?php
271378461SGreg Roach
3*10e06497SGreg Roachdeclare(strict_types=1);
4*10e06497SGreg Roach
571378461SGreg Roachuse Fisharebest\Webtrees\I18N;
67c2c99faSGreg Roachuse Fisharebest\Webtrees\Individual;
771378461SGreg Roachuse Fisharebest\Webtrees\View;
871378461SGreg Roach
97c2c99faSGreg Roach/**
107c2c99faSGreg Roach * @var Individual|null $individual
117c2c99faSGreg Roach */
127c2c99faSGreg Roach
1371378461SGreg Roach?>
14dd6b2bfcSGreg Roach
1577b47e31SGreg Roach<div class="census-assistant-link" hidden>
16dd6b2bfcSGreg Roach    <a href="#">
17dd6b2bfcSGreg Roach        <?= I18N::translate('Create a shared note using the census assistant') ?>
18dd6b2bfcSGreg Roach    </a>
19dd6b2bfcSGreg Roach</div>
20dd6b2bfcSGreg Roach
21dd6b2bfcSGreg Roach<div id="census-assistant" hidden>
2277b47e31SGreg Roach    <input type="hidden" name="ca_census" class="census-assistant-class">
239e3c2cf9SGreg Roach    <div class="mb-3">
24dd6b2bfcSGreg Roach        <div class="input-group">
25dd6b2bfcSGreg Roach            <label class="input-group-text" for="census-assistant-title">
26dd6b2bfcSGreg Roach                <?= I18N::translate('Title') ?>
27dd6b2bfcSGreg Roach            </label>
28315eb316SGreg Roach
29dd6b2bfcSGreg Roach            <input class="form-control" id="census-assistant-title" name="ca_title" value="">
30dd6b2bfcSGreg Roach        </div>
31dd6b2bfcSGreg Roach    </div>
32dd6b2bfcSGreg Roach
33b620cb3bSGreg Roach    <div class="row mb-3">
349e3c2cf9SGreg Roach        <div class="col-sm-6">
35dd6b2bfcSGreg Roach            <div class="input-group">
36b6c326d8SGreg Roach                <label class="input-group-text" for="census-assistant-citation">
37dd6b2bfcSGreg Roach                    <?= I18N::translate('Citation') ?>
38dd6b2bfcSGreg Roach                </label>
39315eb316SGreg Roach
40dd6b2bfcSGreg Roach                <input class="form-control" id="census-assistant-citation" name="ca_citation">
41dd6b2bfcSGreg Roach            </div>
42dd6b2bfcSGreg Roach        </div>
43dd6b2bfcSGreg Roach
449e3c2cf9SGreg Roach        <div class="col-sm-6">
45dd6b2bfcSGreg Roach            <div class="input-group">
46dd6b2bfcSGreg Roach                <label class="input-group-text" for="census-assistant-place">
47dd6b2bfcSGreg Roach                    <?= I18N::translate('Place') ?>
48dd6b2bfcSGreg Roach                </label>
49315eb316SGreg Roach
50dd6b2bfcSGreg Roach                <input class="form-control" id="census-assistant-place" name="ca_place">
51dd6b2bfcSGreg Roach            </div>
52dd6b2bfcSGreg Roach        </div>
53dd6b2bfcSGreg Roach    </div>
54dd6b2bfcSGreg Roach
559e3c2cf9SGreg Roach    <div class="mb-3">
56dd6b2bfcSGreg Roach        <div class="input-group">
57dd6b2bfcSGreg Roach            <span class="input-group-text">
58dd6b2bfcSGreg Roach                <?= I18N::translate('Individuals') ?>
59dd6b2bfcSGreg Roach            </span>
60315eb316SGreg Roach
61b6c326d8SGreg Roach            <?= view('components/select-individual', ['name' => 'census-assistant-individual', 'individual' => $individual, 'tree' => $individual->tree()]) ?>
62315eb316SGreg Roach
6377b47e31SGreg Roach            <button type="button" class="btn btn-primary census-assistant-add" title="<?= I18N::translate('add') ?>">
646a83cfe8SGreg Roach                <?= view('icons/add') ?>
65dd6b2bfcSGreg Roach            </button>
66b6c326d8SGreg Roach        </div>
67dd6b2bfcSGreg Roach    </div>
68dd6b2bfcSGreg Roach
69b38a7b85SGreg Roach    <div class="table-responsive">
70b620cb3bSGreg Roach        <table class="table table-sm table-borderless small wt-census-assistant-table" id="census-assistant-table">
71dd6b2bfcSGreg Roach            <thead class="wt-census-assistant-header"></thead>
72dd6b2bfcSGreg Roach            <tbody class="wt-census-assistant-body"></tbody>
73dd6b2bfcSGreg Roach        </table>
74b38a7b85SGreg Roach    </div>
75dd6b2bfcSGreg Roach
769e3c2cf9SGreg Roach    <div class="mb-3">
77dd6b2bfcSGreg Roach        <div class="input-group">
78dd6b2bfcSGreg Roach            <label class="input-group-text" for="census-assistant-notes">
79a97088d0SGreg Roach                <?= I18N::translate('Comments') ?>
80dd6b2bfcSGreg Roach            </label>
81315eb316SGreg Roach
82a97088d0SGreg Roach            <textarea class="form-control" id="census-assistant-notes" name="ca_notes" rows="3"></textarea>
83dd6b2bfcSGreg Roach        </div>
84dd6b2bfcSGreg Roach    </div>
85dd6b2bfcSGreg Roach</div>
86dd6b2bfcSGreg Roach
877a18bd58SGreg Roach<?php View::push('javascript') ?>
88dd6b2bfcSGreg Roach<script>
89dd6b2bfcSGreg Roach  // When a census date/place is selected, activate the census-assistant
90dd6b2bfcSGreg Roach  function censusAssistantSelect () {
9177b47e31SGreg Roach    const select = this;
9277b47e31SGreg Roach    const option = select.options[select.selectedIndex];
9377b47e31SGreg Roach    const form = select.closest('form');
9477b47e31SGreg Roach
95d4786c66SGreg Roach    form.querySelector('.census-assistant-class').value = option.dataset.wtCensus;
9677b47e31SGreg Roach
9777b47e31SGreg Roach    var censusAssistantLink = document.querySelector('.census-assistant-link');
98dd6b2bfcSGreg Roach    var censusAssistant = document.querySelector('#census-assistant');
99dd6b2bfcSGreg Roach    var censusOption = this.options[this.selectedIndex];
100d4786c66SGreg Roach    var census = censusOption.dataset.wtCensus;
101d4786c66SGreg Roach    var censusPlace = censusOption.dataset.wtPlace;
102d4786c66SGreg Roach    var censusYear = censusOption.dataset.wtDate.substr(-4);
103dd6b2bfcSGreg Roach
104dd6b2bfcSGreg Roach    if (censusOption.value !== '') {
105dd6b2bfcSGreg Roach      censusAssistantLink.removeAttribute('hidden');
106dd6b2bfcSGreg Roach    } else {
107dd6b2bfcSGreg Roach      censusAssistantLink.setAttribute('hidden', '');
108dd6b2bfcSGreg Roach    }
109dd6b2bfcSGreg Roach
110dd6b2bfcSGreg Roach    censusAssistant.setAttribute('hidden', '');
11177b47e31SGreg Roach    document.querySelector('.census-assistant-class').value = census;
11239ca88baSGreg Roach    document.querySelector('#census-assistant-title').value = censusYear + ' ' + censusPlace + ' - <?= I18N::translate('Census transcript') ?> - <?= strip_tags($individual->fullName()) ?> - <?= I18N::translate('Household') ?>';
113dd6b2bfcSGreg Roach
1147da59459SGreg Roach    let formData = new FormData();
1157da59459SGreg Roach    formData.append('census', census);
116e240f5e1SGreg Roach    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
1177da59459SGreg Roach
118313cf418SGreg Roach    const url = <?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusHeader', 'tree' => $individual->tree()->name()]), JSON_THROW_ON_ERROR) ?>;
119313cf418SGreg Roach    webtrees.httpPost(url, formData)
1207da59459SGreg Roach      .then(response => response.text())
121dd6b2bfcSGreg Roach      .then(function (text) {
122dd6b2bfcSGreg Roach        document.querySelector('#census-assistant-table thead').innerHTML = text;
123dd6b2bfcSGreg Roach        document.querySelector('#census-assistant-table tbody').innerHTML = '';
124dd6b2bfcSGreg Roach      });
125dd6b2bfcSGreg Roach  }
126dd6b2bfcSGreg Roach
127dd6b2bfcSGreg Roach  // When the census assistant is activated, show the input fields
128dd6b2bfcSGreg Roach  function censusAssistantLink () {
12977b47e31SGreg Roach    document.querySelector('.census-selector').setAttribute('hidden', '');
130dd6b2bfcSGreg Roach    this.setAttribute('hidden', '');
131dd6b2bfcSGreg Roach    document.getElementById('census-assistant').removeAttribute('hidden');
132dd6b2bfcSGreg Roach    // Set the current individual as the head of household.
1330b1e93f0SGreg Roach    censusAssistantAdd();
134dd6b2bfcSGreg Roach
135dd6b2bfcSGreg Roach    return false;
136dd6b2bfcSGreg Roach  }
137dd6b2bfcSGreg Roach
138dd6b2bfcSGreg Roach  // Add the currently selected individual to the census
139dd6b2bfcSGreg Roach  function censusAssistantAdd () {
14077b47e31SGreg Roach    var censusSelector = document.querySelector('.census-selector');
141d4786c66SGreg Roach    var census = censusSelector.options[censusSelector.selectedIndex].dataset.wtCensus;
142dd6b2bfcSGreg Roach    var indi_selector = document.querySelector('#census-assistant-individual');
143dd6b2bfcSGreg Roach    var xref = indi_selector.options[indi_selector.selectedIndex].value;
1440483c93fSGreg Roach    var headInput = document.querySelector('#census-assistant-table td input');
1450483c93fSGreg Roach    var head = headInput === null ? xref : headInput.value;
146dd6b2bfcSGreg Roach
1477da59459SGreg Roach    let formData = new FormData();
1487da59459SGreg Roach    formData.append('census', census);
149e240f5e1SGreg Roach    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
1507da59459SGreg Roach    formData.append('head', head);
1517da59459SGreg Roach    formData.append('xref', xref);
1527da59459SGreg Roach
153313cf418SGreg Roach    const url = <?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()]), JSON_THROW_ON_ERROR) ?>;
154313cf418SGreg Roach
155313cf418SGreg Roach    webtrees.httpPost(url, formData)
1567da59459SGreg Roach      .then(response => response.text())
157dd6b2bfcSGreg Roach      .then(function (text) {
158bfc545ddSGreg Roach        document.querySelector('#census-assistant-table tbody').insertAdjacentHTML('beforeend', text);
159c8d78f19SGreg Roach        webtrees.resetTomSelect(document.querySelector('#census-assistant-individual').tomselect, '', '');
160dd6b2bfcSGreg Roach      });
161dd6b2bfcSGreg Roach
162dd6b2bfcSGreg Roach    return false;
163dd6b2bfcSGreg Roach  }
164dd6b2bfcSGreg Roach
16577b47e31SGreg Roach  document.querySelectorAll('.census-selector').forEach(function (el) {
16677b47e31SGreg Roach    el.addEventListener('change', censusAssistantSelect);
16777b47e31SGreg Roach  });
1687a18bd58SGreg Roach
16977b47e31SGreg Roach  document.querySelectorAll('.census-assistant-link').forEach(function (el) {
17077b47e31SGreg Roach    el.addEventListener('click', censusAssistantLink);
17177b47e31SGreg Roach  });
1727a18bd58SGreg Roach
17377b47e31SGreg Roach  document.querySelectorAll('.census-assistant-add').forEach(function (el) {
17477b47e31SGreg Roach    el.addEventListener('click', censusAssistantAdd);
17577b47e31SGreg Roach  });
17677b47e31SGreg Roach
17777b47e31SGreg Roach  document.querySelectorAll('#census-assistant-table').forEach(function (el) {
17877b47e31SGreg Roach    el.addEventListener('click', function (el) {
17977b47e31SGreg Roach      if (el.target.matches('.wt-icon-delete')) {
18077b47e31SGreg Roach        el.target.closest('tr').remove();
18177b47e31SGreg Roach      }
182313cf418SGreg Roach    });
183313cf418SGreg Roach  });
184dd6b2bfcSGreg Roach</script>
1857a18bd58SGreg Roach<?php View::endpush() ?>
186