xref: /webtrees/app/Module/CensusAssistantModule.php (revision 1f244d772ed55c1ea8eecba8f9ca36a77f054c34)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5a091ac74SGreg Roach * Copyright (C) 2020 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
158c2e8227SGreg Roach * along with this program. If not, see <http://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;
23a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2599f222b3SGreg Roachuse Fisharebest\Webtrees\Individual;
264ea62551SGreg Roachuse Fisharebest\Webtrees\Tree;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298c2e8227SGreg Roach
303cc4e3a0SGreg Roachuse function array_keys;
314ea62551SGreg Roachuse function assert;
323cc4e3a0SGreg Roachuse function count;
333cc4e3a0SGreg Roachuse function e;
34d8869271SGreg Roachuse function response;
353cc4e3a0SGreg Roachuse function str_repeat;
363cc4e3a0SGreg Roachuse function str_replace;
373cc4e3a0SGreg Roachuse function view;
384ea62551SGreg Roach
398c2e8227SGreg Roach/**
408c2e8227SGreg Roach * Class CensusAssistantModule
418c2e8227SGreg Roach */
425206405dSRico Sonntagclass CensusAssistantModule extends AbstractModule
43c1010edaSGreg Roach{
44961ec755SGreg Roach    /**
450cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
46961ec755SGreg Roach     *
47961ec755SGreg Roach     * @return string
48961ec755SGreg Roach     */
4949a243cbSGreg Roach    public function title(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Name of a module */
52bbb76c12SGreg Roach        return I18N::translate('Census assistant');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
55961ec755SGreg Roach    /**
56961ec755SGreg Roach     * A sentence describing what this module does.
57961ec755SGreg Roach     *
58961ec755SGreg Roach     * @return string
59961ec755SGreg Roach     */
6049a243cbSGreg Roach    public function description(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of the “Census assistant” module */
63bbb76c12SGreg Roach        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
6676692c8bSGreg Roach    /**
676ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
689001c0b3SGreg Roach     *
696ccdf4f0SGreg Roach     * @return ResponseInterface
709001c0b3SGreg Roach     */
717da59459SGreg Roach    public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface
72c1010edaSGreg Roach    {
73b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
74b46c87bdSGreg Roach
75b46c87bdSGreg Roach        $census = $params['census'];
769001c0b3SGreg Roach
7759f2f229SGreg Roach        $html = $this->censusTableHeader(new $census());
789001c0b3SGreg Roach
796ccdf4f0SGreg Roach        return response($html);
809001c0b3SGreg Roach    }
819001c0b3SGreg Roach
829001c0b3SGreg Roach    /**
836ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
849001c0b3SGreg Roach     *
856ccdf4f0SGreg Roach     * @return ResponseInterface
869001c0b3SGreg Roach     */
877da59459SGreg Roach    public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
88c1010edaSGreg Roach    {
8957ab2231SGreg Roach        $tree = $request->getAttribute('tree');
904ea62551SGreg Roach        assert($tree instanceof Tree);
914ea62551SGreg Roach
92b46c87bdSGreg Roach        $params       = (array) $request->getParsedBody();
93a091ac74SGreg Roach        $individual   = Factory::individual()->make($params['xref'], $tree);
94a091ac74SGreg Roach        $head         = Factory::individual()->make($params['head'], $tree);
95d8869271SGreg Roach        $census_class = $params['census'];
96d8869271SGreg Roach        $census       = new $census_class();
97d45cb9d3SGreg Roach
98*1f244d77SGreg Roach        // No head of household?  Create a fake one.
99a091ac74SGreg Roach        $head = $head ?? Factory::individual()->new('X', '0 @X@ INDI', null, $tree);
100dd1e545cSGreg Roach
101d5b0584dSGreg Roach        // Generate columns (e.g. relationship name) using the correct language.
102d5b0584dSGreg Roach        I18N::init($census->censusLanguage());
103d5b0584dSGreg Roach
104d45cb9d3SGreg Roach        if ($individual instanceof Individual && $head instanceof Individual) {
105d8869271SGreg Roach            $html = $this->censusTableRow($census, $individual, $head);
106d8869271SGreg Roach        } else {
107d8869271SGreg Roach            $html = $this->censusTableEmptyRow($census);
108d45cb9d3SGreg Roach        }
109e364afe4SGreg Roach
110d8869271SGreg Roach        return response($html);
1119001c0b3SGreg Roach    }
1129001c0b3SGreg Roach
1139001c0b3SGreg Roach    /**
11415d603e7SGreg Roach     * @param Individual $individual
1159001c0b3SGreg Roach     *
1169001c0b3SGreg Roach     * @return string
1178c2e8227SGreg Roach     */
1188f53f488SRico Sonntag    public function createCensusAssistant(Individual $individual): string
119c1010edaSGreg Roach    {
120225e381fSGreg Roach        return view('modules/census-assistant', [
12134cd602eSGreg Roach            'individual' => $individual,
12234cd602eSGreg Roach        ]);
12315d603e7SGreg Roach    }
12415d603e7SGreg Roach
12515d603e7SGreg Roach    /**
1266ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
12715d603e7SGreg Roach     * @param Individual             $individual
12860bc3e3fSGreg Roach     * @param string                 $fact_id
12915d603e7SGreg Roach     * @param string                 $newged
13060bc3e3fSGreg Roach     * @param bool                   $keep_chan
13115d603e7SGreg Roach     *
13215d603e7SGreg Roach     * @return string
13315d603e7SGreg Roach     */
1346ccdf4f0SGreg Roach    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
135c1010edaSGreg Roach    {
136b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
137abaef046SGreg Roach
138abaef046SGreg Roach        $ca_title       = $params['ca_title'] ?? '';
139abaef046SGreg Roach        $ca_place       = $params['ca_place'] ?? '';
140abaef046SGreg Roach        $ca_citation    = $params['ca_citation'] ?? '';
141abaef046SGreg Roach        $ca_individuals = $params['ca_individuals'] ?? [];
142abaef046SGreg Roach        $ca_notes       = $params['ca_notes'] ?? '';
143abaef046SGreg Roach        $ca_census      = $params['ca_census'] ?? '';
14415d603e7SGreg Roach
145a91af26aSGreg Roach        if ($ca_census !== '' && $ca_individuals !== []) {
14659f2f229SGreg Roach            $census = new $ca_census();
14715d603e7SGreg Roach
14815d603e7SGreg Roach            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
149afb591d7SGreg Roach            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
150f4afa648SGreg Roach            $note        = $individual->tree()->createRecord($note_gedcom);
15115d603e7SGreg Roach
152c0935879SGreg Roach            $newged .= "\n2 NOTE @" . $note->xref() . '@';
15315d603e7SGreg Roach
15415d603e7SGreg Roach            // Add the census fact to the rest of the household
1550483c93fSGreg Roach            foreach ($ca_individuals['xref'] ?? [] as $xref) {
1560483c93fSGreg Roach                if ($xref !== '' && $xref !== $individual->xref()) {
157a091ac74SGreg Roach                    Factory::individual()->make($xref, $individual->tree())
15815d603e7SGreg Roach                        ->updateFact($fact_id, $newged, !$keep_chan);
15915d603e7SGreg Roach                }
16015d603e7SGreg Roach            }
16115d603e7SGreg Roach        }
16215d603e7SGreg Roach
16315d603e7SGreg Roach        return $newged;
16415d603e7SGreg Roach    }
16515d603e7SGreg Roach
16615d603e7SGreg Roach    /**
16715d603e7SGreg Roach     * @param CensusInterface $census
16815d603e7SGreg Roach     * @param string          $ca_title
16915d603e7SGreg Roach     * @param string          $ca_place
17015d603e7SGreg Roach     * @param string          $ca_citation
17115d603e7SGreg Roach     * @param string[][]      $ca_individuals
17215d603e7SGreg Roach     * @param string          $ca_notes
17315d603e7SGreg Roach     *
17415d603e7SGreg Roach     * @return string
17515d603e7SGreg Roach     */
1768f53f488SRico Sonntag    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
177c1010edaSGreg Roach    {
1783cc4e3a0SGreg Roach        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n|";
17915d603e7SGreg Roach
1803cc4e3a0SGreg Roach        foreach ($census->columns() as $column) {
1813cc4e3a0SGreg Roach            $text .= ' ' . $column->abbreviation() . ' |';
1820d46ec71SGreg Roach        }
1830d46ec71SGreg Roach
1843cc4e3a0SGreg Roach        $text .= "\n|" . str_repeat(' ----- |', count($census->columns()));
18515d603e7SGreg Roach
1860483c93fSGreg Roach        foreach (array_keys($ca_individuals['xref'] ?? []) as $key) {
1873cc4e3a0SGreg Roach            $text .= "\n|";
1883cc4e3a0SGreg Roach
1890483c93fSGreg Roach            foreach ($census->columns() as $n => $column) {
1903cc4e3a0SGreg Roach                $text .= ' ' . $ca_individuals[$n][$key] . ' |';
1910483c93fSGreg Roach            }
19215d603e7SGreg Roach        }
19315d603e7SGreg Roach
194a97088d0SGreg Roach        return $text . "\n\n" . strtr($ca_notes, ["\r" => '']);
19540990b78SGreg Roach    }
19640990b78SGreg Roach
19740990b78SGreg Roach    /**
198ad51e0bbSGreg Roach     * Generate an HTML row of data for the census header
19952bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
20052bc9faeSGreg Roach     * Add suffix cell (delete button)
20152bc9faeSGreg Roach     *
202ad51e0bbSGreg Roach     * @param CensusInterface $census
20399f222b3SGreg Roach     *
204ad51e0bbSGreg Roach     * @return string
20599f222b3SGreg Roach     */
2065a62e0a6SGreg Roach    protected function censusTableHeader(CensusInterface $census): string
207c1010edaSGreg Roach    {
20852bc9faeSGreg Roach        $html = '';
209ad51e0bbSGreg Roach        foreach ($census->columns() as $column) {
21015d603e7SGreg Roach            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
21199f222b3SGreg Roach        }
21299f222b3SGreg Roach
21315d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
214ad51e0bbSGreg Roach    }
21599f222b3SGreg Roach
216ad51e0bbSGreg Roach    /**
217ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
21852bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
21952bc9faeSGreg Roach     * Add suffix cell (delete button)
22052bc9faeSGreg Roach     *
221e5a6b4d4SGreg Roach     * @param CensusInterface $census
222ad51e0bbSGreg Roach     *
223ad51e0bbSGreg Roach     * @return string
224ad51e0bbSGreg Roach     */
2258b9cfadbSGreg Roach    public function censusTableEmptyRow(CensusInterface $census): string
226c1010edaSGreg Roach    {
2270483c93fSGreg Roach        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>';
2280483c93fSGreg Roach
2290483c93fSGreg Roach        foreach ($census->columns() as $n => $column) {
2300483c93fSGreg 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>';
2310483c93fSGreg Roach        }
2320483c93fSGreg Roach
2330483c93fSGreg Roach        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
2340483c93fSGreg Roach
2350483c93fSGreg Roach        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
236ad51e0bbSGreg Roach    }
23799f222b3SGreg Roach
238ad51e0bbSGreg Roach    /**
239ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
24052bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
24152bc9faeSGreg Roach     * Add suffix cell (delete button)
24252bc9faeSGreg Roach     *
243ad51e0bbSGreg Roach     * @param CensusInterface $census
244ad51e0bbSGreg Roach     * @param Individual      $individual
245ad51e0bbSGreg Roach     * @param Individual      $head
246ad51e0bbSGreg Roach     *
247ad51e0bbSGreg Roach     * @return string
248ad51e0bbSGreg Roach     */
2498b9cfadbSGreg Roach    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
250c1010edaSGreg Roach    {
2510483c93fSGreg Roach        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>';
2520483c93fSGreg Roach
2530483c93fSGreg Roach        foreach ($census->columns() as $n => $column) {
2540483c93fSGreg 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>';
25599f222b3SGreg Roach        }
25699f222b3SGreg Roach
2570483c93fSGreg Roach        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
2580483c93fSGreg Roach
2590483c93fSGreg Roach        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
26099f222b3SGreg Roach    }
2618c2e8227SGreg Roach}
262