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