18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 22ad51e0bbSGreg Roachuse Fisharebest\Webtrees\Census\CensusInterface; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2499f222b3SGreg Roachuse Fisharebest\Webtrees\Individual; 2509482a55SGreg Roachuse Fisharebest\Webtrees\Registry; 26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 298c2e8227SGreg Roach 303cc4e3a0SGreg Roachuse function array_keys; 313cc4e3a0SGreg Roachuse function count; 323cc4e3a0SGreg Roachuse function e; 33d8869271SGreg Roachuse function response; 343cc4e3a0SGreg Roachuse function str_repeat; 353cc4e3a0SGreg Roachuse function str_replace; 363cc4e3a0SGreg Roachuse function view; 374ea62551SGreg Roach 388c2e8227SGreg Roach/** 398c2e8227SGreg Roach * Class CensusAssistantModule 408c2e8227SGreg Roach */ 415206405dSRico Sonntagclass CensusAssistantModule extends AbstractModule 42c1010edaSGreg Roach{ 43961ec755SGreg Roach /** 440cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 45961ec755SGreg Roach * 46961ec755SGreg Roach * @return string 47961ec755SGreg Roach */ 4849a243cbSGreg Roach public function title(): string 49c1010edaSGreg Roach { 50bbb76c12SGreg Roach /* I18N: Name of a module */ 51bbb76c12SGreg Roach return I18N::translate('Census assistant'); 528c2e8227SGreg Roach } 538c2e8227SGreg Roach 54961ec755SGreg Roach /** 55961ec755SGreg Roach * A sentence describing what this module does. 56961ec755SGreg Roach * 57961ec755SGreg Roach * @return string 58961ec755SGreg Roach */ 5949a243cbSGreg Roach public function description(): string 60c1010edaSGreg Roach { 61bbb76c12SGreg Roach /* I18N: Description of the “Census assistant” module */ 62bbb76c12SGreg Roach return I18N::translate('An alternative way to enter census transcripts and link them to individuals.'); 638c2e8227SGreg Roach } 648c2e8227SGreg Roach 6576692c8bSGreg Roach /** 666ccdf4f0SGreg Roach * @param ServerRequestInterface $request 679001c0b3SGreg Roach * 686ccdf4f0SGreg Roach * @return ResponseInterface 699001c0b3SGreg Roach */ 707da59459SGreg Roach public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface 71c1010edaSGreg Roach { 72748dbe15SGreg Roach $census_class = Validator::parsedBody($request)->string('census'); 73b46c87bdSGreg Roach 74748dbe15SGreg Roach $html = $this->censusTableHeader(new $census_class()); 759001c0b3SGreg Roach 766ccdf4f0SGreg Roach return response($html); 779001c0b3SGreg Roach } 789001c0b3SGreg Roach 799001c0b3SGreg Roach /** 806ccdf4f0SGreg Roach * @param ServerRequestInterface $request 819001c0b3SGreg Roach * 826ccdf4f0SGreg Roach * @return ResponseInterface 839001c0b3SGreg Roach */ 847da59459SGreg Roach public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface 85c1010edaSGreg Roach { 86b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 87dfe6841dSGreg Roach $indi_xref = Validator::parsedBody($request)->isXref()->string('xref', ''); 88dfe6841dSGreg Roach $head_xref = Validator::parsedBody($request)->isXref()->string('head', ''); 89748dbe15SGreg Roach $individual = Registry::individualFactory()->make($indi_xref, $tree); 90748dbe15SGreg Roach $head = Registry::individualFactory()->make($head_xref, $tree); 91748dbe15SGreg Roach $census_class = Validator::parsedBody($request)->string('census'); 92d8869271SGreg Roach $census = new $census_class(); 93d45cb9d3SGreg Roach 941f244d77SGreg Roach // No head of household? Create a fake one. 953529c469SGreg Roach $head ??= Registry::individualFactory()->new('X', '0 @X@ INDI', null, $tree); 96dd1e545cSGreg Roach 97d5b0584dSGreg Roach // Generate columns (e.g. relationship name) using the correct language. 98d5b0584dSGreg Roach I18N::init($census->censusLanguage()); 99d5b0584dSGreg Roach 100d45cb9d3SGreg Roach if ($individual instanceof Individual && $head instanceof Individual) { 101d8869271SGreg Roach $html = $this->censusTableRow($census, $individual, $head); 102d8869271SGreg Roach } else { 103d8869271SGreg Roach $html = $this->censusTableEmptyRow($census); 104d45cb9d3SGreg Roach } 105e364afe4SGreg Roach 106d8869271SGreg Roach return response($html); 1079001c0b3SGreg Roach } 1089001c0b3SGreg Roach 1099001c0b3SGreg Roach /** 11015d603e7SGreg Roach * @param Individual $individual 1119001c0b3SGreg Roach * 1129001c0b3SGreg Roach * @return string 1138c2e8227SGreg Roach */ 1148f53f488SRico Sonntag public function createCensusAssistant(Individual $individual): string 115c1010edaSGreg Roach { 116225e381fSGreg Roach return view('modules/census-assistant', [ 11734cd602eSGreg Roach 'individual' => $individual, 11834cd602eSGreg Roach ]); 11915d603e7SGreg Roach } 12015d603e7SGreg Roach 12115d603e7SGreg Roach /** 1226ccdf4f0SGreg Roach * @param ServerRequestInterface $request 12315d603e7SGreg Roach * @param Individual $individual 12460bc3e3fSGreg Roach * @param string $fact_id 12515d603e7SGreg Roach * @param string $newged 12660bc3e3fSGreg Roach * @param bool $keep_chan 12715d603e7SGreg Roach * 12815d603e7SGreg Roach * @return string 12915d603e7SGreg Roach */ 1306ccdf4f0SGreg Roach public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string 131c1010edaSGreg Roach { 132748dbe15SGreg Roach $ca_title = Validator::parsedBody($request)->string('ca_title'); 133748dbe15SGreg Roach $ca_place = Validator::parsedBody($request)->string('ca_place'); 134748dbe15SGreg Roach $ca_citation = Validator::parsedBody($request)->string('ca_citation'); 135748dbe15SGreg Roach $ca_individuals = Validator::parsedBody($request)->array('ca_individuals'); 136748dbe15SGreg Roach $ca_notes = Validator::parsedBody($request)->string('ca_notes'); 137748dbe15SGreg Roach $ca_census = Validator::parsedBody($request)->string('ca_census'); 13815d603e7SGreg Roach 139a91af26aSGreg Roach if ($ca_census !== '' && $ca_individuals !== []) { 14059f2f229SGreg Roach $census = new $ca_census(); 14115d603e7SGreg Roach 14215d603e7SGreg Roach $note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); 143afb591d7SGreg Roach $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); 144f4afa648SGreg Roach $note = $individual->tree()->createRecord($note_gedcom); 14515d603e7SGreg Roach 146c0935879SGreg Roach $newged .= "\n2 NOTE @" . $note->xref() . '@'; 14715d603e7SGreg Roach 14815d603e7SGreg Roach // Add the census fact to the rest of the household 1490483c93fSGreg Roach foreach ($ca_individuals['xref'] ?? [] as $xref) { 1500483c93fSGreg Roach if ($xref !== '' && $xref !== $individual->xref()) { 1516b9cb339SGreg Roach Registry::individualFactory()->make($xref, $individual->tree()) 15215d603e7SGreg Roach ->updateFact($fact_id, $newged, !$keep_chan); 15315d603e7SGreg Roach } 15415d603e7SGreg Roach } 15515d603e7SGreg Roach } 15615d603e7SGreg Roach 15715d603e7SGreg Roach return $newged; 15815d603e7SGreg Roach } 15915d603e7SGreg Roach 16015d603e7SGreg Roach /** 16115d603e7SGreg Roach * @param CensusInterface $census 16215d603e7SGreg Roach * @param string $ca_title 16315d603e7SGreg Roach * @param string $ca_place 16415d603e7SGreg Roach * @param string $ca_citation 16509482a55SGreg Roach * @param array<array<string>> $ca_individuals 16615d603e7SGreg Roach * @param string $ca_notes 16715d603e7SGreg Roach * 16815d603e7SGreg Roach * @return string 16915d603e7SGreg Roach */ 17024f2a3afSGreg Roach private function createNoteText(CensusInterface $census, string $ca_title, string $ca_place, string $ca_citation, array $ca_individuals, string $ca_notes): string 171c1010edaSGreg Roach { 172e380ca62SGreg Roach $text = $ca_title; 173e380ca62SGreg Roach 174e380ca62SGreg Roach if ($ca_citation !== '') { 175b6b785a1SGreg Roach $text .= "\n" . $ca_citation; 176e380ca62SGreg Roach } 177e380ca62SGreg Roach 178e380ca62SGreg Roach if ($ca_place !== '') { 179b6b785a1SGreg Roach $text .= "\n" . $ca_place; 180e380ca62SGreg Roach } 181e380ca62SGreg Roach 182e380ca62SGreg Roach $text .= "\n\n|"; 18315d603e7SGreg Roach 1843cc4e3a0SGreg Roach foreach ($census->columns() as $column) { 1853cc4e3a0SGreg Roach $text .= ' ' . $column->abbreviation() . ' |'; 1860d46ec71SGreg Roach } 1870d46ec71SGreg Roach 1883cc4e3a0SGreg Roach $text .= "\n|" . str_repeat(' ----- |', count($census->columns())); 18915d603e7SGreg Roach 1900483c93fSGreg Roach foreach (array_keys($ca_individuals['xref'] ?? []) as $key) { 1913cc4e3a0SGreg Roach $text .= "\n|"; 1923cc4e3a0SGreg Roach 1930483c93fSGreg Roach foreach ($census->columns() as $n => $column) { 1943cc4e3a0SGreg Roach $text .= ' ' . $ca_individuals[$n][$key] . ' |'; 1950483c93fSGreg Roach } 19615d603e7SGreg Roach } 19715d603e7SGreg Roach 198b6b785a1SGreg Roach if ($ca_notes !== '') { 199b6b785a1SGreg Roach $text .= "\n\n" . strtr($ca_notes, ["\r" => '']); 200b6b785a1SGreg Roach } 201b6b785a1SGreg Roach 202b6b785a1SGreg Roach return $text; 20340990b78SGreg Roach } 20440990b78SGreg Roach 20540990b78SGreg Roach /** 206ad51e0bbSGreg Roach * Generate an HTML row of data for the census header 20752bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 20852bc9faeSGreg Roach * Add suffix cell (delete button) 20952bc9faeSGreg Roach * 210ad51e0bbSGreg Roach * @param CensusInterface $census 21199f222b3SGreg Roach * 212ad51e0bbSGreg Roach * @return string 21399f222b3SGreg Roach */ 2145a62e0a6SGreg Roach protected function censusTableHeader(CensusInterface $census): string 215c1010edaSGreg Roach { 21652bc9faeSGreg Roach $html = ''; 217ad51e0bbSGreg Roach foreach ($census->columns() as $column) { 21815d603e7SGreg Roach $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>'; 21999f222b3SGreg Roach } 22099f222b3SGreg Roach 22115d603e7SGreg Roach return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>'; 222ad51e0bbSGreg Roach } 22399f222b3SGreg Roach 224ad51e0bbSGreg Roach /** 225ad51e0bbSGreg Roach * Generate an HTML row of data for the census 22652bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 22752bc9faeSGreg Roach * Add suffix cell (delete button) 22852bc9faeSGreg Roach * 229e5a6b4d4SGreg Roach * @param CensusInterface $census 230ad51e0bbSGreg Roach * 231ad51e0bbSGreg Roach * @return string 232ad51e0bbSGreg Roach */ 2338b9cfadbSGreg Roach public function censusTableEmptyRow(CensusInterface $census): string 234c1010edaSGreg Roach { 2350483c93fSGreg Roach $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>'; 2360483c93fSGreg Roach 2370483c93fSGreg Roach foreach ($census->columns() as $n => $column) { 2380483c93fSGreg Roach $html .= '<td class="wt-census-assistant-field p-0"><input class="form-control wt-census-assistant-form-control p-0" type="text" name="ca_individuals[' . $n . '][]"></td>'; 2390483c93fSGreg Roach } 2400483c93fSGreg Roach 2410483c93fSGreg Roach $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>'; 2420483c93fSGreg Roach 2430483c93fSGreg Roach return '<tr class="wt-census-assistant-row">' . $html . '</tr>'; 244ad51e0bbSGreg Roach } 24599f222b3SGreg Roach 246ad51e0bbSGreg Roach /** 247ad51e0bbSGreg Roach * Generate an HTML row of data for the census 24852bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 24952bc9faeSGreg Roach * Add suffix cell (delete button) 25052bc9faeSGreg Roach * 251ad51e0bbSGreg Roach * @param CensusInterface $census 252ad51e0bbSGreg Roach * @param Individual $individual 253ad51e0bbSGreg Roach * @param Individual $head 254ad51e0bbSGreg Roach * 255ad51e0bbSGreg Roach * @return string 256ad51e0bbSGreg Roach */ 2578b9cfadbSGreg Roach public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string 258c1010edaSGreg Roach { 2590483c93fSGreg Roach $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>'; 2600483c93fSGreg Roach 2610483c93fSGreg Roach foreach ($census->columns() as $n => $column) { 2620483c93fSGreg Roach $html .= '<td class="wt-census-assistant-field p-0"><input class="form-control wt-census-assistant-form-control p-0" type="text" value="' . $column->generate($individual, $head) . '" name="ca_individuals[' . $n . '][]"></td>'; 26399f222b3SGreg Roach } 26499f222b3SGreg Roach 2650483c93fSGreg Roach $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>'; 2660483c93fSGreg Roach 2670483c93fSGreg Roach return '<tr class="wt-census-assistant-row">' . $html . '</tr>'; 26899f222b3SGreg Roach } 2698c2e8227SGreg Roach} 270