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