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