xref: /webtrees/resources/views/modules/census-assistant.phtml (revision a92a07c79bdf61915fb2107b92a9eaae1d2f2d04)
1<?php
2
3use Fisharebest\Webtrees\I18N;
4use Fisharebest\Webtrees\Individual;
5use Fisharebest\Webtrees\View;
6
7/**
8 * @var Individual|null $individual
9 */
10
11?>
12
13<div class="census-assistant-link" hidden>
14    <a href="#">
15        <?= I18N::translate('Create a shared note using the census assistant') ?>
16    </a>
17</div>
18
19<div id="census-assistant" hidden>
20    <input type="hidden" name="ca_census" class="census-assistant-class">
21    <div class="mb-3">
22        <div class="input-group">
23            <label class="input-group-text" for="census-assistant-title">
24                <?= I18N::translate('Title') ?>
25            </label>
26
27            <input class="form-control" id="census-assistant-title" name="ca_title" value="">
28        </div>
29    </div>
30
31    <div class="row mb-3">
32        <div class="col-sm-6">
33            <div class="input-group">
34                <label class="input-group-text" for="census-assistant-citation">
35                    <?= I18N::translate('Citation') ?>
36                </label>
37
38                <input class="form-control" id="census-assistant-citation" name="ca_citation">
39            </div>
40        </div>
41
42        <div class="col-sm-6">
43            <div class="input-group">
44                <label class="input-group-text" for="census-assistant-place">
45                    <?= I18N::translate('Place') ?>
46                </label>
47
48                <input class="form-control" id="census-assistant-place" name="ca_place">
49            </div>
50        </div>
51    </div>
52
53    <div class="mb-3">
54        <div class="input-group">
55            <span class="input-group-text">
56                <?= I18N::translate('Individuals') ?>
57            </span>
58
59            <?= view('components/select-individual', ['name' => 'census-assistant-individual', 'individual' => $individual, 'tree' => $individual->tree()]) ?>
60
61            <button type="button" class="btn btn-primary census-assistant-add" title="<?= I18N::translate('add') ?>">
62                <?= view('icons/add') ?>
63            </button>
64        </div>
65    </div>
66
67    <div class="table-responsive">
68        <table class="table table-sm table-borderless small wt-census-assistant-table" id="census-assistant-table">
69            <thead class="wt-census-assistant-header"></thead>
70            <tbody class="wt-census-assistant-body"></tbody>
71        </table>
72    </div>
73
74    <div class="mb-3">
75        <div class="input-group">
76            <label class="input-group-text" for="census-assistant-notes">
77                <?= I18N::translate('Comments') ?>
78            </label>
79
80            <textarea class="form-control" id="census-assistant-notes" name="ca_notes" rows="3"></textarea>
81        </div>
82    </div>
83</div>
84
85<?php View::push('javascript') ?>
86<script>
87  // When a census date/place is selected, activate the census-assistant
88  function censusAssistantSelect () {
89    const select = this;
90    const option = select.options[select.selectedIndex];
91    const form = select.closest('form');
92
93    form.querySelector('.census-assistant-class').value = option.dataset.wtCensus;
94
95    var censusAssistantLink = document.querySelector('.census-assistant-link');
96    var censusAssistant = document.querySelector('#census-assistant');
97    var censusOption = this.options[this.selectedIndex];
98    var census = censusOption.dataset.wtCensus;
99    var censusPlace = censusOption.dataset.wtPlace;
100    var censusYear = censusOption.dataset.wtDate.substr(-4);
101
102    if (censusOption.value !== '') {
103      censusAssistantLink.removeAttribute('hidden');
104    } else {
105      censusAssistantLink.setAttribute('hidden', '');
106    }
107
108    censusAssistant.setAttribute('hidden', '');
109    document.querySelector('.census-assistant-class').value = census;
110    document.querySelector('#census-assistant-title').value = censusYear + ' ' + censusPlace + ' - <?= I18N::translate('Census transcript') ?> - <?= strip_tags($individual->fullName()) ?> - <?= I18N::translate('Household') ?>';
111
112    let formData = new FormData();
113    formData.append('census', census);
114    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
115
116    const url = <?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusHeader', 'tree' => $individual->tree()->name()]), JSON_THROW_ON_ERROR) ?>;
117    webtrees.httpPost(url, formData)
118      .then(response => response.text())
119      .then(function (text) {
120        document.querySelector('#census-assistant-table thead').innerHTML = text;
121        document.querySelector('#census-assistant-table tbody').innerHTML = '';
122      });
123  }
124
125  // When the census assistant is activated, show the input fields
126  function censusAssistantLink () {
127    document.querySelector('.census-selector').setAttribute('hidden', '');
128    this.setAttribute('hidden', '');
129    document.getElementById('census-assistant').removeAttribute('hidden');
130    // Set the current individual as the head of household.
131    censusAssistantAdd();
132
133    return false;
134  }
135
136  // Add the currently selected individual to the census
137  function censusAssistantAdd () {
138    var censusSelector = document.querySelector('.census-selector');
139    var census = censusSelector.options[censusSelector.selectedIndex].dataset.wtCensus;
140    var indi_selector = document.querySelector('#census-assistant-individual');
141    var xref = indi_selector.options[indi_selector.selectedIndex].value;
142    var headInput = document.querySelector('#census-assistant-table td input');
143    var head = headInput === null ? xref : headInput.value;
144
145    let formData = new FormData();
146    formData.append('census', census);
147    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
148    formData.append('head', head);
149    formData.append('xref', xref);
150
151    const url = <?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()]), JSON_THROW_ON_ERROR) ?>;
152
153    webtrees.httpPost(url, formData)
154      .then(response => response.text())
155      .then(function (text) {
156        document.querySelector('#census-assistant-table tbody').insertAdjacentHTML('beforeend', text);
157        webtrees.resetTomSelect(document.querySelector('#census-assistant-individual').tomselect, '', '');
158      });
159
160    return false;
161  }
162
163  document.querySelectorAll('.census-selector').forEach(function (el) {
164    el.addEventListener('change', censusAssistantSelect);
165  });
166
167  document.querySelectorAll('.census-assistant-link').forEach(function (el) {
168    el.addEventListener('click', censusAssistantLink);
169  });
170
171  document.querySelectorAll('.census-assistant-add').forEach(function (el) {
172    el.addEventListener('click', censusAssistantAdd);
173  });
174
175  document.querySelectorAll('#census-assistant-table').forEach(function (el) {
176    el.addEventListener('click', function (el) {
177      if (el.target.matches('.wt-icon-delete')) {
178        el.target.closest('tr').remove();
179      }
180    });
181  });
182</script>
183<?php View::endpush() ?>
184