. */ namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Census\CensusInterface; use Fisharebest\Webtrees\Filter; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Tree; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** * Class CensusAssistantModule */ class CensusAssistantModule extends AbstractModule { /** {@inheritdoc} */ public function getTitle() { return /* I18N: Name of a module */ I18N::translate('Census assistant'); } /** {@inheritdoc} */ public function getDescription() { return /* I18N: Description of the “Census assistant” module */ I18N::translate('An alternative way to enter census transcripts and link them to individuals.'); } /** * @param Request $request * * @return Response */ public function getCensusHeaderAction(Request $request): Response { $census = $request->get('census'); $html = $this->censusTableHeader(new $census); return new Response($html); } /** * @param Request $request * * @return Response */ public function getCensusIndividualAction(Request $request): Response { /** @var Tree $tree */ $tree = $request->attributes->get('tree'); $census = $request->get('census'); $individual = Individual::getInstance($request->get('xref'), $tree); $head = Individual::getInstance($request->get('head'), $tree); $html = $this->censusTableRow(new $census, $individual, $head); return new Response($html); } /** * @param Individual $individual * * @return string */ public function createCensusAssistant(Individual $individual) { return view('modules/census-assistant', [ 'individual' => $individual, ]); } /** * @param Individual $individual * @param string $fact_id * @param string $newged * @param bool $keep_chan * * @return string */ public function updateCensusAssistant(Individual $individual, $fact_id, $newged, $keep_chan) { $ca_title = Filter::post('ca_title'); $ca_place = Filter::post('ca_place'); $ca_citation = Filter::post('ca_citation'); $ca_individuals = Filter::postArray('ca_individuals'); $ca_notes = Filter::post('ca_notes'); $ca_census = Filter::post('ca_census', 'Fisharebest\\\\Webtrees\\\\Census\\\\CensusOf[A-Za-z0-9]+'); if ($ca_census !== '' && !empty($ca_individuals)) { $census = new $ca_census; $note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); $note_gedcom = '0 @new@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); $note = $individual->getTree()->createRecord($note_gedcom); $newged .= "\n2 NOTE @" . $note->getXref() . '@'; // Add the census fact to the rest of the household foreach (array_keys($ca_individuals) as $xref) { if ($xref !== $individual->getXref()) { Individual::getInstance($xref, $individual->getTree()) ->updateFact($fact_id, $newged, !$keep_chan); } } } return $newged; } /** * @param CensusInterface $census * @param string $ca_title * @param string $ca_place * @param string $ca_citation * @param string[][] $ca_individuals * @param string $ca_notes * * @return string */ private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes) { $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n"; foreach ($census->columns() as $n => $column) { if ($n === 0) { $text .= "\n"; } else { $text .= ' | '; } $text .= $column->abbreviation(); } foreach ($census->columns() as $n => $column) { if ($n === 0) { $text .= "\n"; } else { $text .= ' | '; } $text .= '-----'; } foreach ($ca_individuals as $xref => $columns) { $text .= "\n" . implode(' | ', $columns); } return $text . "\n\n" . $ca_notes; } /** * Generate an HTML row of data for the census header * Add prefix cell (store XREF and drag/drop) * Add suffix cell (delete button) * * @param CensusInterface $census * * @return string */ public static function censusTableHeader(CensusInterface $census) { $html = ''; foreach ($census->columns() as $column) { $html .= '' . $column->abbreviation() . ''; } return '' . $html . ''; } /** * Generate an HTML row of data for the census * Add prefix cell (store XREF and drag/drop) * Add suffix cell (delete button) * * @param CensusInterface $census * * @return string */ public static function censusTableEmptyRow(CensusInterface $census) { return '' . str_repeat('', count($census->columns())) . ''; } /** * Generate an HTML row of data for the census * Add prefix cell (store XREF and drag/drop) * Add suffix cell (delete button) * * @param CensusInterface $census * @param Individual $individual * @param Individual $head * * @return string */ public static function censusTableRow(CensusInterface $census, Individual $individual, Individual $head) { $html = ''; foreach ($census->columns() as $column) { $html .= ''; } return '' . $individual->getXref() . '' . $html . ''; } }