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