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