xref: /webtrees/app/Module/CensusAssistantModule.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
22ad51e0bbSGreg Roachuse Fisharebest\Webtrees\Census\CensusInterface;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2499f222b3SGreg Roachuse Fisharebest\Webtrees\Individual;
2509482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
26*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298c2e8227SGreg Roach
303cc4e3a0SGreg Roachuse function array_keys;
313cc4e3a0SGreg Roachuse function count;
323cc4e3a0SGreg Roachuse function e;
33d8869271SGreg Roachuse function response;
343cc4e3a0SGreg Roachuse function str_repeat;
353cc4e3a0SGreg Roachuse function str_replace;
363cc4e3a0SGreg Roachuse function view;
374ea62551SGreg Roach
388c2e8227SGreg Roach/**
398c2e8227SGreg Roach * Class CensusAssistantModule
408c2e8227SGreg Roach */
415206405dSRico Sonntagclass CensusAssistantModule extends AbstractModule
42c1010edaSGreg Roach{
43961ec755SGreg Roach    /**
440cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
45961ec755SGreg Roach     *
46961ec755SGreg Roach     * @return string
47961ec755SGreg Roach     */
4849a243cbSGreg Roach    public function title(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Name of a module */
51bbb76c12SGreg Roach        return I18N::translate('Census assistant');
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
54961ec755SGreg Roach    /**
55961ec755SGreg Roach     * A sentence describing what this module does.
56961ec755SGreg Roach     *
57961ec755SGreg Roach     * @return string
58961ec755SGreg Roach     */
5949a243cbSGreg Roach    public function description(): string
60c1010edaSGreg Roach    {
61bbb76c12SGreg Roach        /* I18N: Description of the “Census assistant” module */
62bbb76c12SGreg Roach        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
6576692c8bSGreg Roach    /**
666ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
679001c0b3SGreg Roach     *
686ccdf4f0SGreg Roach     * @return ResponseInterface
699001c0b3SGreg Roach     */
707da59459SGreg Roach    public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface
71c1010edaSGreg Roach    {
72b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
73b46c87bdSGreg Roach
74b46c87bdSGreg Roach        $census = $params['census'];
759001c0b3SGreg Roach
7659f2f229SGreg Roach        $html = $this->censusTableHeader(new $census());
779001c0b3SGreg Roach
786ccdf4f0SGreg Roach        return response($html);
799001c0b3SGreg Roach    }
809001c0b3SGreg Roach
819001c0b3SGreg Roach    /**
826ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
839001c0b3SGreg Roach     *
846ccdf4f0SGreg Roach     * @return ResponseInterface
859001c0b3SGreg Roach     */
867da59459SGreg Roach    public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
87c1010edaSGreg Roach    {
88*b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
894ea62551SGreg Roach
90b46c87bdSGreg Roach        $params       = (array) $request->getParsedBody();
916b9cb339SGreg Roach        $individual   = Registry::individualFactory()->make($params['xref'], $tree);
926b9cb339SGreg Roach        $head         = Registry::individualFactory()->make($params['head'], $tree);
93d8869271SGreg Roach        $census_class = $params['census'];
94d8869271SGreg Roach        $census       = new $census_class();
95d45cb9d3SGreg Roach
961f244d77SGreg Roach        // No head of household?  Create a fake one.
976b9cb339SGreg Roach        $head = $head ?? Registry::individualFactory()->new('X', '0 @X@ INDI', null, $tree);
98dd1e545cSGreg Roach
99d5b0584dSGreg Roach        // Generate columns (e.g. relationship name) using the correct language.
100d5b0584dSGreg Roach        I18N::init($census->censusLanguage());
101d5b0584dSGreg Roach
102d45cb9d3SGreg Roach        if ($individual instanceof Individual && $head instanceof Individual) {
103d8869271SGreg Roach            $html = $this->censusTableRow($census, $individual, $head);
104d8869271SGreg Roach        } else {
105d8869271SGreg Roach            $html = $this->censusTableEmptyRow($census);
106d45cb9d3SGreg Roach        }
107e364afe4SGreg Roach
108d8869271SGreg Roach        return response($html);
1099001c0b3SGreg Roach    }
1109001c0b3SGreg Roach
1119001c0b3SGreg Roach    /**
11215d603e7SGreg Roach     * @param Individual $individual
1139001c0b3SGreg Roach     *
1149001c0b3SGreg Roach     * @return string
1158c2e8227SGreg Roach     */
1168f53f488SRico Sonntag    public function createCensusAssistant(Individual $individual): string
117c1010edaSGreg Roach    {
118225e381fSGreg Roach        return view('modules/census-assistant', [
11934cd602eSGreg Roach            'individual' => $individual,
12034cd602eSGreg Roach        ]);
12115d603e7SGreg Roach    }
12215d603e7SGreg Roach
12315d603e7SGreg Roach    /**
1246ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
12515d603e7SGreg Roach     * @param Individual             $individual
12660bc3e3fSGreg Roach     * @param string                 $fact_id
12715d603e7SGreg Roach     * @param string                 $newged
12860bc3e3fSGreg Roach     * @param bool                   $keep_chan
12915d603e7SGreg Roach     *
13015d603e7SGreg Roach     * @return string
13115d603e7SGreg Roach     */
1326ccdf4f0SGreg Roach    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
133c1010edaSGreg Roach    {
134b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
135abaef046SGreg Roach
136abaef046SGreg Roach        $ca_title       = $params['ca_title'] ?? '';
137abaef046SGreg Roach        $ca_place       = $params['ca_place'] ?? '';
138abaef046SGreg Roach        $ca_citation    = $params['ca_citation'] ?? '';
139abaef046SGreg Roach        $ca_individuals = $params['ca_individuals'] ?? [];
140abaef046SGreg Roach        $ca_notes       = $params['ca_notes'] ?? '';
141abaef046SGreg Roach        $ca_census      = $params['ca_census'] ?? '';
14215d603e7SGreg Roach
143a91af26aSGreg Roach        if ($ca_census !== '' && $ca_individuals !== []) {
14459f2f229SGreg Roach            $census = new $ca_census();
14515d603e7SGreg Roach
14615d603e7SGreg Roach            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
147afb591d7SGreg Roach            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
148f4afa648SGreg Roach            $note        = $individual->tree()->createRecord($note_gedcom);
14915d603e7SGreg Roach
150c0935879SGreg Roach            $newged .= "\n2 NOTE @" . $note->xref() . '@';
15115d603e7SGreg Roach
15215d603e7SGreg Roach            // Add the census fact to the rest of the household
1530483c93fSGreg Roach            foreach ($ca_individuals['xref'] ?? [] as $xref) {
1540483c93fSGreg Roach                if ($xref !== '' && $xref !== $individual->xref()) {
1556b9cb339SGreg Roach                    Registry::individualFactory()->make($xref, $individual->tree())
15615d603e7SGreg Roach                        ->updateFact($fact_id, $newged, !$keep_chan);
15715d603e7SGreg Roach                }
15815d603e7SGreg Roach            }
15915d603e7SGreg Roach        }
16015d603e7SGreg Roach
16115d603e7SGreg Roach        return $newged;
16215d603e7SGreg Roach    }
16315d603e7SGreg Roach
16415d603e7SGreg Roach    /**
16515d603e7SGreg Roach     * @param CensusInterface      $census
16615d603e7SGreg Roach     * @param string               $ca_title
16715d603e7SGreg Roach     * @param string               $ca_place
16815d603e7SGreg Roach     * @param string               $ca_citation
16909482a55SGreg Roach     * @param array<array<string>> $ca_individuals
17015d603e7SGreg Roach     * @param string               $ca_notes
17115d603e7SGreg Roach     *
17215d603e7SGreg Roach     * @return string
17315d603e7SGreg Roach     */
17424f2a3afSGreg Roach    private function createNoteText(CensusInterface $census, string $ca_title, string $ca_place, string $ca_citation, array $ca_individuals, string $ca_notes): string
175c1010edaSGreg Roach    {
176e380ca62SGreg Roach        $text = $ca_title;
177e380ca62SGreg Roach
178e380ca62SGreg Roach        if ($ca_citation !== '') {
179b6b785a1SGreg Roach            $text .= "\n" . $ca_citation;
180e380ca62SGreg Roach        }
181e380ca62SGreg Roach
182e380ca62SGreg Roach        if ($ca_place !== '') {
183b6b785a1SGreg Roach            $text .= "\n" . $ca_place;
184e380ca62SGreg Roach        }
185e380ca62SGreg Roach
186e380ca62SGreg Roach        $text .= "\n\n|";
18715d603e7SGreg Roach
1883cc4e3a0SGreg Roach        foreach ($census->columns() as $column) {
1893cc4e3a0SGreg Roach            $text .= ' ' . $column->abbreviation() . ' |';
1900d46ec71SGreg Roach        }
1910d46ec71SGreg Roach
1923cc4e3a0SGreg Roach        $text .= "\n|" . str_repeat(' ----- |', count($census->columns()));
19315d603e7SGreg Roach
1940483c93fSGreg Roach        foreach (array_keys($ca_individuals['xref'] ?? []) as $key) {
1953cc4e3a0SGreg Roach            $text .= "\n|";
1963cc4e3a0SGreg Roach
1970483c93fSGreg Roach            foreach ($census->columns() as $n => $column) {
1983cc4e3a0SGreg Roach                $text .= ' ' . $ca_individuals[$n][$key] . ' |';
1990483c93fSGreg Roach            }
20015d603e7SGreg Roach        }
20115d603e7SGreg Roach
202b6b785a1SGreg Roach        if ($ca_notes !== '') {
203b6b785a1SGreg Roach            $text .= "\n\n" . strtr($ca_notes, ["\r" => '']);
204b6b785a1SGreg Roach        }
205b6b785a1SGreg Roach
206b6b785a1SGreg Roach        return $text;
20740990b78SGreg Roach    }
20840990b78SGreg Roach
20940990b78SGreg Roach    /**
210ad51e0bbSGreg Roach     * Generate an HTML row of data for the census header
21152bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
21252bc9faeSGreg Roach     * Add suffix cell (delete button)
21352bc9faeSGreg Roach     *
214ad51e0bbSGreg Roach     * @param CensusInterface $census
21599f222b3SGreg Roach     *
216ad51e0bbSGreg Roach     * @return string
21799f222b3SGreg Roach     */
2185a62e0a6SGreg Roach    protected function censusTableHeader(CensusInterface $census): string
219c1010edaSGreg Roach    {
22052bc9faeSGreg Roach        $html = '';
221ad51e0bbSGreg Roach        foreach ($census->columns() as $column) {
22215d603e7SGreg Roach            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
22399f222b3SGreg Roach        }
22499f222b3SGreg Roach
22515d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
226ad51e0bbSGreg Roach    }
22799f222b3SGreg Roach
228ad51e0bbSGreg Roach    /**
229ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
23052bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
23152bc9faeSGreg Roach     * Add suffix cell (delete button)
23252bc9faeSGreg Roach     *
233e5a6b4d4SGreg Roach     * @param CensusInterface $census
234ad51e0bbSGreg Roach     *
235ad51e0bbSGreg Roach     * @return string
236ad51e0bbSGreg Roach     */
2378b9cfadbSGreg Roach    public function censusTableEmptyRow(CensusInterface $census): string
238c1010edaSGreg Roach    {
2390483c93fSGreg Roach        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>';
2400483c93fSGreg Roach
2410483c93fSGreg Roach        foreach ($census->columns() as $n => $column) {
2420483c93fSGreg Roach            $html .= '<td class="wt-census-assistant-field p-0"><input class="form-control wt-census-assistant-form-control p-0" type="text" name="ca_individuals[' . $n . '][]"></td>';
2430483c93fSGreg Roach        }
2440483c93fSGreg Roach
2450483c93fSGreg Roach        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
2460483c93fSGreg Roach
2470483c93fSGreg Roach        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
248ad51e0bbSGreg Roach    }
24999f222b3SGreg Roach
250ad51e0bbSGreg Roach    /**
251ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
25252bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
25352bc9faeSGreg Roach     * Add suffix cell (delete button)
25452bc9faeSGreg Roach     *
255ad51e0bbSGreg Roach     * @param CensusInterface $census
256ad51e0bbSGreg Roach     * @param Individual      $individual
257ad51e0bbSGreg Roach     * @param Individual      $head
258ad51e0bbSGreg Roach     *
259ad51e0bbSGreg Roach     * @return string
260ad51e0bbSGreg Roach     */
2618b9cfadbSGreg Roach    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
262c1010edaSGreg Roach    {
2630483c93fSGreg Roach        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>';
2640483c93fSGreg Roach
2650483c93fSGreg Roach        foreach ($census->columns() as $n => $column) {
2660483c93fSGreg Roach            $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[' . $n . '][]"></td>';
26799f222b3SGreg Roach        }
26899f222b3SGreg Roach
2690483c93fSGreg Roach        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
2700483c93fSGreg Roach
2710483c93fSGreg Roach        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
27299f222b3SGreg Roach    }
2738c2e8227SGreg Roach}
274