1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://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 Psr\Http\Message\ResponseInterface; 26use Psr\Http\Message\ServerRequestInterface; 27use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 28 29/** 30 * Class CensusAssistantModule 31 */ 32class CensusAssistantModule extends AbstractModule 33{ 34 /** 35 * How should this module be identified in the control panel, etc.? 36 * 37 * @return string 38 */ 39 public function title(): string 40 { 41 /* I18N: Name of a module */ 42 return I18N::translate('Census assistant'); 43 } 44 45 /** 46 * A sentence describing what this module does. 47 * 48 * @return string 49 */ 50 public function description(): string 51 { 52 /* I18N: Description of the “Census assistant” module */ 53 return I18N::translate('An alternative way to enter census transcripts and link them to individuals.'); 54 } 55 56 /** 57 * @param ServerRequestInterface $request 58 * 59 * @return ResponseInterface 60 */ 61 public function getCensusHeaderAction(ServerRequestInterface $request): ResponseInterface 62 { 63 $census = $request->getQueryParams()['census']; 64 65 $html = $this->censusTableHeader(new $census()); 66 67 return response($html); 68 } 69 70 /** 71 * @param ServerRequestInterface $request 72 * 73 * @return ResponseInterface 74 */ 75 public function getCensusIndividualAction(ServerRequestInterface $request): ResponseInterface 76 { 77 $tree = $request->getAttribute('tree'); 78 $params = $request->getQueryParams(); 79 $individual = Individual::getInstance($params['xref'], $tree); 80 $head = Individual::getInstance($params['head'], $tree); 81 $census = $params['census']; 82 83 if ($individual instanceof Individual && $head instanceof Individual) { 84 $html = $this->censusTableRow(new $census(), $individual, $head); 85 86 return response($html); 87 } 88 89 throw new NotFoundHttpException(); 90 } 91 92 /** 93 * @param Individual $individual 94 * 95 * @return string 96 */ 97 public function createCensusAssistant(Individual $individual): string 98 { 99 return view('modules/census-assistant', [ 100 'individual' => $individual, 101 ]); 102 } 103 104 /** 105 * @param ServerRequestInterface $request 106 * @param Individual $individual 107 * @param string $fact_id 108 * @param string $newged 109 * @param bool $keep_chan 110 * 111 * @return string 112 */ 113 public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string 114 { 115 $params = $request->getParsedBody(); 116 117 $ca_title = $params['ca_title'] ?? ''; 118 $ca_place = $params['ca_place'] ?? ''; 119 $ca_citation = $params['ca_citation'] ?? ''; 120 $ca_individuals = $params['ca_individuals'] ?? []; 121 $ca_notes = $params['ca_notes'] ?? ''; 122 $ca_census = $params['ca_census'] ?? ''; 123 124 if ($ca_census !== '' && $ca_individuals !== []) { 125 $census = new $ca_census(); 126 127 $note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); 128 $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); 129 $note = $individual->tree()->createRecord($note_gedcom); 130 131 $newged .= "\n2 NOTE @" . $note->xref() . '@'; 132 133 // Add the census fact to the rest of the household 134 foreach (array_keys($ca_individuals) as $xref) { 135 if ($xref !== $individual->xref()) { 136 Individual::getInstance($xref, $individual->tree()) 137 ->updateFact($fact_id, $newged, !$keep_chan); 138 } 139 } 140 } 141 142 return $newged; 143 } 144 145 /** 146 * @param CensusInterface $census 147 * @param string $ca_title 148 * @param string $ca_place 149 * @param string $ca_citation 150 * @param string[][] $ca_individuals 151 * @param string $ca_notes 152 * 153 * @return string 154 */ 155 private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string 156 { 157 $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n"; 158 159 foreach ($census->columns() as $n => $column) { 160 if ($n === 0) { 161 $text .= "\n"; 162 } else { 163 $text .= ' | '; 164 } 165 $text .= $column->abbreviation(); 166 } 167 168 foreach ($census->columns() as $n => $column) { 169 if ($n === 0) { 170 $text .= "\n"; 171 } else { 172 $text .= ' | '; 173 } 174 $text .= '-----'; 175 } 176 177 foreach ($ca_individuals as $xref => $columns) { 178 $text .= "\n" . implode(' | ', $columns); 179 } 180 181 return $text . "\n\n" . $ca_notes; 182 } 183 184 /** 185 * Generate an HTML row of data for the census header 186 * Add prefix cell (store XREF and drag/drop) 187 * Add suffix cell (delete button) 188 * 189 * @param CensusInterface $census 190 * 191 * @return string 192 */ 193 protected function censusTableHeader(CensusInterface $census): string 194 { 195 $html = ''; 196 foreach ($census->columns() as $column) { 197 $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>'; 198 } 199 200 return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>'; 201 } 202 203 /** 204 * Generate an HTML row of data for the census 205 * Add prefix cell (store XREF and drag/drop) 206 * Add suffix cell (delete button) 207 * 208 * @param CensusInterface $census 209 * 210 * @return string 211 */ 212 public function censusTableEmptyRow(CensusInterface $census): string 213 { 214 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>'; 215 } 216 217 /** 218 * Generate an HTML row of data for the census 219 * Add prefix cell (store XREF and drag/drop) 220 * Add suffix cell (delete button) 221 * 222 * @param CensusInterface $census 223 * @param Individual $individual 224 * @param Individual $head 225 * 226 * @return string 227 */ 228 public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string 229 { 230 $html = ''; 231 foreach ($census->columns() as $column) { 232 $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>'; 233 } 234 235 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>'; 236 } 237} 238