1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18 19namespace Fisharebest\Webtrees\Module; 20 21use Fisharebest\Webtrees\Census\CensusInterface; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Individual; 24use Fisharebest\Webtrees\Tree; 25use Symfony\Component\HttpFoundation\Request; 26use Symfony\Component\HttpFoundation\Response; 27use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 28 29/** 30 * Class CensusAssistantModule 31 */ 32class CensusAssistantModule extends AbstractModule implements ModuleInterface 33{ 34 /** {@inheritdoc} */ 35 public function title(): string 36 { 37 /* I18N: Name of a module */ 38 return I18N::translate('Census assistant'); 39 } 40 41 /** {@inheritdoc} */ 42 public function description(): string 43 { 44 /* I18N: Description of the “Census assistant” module */ 45 return I18N::translate('An alternative way to enter census transcripts and link them to individuals.'); 46 } 47 48 /** 49 * @param Request $request 50 * 51 * @return Response 52 */ 53 public function getCensusHeaderAction(Request $request): Response 54 { 55 $census = $request->get('census'); 56 57 $html = $this->censusTableHeader(new $census()); 58 59 return new Response($html); 60 } 61 62 /** 63 * @param Request $request 64 * @param Tree $tree 65 * 66 * @return Response 67 */ 68 public function getCensusIndividualAction(Request $request, Tree $tree): Response 69 { 70 $census = $request->get('census', ''); 71 72 $individual = Individual::getInstance($request->get('xref', ''), $tree); 73 $head = Individual::getInstance($request->get('head', ''), $tree); 74 75 if ($individual instanceof Individual && $head instanceof Individual) { 76 $html = $this->censusTableRow(new $census(), $individual, $head); 77 78 return new Response($html); 79 } else { 80 throw new NotFoundHttpException(); 81 } 82 } 83 84 /** 85 * @param Individual $individual 86 * 87 * @return string 88 */ 89 public function createCensusAssistant(Individual $individual): string 90 { 91 return view('modules/census-assistant', [ 92 'individual' => $individual, 93 ]); 94 } 95 96 /** 97 * @param Request $request 98 * @param Individual $individual 99 * @param string $fact_id 100 * @param string $newged 101 * @param bool $keep_chan 102 * 103 * @return string 104 */ 105 public function updateCensusAssistant(Request $request, Individual $individual, $fact_id, $newged, $keep_chan): string 106 { 107 $ca_title = $request->get('ca_title', ''); 108 $ca_place = $request->get('ca_place', ''); 109 $ca_citation = $request->get('ca_citation', ''); 110 $ca_individuals = (array) $request->get('ca_individuals'); 111 $ca_notes = $request->get('ca_notes', ''); 112 $ca_census = $request->get('ca_census', ''); 113 114 if ($ca_census !== '' && !empty($ca_individuals)) { 115 $census = new $ca_census(); 116 117 $note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); 118 $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); 119 $note = $individual->tree()->createRecord($note_gedcom); 120 121 $newged .= "\n2 NOTE @" . $note->xref() . '@'; 122 123 // Add the census fact to the rest of the household 124 foreach (array_keys($ca_individuals) as $xref) { 125 if ($xref !== $individual->xref()) { 126 Individual::getInstance($xref, $individual->tree()) 127 ->updateFact($fact_id, $newged, !$keep_chan); 128 } 129 } 130 } 131 132 return $newged; 133 } 134 135 /** 136 * @param CensusInterface $census 137 * @param string $ca_title 138 * @param string $ca_place 139 * @param string $ca_citation 140 * @param string[][] $ca_individuals 141 * @param string $ca_notes 142 * 143 * @return string 144 */ 145 private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string 146 { 147 $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n"; 148 149 foreach ($census->columns() as $n => $column) { 150 if ($n === 0) { 151 $text .= "\n"; 152 } else { 153 $text .= ' | '; 154 } 155 $text .= $column->abbreviation(); 156 } 157 158 foreach ($census->columns() as $n => $column) { 159 if ($n === 0) { 160 $text .= "\n"; 161 } else { 162 $text .= ' | '; 163 } 164 $text .= '-----'; 165 } 166 167 foreach ($ca_individuals as $xref => $columns) { 168 $text .= "\n" . implode(' | ', $columns); 169 } 170 171 return $text . "\n\n" . $ca_notes; 172 } 173 174 /** 175 * Generate an HTML row of data for the census header 176 * Add prefix cell (store XREF and drag/drop) 177 * Add suffix cell (delete button) 178 * 179 * @param CensusInterface $census 180 * 181 * @return string 182 */ 183 protected function censusTableHeader(CensusInterface $census): string 184 { 185 $html = ''; 186 foreach ($census->columns() as $column) { 187 $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>'; 188 } 189 190 return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>'; 191 } 192 193 /** 194 * Generate an HTML row of data for the census 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 public static function censusTableEmptyRow(CensusInterface $census): string 203 { 204 return '<tr class="wt-census-assistant-row"><td hidden></td>' . str_repeat('<td class="wt-census-assistant-field"><input type="text" class="form-control wt-census-assistant-form-control"></td>', count($census->columns())) . '<td><a class="icon-remove" href="#" title="' . I18N::translate('Remove') . '"></a></td></tr>'; 205 } 206 207 /** 208 * Generate an HTML row of data for the census 209 * Add prefix cell (store XREF and drag/drop) 210 * Add suffix cell (delete button) 211 * 212 * @param CensusInterface $census 213 * @param Individual $individual 214 * @param Individual $head 215 * 216 * @return string 217 */ 218 public static function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string 219 { 220 $html = ''; 221 foreach ($census->columns() as $column) { 222 $html .= '<td class="wt-census-assistant-field"><input class="form-control wt-census-assistant-form-control" type="text" value="' . $column->generate($individual, $head) . '" name="ca_individuals[' . $individual->xref() . '][]"></td>'; 223 } 224 225 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>'; 226 } 227} 228