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