xref: /webtrees/resources/views/modules/census-assistant.phtml (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
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" title="<?= 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" title="<?= I18N::translate('Head of household') ?>">
66                    <?= view('icons/individual') ?>
67                </button>
68            </div>
69        </div>
70    </div>
71
72    <div class="table-responsive">
73        <table class="table table-bordered table-small small wt-census-assistant-table"
74        id="census-assistant-table">
75            <thead class="wt-census-assistant-header"></thead>
76            <tbody class="wt-census-assistant-body"></tbody>
77        </table>
78    </div>
79
80    <div class="form-group">
81        <div class="input-group">
82            <div class="input-group-prepend">
83                <label class="input-group-text" for="census-assistant-notes">
84                    <?= I18N::translate('Notes') ?>
85                </label>
86            </div>
87            <input class="form-control" id="census-assistant-notes" name="ca_notes">
88        </div>
89    </div>
90</div>
91
92<?php View::push('javascript') ?>
93<script>
94  // When a census date/place is selected, activate the census-assistant
95  function censusAssistantSelect() {
96    var censusAssistantLink = document.querySelector('#census-assistant-link');
97    var censusAssistant     = document.querySelector('#census-assistant');
98    var censusOption        = this.options[this.selectedIndex];
99    var census              = censusOption.dataset.census;
100    var censusPlace         = censusOption.dataset.place;
101    var censusYear          = censusOption.value.substr(-4);
102
103    if (censusOption.value !== '') {
104      censusAssistantLink.removeAttribute('hidden');
105    } else {
106      censusAssistantLink.setAttribute('hidden', '');
107    }
108
109    censusAssistant.setAttribute('hidden', '');
110    document.querySelector('#census-assistant-class').value = census;
111    document.querySelector('#census-assistant-title').value = censusYear + ' ' + censusPlace + ' - <?= I18N::translate('Census transcript') ?> - <?= strip_tags($individual->fullName()) ?> - <?= I18N::translate('Household') ?>';
112
113    let formData = new FormData();
114    formData.append('census', census);
115    formData.append('csrf', document.querySelector('meta[name=csrf]').content);
116
117    fetch(<?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusHeader', 'tree' => $individual->tree()->name()])) ?>, {
118        credentials: 'same-origin',
119        body: formData,
120        method: 'POST',
121    })
122    .then(response => response.text())
123    .then(function (text) {
124        document.querySelector('#census-assistant-table thead').innerHTML = text;
125        document.querySelector('#census-assistant-table tbody').innerHTML = '';
126    });
127  }
128
129  // When the census assistant is activated, show the input fields
130  function censusAssistantLink() {
131    document.querySelector('#census-selector').setAttribute('hidden', '');
132    this.setAttribute('hidden', '');
133    document.getElementById('census-assistant').removeAttribute('hidden');
134    // Set the current individual as the head of household.
135    censusAssistantHead();
136
137    return false;
138  }
139
140  // Add the currently selected individual to the census
141  function censusAssistantAdd() {
142    var censusSelector = document.querySelector('#census-selector');
143    var census         = censusSelector.options[censusSelector.selectedIndex].dataset.census;
144    var indi_selector  = document.querySelector('#census-assistant-individual');
145    var xref           = indi_selector.options[indi_selector.selectedIndex].value;
146    var headTd         = document.querySelector('#census-assistant-table td');
147    var head           = headTd === null ? xref : headTd.innerHTML;
148
149    let formData = new FormData();
150    formData.append('census', census);
151    formData.append('csrf', document.querySelector('meta[name=csrf]').content);
152    formData.append('head', head);
153    formData.append('xref', xref);
154
155    fetch(<?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()])) ?>, {
156        credentials: 'same-origin',
157        body: formData,
158        method: 'POST'
159    })
160    .then(response => response.text())
161    .then(function (text) {
162        document.querySelector('#census-assistant-table tbody').innerHTML += text;
163    });
164
165    return false;
166  }
167
168  // Set the currently selected individual as the head of household
169  function censusAssistantHead() {
170    var censusSelector = document.querySelector('#census-selector');
171    var census         = censusSelector.options[censusSelector.selectedIndex].dataset.census;
172    var indi_selector  = document.querySelector('#census-assistant-individual');
173    var xref           = indi_selector.options[indi_selector.selectedIndex].value;
174
175    let formData = new FormData();
176    formData.append('census', census);
177    formData.append('csrf', document.querySelector('meta[name=csrf]').content);
178    formData.append('head', xref);
179    formData.append('xref', xref);
180
181    fetch(<?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()])) ?> , {
182        credentials: 'same-origin',
183        body: formData,
184        method: 'POST',
185    })
186    .then(response => response.text())
187    .then(function (text) {
188        document.querySelector('#census-assistant-table tbody').innerHTML = text;
189    });
190
191    return false;
192  }
193
194  $('#census-selector').on('change', censusAssistantSelect);
195  $('#census-assistant-link').on('click', censusAssistantLink);
196  $('#census-assistant-add').on('click', censusAssistantAdd);
197  $('#census-assistant-head').on('click', censusAssistantHead);
198
199  $('#census-assistant-table').on('click', '.wt-icon-delete', function() {
200    $(this).closest('tr').remove();
201
202    return false;
203  })
204</script>
205<?php View::endpush() ?>
206