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