xref: /webtrees/resources/views/modules/census-assistant.phtml (revision 313cf4187f9ffd42e2de8c4dc152465587da0d16)
171378461SGreg Roach<?php
271378461SGreg Roach
371378461SGreg Roachuse Fisharebest\Webtrees\I18N;
47c2c99faSGreg Roachuse Fisharebest\Webtrees\Individual;
571378461SGreg Roachuse Fisharebest\Webtrees\View;
671378461SGreg Roach
77c2c99faSGreg Roach/**
87c2c99faSGreg Roach * @var Individual|null $individual
97c2c99faSGreg Roach */
107c2c99faSGreg Roach
1171378461SGreg Roach?>
12dd6b2bfcSGreg Roach
1377b47e31SGreg Roach<div class="census-assistant-link" hidden>
14dd6b2bfcSGreg Roach    <a href="#">
15dd6b2bfcSGreg Roach        <?= I18N::translate('Create a shared note using the census assistant') ?>
16dd6b2bfcSGreg Roach    </a>
17dd6b2bfcSGreg Roach</div>
18dd6b2bfcSGreg Roach
19dd6b2bfcSGreg Roach<div id="census-assistant" hidden>
2077b47e31SGreg Roach    <input type="hidden" name="ca_census" class="census-assistant-class">
219e3c2cf9SGreg Roach    <div class="mb-3">
22dd6b2bfcSGreg Roach        <div class="input-group">
23dd6b2bfcSGreg Roach            <label class="input-group-text" for="census-assistant-title">
24dd6b2bfcSGreg Roach                <?= I18N::translate('Title') ?>
25dd6b2bfcSGreg Roach            </label>
26315eb316SGreg Roach
27dd6b2bfcSGreg Roach            <input class="form-control" id="census-assistant-title" name="ca_title" value="">
28dd6b2bfcSGreg Roach        </div>
29dd6b2bfcSGreg Roach    </div>
30dd6b2bfcSGreg Roach
31b620cb3bSGreg Roach    <div class="row mb-3">
329e3c2cf9SGreg Roach        <div class="col-sm-6">
33dd6b2bfcSGreg Roach            <div class="input-group">
34b6c326d8SGreg Roach                <label class="input-group-text" for="census-assistant-citation">
35dd6b2bfcSGreg Roach                    <?= I18N::translate('Citation') ?>
36dd6b2bfcSGreg Roach                </label>
37315eb316SGreg Roach
38dd6b2bfcSGreg Roach                <input class="form-control" id="census-assistant-citation" name="ca_citation">
39dd6b2bfcSGreg Roach            </div>
40dd6b2bfcSGreg Roach        </div>
41dd6b2bfcSGreg Roach
429e3c2cf9SGreg Roach        <div class="col-sm-6">
43dd6b2bfcSGreg Roach            <div class="input-group">
44dd6b2bfcSGreg Roach                <label class="input-group-text" for="census-assistant-place">
45dd6b2bfcSGreg Roach                    <?= I18N::translate('Place') ?>
46dd6b2bfcSGreg Roach                </label>
47315eb316SGreg Roach
48dd6b2bfcSGreg Roach                <input class="form-control" id="census-assistant-place" name="ca_place">
49dd6b2bfcSGreg Roach            </div>
50dd6b2bfcSGreg Roach        </div>
51dd6b2bfcSGreg Roach    </div>
52dd6b2bfcSGreg Roach
539e3c2cf9SGreg Roach    <div class="mb-3">
54dd6b2bfcSGreg Roach        <div class="input-group">
55dd6b2bfcSGreg Roach            <span class="input-group-text">
56dd6b2bfcSGreg Roach                <?= I18N::translate('Individuals') ?>
57dd6b2bfcSGreg Roach            </span>
58315eb316SGreg Roach
59b6c326d8SGreg Roach            <?= view('components/select-individual', ['name' => 'census-assistant-individual', 'individual' => $individual, 'tree' => $individual->tree()]) ?>
60315eb316SGreg Roach
6177b47e31SGreg Roach            <button type="button" class="btn btn-primary census-assistant-add" title="<?= I18N::translate('add') ?>">
626a83cfe8SGreg Roach                <?= view('icons/add') ?>
63dd6b2bfcSGreg Roach            </button>
64b6c326d8SGreg Roach        </div>
65dd6b2bfcSGreg Roach    </div>
66dd6b2bfcSGreg Roach
67b38a7b85SGreg Roach    <div class="table-responsive">
68b620cb3bSGreg Roach        <table class="table table-sm table-borderless small wt-census-assistant-table" id="census-assistant-table">
69dd6b2bfcSGreg Roach            <thead class="wt-census-assistant-header"></thead>
70dd6b2bfcSGreg Roach            <tbody class="wt-census-assistant-body"></tbody>
71dd6b2bfcSGreg Roach        </table>
72b38a7b85SGreg Roach    </div>
73dd6b2bfcSGreg Roach
749e3c2cf9SGreg Roach    <div class="mb-3">
75dd6b2bfcSGreg Roach        <div class="input-group">
76dd6b2bfcSGreg Roach            <label class="input-group-text" for="census-assistant-notes">
77a97088d0SGreg Roach                <?= I18N::translate('Comments') ?>
78dd6b2bfcSGreg Roach            </label>
79315eb316SGreg Roach
80a97088d0SGreg Roach            <textarea class="form-control" id="census-assistant-notes" name="ca_notes" rows="3"></textarea>
81dd6b2bfcSGreg Roach        </div>
82dd6b2bfcSGreg Roach    </div>
83dd6b2bfcSGreg Roach</div>
84dd6b2bfcSGreg Roach
857a18bd58SGreg Roach<?php View::push('javascript') ?>
86dd6b2bfcSGreg Roach<script>
87dd6b2bfcSGreg Roach  // When a census date/place is selected, activate the census-assistant
88dd6b2bfcSGreg Roach  function censusAssistantSelect () {
8977b47e31SGreg Roach    const select = this;
9077b47e31SGreg Roach    const option = select.options[select.selectedIndex];
9177b47e31SGreg Roach    const form = select.closest('form');
9277b47e31SGreg Roach
93d4786c66SGreg Roach    form.querySelector('.census-assistant-class').value = option.dataset.wtCensus;
9477b47e31SGreg Roach
9577b47e31SGreg Roach    var censusAssistantLink = document.querySelector('.census-assistant-link');
96dd6b2bfcSGreg Roach    var censusAssistant = document.querySelector('#census-assistant');
97dd6b2bfcSGreg Roach    var censusOption = this.options[this.selectedIndex];
98d4786c66SGreg Roach    var census = censusOption.dataset.wtCensus;
99d4786c66SGreg Roach    var censusPlace = censusOption.dataset.wtPlace;
100d4786c66SGreg Roach    var censusYear = censusOption.dataset.wtDate.substr(-4);
101dd6b2bfcSGreg Roach
102dd6b2bfcSGreg Roach    if (censusOption.value !== '') {
103dd6b2bfcSGreg Roach      censusAssistantLink.removeAttribute('hidden');
104dd6b2bfcSGreg Roach    } else {
105dd6b2bfcSGreg Roach      censusAssistantLink.setAttribute('hidden', '');
106dd6b2bfcSGreg Roach    }
107dd6b2bfcSGreg Roach
108dd6b2bfcSGreg Roach    censusAssistant.setAttribute('hidden', '');
10977b47e31SGreg Roach    document.querySelector('.census-assistant-class').value = census;
11039ca88baSGreg Roach    document.querySelector('#census-assistant-title').value = censusYear + ' ' + censusPlace + ' - <?= I18N::translate('Census transcript') ?> - <?= strip_tags($individual->fullName()) ?> - <?= I18N::translate('Household') ?>';
111dd6b2bfcSGreg Roach
1127da59459SGreg Roach    let formData = new FormData();
1137da59459SGreg Roach    formData.append('census', census);
114e240f5e1SGreg Roach    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
1157da59459SGreg Roach
116*313cf418SGreg Roach    const url = <?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusHeader', 'tree' => $individual->tree()->name()]), JSON_THROW_ON_ERROR) ?>;
117*313cf418SGreg Roach    webtrees.httpPost(url, formData)
1187da59459SGreg Roach      .then(response => response.text())
119dd6b2bfcSGreg Roach      .then(function (text) {
120dd6b2bfcSGreg Roach        document.querySelector('#census-assistant-table thead').innerHTML = text;
121dd6b2bfcSGreg Roach        document.querySelector('#census-assistant-table tbody').innerHTML = '';
122dd6b2bfcSGreg Roach      });
123dd6b2bfcSGreg Roach  }
124dd6b2bfcSGreg Roach
125dd6b2bfcSGreg Roach  // When the census assistant is activated, show the input fields
126dd6b2bfcSGreg Roach  function censusAssistantLink () {
12777b47e31SGreg Roach    document.querySelector('.census-selector').setAttribute('hidden', '');
128dd6b2bfcSGreg Roach    this.setAttribute('hidden', '');
129dd6b2bfcSGreg Roach    document.getElementById('census-assistant').removeAttribute('hidden');
130dd6b2bfcSGreg Roach    // Set the current individual as the head of household.
1310b1e93f0SGreg Roach    censusAssistantAdd();
132dd6b2bfcSGreg Roach
133dd6b2bfcSGreg Roach    return false;
134dd6b2bfcSGreg Roach  }
135dd6b2bfcSGreg Roach
136dd6b2bfcSGreg Roach  // Add the currently selected individual to the census
137dd6b2bfcSGreg Roach  function censusAssistantAdd () {
13877b47e31SGreg Roach    var censusSelector = document.querySelector('.census-selector');
139d4786c66SGreg Roach    var census = censusSelector.options[censusSelector.selectedIndex].dataset.wtCensus;
140dd6b2bfcSGreg Roach    var indi_selector = document.querySelector('#census-assistant-individual');
141dd6b2bfcSGreg Roach    var xref = indi_selector.options[indi_selector.selectedIndex].value;
1420483c93fSGreg Roach    var headInput = document.querySelector('#census-assistant-table td input');
1430483c93fSGreg Roach    var head = headInput === null ? xref : headInput.value;
144dd6b2bfcSGreg Roach
1457da59459SGreg Roach    let formData = new FormData();
1467da59459SGreg Roach    formData.append('census', census);
147e240f5e1SGreg Roach    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
1487da59459SGreg Roach    formData.append('head', head);
1497da59459SGreg Roach    formData.append('xref', xref);
1507da59459SGreg Roach
151*313cf418SGreg Roach    const url = <?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()]), JSON_THROW_ON_ERROR) ?>;
152*313cf418SGreg Roach
153*313cf418SGreg Roach    webtrees.httpPost(url, formData)
1547da59459SGreg Roach      .then(response => response.text())
155dd6b2bfcSGreg Roach      .then(function (text) {
156bfc545ddSGreg Roach        document.querySelector('#census-assistant-table tbody').insertAdjacentHTML('beforeend', text);
157c8d78f19SGreg Roach        webtrees.resetTomSelect(document.querySelector('#census-assistant-individual').tomselect, '', '');
158dd6b2bfcSGreg Roach      });
159dd6b2bfcSGreg Roach
160dd6b2bfcSGreg Roach    return false;
161dd6b2bfcSGreg Roach  }
162dd6b2bfcSGreg Roach
16377b47e31SGreg Roach  document.querySelectorAll('.census-selector').forEach(function (el) {
16477b47e31SGreg Roach    el.addEventListener('change', censusAssistantSelect);
16577b47e31SGreg Roach  });
1667a18bd58SGreg Roach
16777b47e31SGreg Roach  document.querySelectorAll('.census-assistant-link').forEach(function (el) {
16877b47e31SGreg Roach    el.addEventListener('click', censusAssistantLink);
16977b47e31SGreg Roach  });
1707a18bd58SGreg Roach
17177b47e31SGreg Roach  document.querySelectorAll('.census-assistant-add').forEach(function (el) {
17277b47e31SGreg Roach    el.addEventListener('click', censusAssistantAdd);
17377b47e31SGreg Roach  });
17477b47e31SGreg Roach
17577b47e31SGreg Roach  document.querySelectorAll('#census-assistant-table').forEach(function (el) {
17677b47e31SGreg Roach    el.addEventListener('click', function (el) {
17777b47e31SGreg Roach      if (el.target.matches('.wt-icon-delete')) {
17877b47e31SGreg Roach        el.target.closest('tr').remove();
17977b47e31SGreg Roach      }
180*313cf418SGreg Roach    });
181*313cf418SGreg Roach  });
182dd6b2bfcSGreg Roach</script>
1837a18bd58SGreg Roach<?php View::endpush() ?>
184