18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1815d603e7SGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2076692c8bSGreg Roach 21ad51e0bbSGreg Roachuse Fisharebest\Webtrees\Census\CensusInterface; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2399f222b3SGreg Roachuse Fisharebest\Webtrees\Individual; 249001c0b3SGreg Roachuse Fisharebest\Webtrees\Tree; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 266ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 27d45cb9d3SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 288c2e8227SGreg Roach 298c2e8227SGreg Roach/** 308c2e8227SGreg Roach * Class CensusAssistantModule 318c2e8227SGreg Roach */ 325206405dSRico Sonntagclass CensusAssistantModule extends AbstractModule 33c1010edaSGreg Roach{ 34961ec755SGreg Roach /** 350cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 36961ec755SGreg Roach * 37961ec755SGreg Roach * @return string 38961ec755SGreg Roach */ 3949a243cbSGreg Roach public function title(): string 40c1010edaSGreg Roach { 41bbb76c12SGreg Roach /* I18N: Name of a module */ 42bbb76c12SGreg Roach return I18N::translate('Census assistant'); 438c2e8227SGreg Roach } 448c2e8227SGreg Roach 45961ec755SGreg Roach /** 46961ec755SGreg Roach * A sentence describing what this module does. 47961ec755SGreg Roach * 48961ec755SGreg Roach * @return string 49961ec755SGreg Roach */ 5049a243cbSGreg Roach public function description(): string 51c1010edaSGreg Roach { 52bbb76c12SGreg Roach /* I18N: Description of the “Census assistant” module */ 53bbb76c12SGreg Roach return I18N::translate('An alternative way to enter census transcripts and link them to individuals.'); 548c2e8227SGreg Roach } 558c2e8227SGreg Roach 5676692c8bSGreg Roach /** 576ccdf4f0SGreg Roach * @param ServerRequestInterface $request 589001c0b3SGreg Roach * 596ccdf4f0SGreg Roach * @return ResponseInterface 609001c0b3SGreg Roach */ 616ccdf4f0SGreg Roach public function getCensusHeaderAction(ServerRequestInterface $request): ResponseInterface 62c1010edaSGreg Roach { 63*abaef046SGreg Roach $census = $request->getQueryParams()['census']; 649001c0b3SGreg Roach 6559f2f229SGreg Roach $html = $this->censusTableHeader(new $census()); 669001c0b3SGreg Roach 676ccdf4f0SGreg Roach return response($html); 689001c0b3SGreg Roach } 699001c0b3SGreg Roach 709001c0b3SGreg Roach /** 716ccdf4f0SGreg Roach * @param ServerRequestInterface $request 72b6db7c1fSGreg Roach * @param Tree $tree 739001c0b3SGreg Roach * 746ccdf4f0SGreg Roach * @return ResponseInterface 759001c0b3SGreg Roach */ 766ccdf4f0SGreg Roach public function getCensusIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 77c1010edaSGreg Roach { 78*abaef046SGreg Roach $params = $request->getQueryParams(); 79*abaef046SGreg Roach $individual = Individual::getInstance($params['xref'], $tree); 80*abaef046SGreg Roach $head = Individual::getInstance($params['head'], $tree); 81*abaef046SGreg Roach $census = $params['census']; 82d45cb9d3SGreg Roach 83d45cb9d3SGreg Roach if ($individual instanceof Individual && $head instanceof Individual) { 8459f2f229SGreg Roach $html = $this->censusTableRow(new $census(), $individual, $head); 859001c0b3SGreg Roach 866ccdf4f0SGreg Roach return response($html); 87d45cb9d3SGreg Roach } 88e364afe4SGreg Roach 89e364afe4SGreg Roach throw new NotFoundHttpException(); 909001c0b3SGreg Roach } 919001c0b3SGreg Roach 929001c0b3SGreg Roach /** 9315d603e7SGreg Roach * @param Individual $individual 949001c0b3SGreg Roach * 959001c0b3SGreg Roach * @return string 968c2e8227SGreg Roach */ 978f53f488SRico Sonntag public function createCensusAssistant(Individual $individual): string 98c1010edaSGreg Roach { 99225e381fSGreg Roach return view('modules/census-assistant', [ 10034cd602eSGreg Roach 'individual' => $individual, 10134cd602eSGreg Roach ]); 10215d603e7SGreg Roach } 10315d603e7SGreg Roach 10415d603e7SGreg Roach /** 1056ccdf4f0SGreg Roach * @param ServerRequestInterface $request 10615d603e7SGreg Roach * @param Individual $individual 10760bc3e3fSGreg Roach * @param string $fact_id 10815d603e7SGreg Roach * @param string $newged 10960bc3e3fSGreg Roach * @param bool $keep_chan 11015d603e7SGreg Roach * 11115d603e7SGreg Roach * @return string 11215d603e7SGreg Roach */ 1136ccdf4f0SGreg Roach public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string 114c1010edaSGreg Roach { 115*abaef046SGreg Roach $params = $request->getParsedBody(); 116*abaef046SGreg Roach 117*abaef046SGreg Roach $ca_title = $params['ca_title'] ?? ''; 118*abaef046SGreg Roach $ca_place = $params['ca_place'] ?? ''; 119*abaef046SGreg Roach $ca_citation = $params['ca_citation'] ?? ''; 120*abaef046SGreg Roach $ca_individuals = $params['ca_individuals'] ?? []; 121*abaef046SGreg Roach $ca_notes = $params['ca_notes'] ?? ''; 122*abaef046SGreg Roach $ca_census = $params['ca_census'] ?? ''; 12315d603e7SGreg Roach 12415d603e7SGreg Roach if ($ca_census !== '' && !empty($ca_individuals)) { 12559f2f229SGreg Roach $census = new $ca_census(); 12615d603e7SGreg Roach 12715d603e7SGreg Roach $note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); 128afb591d7SGreg Roach $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); 129f4afa648SGreg Roach $note = $individual->tree()->createRecord($note_gedcom); 13015d603e7SGreg Roach 131c0935879SGreg Roach $newged .= "\n2 NOTE @" . $note->xref() . '@'; 13215d603e7SGreg Roach 13315d603e7SGreg Roach // Add the census fact to the rest of the household 13415d603e7SGreg Roach foreach (array_keys($ca_individuals) as $xref) { 135c0935879SGreg Roach if ($xref !== $individual->xref()) { 136f4afa648SGreg Roach Individual::getInstance($xref, $individual->tree()) 13715d603e7SGreg Roach ->updateFact($fact_id, $newged, !$keep_chan); 13815d603e7SGreg Roach } 13915d603e7SGreg Roach } 14015d603e7SGreg Roach } 14115d603e7SGreg Roach 14215d603e7SGreg Roach return $newged; 14315d603e7SGreg Roach } 14415d603e7SGreg Roach 14515d603e7SGreg Roach /** 14615d603e7SGreg Roach * @param CensusInterface $census 14715d603e7SGreg Roach * @param string $ca_title 14815d603e7SGreg Roach * @param string $ca_place 14915d603e7SGreg Roach * @param string $ca_citation 15015d603e7SGreg Roach * @param string[][] $ca_individuals 15115d603e7SGreg Roach * @param string $ca_notes 15215d603e7SGreg Roach * 15315d603e7SGreg Roach * @return string 15415d603e7SGreg Roach */ 1558f53f488SRico Sonntag private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string 156c1010edaSGreg Roach { 1570d46ec71SGreg Roach $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n"; 15815d603e7SGreg Roach 15915d603e7SGreg Roach foreach ($census->columns() as $n => $column) { 1600d46ec71SGreg Roach if ($n === 0) { 1610d46ec71SGreg Roach $text .= "\n"; 1620d46ec71SGreg Roach } else { 16315d603e7SGreg Roach $text .= ' | '; 16415d603e7SGreg Roach } 1650d46ec71SGreg Roach $text .= $column->abbreviation(); 1660d46ec71SGreg Roach } 1670d46ec71SGreg Roach 1680d46ec71SGreg Roach foreach ($census->columns() as $n => $column) { 1690d46ec71SGreg Roach if ($n === 0) { 1700d46ec71SGreg Roach $text .= "\n"; 1710d46ec71SGreg Roach } else { 1720d46ec71SGreg Roach $text .= ' | '; 1730d46ec71SGreg Roach } 1740d46ec71SGreg Roach $text .= '-----'; 17515d603e7SGreg Roach } 17615d603e7SGreg Roach 17715d603e7SGreg Roach foreach ($ca_individuals as $xref => $columns) { 17815d603e7SGreg Roach $text .= "\n" . implode(' | ', $columns); 17915d603e7SGreg Roach } 18015d603e7SGreg Roach 1810d46ec71SGreg Roach return $text . "\n\n" . $ca_notes; 18240990b78SGreg Roach } 18340990b78SGreg Roach 18440990b78SGreg Roach /** 185ad51e0bbSGreg Roach * Generate an HTML row of data for the census header 18652bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 18752bc9faeSGreg Roach * Add suffix cell (delete button) 18852bc9faeSGreg Roach * 189ad51e0bbSGreg Roach * @param CensusInterface $census 19099f222b3SGreg Roach * 191ad51e0bbSGreg Roach * @return string 19299f222b3SGreg Roach */ 1935a62e0a6SGreg Roach protected function censusTableHeader(CensusInterface $census): string 194c1010edaSGreg Roach { 19552bc9faeSGreg Roach $html = ''; 196ad51e0bbSGreg Roach foreach ($census->columns() as $column) { 19715d603e7SGreg Roach $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>'; 19899f222b3SGreg Roach } 19999f222b3SGreg Roach 20015d603e7SGreg Roach return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>'; 201ad51e0bbSGreg Roach } 20299f222b3SGreg Roach 203ad51e0bbSGreg Roach /** 204ad51e0bbSGreg Roach * Generate an HTML row of data for the census 20552bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 20652bc9faeSGreg Roach * Add suffix cell (delete button) 20752bc9faeSGreg Roach * 208e5a6b4d4SGreg Roach * @param CensusInterface $census 209ad51e0bbSGreg Roach * 210ad51e0bbSGreg Roach * @return string 211ad51e0bbSGreg Roach */ 2128b9cfadbSGreg Roach public function censusTableEmptyRow(CensusInterface $census): string 213c1010edaSGreg Roach { 214b6c326d8SGreg 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 class="icon-remove" href="#" title="' . I18N::translate('Remove') . '"></a></td></tr>'; 215ad51e0bbSGreg Roach } 21699f222b3SGreg Roach 217ad51e0bbSGreg Roach /** 218ad51e0bbSGreg Roach * Generate an HTML row of data for the census 21952bc9faeSGreg Roach * Add prefix cell (store XREF and drag/drop) 22052bc9faeSGreg Roach * Add suffix cell (delete button) 22152bc9faeSGreg Roach * 222ad51e0bbSGreg Roach * @param CensusInterface $census 223ad51e0bbSGreg Roach * @param Individual $individual 224ad51e0bbSGreg Roach * @param Individual $head 225ad51e0bbSGreg Roach * 226ad51e0bbSGreg Roach * @return string 227ad51e0bbSGreg Roach */ 2288b9cfadbSGreg Roach public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string 229c1010edaSGreg Roach { 23015d603e7SGreg Roach $html = ''; 23115d603e7SGreg Roach foreach ($census->columns() as $column) { 232b6c326d8SGreg 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>'; 23399f222b3SGreg Roach } 23499f222b3SGreg Roach 235c0935879SGreg 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 class="icon-remove" href="#" title="' . I18N::translate('Remove') . '"></a></td></tr>'; 23699f222b3SGreg Roach } 2378c2e8227SGreg Roach} 238