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 5449a243cbSGreg Roach public function description(): string 55c1010edaSGreg Roach { 56bbb76c12SGreg Roach /* I18N: Description of the “Census assistant” module */ 57bbb76c12SGreg Roach return I18N::translate('An alternative way to enter census transcripts and link them to individuals.'); 588c2e8227SGreg Roach } 598c2e8227SGreg Roach 6076692c8bSGreg Roach /** 616ccdf4f0SGreg Roach * @param ServerRequestInterface $request 629001c0b3SGreg Roach * 636ccdf4f0SGreg Roach * @return ResponseInterface 649001c0b3SGreg Roach */ 657da59459SGreg Roach public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface 66c1010edaSGreg Roach { 67748dbe15SGreg Roach $census_class = Validator::parsedBody($request)->string('census'); 68b46c87bdSGreg Roach 69748dbe15SGreg Roach $html = $this->censusTableHeader(new $census_class()); 709001c0b3SGreg Roach 716ccdf4f0SGreg Roach return response($html); 729001c0b3SGreg Roach } 739001c0b3SGreg Roach 749001c0b3SGreg Roach /** 756ccdf4f0SGreg Roach * @param ServerRequestInterface $request 769001c0b3SGreg Roach * 776ccdf4f0SGreg Roach * @return ResponseInterface 789001c0b3SGreg Roach */ 797da59459SGreg Roach public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface 80c1010edaSGreg Roach { 81b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 82dfe6841dSGreg Roach $indi_xref = Validator::parsedBody($request)->isXref()->string('xref', ''); 83dfe6841dSGreg Roach $head_xref = Validator::parsedBody($request)->isXref()->string('head', ''); 84748dbe15SGreg Roach $individual = Registry::individualFactory()->make($indi_xref, $tree); 85748dbe15SGreg Roach $head = Registry::individualFactory()->make($head_xref, $tree); 86748dbe15SGreg Roach $census_class = Validator::parsedBody($request)->string('census'); 87d8869271SGreg Roach $census = new $census_class(); 88d45cb9d3SGreg Roach 891f244d77SGreg Roach // No head of household? Create a fake one. 903529c469SGreg Roach $head ??= Registry::individualFactory()->new('X', '0 @X@ INDI', null, $tree); 91dd1e545cSGreg Roach 92d5b0584dSGreg Roach // Generate columns (e.g. relationship name) using the correct language. 93d5b0584dSGreg Roach I18N::init($census->censusLanguage()); 94d5b0584dSGreg Roach 95d45cb9d3SGreg Roach if ($individual instanceof Individual && $head instanceof Individual) { 96d8869271SGreg Roach $html = $this->censusTableRow($census, $individual, $head); 97d8869271SGreg Roach } else { 98d8869271SGreg Roach $html = $this->censusTableEmptyRow($census); 99d45cb9d3SGreg Roach } 100e364afe4SGreg Roach 101d8869271SGreg Roach return response($html); 1029001c0b3SGreg Roach } 1039001c0b3SGreg Roach 1049001c0b3SGreg Roach /** 10515d603e7SGreg Roach * @param Individual $individual 1069001c0b3SGreg Roach * 1079001c0b3SGreg Roach * @return string 1088c2e8227SGreg Roach */ 1098f53f488SRico Sonntag public function createCensusAssistant(Individual $individual): string 110c1010edaSGreg Roach { 111225e381fSGreg Roach return view('modules/census-assistant', [ 11234cd602eSGreg Roach 'individual' => $individual, 11334cd602eSGreg Roach ]); 11415d603e7SGreg Roach } 11515d603e7SGreg Roach 11615d603e7SGreg Roach /** 1176ccdf4f0SGreg Roach * @param ServerRequestInterface $request 11815d603e7SGreg Roach * @param Individual $individual 11960bc3e3fSGreg Roach * @param string $fact_id 12015d603e7SGreg Roach * @param string $newged 12160bc3e3fSGreg Roach * @param bool $keep_chan 12215d603e7SGreg Roach * 12315d603e7SGreg Roach * @return string 12415d603e7SGreg Roach */ 1256ccdf4f0SGreg Roach public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string 126c1010edaSGreg Roach { 127748dbe15SGreg Roach $ca_title = Validator::parsedBody($request)->string('ca_title'); 128748dbe15SGreg Roach $ca_place = Validator::parsedBody($request)->string('ca_place'); 129748dbe15SGreg Roach $ca_citation = Validator::parsedBody($request)->string('ca_citation'); 130748dbe15SGreg Roach $ca_individuals = Validator::parsedBody($request)->array('ca_individuals'); 131748dbe15SGreg Roach $ca_notes = Validator::parsedBody($request)->string('ca_notes'); 132748dbe15SGreg Roach $ca_census = Validator::parsedBody($request)->string('ca_census'); 13315d603e7SGreg Roach 134a91af26aSGreg Roach if ($ca_census !== '' && $ca_individuals !== []) { 13559f2f229SGreg Roach $census = new $ca_census(); 13615d603e7SGreg Roach 13715d603e7SGreg Roach $note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); 138afb591d7SGreg Roach $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); 139f4afa648SGreg Roach $note = $individual->tree()->createRecord($note_gedcom); 14015d603e7SGreg Roach 141c0935879SGreg Roach $newged .= "\n2 NOTE @" . $note->xref() . '@'; 14215d603e7SGreg Roach 14315d603e7SGreg Roach // Add the census fact to the rest of the household 1440483c93fSGreg Roach foreach ($ca_individuals['xref'] ?? [] as $xref) { 1450483c93fSGreg Roach if ($xref !== '' && $xref !== $individual->xref()) { 1466b9cb339SGreg Roach Registry::individualFactory()->make($xref, $individual->tree()) 14715d603e7SGreg Roach ->updateFact($fact_id, $newged, !$keep_chan); 14815d603e7SGreg Roach } 14915d603e7SGreg Roach } 15015d603e7SGreg Roach } 15115d603e7SGreg Roach 15215d603e7SGreg Roach return $newged; 15315d603e7SGreg Roach } 15415d603e7SGreg Roach 15515d603e7SGreg Roach /** 15615d603e7SGreg Roach * @param CensusInterface $census 15715d603e7SGreg Roach * @param string $ca_title 15815d603e7SGreg Roach * @param string $ca_place 15915d603e7SGreg Roach * @param string $ca_citation 16009482a55SGreg Roach * @param array<array<string>> $ca_individuals 16115d603e7SGreg Roach * @param string $ca_notes 16215d603e7SGreg Roach * 16315d603e7SGreg Roach * @return string 16415d603e7SGreg Roach */ 16524f2a3afSGreg Roach private function createNoteText(CensusInterface $census, string $ca_title, string $ca_place, string $ca_citation, array $ca_individuals, string $ca_notes): string 166c1010edaSGreg Roach { 167e380ca62SGreg Roach $text = $ca_title; 168e380ca62SGreg Roach 169e380ca62SGreg Roach if ($ca_citation !== '') { 170b6b785a1SGreg Roach $text .= "\n" . $ca_citation; 171e380ca62SGreg Roach } 172e380ca62SGreg Roach 173e380ca62SGreg Roach if ($ca_place !== '') { 174b6b785a1SGreg Roach $text .= "\n" . $ca_place; 175e380ca62SGreg Roach } 176e380ca62SGreg Roach 177e380ca62SGreg Roach $text .= "\n\n|"; 17815d603e7SGreg Roach 1793cc4e3a0SGreg Roach foreach ($census->columns() as $column) { 1803cc4e3a0SGreg Roach $text .= ' ' . $column->abbreviation() . ' |'; 1810d46ec71SGreg Roach } 1820d46ec71SGreg Roach 1833cc4e3a0SGreg Roach $text .= "\n|" . str_repeat(' ----- |', count($census->columns())); 18415d603e7SGreg Roach 1850483c93fSGreg Roach foreach (array_keys($ca_individuals['xref'] ?? []) as $key) { 1863cc4e3a0SGreg Roach $text .= "\n|"; 1873cc4e3a0SGreg Roach 1880483c93fSGreg Roach foreach ($census->columns() as $n => $column) { 1893cc4e3a0SGreg Roach $text .= ' ' . $ca_individuals[$n][$key] . ' |'; 1900483c93fSGreg Roach } 19115d603e7SGreg Roach } 19215d603e7SGreg Roach 193b6b785a1SGreg Roach if ($ca_notes !== '') { 194b6b785a1SGreg Roach $text .= "\n\n" . strtr($ca_notes, ["\r" => '']); 195b6b785a1SGreg Roach } 196b6b785a1SGreg Roach 197b6b785a1SGreg Roach return $text; 19840990b78SGreg Roach } 19940990b78SGreg Roach 20040990b78SGreg Roach /** 201ad51e0bbSGreg Roach * Generate an HTML row of data for the census header 20252bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 20352bc9faeSGreg Roach * Add suffix cell (delete button) 20452bc9faeSGreg Roach * 205ad51e0bbSGreg Roach * @param CensusInterface $census 20699f222b3SGreg Roach * 207ad51e0bbSGreg Roach * @return string 20899f222b3SGreg Roach */ 2095a62e0a6SGreg Roach protected function censusTableHeader(CensusInterface $census): string 210c1010edaSGreg Roach { 21152bc9faeSGreg Roach $html = ''; 212ad51e0bbSGreg Roach foreach ($census->columns() as $column) { 21315d603e7SGreg Roach $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>'; 21499f222b3SGreg Roach } 21599f222b3SGreg Roach 21615d603e7SGreg Roach return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>'; 217ad51e0bbSGreg Roach } 21899f222b3SGreg Roach 219ad51e0bbSGreg Roach /** 220ad51e0bbSGreg Roach * Generate an HTML row of data for the census 22152bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 22252bc9faeSGreg Roach * Add suffix cell (delete button) 22352bc9faeSGreg Roach * 224e5a6b4d4SGreg Roach * @param CensusInterface $census 225ad51e0bbSGreg Roach * 226ad51e0bbSGreg Roach * @return string 227ad51e0bbSGreg Roach */ 2288b9cfadbSGreg Roach public function censusTableEmptyRow(CensusInterface $census): string 229c1010edaSGreg Roach { 2300483c93fSGreg Roach $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>'; 2310483c93fSGreg Roach 2320483c93fSGreg Roach foreach ($census->columns() as $n => $column) { 2330483c93fSGreg 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>'; 2340483c93fSGreg Roach } 2350483c93fSGreg Roach 2360483c93fSGreg Roach $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>'; 2370483c93fSGreg Roach 2380483c93fSGreg Roach return '<tr class="wt-census-assistant-row">' . $html . '</tr>'; 239ad51e0bbSGreg Roach } 24099f222b3SGreg Roach 241ad51e0bbSGreg Roach /** 242ad51e0bbSGreg Roach * Generate an HTML row of data for the census 24352bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 24452bc9faeSGreg Roach * Add suffix cell (delete button) 24552bc9faeSGreg Roach * 246ad51e0bbSGreg Roach * @param CensusInterface $census 247ad51e0bbSGreg Roach * @param Individual $individual 248ad51e0bbSGreg Roach * @param Individual $head 249ad51e0bbSGreg Roach * 250ad51e0bbSGreg Roach * @return string 251ad51e0bbSGreg Roach */ 2528b9cfadbSGreg Roach public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string 253c1010edaSGreg Roach { 2540483c93fSGreg Roach $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>'; 2550483c93fSGreg Roach 2560483c93fSGreg Roach foreach ($census->columns() as $n => $column) { 2570483c93fSGreg 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>'; 25899f222b3SGreg Roach } 25999f222b3SGreg Roach 2600483c93fSGreg Roach $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>'; 2610483c93fSGreg Roach 2620483c93fSGreg Roach return '<tr class="wt-census-assistant-row">' . $html . '</tr>'; 26399f222b3SGreg Roach } 2648c2e8227SGreg Roach} 265