. */ namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Census\Census; use Fisharebest\Webtrees\Census\CensusInterface; use Fisharebest\Webtrees\Family; use Fisharebest\Webtrees\Filter; use Fisharebest\Webtrees\FontAwesome; use Fisharebest\Webtrees\Functions\FunctionsDb; use Fisharebest\Webtrees\Functions\FunctionsEdit; use Fisharebest\Webtrees\GedcomRecord; use Fisharebest\Webtrees\Html; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Note; use Fisharebest\Webtrees\Soundex; use Fisharebest\Webtrees\View; /** * 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.'); } /** * This is a general purpose hook, allowing modules to respond to routes * of the form module.php?mod=FOO&mod_action=BAR * * @param string $mod_action */ public function modAction($mod_action) { global $WT_TREE; switch ($mod_action) { case 'census-header': header('Content-Type: text/html; charset=utf8'); $census = Filter::get('census'); echo $this->censusTableHeader(new $census); break; case 'census-individual': header('Content-Type: text/html; charset=utf8'); $census = Filter::get('census'); $individual = Individual::getInstance(Filter::get('xref'), $WT_TREE); $head = Individual::getInstance(Filter::get('head'), $WT_TREE); echo $this->censusTableRow(new $census, $individual, $head); break; case 'media_find': self::mediaFind(); break; case 'media_query_3a': self::mediaQuery(); break; default: http_response_code(404); } } /** * @param Individual $individual */ public function createCensusAssistant(Individual $individual) { return View::make('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; } /** * Find a media object. */ private static function mediaFind() { global $WT_TREE; $controller = new SimpleController; $filter = Filter::get('filter'); $multiple = Filter::getBool('multiple'); $controller ->setPageTitle(I18N::translate('Find an individual')) ->pageHeader(); ?> '; echo ''; echo ''; echo ''; echo '
'; // start column for find text header echo $controller->getPageTitle(); echo '
'; echo '
'; echo ''; echo '
'; $filter = trim($filter); $filter_array = explode(' ', preg_replace('/ {2,}/', ' ', $filter)); echo ''; $myindilist = FunctionsDb::searchIndividualNames($filter_array, [$WT_TREE]); if ($myindilist) { echo ''; } else { echo ''; } echo '
', I18N::translate('Total individuals: %s', count($myindilist)), '
'; echo I18N::translate('No results found.'); echo '
'; echo ''; } /** * Search for a media object. */ private static function mediaQuery() { global $WT_TREE; $iid2 = Filter::get('iid', WT_REGEX_XREF); $controller = new SimpleController; $controller ->setPageTitle(I18N::translate('Link to an existing media object')) ->pageHeader(); $record = GedcomRecord::getInstance($iid2, $WT_TREE); if ($record) { $headjs = ''; if ($record instanceof Family) { if ($record->getHusband()) { $headjs = $record->getHusband()->getXref(); } elseif ($record->getWife()) { $headjs = $record->getWife()->getXref(); } } ?> getNote(), $match)) { // This looks like a census-assistant shared note $title = Html::escape($match[1]); $preamble = Html::escape($match[2]); $header = Html::escape($match[3]); $data = Html::escape($match[4]); $postamble = Html::escape($match[5]); // Get the column headers for the census to which this note refers // requires the fact place & date to match the specific census // censusPlace() (Soundex match) and censusDate() functions $fmt_headers = []; /** @var GedcomRecord[] $linkedRecords */ $linkedRecords = array_merge($note->linkedIndividuals('NOTE'), $note->linkedFamilies('NOTE')); $firstRecord = array_shift($linkedRecords); if ($firstRecord) { $countryCode = ''; $date = ''; foreach ($firstRecord->getFacts('CENS') as $fact) { if (trim($fact->getAttribute('NOTE'), '@') === $note->getXref()) { $date = $fact->getAttribute('DATE'); $place = explode(',', strip_tags($fact->getPlace()->getFullName())); $countryCode = Soundex::daitchMokotoff(array_pop($place)); break; } } foreach (Census::allCensusPlaces() as $censusPlace) { if (Soundex::compare($countryCode, Soundex::daitchMokotoff($censusPlace->censusPlace()))) { foreach ($censusPlace->allCensusDates() as $census) { if ($census->censusDate() == $date) { foreach ($census->columns() as $column) { $abbrev = $column->abbreviation(); if ($abbrev) { $description = $column->title() ? $column->title() : I18N::translate('Description unavailable'); $fmt_headers[$abbrev] = '' . $abbrev . ''; } } break 2; } } } } } // Substitute header labels and format as HTML $thead = '' . strtr(str_replace('|', '', $header), $fmt_headers) . ''; $thead = str_replace('.b.', '', $thead); // Format data as HTML $tbody = ''; foreach (explode("\n", $data) as $row) { $tbody .= ''; foreach (explode('|', $row) as $column) { $tbody .= '' . $column . ''; } $tbody .= ''; } return $title . "\n" . // The newline allows the framework to expand the details and turn the first line into a link '
' . '

' . $preamble . '

' . '' . '' . $thead . '' . '' . $tbody . '' . '
' . '

' . $postamble . '

' . '
'; } else { // Not a census-assistant shared note - apply default formatting return Filter::formatText($note->getNote(), $note->getTree()); } } /** * 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 . ''; } }