xref: /webtrees/resources/views/modules/census-assistant.phtml (revision b4c5c807eb501a4b2f3da3cb8eb38981bf4a3a25)
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="form-group 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="form-group 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="form-group 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="form-group 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="form-group">
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    fetch(<?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusHeader', 'tree' => $individual->tree()->name()]), JSON_THROW_ON_ERROR) ?>, {
117        credentials: 'same-origin',
118        body: formData,
119        method: 'POST',
120    })
121    .then(response => response.text())
122    .then(function (text) {
123        document.querySelector('#census-assistant-table thead').innerHTML = text;
124        document.querySelector('#census-assistant-table tbody').innerHTML = '';
125    });
126  }
127
128  // When the census assistant is activated, show the input fields
129  function censusAssistantLink() {
130    document.querySelector('.census-selector').setAttribute('hidden', '');
131    this.setAttribute('hidden', '');
132    document.getElementById('census-assistant').removeAttribute('hidden');
133    // Set the current individual as the head of household.
134    censusAssistantAdd();
135
136    return false;
137  }
138
139  // Add the currently selected individual to the census
140  function censusAssistantAdd() {
141    var censusSelector = document.querySelector('.census-selector');
142    var census         = censusSelector.options[censusSelector.selectedIndex].dataset.wtCensus;
143    var indi_selector  = document.querySelector('#census-assistant-individual');
144    var xref           = indi_selector.options[indi_selector.selectedIndex].value;
145    var headInput      = document.querySelector('#census-assistant-table td input');
146    var head           = headInput === null ? xref : headInput.value;
147
148    let formData = new FormData();
149    formData.append('census', census);
150    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
151    formData.append('head', head);
152    formData.append('xref', xref);
153
154    fetch(<?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()]), JSON_THROW_ON_ERROR) ?>, {
155      credentials: 'same-origin',
156      body: formData,
157      method: 'POST'
158    })
159      .then(response => response.text())
160      .then(function (text) {
161        document.querySelector('#census-assistant-table tbody').insertAdjacentHTML('beforeend', text);
162        webtrees.resetTomSelect(document.querySelector('#census-assistant-individual').tomselect, '', '');
163      });
164
165    return false;
166  }
167
168  document.querySelectorAll('.census-selector').forEach(function (el) {
169    el.addEventListener('change', censusAssistantSelect);
170  });
171
172  document.querySelectorAll('.census-assistant-link').forEach(function (el) {
173    el.addEventListener('click', censusAssistantLink);
174  });
175
176  document.querySelectorAll('.census-assistant-add').forEach(function (el) {
177    el.addEventListener('click', censusAssistantAdd);
178  });
179
180  document.querySelectorAll('#census-assistant-table').forEach(function (el) {
181    el.addEventListener('click', function (el) {
182      if (el.target.matches('.wt-icon-delete')) {
183        el.target.closest('tr').remove();
184      }
185    })
186  })
187</script>
188<?php View::endpush() ?>
189