xref: /webtrees/resources/views/modules/census-assistant.phtml (revision 7c2c99fad7cacd17c98be88238ff735d82c6351b)
171378461SGreg Roach<?php
271378461SGreg Roach
371378461SGreg Roachuse Fisharebest\Webtrees\I18N;
4*7c2c99faSGreg Roachuse Fisharebest\Webtrees\Individual;
571378461SGreg Roachuse Fisharebest\Webtrees\View;
671378461SGreg Roach
7*7c2c99faSGreg Roach/**
8*7c2c99faSGreg Roach * @var Individual|null $individual
9*7c2c99faSGreg Roach */
10*7c2c99faSGreg Roach
1171378461SGreg Roach?>
12dd6b2bfcSGreg Roach
13dd6b2bfcSGreg Roach<div id="census-assistant-link" hidden>
14dd6b2bfcSGreg Roach    <a href="#">
15dd6b2bfcSGreg Roach        <?= I18N::translate('Create a shared note using the census assistant') ?>
16dd6b2bfcSGreg Roach    </a>
17dd6b2bfcSGreg Roach</div>
18dd6b2bfcSGreg Roach
19dd6b2bfcSGreg Roach<div id="census-assistant" hidden>
20dd6b2bfcSGreg Roach    <input type="hidden" name="ca_census" id="census-assistant-class">
21dd6b2bfcSGreg Roach    <div class="form-group">
22dd6b2bfcSGreg Roach        <div class="input-group">
23dd6b2bfcSGreg Roach            <div class="input-group-prepend">
24dd6b2bfcSGreg Roach                <label class="input-group-text" for="census-assistant-title">
25dd6b2bfcSGreg Roach                    <?= I18N::translate('Title') ?>
26dd6b2bfcSGreg Roach                </label>
27dd6b2bfcSGreg Roach            </div>
28dd6b2bfcSGreg Roach            <input class="form-control" id="census-assistant-title" name="ca_title" value="">
29dd6b2bfcSGreg Roach        </div>
30dd6b2bfcSGreg Roach    </div>
31dd6b2bfcSGreg Roach
32dd6b2bfcSGreg Roach    <div class="row">
33dd6b2bfcSGreg Roach        <div class="form-group col-sm-6">
34dd6b2bfcSGreg Roach            <div class="input-group">
35dd6b2bfcSGreg Roach                <div class="input-group-prepend">
36b6c326d8SGreg Roach                    <label class="input-group-text" for="census-assistant-citation">
37dd6b2bfcSGreg Roach                        <?= I18N::translate('Citation') ?>
38dd6b2bfcSGreg Roach                    </label>
39dd6b2bfcSGreg Roach                </div>
40dd6b2bfcSGreg Roach                <input class="form-control" id="census-assistant-citation" name="ca_citation">
41dd6b2bfcSGreg Roach            </div>
42dd6b2bfcSGreg Roach        </div>
43dd6b2bfcSGreg Roach
44dd6b2bfcSGreg Roach        <div class="form-group col-sm-6">
45dd6b2bfcSGreg Roach            <div class="input-group">
46dd6b2bfcSGreg Roach                <div class="input-group-prepend">
47dd6b2bfcSGreg Roach                    <label class="input-group-text" for="census-assistant-place">
48dd6b2bfcSGreg Roach                        <?= I18N::translate('Place') ?>
49dd6b2bfcSGreg Roach                    </label>
50dd6b2bfcSGreg Roach                </div>
51dd6b2bfcSGreg Roach                <input class="form-control" id="census-assistant-place" name="ca_place">
52dd6b2bfcSGreg Roach            </div>
53dd6b2bfcSGreg Roach        </div>
54dd6b2bfcSGreg Roach    </div>
55dd6b2bfcSGreg Roach
56dd6b2bfcSGreg Roach    <div class="form-group">
57dd6b2bfcSGreg Roach        <div class="input-group">
58dd6b2bfcSGreg Roach            <div class="input-group-prepend">
59dd6b2bfcSGreg Roach                <span class="input-group-text">
60dd6b2bfcSGreg Roach                    <?= I18N::translate('Individuals') ?>
61dd6b2bfcSGreg Roach                </span>
62dd6b2bfcSGreg Roach            </div>
63b6c326d8SGreg Roach            <?= view('components/select-individual', ['name' => 'census-assistant-individual', 'individual' => $individual, 'tree' => $individual->tree()]) ?>
64b6c326d8SGreg Roach            <div class="input-group-append">
6508362db4SGreg Roach                <button type="button" class="btn btn-primary" id="census-assistant-add" title="<?= I18N::translate('add') ?>">
666a83cfe8SGreg Roach                    <?= view('icons/add') ?>
67dd6b2bfcSGreg Roach                </button>
68b6c326d8SGreg Roach            </div>
69dd6b2bfcSGreg Roach        </div>
70dd6b2bfcSGreg Roach    </div>
71dd6b2bfcSGreg Roach
72b38a7b85SGreg Roach    <div class="table-responsive">
73b38a7b85SGreg Roach        <table class="table table-bordered table-small small wt-census-assistant-table"
74dd6b2bfcSGreg Roach        id="census-assistant-table">
75dd6b2bfcSGreg Roach            <thead class="wt-census-assistant-header"></thead>
76dd6b2bfcSGreg Roach            <tbody class="wt-census-assistant-body"></tbody>
77dd6b2bfcSGreg Roach        </table>
78b38a7b85SGreg Roach    </div>
79dd6b2bfcSGreg Roach
80dd6b2bfcSGreg Roach    <div class="form-group">
81dd6b2bfcSGreg Roach        <div class="input-group">
82dd6b2bfcSGreg Roach            <div class="input-group-prepend">
83dd6b2bfcSGreg Roach                <label class="input-group-text" for="census-assistant-notes">
84a97088d0SGreg Roach                    <?= I18N::translate('Comments') ?>
85dd6b2bfcSGreg Roach                </label>
86dd6b2bfcSGreg Roach            </div>
87a97088d0SGreg Roach            <textarea class="form-control" id="census-assistant-notes" name="ca_notes" rows="3"></textarea>
88dd6b2bfcSGreg Roach        </div>
89dd6b2bfcSGreg Roach    </div>
90dd6b2bfcSGreg Roach</div>
91dd6b2bfcSGreg Roach
927a18bd58SGreg Roach<?php View::push('javascript') ?>
93dd6b2bfcSGreg Roach<script>
94dd6b2bfcSGreg Roach  // When a census date/place is selected, activate the census-assistant
95dd6b2bfcSGreg Roach  function censusAssistantSelect() {
96dd6b2bfcSGreg Roach    var censusAssistantLink = document.querySelector('#census-assistant-link');
97dd6b2bfcSGreg Roach    var censusAssistant     = document.querySelector('#census-assistant');
98dd6b2bfcSGreg Roach    var censusOption        = this.options[this.selectedIndex];
99dd6b2bfcSGreg Roach    var census              = censusOption.dataset.census;
100dd6b2bfcSGreg Roach    var censusPlace         = censusOption.dataset.place;
101dd6b2bfcSGreg Roach    var censusYear          = censusOption.value.substr(-4);
102dd6b2bfcSGreg Roach
103dd6b2bfcSGreg Roach    if (censusOption.value !== '') {
104dd6b2bfcSGreg Roach      censusAssistantLink.removeAttribute('hidden');
105dd6b2bfcSGreg Roach    } else {
106dd6b2bfcSGreg Roach      censusAssistantLink.setAttribute('hidden', '');
107dd6b2bfcSGreg Roach    }
108dd6b2bfcSGreg Roach
109dd6b2bfcSGreg Roach    censusAssistant.setAttribute('hidden', '');
110dd6b2bfcSGreg Roach    document.querySelector('#census-assistant-class').value = census;
11139ca88baSGreg Roach    document.querySelector('#census-assistant-title').value = censusYear + ' ' + censusPlace + ' - <?= I18N::translate('Census transcript') ?> - <?= strip_tags($individual->fullName()) ?> - <?= I18N::translate('Household') ?>';
112dd6b2bfcSGreg Roach
1137da59459SGreg Roach    let formData = new FormData();
1147da59459SGreg Roach    formData.append('census', census);
115e240f5e1SGreg Roach    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
1167da59459SGreg Roach
1177da59459SGreg Roach    fetch(<?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusHeader', 'tree' => $individual->tree()->name()])) ?>, {
1187da59459SGreg Roach        credentials: 'same-origin',
1197da59459SGreg Roach        body: formData,
1207da59459SGreg Roach        method: 'POST',
121dd6b2bfcSGreg Roach    })
1227da59459SGreg Roach    .then(response => response.text())
123dd6b2bfcSGreg Roach    .then(function (text) {
124dd6b2bfcSGreg Roach        document.querySelector('#census-assistant-table thead').innerHTML = text;
125dd6b2bfcSGreg Roach        document.querySelector('#census-assistant-table tbody').innerHTML = '';
126dd6b2bfcSGreg Roach    });
127dd6b2bfcSGreg Roach  }
128dd6b2bfcSGreg Roach
129dd6b2bfcSGreg Roach  // When the census assistant is activated, show the input fields
130dd6b2bfcSGreg Roach  function censusAssistantLink() {
131dd6b2bfcSGreg Roach    document.querySelector('#census-selector').setAttribute('hidden', '');
132dd6b2bfcSGreg Roach    this.setAttribute('hidden', '');
133dd6b2bfcSGreg Roach    document.getElementById('census-assistant').removeAttribute('hidden');
134dd6b2bfcSGreg Roach    // Set the current individual as the head of household.
135dd6b2bfcSGreg Roach    censusAssistantHead();
136dd6b2bfcSGreg Roach
137dd6b2bfcSGreg Roach    return false;
138dd6b2bfcSGreg Roach  }
139dd6b2bfcSGreg Roach
140dd6b2bfcSGreg Roach  // Add the currently selected individual to the census
141dd6b2bfcSGreg Roach  function censusAssistantAdd() {
142dd6b2bfcSGreg Roach    var censusSelector = document.querySelector('#census-selector');
143dd6b2bfcSGreg Roach    var census         = censusSelector.options[censusSelector.selectedIndex].dataset.census;
144dd6b2bfcSGreg Roach    var indi_selector  = document.querySelector('#census-assistant-individual');
145dd6b2bfcSGreg Roach    var xref           = indi_selector.options[indi_selector.selectedIndex].value;
1460483c93fSGreg Roach    var headInput      = document.querySelector('#census-assistant-table td input');
1470483c93fSGreg Roach    var head           = headInput === null ? xref : headInput.value;
148dd6b2bfcSGreg Roach
1497da59459SGreg Roach    let formData = new FormData();
1507da59459SGreg Roach    formData.append('census', census);
151e240f5e1SGreg Roach    formData.append('_csrf', document.querySelector('meta[name=csrf]').content);
1527da59459SGreg Roach    formData.append('head', head);
1537da59459SGreg Roach    formData.append('xref', xref);
1547da59459SGreg Roach
1557da59459SGreg Roach    fetch(<?= json_encode(route('module', ['module' => 'GEDFact_assistant', 'action' => 'CensusIndividual', 'tree' => $individual->tree()->name()])) ?>, {
1567da59459SGreg Roach        credentials: 'same-origin',
1577da59459SGreg Roach        body: formData,
1587da59459SGreg Roach        method: 'POST'
159dd6b2bfcSGreg Roach    })
1607da59459SGreg Roach    .then(response => response.text())
161dd6b2bfcSGreg Roach    .then(function (text) {
162bfc545ddSGreg Roach        document.querySelector('#census-assistant-table tbody').insertAdjacentHTML('beforeend', text);
163dd6b2bfcSGreg Roach    });
164dd6b2bfcSGreg Roach
165dd6b2bfcSGreg Roach    return false;
166dd6b2bfcSGreg Roach  }
167dd6b2bfcSGreg Roach
1687a18bd58SGreg Roach  $('#census-selector').on('change', censusAssistantSelect);
1697a18bd58SGreg Roach  $('#census-assistant-link').on('click', censusAssistantLink);
1707a18bd58SGreg Roach  $('#census-assistant-add').on('click', censusAssistantAdd);
1717a18bd58SGreg Roach
17208362db4SGreg Roach  $('#census-assistant-table').on('click', '.wt-icon-delete', function() {
1737a18bd58SGreg Roach    $(this).closest('tr').remove();
1747a18bd58SGreg Roach
1757a18bd58SGreg Roach    return false;
1767a18bd58SGreg Roach  })
177dd6b2bfcSGreg Roach</script>
1787a18bd58SGreg Roach<?php View::endpush() ?>
179