1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Census\CensusInterface; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Registry; 26use Fisharebest\Webtrees\Validator; 27use Psr\Http\Message\ResponseInterface; 28use Psr\Http\Message\ServerRequestInterface; 29 30use function array_keys; 31use function count; 32use function e; 33use function response; 34use function str_repeat; 35use function str_replace; 36use function view; 37 38/** 39 * Class CensusAssistantModule 40 */ 41class CensusAssistantModule extends AbstractModule 42{ 43 /** 44 * How should this module be identified in the control panel, etc.? 45 * 46 * @return string 47 */ 48 public function title(): string 49 { 50 /* I18N: Name of a module */ 51 return I18N::translate('Census assistant'); 52 } 53 54 /** 55 * A sentence describing what this module does. 56 * 57 * @return string 58 */ 59 public function description(): string 60 { 61 /* I18N: Description of the “Census assistant” module */ 62 return I18N::translate('An alternative way to enter census transcripts and link them to individuals.'); 63 } 64 65 /** 66 * @param ServerRequestInterface $request 67 * 68 * @return ResponseInterface 69 */ 70 public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface 71 { 72 $params = (array) $request->getParsedBody(); 73 74 $census = $params['census']; 75 76 $html = $this->censusTableHeader(new $census()); 77 78 return response($html); 79 } 80 81 /** 82 * @param ServerRequestInterface $request 83 * 84 * @return ResponseInterface 85 */ 86 public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface 87 { 88 $tree = Validator::attributes($request)->tree(); 89 90 $params = (array) $request->getParsedBody(); 91 $individual = Registry::individualFactory()->make($params['xref'], $tree); 92 $head = Registry::individualFactory()->make($params['head'], $tree); 93 $census_class = $params['census']; 94 $census = new $census_class(); 95 96 // No head of household? Create a fake one. 97 $head = $head ?? Registry::individualFactory()->new('X', '0 @X@ INDI', null, $tree); 98 99 // Generate columns (e.g. relationship name) using the correct language. 100 I18N::init($census->censusLanguage()); 101 102 if ($individual instanceof Individual && $head instanceof Individual) { 103 $html = $this->censusTableRow($census, $individual, $head); 104 } else { 105 $html = $this->censusTableEmptyRow($census); 106 } 107 108 return response($html); 109 } 110 111 /** 112 * @param Individual $individual 113 * 114 * @return string 115 */ 116 public function createCensusAssistant(Individual $individual): string 117 { 118 return view('modules/census-assistant', [ 119 'individual' => $individual, 120 ]); 121 } 122 123 /** 124 * @param ServerRequestInterface $request 125 * @param Individual $individual 126 * @param string $fact_id 127 * @param string $newged 128 * @param bool $keep_chan 129 * 130 * @return string 131 */ 132 public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string 133 { 134 $params = (array) $request->getParsedBody(); 135 136 $ca_title = $params['ca_title'] ?? ''; 137 $ca_place = $params['ca_place'] ?? ''; 138 $ca_citation = $params['ca_citation'] ?? ''; 139 $ca_individuals = $params['ca_individuals'] ?? []; 140 $ca_notes = $params['ca_notes'] ?? ''; 141 $ca_census = $params['ca_census'] ?? ''; 142 143 if ($ca_census !== '' && $ca_individuals !== []) { 144 $census = new $ca_census(); 145 146 $note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); 147 $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); 148 $note = $individual->tree()->createRecord($note_gedcom); 149 150 $newged .= "\n2 NOTE @" . $note->xref() . '@'; 151 152 // Add the census fact to the rest of the household 153 foreach ($ca_individuals['xref'] ?? [] as $xref) { 154 if ($xref !== '' && $xref !== $individual->xref()) { 155 Registry::individualFactory()->make($xref, $individual->tree()) 156 ->updateFact($fact_id, $newged, !$keep_chan); 157 } 158 } 159 } 160 161 return $newged; 162 } 163 164 /** 165 * @param CensusInterface $census 166 * @param string $ca_title 167 * @param string $ca_place 168 * @param string $ca_citation 169 * @param array<array<string>> $ca_individuals 170 * @param string $ca_notes 171 * 172 * @return string 173 */ 174 private function createNoteText(CensusInterface $census, string $ca_title, string $ca_place, string $ca_citation, array $ca_individuals, string $ca_notes): string 175 { 176 $text = $ca_title; 177 178 if ($ca_citation !== '') { 179 $text .= "\n" . $ca_citation; 180 } 181 182 if ($ca_place !== '') { 183 $text .= "\n" . $ca_place; 184 } 185 186 $text .= "\n\n|"; 187 188 foreach ($census->columns() as $column) { 189 $text .= ' ' . $column->abbreviation() . ' |'; 190 } 191 192 $text .= "\n|" . str_repeat(' ----- |', count($census->columns())); 193 194 foreach (array_keys($ca_individuals['xref'] ?? []) as $key) { 195 $text .= "\n|"; 196 197 foreach ($census->columns() as $n => $column) { 198 $text .= ' ' . $ca_individuals[$n][$key] . ' |'; 199 } 200 } 201 202 if ($ca_notes !== '') { 203 $text .= "\n\n" . strtr($ca_notes, ["\r" => '']); 204 } 205 206 return $text; 207 } 208 209 /** 210 * Generate an HTML row of data for the census header 211 * Add prefix cell (store XREF and drag/drop) 212 * Add suffix cell (delete button) 213 * 214 * @param CensusInterface $census 215 * 216 * @return string 217 */ 218 protected function censusTableHeader(CensusInterface $census): string 219 { 220 $html = ''; 221 foreach ($census->columns() as $column) { 222 $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>'; 223 } 224 225 return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>'; 226 } 227 228 /** 229 * Generate an HTML row of data for the census 230 * Add prefix cell (store XREF and drag/drop) 231 * Add suffix cell (delete button) 232 * 233 * @param CensusInterface $census 234 * 235 * @return string 236 */ 237 public function censusTableEmptyRow(CensusInterface $census): string 238 { 239 $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>'; 240 241 foreach ($census->columns() as $n => $column) { 242 $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>'; 243 } 244 245 $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>'; 246 247 return '<tr class="wt-census-assistant-row">' . $html . '</tr>'; 248 } 249 250 /** 251 * Generate an HTML row of data for the census 252 * Add prefix cell (store XREF and drag/drop) 253 * Add suffix cell (delete button) 254 * 255 * @param CensusInterface $census 256 * @param Individual $individual 257 * @param Individual $head 258 * 259 * @return string 260 */ 261 public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string 262 { 263 $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>'; 264 265 foreach ($census->columns() as $n => $column) { 266 $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>'; 267 } 268 269 $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>'; 270 271 return '<tr class="wt-census-assistant-row">' . $html . '</tr>'; 272 } 273} 274