18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 158c2e8227SGreg Roach * along with this program. If not, see <http://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; 23d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2599f222b3SGreg Roachuse Fisharebest\Webtrees\Individual; 264ea62551SGreg Roachuse Fisharebest\Webtrees\Tree; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 298c2e8227SGreg Roach 304ea62551SGreg Roachuse function assert; 31*d8869271SGreg Roachuse function response; 324ea62551SGreg Roach 338c2e8227SGreg Roach/** 348c2e8227SGreg Roach * Class CensusAssistantModule 358c2e8227SGreg Roach */ 365206405dSRico Sonntagclass CensusAssistantModule extends AbstractModule 37c1010edaSGreg Roach{ 38961ec755SGreg Roach /** 390cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 40961ec755SGreg Roach * 41961ec755SGreg Roach * @return string 42961ec755SGreg Roach */ 4349a243cbSGreg Roach public function title(): string 44c1010edaSGreg Roach { 45bbb76c12SGreg Roach /* I18N: Name of a module */ 46bbb76c12SGreg Roach return I18N::translate('Census assistant'); 478c2e8227SGreg Roach } 488c2e8227SGreg Roach 49961ec755SGreg Roach /** 50961ec755SGreg Roach * A sentence describing what this module does. 51961ec755SGreg Roach * 52961ec755SGreg Roach * @return string 53961ec755SGreg 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 { 67b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 68b46c87bdSGreg Roach 69b46c87bdSGreg Roach $census = $params['census']; 709001c0b3SGreg Roach 7159f2f229SGreg Roach $html = $this->censusTableHeader(new $census()); 729001c0b3SGreg Roach 736ccdf4f0SGreg Roach return response($html); 749001c0b3SGreg Roach } 759001c0b3SGreg Roach 769001c0b3SGreg Roach /** 776ccdf4f0SGreg Roach * @param ServerRequestInterface $request 789001c0b3SGreg Roach * 796ccdf4f0SGreg Roach * @return ResponseInterface 809001c0b3SGreg Roach */ 817da59459SGreg Roach public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface 82c1010edaSGreg Roach { 8357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 844ea62551SGreg Roach assert($tree instanceof Tree); 854ea62551SGreg Roach 86b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 87abaef046SGreg Roach $individual = Individual::getInstance($params['xref'], $tree); 88abaef046SGreg Roach $head = Individual::getInstance($params['head'], $tree); 89*d8869271SGreg Roach $census_class = $params['census']; 90*d8869271SGreg Roach $census = new $census_class(); 91d45cb9d3SGreg Roach 92d45cb9d3SGreg Roach if ($individual instanceof Individual && $head instanceof Individual) { 93*d8869271SGreg Roach $html = $this->censusTableRow($census, $individual, $head); 94*d8869271SGreg Roach } else { 95*d8869271SGreg Roach $html = $this->censusTableEmptyRow($census); 96d45cb9d3SGreg Roach } 97e364afe4SGreg Roach 98*d8869271SGreg Roach return response($html); 999001c0b3SGreg Roach } 1009001c0b3SGreg Roach 1019001c0b3SGreg Roach /** 10215d603e7SGreg Roach * @param Individual $individual 1039001c0b3SGreg Roach * 1049001c0b3SGreg Roach * @return string 1058c2e8227SGreg Roach */ 1068f53f488SRico Sonntag public function createCensusAssistant(Individual $individual): string 107c1010edaSGreg Roach { 108225e381fSGreg Roach return view('modules/census-assistant', [ 10934cd602eSGreg Roach 'individual' => $individual, 11034cd602eSGreg Roach ]); 11115d603e7SGreg Roach } 11215d603e7SGreg Roach 11315d603e7SGreg Roach /** 1146ccdf4f0SGreg Roach * @param ServerRequestInterface $request 11515d603e7SGreg Roach * @param Individual $individual 11660bc3e3fSGreg Roach * @param string $fact_id 11715d603e7SGreg Roach * @param string $newged 11860bc3e3fSGreg Roach * @param bool $keep_chan 11915d603e7SGreg Roach * 12015d603e7SGreg Roach * @return string 12115d603e7SGreg Roach */ 1226ccdf4f0SGreg Roach public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string 123c1010edaSGreg Roach { 124b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 125abaef046SGreg Roach 126abaef046SGreg Roach $ca_title = $params['ca_title'] ?? ''; 127abaef046SGreg Roach $ca_place = $params['ca_place'] ?? ''; 128abaef046SGreg Roach $ca_citation = $params['ca_citation'] ?? ''; 129abaef046SGreg Roach $ca_individuals = $params['ca_individuals'] ?? []; 130abaef046SGreg Roach $ca_notes = $params['ca_notes'] ?? ''; 131abaef046SGreg Roach $ca_census = $params['ca_census'] ?? ''; 13215d603e7SGreg Roach 133a91af26aSGreg Roach if ($ca_census !== '' && $ca_individuals !== []) { 13459f2f229SGreg Roach $census = new $ca_census(); 13515d603e7SGreg Roach 13615d603e7SGreg Roach $note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); 137afb591d7SGreg Roach $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); 138f4afa648SGreg Roach $note = $individual->tree()->createRecord($note_gedcom); 13915d603e7SGreg Roach 140c0935879SGreg Roach $newged .= "\n2 NOTE @" . $note->xref() . '@'; 14115d603e7SGreg Roach 14215d603e7SGreg Roach // Add the census fact to the rest of the household 14315d603e7SGreg Roach foreach (array_keys($ca_individuals) as $xref) { 144c0935879SGreg Roach if ($xref !== $individual->xref()) { 145f4afa648SGreg Roach Individual::getInstance($xref, $individual->tree()) 14615d603e7SGreg Roach ->updateFact($fact_id, $newged, !$keep_chan); 14715d603e7SGreg Roach } 14815d603e7SGreg Roach } 14915d603e7SGreg Roach } 15015d603e7SGreg Roach 15115d603e7SGreg Roach return $newged; 15215d603e7SGreg Roach } 15315d603e7SGreg Roach 15415d603e7SGreg Roach /** 15515d603e7SGreg Roach * @param CensusInterface $census 15615d603e7SGreg Roach * @param string $ca_title 15715d603e7SGreg Roach * @param string $ca_place 15815d603e7SGreg Roach * @param string $ca_citation 15915d603e7SGreg Roach * @param string[][] $ca_individuals 16015d603e7SGreg Roach * @param string $ca_notes 16115d603e7SGreg Roach * 16215d603e7SGreg Roach * @return string 16315d603e7SGreg Roach */ 1648f53f488SRico Sonntag private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string 165c1010edaSGreg Roach { 1660d46ec71SGreg Roach $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n"; 16715d603e7SGreg Roach 16815d603e7SGreg Roach foreach ($census->columns() as $n => $column) { 1690d46ec71SGreg Roach if ($n === 0) { 1700d46ec71SGreg Roach $text .= "\n"; 1710d46ec71SGreg Roach } else { 17215d603e7SGreg Roach $text .= ' | '; 17315d603e7SGreg Roach } 1740d46ec71SGreg Roach $text .= $column->abbreviation(); 1750d46ec71SGreg Roach } 1760d46ec71SGreg Roach 1770d46ec71SGreg Roach foreach ($census->columns() as $n => $column) { 1780d46ec71SGreg Roach if ($n === 0) { 1790d46ec71SGreg Roach $text .= "\n"; 1800d46ec71SGreg Roach } else { 1810d46ec71SGreg Roach $text .= ' | '; 1820d46ec71SGreg Roach } 1830d46ec71SGreg Roach $text .= '-----'; 18415d603e7SGreg Roach } 18515d603e7SGreg Roach 18615d603e7SGreg Roach foreach ($ca_individuals as $xref => $columns) { 18715d603e7SGreg Roach $text .= "\n" . implode(' | ', $columns); 18815d603e7SGreg Roach } 18915d603e7SGreg Roach 1900d46ec71SGreg Roach return $text . "\n\n" . $ca_notes; 19140990b78SGreg Roach } 19240990b78SGreg Roach 19340990b78SGreg Roach /** 194ad51e0bbSGreg Roach * Generate an HTML row of data for the census header 19552bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 19652bc9faeSGreg Roach * Add suffix cell (delete button) 19752bc9faeSGreg Roach * 198ad51e0bbSGreg Roach * @param CensusInterface $census 19999f222b3SGreg Roach * 200ad51e0bbSGreg Roach * @return string 20199f222b3SGreg Roach */ 2025a62e0a6SGreg Roach protected function censusTableHeader(CensusInterface $census): string 203c1010edaSGreg Roach { 20452bc9faeSGreg Roach $html = ''; 205ad51e0bbSGreg Roach foreach ($census->columns() as $column) { 20615d603e7SGreg Roach $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>'; 20799f222b3SGreg Roach } 20899f222b3SGreg Roach 20915d603e7SGreg Roach return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>'; 210ad51e0bbSGreg Roach } 21199f222b3SGreg Roach 212ad51e0bbSGreg Roach /** 213ad51e0bbSGreg Roach * Generate an HTML row of data for the census 21452bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 21552bc9faeSGreg Roach * Add suffix cell (delete button) 21652bc9faeSGreg Roach * 217e5a6b4d4SGreg Roach * @param CensusInterface $census 218ad51e0bbSGreg Roach * 219ad51e0bbSGreg Roach * @return string 220ad51e0bbSGreg Roach */ 2218b9cfadbSGreg Roach public function censusTableEmptyRow(CensusInterface $census): string 222c1010edaSGreg Roach { 22308362db4SGreg Roach return '<tr class="wt-census-assistant-row"><td hidden></td>' . str_repeat('<td class="wt-census-assistant-field p-0"><input type="text" class="form-control wt-census-assistant-form-control p-0"></td>', count($census->columns())) . '<td><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td></tr>'; 224ad51e0bbSGreg Roach } 22599f222b3SGreg Roach 226ad51e0bbSGreg Roach /** 227ad51e0bbSGreg Roach * Generate an HTML row of data for the census 22852bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 22952bc9faeSGreg Roach * Add suffix cell (delete button) 23052bc9faeSGreg Roach * 231ad51e0bbSGreg Roach * @param CensusInterface $census 232ad51e0bbSGreg Roach * @param Individual $individual 233ad51e0bbSGreg Roach * @param Individual $head 234ad51e0bbSGreg Roach * 235ad51e0bbSGreg Roach * @return string 236ad51e0bbSGreg Roach */ 2378b9cfadbSGreg Roach public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string 238c1010edaSGreg Roach { 23915d603e7SGreg Roach $html = ''; 24015d603e7SGreg Roach foreach ($census->columns() as $column) { 241b6c326d8SGreg 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[' . $individual->xref() . '][]"></td>'; 24299f222b3SGreg Roach } 24399f222b3SGreg Roach 24408362db4SGreg Roach return '<tr class="wt-census-assistant-row"><td class="wt-census-assistant-field" hidden>' . $individual->xref() . '</td>' . $html . '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td></tr>'; 24599f222b3SGreg Roach } 2468c2e8227SGreg Roach} 247