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