xref: /webtrees/resources/views/modules/census-assistant.phtml (revision 4874f72da8279544d9c0a459e2920a9986acfaa0)
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    fetch('<?= route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusHeader', 'tree' => $individual->tree()->name()]) ?>&census=' + census)
112      .then(function (response) {
113        return response.text();
114      })
115      .then(function (text) {
116        document.querySelector('#census-assistant-table thead').innerHTML = text;
117        document.querySelector('#census-assistant-table tbody').innerHTML = '';
118      });
119  }
120
121  // When the census assistant is activated, show the input fields
122  function censusAssistantLink() {
123    document.querySelector('#census-selector').setAttribute('hidden', '');
124    this.setAttribute('hidden', '');
125    document.getElementById('census-assistant').removeAttribute('hidden');
126    // Set the current individual as the head of household.
127    censusAssistantHead();
128
129    return false;
130  }
131
132  // Add the currently selected individual to the census
133  function censusAssistantAdd() {
134    var censusSelector = document.querySelector('#census-selector');
135    var census         = censusSelector.options[censusSelector.selectedIndex].dataset.census;
136    var indi_selector  = document.querySelector('#census-assistant-individual');
137    var xref           = indi_selector.options[indi_selector.selectedIndex].value;
138    var headTd         = document.querySelector('#census-assistant-table td');
139    var head           = headTd === null ? xref : headTd.innerHTML;
140
141    fetch('<?= route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()]) ?>&census=' + encodeURIComponent(census) + '&xref=' + encodeURIComponent(xref) + '&head=' + encodeURIComponent(head), {credentials: 'same-origin'})
142      .then(function (response) {
143        return response.text();
144      })
145      .then(function (text) {
146        document.querySelector('#census-assistant-table tbody').innerHTML += text;
147      });
148
149    return false;
150  }
151
152  // Set the currently selected individual as the head of household
153  function censusAssistantHead() {
154    var censusSelector = document.querySelector('#census-selector');
155    var census         = censusSelector.options[censusSelector.selectedIndex].dataset.census;
156    var indi_selector  = document.querySelector('#census-assistant-individual');
157    var xref           = indi_selector.options[indi_selector.selectedIndex].value;
158
159    fetch('<?= route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()]) ?>&census=' + encodeURIComponent(census) + '&xref=' + encodeURIComponent(xref) + '&head=' + encodeURIComponent(xref), {credentials: 'same-origin'})
160      .then(function (response) {
161        return response.text();
162      })
163      .then(function (text) {
164        document.querySelector('#census-assistant-table tbody').innerHTML = text;
165      });
166
167    return false;
168  }
169
170  $('#census-selector').on('change', censusAssistantSelect);
171  $('#census-assistant-link').on('click', censusAssistantLink);
172  $('#census-assistant-add').on('click', censusAssistantAdd);
173  $('#census-assistant-head').on('click', censusAssistantHead);
174
175  $('#census-assistant-table').on('click', 'a.icon-remove', function() {
176    $(this).closest('tr').remove();
177
178    return false;
179  })
180</script>
181<?php View::endpush() ?>
182