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