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