xref: /webtrees/resources/views/modules/census-assistant.phtml (revision f7cf8a155e2743f3d124eef3d30a558ab062fa4b)
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 id="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" id="census-assistant-class">
21    <div class="form-group">
22        <div class="input-group">
23            <div class="input-group-prepend">
24                <label class="input-group-text" for="census-assistant-title">
25                    <?= I18N::translate('Title') ?>
26                </label>
27            </div>
28            <input class="form-control" id="census-assistant-title" name="ca_title" value="">
29        </div>
30    </div>
31
32    <div class="row">
33        <div class="form-group col-sm-6">
34            <div class="input-group">
35                <div class="input-group-prepend">
36                    <label class="input-group-text" for="census-assistant-citation">
37                        <?= I18N::translate('Citation') ?>
38                    </label>
39                </div>
40                <input class="form-control" id="census-assistant-citation" name="ca_citation">
41            </div>
42        </div>
43
44        <div class="form-group col-sm-6">
45            <div class="input-group">
46                <div class="input-group-prepend">
47                    <label class="input-group-text" for="census-assistant-place">
48                        <?= I18N::translate('Place') ?>
49                    </label>
50                </div>
51                <input class="form-control" id="census-assistant-place" name="ca_place">
52            </div>
53        </div>
54    </div>
55
56    <div class="form-group">
57        <div class="input-group">
58            <div class="input-group-prepend">
59                <span class="input-group-text">
60                    <?= I18N::translate('Individuals') ?>
61                </span>
62            </div>
63            <?= view('components/select-individual', ['name' => 'census-assistant-individual', 'individual' => $individual, 'tree' => $individual->tree()]) ?>
64            <div class="input-group-append">
65                <button type="button" class="btn btn-primary" id="census-assistant-add" title="<?= I18N::translate('add') ?>">
66                    <?= view('icons/add') ?>
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('Comments') ?>
85                </label>
86            </div>
87            <textarea class="form-control" id="census-assistant-notes" name="ca_notes" rows="3"></textarea>
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    censusAssistantAdd();
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 headInput      = document.querySelector('#census-assistant-table td input');
147    var head           = headInput === null ? xref : headInput.value;
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').insertAdjacentHTML('beforeend', text);
163        document.querySelector('#select2-census-assistant-individual-container .select2-selection__clear').click();
164        $(indi_selector).trigger({
165          type: 'select2:unselect'
166        });
167      });
168
169    return false;
170  }
171
172  $('#census-selector').on('change', censusAssistantSelect);
173  $('#census-assistant-link').on('click', censusAssistantLink);
174  $('#census-assistant-add').on('click', censusAssistantAdd);
175
176  $('#census-assistant-table').on('click', '.wt-icon-delete', function() {
177    $(this).closest('tr').remove();
178
179    return false;
180  })
181</script>
182<?php View::endpush() ?>
183