xref: /webtrees/app/Module/CensusAssistantModule.php (revision d501c45d339d4a2d06248f9197d7875a4df14e48)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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;
23*d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
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
304ea62551SGreg Roachuse function assert;
314ea62551SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class CensusAssistantModule
348c2e8227SGreg Roach */
355206405dSRico Sonntagclass CensusAssistantModule extends AbstractModule
36c1010edaSGreg Roach{
37961ec755SGreg Roach    /**
380cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
39961ec755SGreg Roach     *
40961ec755SGreg Roach     * @return string
41961ec755SGreg Roach     */
4249a243cbSGreg Roach    public function title(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Name of a module */
45bbb76c12SGreg Roach        return I18N::translate('Census assistant');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
48961ec755SGreg Roach    /**
49961ec755SGreg Roach     * A sentence describing what this module does.
50961ec755SGreg Roach     *
51961ec755SGreg Roach     * @return string
52961ec755SGreg Roach     */
5349a243cbSGreg Roach    public function description(): string
54c1010edaSGreg Roach    {
55bbb76c12SGreg Roach        /* I18N: Description of the “Census assistant” module */
56bbb76c12SGreg Roach        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
578c2e8227SGreg Roach    }
588c2e8227SGreg Roach
5976692c8bSGreg Roach    /**
606ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
619001c0b3SGreg Roach     *
626ccdf4f0SGreg Roach     * @return ResponseInterface
639001c0b3SGreg Roach     */
647da59459SGreg Roach    public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface
65c1010edaSGreg Roach    {
667da59459SGreg Roach        $census = $request->getParsedBody()['census'];
679001c0b3SGreg Roach
6859f2f229SGreg Roach        $html = $this->censusTableHeader(new $census());
699001c0b3SGreg Roach
706ccdf4f0SGreg Roach        return response($html);
719001c0b3SGreg Roach    }
729001c0b3SGreg Roach
739001c0b3SGreg Roach    /**
746ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
759001c0b3SGreg Roach     *
766ccdf4f0SGreg Roach     * @return ResponseInterface
779001c0b3SGreg Roach     */
787da59459SGreg Roach    public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
79c1010edaSGreg Roach    {
8057ab2231SGreg Roach        $tree = $request->getAttribute('tree');
814ea62551SGreg Roach        assert($tree instanceof Tree);
824ea62551SGreg Roach
837da59459SGreg Roach        $params     = $request->getParsedBody();
84abaef046SGreg Roach        $individual = Individual::getInstance($params['xref'], $tree);
85abaef046SGreg Roach        $head       = Individual::getInstance($params['head'], $tree);
86abaef046SGreg Roach        $census     = $params['census'];
87d45cb9d3SGreg Roach
88d45cb9d3SGreg Roach        if ($individual instanceof Individual && $head instanceof Individual) {
8959f2f229SGreg Roach            $html = $this->censusTableRow(new $census(), $individual, $head);
909001c0b3SGreg Roach
916ccdf4f0SGreg Roach            return response($html);
92d45cb9d3SGreg Roach        }
93e364afe4SGreg Roach
94*d501c45dSGreg Roach        throw new HttpNotFoundException();
959001c0b3SGreg Roach    }
969001c0b3SGreg Roach
979001c0b3SGreg Roach    /**
9815d603e7SGreg Roach     * @param Individual $individual
999001c0b3SGreg Roach     *
1009001c0b3SGreg Roach     * @return string
1018c2e8227SGreg Roach     */
1028f53f488SRico Sonntag    public function createCensusAssistant(Individual $individual): string
103c1010edaSGreg Roach    {
104225e381fSGreg Roach        return view('modules/census-assistant', [
10534cd602eSGreg Roach            'individual' => $individual,
10634cd602eSGreg Roach        ]);
10715d603e7SGreg Roach    }
10815d603e7SGreg Roach
10915d603e7SGreg Roach    /**
1106ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
11115d603e7SGreg Roach     * @param Individual             $individual
11260bc3e3fSGreg Roach     * @param string                 $fact_id
11315d603e7SGreg Roach     * @param string                 $newged
11460bc3e3fSGreg Roach     * @param bool                   $keep_chan
11515d603e7SGreg Roach     *
11615d603e7SGreg Roach     * @return string
11715d603e7SGreg Roach     */
1186ccdf4f0SGreg Roach    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
119c1010edaSGreg Roach    {
120abaef046SGreg Roach        $params = $request->getParsedBody();
121abaef046SGreg Roach
122abaef046SGreg Roach        $ca_title       = $params['ca_title'] ?? '';
123abaef046SGreg Roach        $ca_place       = $params['ca_place'] ?? '';
124abaef046SGreg Roach        $ca_citation    = $params['ca_citation'] ?? '';
125abaef046SGreg Roach        $ca_individuals = $params['ca_individuals'] ?? [];
126abaef046SGreg Roach        $ca_notes       = $params['ca_notes'] ?? '';
127abaef046SGreg Roach        $ca_census      = $params['ca_census'] ?? '';
12815d603e7SGreg Roach
129a91af26aSGreg Roach        if ($ca_census !== '' && $ca_individuals !== []) {
13059f2f229SGreg Roach            $census = new $ca_census();
13115d603e7SGreg Roach
13215d603e7SGreg Roach            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
133afb591d7SGreg Roach            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
134f4afa648SGreg Roach            $note        = $individual->tree()->createRecord($note_gedcom);
13515d603e7SGreg Roach
136c0935879SGreg Roach            $newged .= "\n2 NOTE @" . $note->xref() . '@';
13715d603e7SGreg Roach
13815d603e7SGreg Roach            // Add the census fact to the rest of the household
13915d603e7SGreg Roach            foreach (array_keys($ca_individuals) as $xref) {
140c0935879SGreg Roach                if ($xref !== $individual->xref()) {
141f4afa648SGreg Roach                    Individual::getInstance($xref, $individual->tree())
14215d603e7SGreg Roach                        ->updateFact($fact_id, $newged, !$keep_chan);
14315d603e7SGreg Roach                }
14415d603e7SGreg Roach            }
14515d603e7SGreg Roach        }
14615d603e7SGreg Roach
14715d603e7SGreg Roach        return $newged;
14815d603e7SGreg Roach    }
14915d603e7SGreg Roach
15015d603e7SGreg Roach    /**
15115d603e7SGreg Roach     * @param CensusInterface $census
15215d603e7SGreg Roach     * @param string          $ca_title
15315d603e7SGreg Roach     * @param string          $ca_place
15415d603e7SGreg Roach     * @param string          $ca_citation
15515d603e7SGreg Roach     * @param string[][]      $ca_individuals
15615d603e7SGreg Roach     * @param string          $ca_notes
15715d603e7SGreg Roach     *
15815d603e7SGreg Roach     * @return string
15915d603e7SGreg Roach     */
1608f53f488SRico Sonntag    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
161c1010edaSGreg Roach    {
1620d46ec71SGreg Roach        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
16315d603e7SGreg Roach
16415d603e7SGreg Roach        foreach ($census->columns() as $n => $column) {
1650d46ec71SGreg Roach            if ($n === 0) {
1660d46ec71SGreg Roach                $text .= "\n";
1670d46ec71SGreg Roach            } else {
16815d603e7SGreg Roach                $text .= ' | ';
16915d603e7SGreg Roach            }
1700d46ec71SGreg Roach            $text .= $column->abbreviation();
1710d46ec71SGreg Roach        }
1720d46ec71SGreg Roach
1730d46ec71SGreg Roach        foreach ($census->columns() as $n => $column) {
1740d46ec71SGreg Roach            if ($n === 0) {
1750d46ec71SGreg Roach                $text .= "\n";
1760d46ec71SGreg Roach            } else {
1770d46ec71SGreg Roach                $text .= ' | ';
1780d46ec71SGreg Roach            }
1790d46ec71SGreg Roach            $text .= '-----';
18015d603e7SGreg Roach        }
18115d603e7SGreg Roach
18215d603e7SGreg Roach        foreach ($ca_individuals as $xref => $columns) {
18315d603e7SGreg Roach            $text .= "\n" . implode(' | ', $columns);
18415d603e7SGreg Roach        }
18515d603e7SGreg Roach
1860d46ec71SGreg Roach        return $text . "\n\n" . $ca_notes;
18740990b78SGreg Roach    }
18840990b78SGreg Roach
18940990b78SGreg Roach    /**
190ad51e0bbSGreg Roach     * Generate an HTML row of data for the census header
19152bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
19252bc9faeSGreg Roach     * Add suffix cell (delete button)
19352bc9faeSGreg Roach     *
194ad51e0bbSGreg Roach     * @param CensusInterface $census
19599f222b3SGreg Roach     *
196ad51e0bbSGreg Roach     * @return string
19799f222b3SGreg Roach     */
1985a62e0a6SGreg Roach    protected function censusTableHeader(CensusInterface $census): string
199c1010edaSGreg Roach    {
20052bc9faeSGreg Roach        $html = '';
201ad51e0bbSGreg Roach        foreach ($census->columns() as $column) {
20215d603e7SGreg Roach            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
20399f222b3SGreg Roach        }
20499f222b3SGreg Roach
20515d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
206ad51e0bbSGreg Roach    }
20799f222b3SGreg Roach
208ad51e0bbSGreg Roach    /**
209ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
21052bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
21152bc9faeSGreg Roach     * Add suffix cell (delete button)
21252bc9faeSGreg Roach     *
213e5a6b4d4SGreg Roach     * @param CensusInterface $census
214ad51e0bbSGreg Roach     *
215ad51e0bbSGreg Roach     * @return string
216ad51e0bbSGreg Roach     */
2178b9cfadbSGreg Roach    public function censusTableEmptyRow(CensusInterface $census): string
218c1010edaSGreg Roach    {
21908362db4SGreg Roach        return '<tr class="wt-census-assistant-row"><td hidden></td>' . str_repeat('<td class="wt-census-assistant-field p-0"><input type="text" class="form-control wt-census-assistant-form-control p-0"></td>', count($census->columns())) . '<td><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td></tr>';
220ad51e0bbSGreg Roach    }
22199f222b3SGreg Roach
222ad51e0bbSGreg Roach    /**
223ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
22452bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
22552bc9faeSGreg Roach     * Add suffix cell (delete button)
22652bc9faeSGreg Roach     *
227ad51e0bbSGreg Roach     * @param CensusInterface $census
228ad51e0bbSGreg Roach     * @param Individual      $individual
229ad51e0bbSGreg Roach     * @param Individual      $head
230ad51e0bbSGreg Roach     *
231ad51e0bbSGreg Roach     * @return string
232ad51e0bbSGreg Roach     */
2338b9cfadbSGreg Roach    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
234c1010edaSGreg Roach    {
23515d603e7SGreg Roach        $html = '';
23615d603e7SGreg Roach        foreach ($census->columns() as $column) {
237b6c326d8SGreg 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[' . $individual->xref() . '][]"></td>';
23899f222b3SGreg Roach        }
23999f222b3SGreg Roach
24008362db4SGreg Roach        return '<tr class="wt-census-assistant-row"><td class="wt-census-assistant-field" hidden>' . $individual->xref() . '</td>' . $html . '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td></tr>';
24199f222b3SGreg Roach    }
2428c2e8227SGreg Roach}
243