xref: /webtrees/app/Module/CensusAssistantModule.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
18c2e8227SGreg Roach<?php
2*3976b470SGreg 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 */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
21ad51e0bbSGreg Roachuse Fisharebest\Webtrees\Census\CensusInterface;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2399f222b3SGreg Roachuse Fisharebest\Webtrees\Individual;
246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26d45cb9d3SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class CensusAssistantModule
308c2e8227SGreg Roach */
315206405dSRico Sonntagclass CensusAssistantModule extends AbstractModule
32c1010edaSGreg Roach{
33961ec755SGreg Roach    /**
340cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
35961ec755SGreg Roach     *
36961ec755SGreg Roach     * @return string
37961ec755SGreg Roach     */
3849a243cbSGreg Roach    public function title(): string
39c1010edaSGreg Roach    {
40bbb76c12SGreg Roach        /* I18N: Name of a module */
41bbb76c12SGreg Roach        return I18N::translate('Census assistant');
428c2e8227SGreg Roach    }
438c2e8227SGreg Roach
44961ec755SGreg Roach    /**
45961ec755SGreg Roach     * A sentence describing what this module does.
46961ec755SGreg Roach     *
47961ec755SGreg Roach     * @return string
48961ec755SGreg Roach     */
4949a243cbSGreg Roach    public function description(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Description of the “Census assistant” module */
52bbb76c12SGreg Roach        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
5576692c8bSGreg Roach    /**
566ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
579001c0b3SGreg Roach     *
586ccdf4f0SGreg Roach     * @return ResponseInterface
599001c0b3SGreg Roach     */
606ccdf4f0SGreg Roach    public function getCensusHeaderAction(ServerRequestInterface $request): ResponseInterface
61c1010edaSGreg Roach    {
62abaef046SGreg Roach        $census = $request->getQueryParams()['census'];
639001c0b3SGreg Roach
6459f2f229SGreg Roach        $html = $this->censusTableHeader(new $census());
659001c0b3SGreg Roach
666ccdf4f0SGreg Roach        return response($html);
679001c0b3SGreg Roach    }
689001c0b3SGreg Roach
699001c0b3SGreg Roach    /**
706ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
719001c0b3SGreg Roach     *
726ccdf4f0SGreg Roach     * @return ResponseInterface
739001c0b3SGreg Roach     */
7457ab2231SGreg Roach    public function getCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
75c1010edaSGreg Roach    {
7657ab2231SGreg Roach        $tree       = $request->getAttribute('tree');
77abaef046SGreg Roach        $params     = $request->getQueryParams();
78abaef046SGreg Roach        $individual = Individual::getInstance($params['xref'], $tree);
79abaef046SGreg Roach        $head       = Individual::getInstance($params['head'], $tree);
80abaef046SGreg Roach        $census     = $params['census'];
81d45cb9d3SGreg Roach
82d45cb9d3SGreg Roach        if ($individual instanceof Individual && $head instanceof Individual) {
8359f2f229SGreg Roach            $html = $this->censusTableRow(new $census(), $individual, $head);
849001c0b3SGreg Roach
856ccdf4f0SGreg Roach            return response($html);
86d45cb9d3SGreg Roach        }
87e364afe4SGreg Roach
88e364afe4SGreg Roach        throw new NotFoundHttpException();
899001c0b3SGreg Roach    }
909001c0b3SGreg Roach
919001c0b3SGreg Roach    /**
9215d603e7SGreg Roach     * @param Individual $individual
939001c0b3SGreg Roach     *
949001c0b3SGreg Roach     * @return string
958c2e8227SGreg Roach     */
968f53f488SRico Sonntag    public function createCensusAssistant(Individual $individual): string
97c1010edaSGreg Roach    {
98225e381fSGreg Roach        return view('modules/census-assistant', [
9934cd602eSGreg Roach            'individual' => $individual,
10034cd602eSGreg Roach        ]);
10115d603e7SGreg Roach    }
10215d603e7SGreg Roach
10315d603e7SGreg Roach    /**
1046ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
10515d603e7SGreg Roach     * @param Individual             $individual
10660bc3e3fSGreg Roach     * @param string                 $fact_id
10715d603e7SGreg Roach     * @param string                 $newged
10860bc3e3fSGreg Roach     * @param bool                   $keep_chan
10915d603e7SGreg Roach     *
11015d603e7SGreg Roach     * @return string
11115d603e7SGreg Roach     */
1126ccdf4f0SGreg Roach    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
113c1010edaSGreg Roach    {
114abaef046SGreg Roach        $params = $request->getParsedBody();
115abaef046SGreg Roach
116abaef046SGreg Roach        $ca_title       = $params['ca_title'] ?? '';
117abaef046SGreg Roach        $ca_place       = $params['ca_place'] ?? '';
118abaef046SGreg Roach        $ca_citation    = $params['ca_citation'] ?? '';
119abaef046SGreg Roach        $ca_individuals = $params['ca_individuals'] ?? [];
120abaef046SGreg Roach        $ca_notes       = $params['ca_notes'] ?? '';
121abaef046SGreg Roach        $ca_census      = $params['ca_census'] ?? '';
12215d603e7SGreg Roach
12315d603e7SGreg Roach        if ($ca_census !== '' && !empty($ca_individuals)) {
12459f2f229SGreg Roach            $census = new $ca_census();
12515d603e7SGreg Roach
12615d603e7SGreg Roach            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
127afb591d7SGreg Roach            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
128f4afa648SGreg Roach            $note        = $individual->tree()->createRecord($note_gedcom);
12915d603e7SGreg Roach
130c0935879SGreg Roach            $newged .= "\n2 NOTE @" . $note->xref() . '@';
13115d603e7SGreg Roach
13215d603e7SGreg Roach            // Add the census fact to the rest of the household
13315d603e7SGreg Roach            foreach (array_keys($ca_individuals) as $xref) {
134c0935879SGreg Roach                if ($xref !== $individual->xref()) {
135f4afa648SGreg Roach                    Individual::getInstance($xref, $individual->tree())
13615d603e7SGreg Roach                        ->updateFact($fact_id, $newged, !$keep_chan);
13715d603e7SGreg Roach                }
13815d603e7SGreg Roach            }
13915d603e7SGreg Roach        }
14015d603e7SGreg Roach
14115d603e7SGreg Roach        return $newged;
14215d603e7SGreg Roach    }
14315d603e7SGreg Roach
14415d603e7SGreg Roach    /**
14515d603e7SGreg Roach     * @param CensusInterface $census
14615d603e7SGreg Roach     * @param string          $ca_title
14715d603e7SGreg Roach     * @param string          $ca_place
14815d603e7SGreg Roach     * @param string          $ca_citation
14915d603e7SGreg Roach     * @param string[][]      $ca_individuals
15015d603e7SGreg Roach     * @param string          $ca_notes
15115d603e7SGreg Roach     *
15215d603e7SGreg Roach     * @return string
15315d603e7SGreg Roach     */
1548f53f488SRico Sonntag    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
155c1010edaSGreg Roach    {
1560d46ec71SGreg Roach        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
15715d603e7SGreg Roach
15815d603e7SGreg Roach        foreach ($census->columns() as $n => $column) {
1590d46ec71SGreg Roach            if ($n === 0) {
1600d46ec71SGreg Roach                $text .= "\n";
1610d46ec71SGreg Roach            } else {
16215d603e7SGreg Roach                $text .= ' | ';
16315d603e7SGreg Roach            }
1640d46ec71SGreg Roach            $text .= $column->abbreviation();
1650d46ec71SGreg Roach        }
1660d46ec71SGreg Roach
1670d46ec71SGreg Roach        foreach ($census->columns() as $n => $column) {
1680d46ec71SGreg Roach            if ($n === 0) {
1690d46ec71SGreg Roach                $text .= "\n";
1700d46ec71SGreg Roach            } else {
1710d46ec71SGreg Roach                $text .= ' | ';
1720d46ec71SGreg Roach            }
1730d46ec71SGreg Roach            $text .= '-----';
17415d603e7SGreg Roach        }
17515d603e7SGreg Roach
17615d603e7SGreg Roach        foreach ($ca_individuals as $xref => $columns) {
17715d603e7SGreg Roach            $text .= "\n" . implode(' | ', $columns);
17815d603e7SGreg Roach        }
17915d603e7SGreg Roach
1800d46ec71SGreg Roach        return $text . "\n\n" . $ca_notes;
18140990b78SGreg Roach    }
18240990b78SGreg Roach
18340990b78SGreg Roach    /**
184ad51e0bbSGreg Roach     * Generate an HTML row of data for the census header
18552bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
18652bc9faeSGreg Roach     * Add suffix cell (delete button)
18752bc9faeSGreg Roach     *
188ad51e0bbSGreg Roach     * @param CensusInterface $census
18999f222b3SGreg Roach     *
190ad51e0bbSGreg Roach     * @return string
19199f222b3SGreg Roach     */
1925a62e0a6SGreg Roach    protected function censusTableHeader(CensusInterface $census): string
193c1010edaSGreg Roach    {
19452bc9faeSGreg Roach        $html = '';
195ad51e0bbSGreg Roach        foreach ($census->columns() as $column) {
19615d603e7SGreg Roach            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
19799f222b3SGreg Roach        }
19899f222b3SGreg Roach
19915d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
200ad51e0bbSGreg Roach    }
20199f222b3SGreg Roach
202ad51e0bbSGreg Roach    /**
203ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
20452bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
20552bc9faeSGreg Roach     * Add suffix cell (delete button)
20652bc9faeSGreg Roach     *
207e5a6b4d4SGreg Roach     * @param CensusInterface $census
208ad51e0bbSGreg Roach     *
209ad51e0bbSGreg Roach     * @return string
210ad51e0bbSGreg Roach     */
2118b9cfadbSGreg Roach    public function censusTableEmptyRow(CensusInterface $census): string
212c1010edaSGreg Roach    {
213b6c326d8SGreg 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 class="icon-remove" href="#" title="' . I18N::translate('Remove') . '"></a></td></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     *
221ad51e0bbSGreg Roach     * @param CensusInterface $census
222ad51e0bbSGreg Roach     * @param Individual      $individual
223ad51e0bbSGreg Roach     * @param Individual      $head
224ad51e0bbSGreg Roach     *
225ad51e0bbSGreg Roach     * @return string
226ad51e0bbSGreg Roach     */
2278b9cfadbSGreg Roach    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
228c1010edaSGreg Roach    {
22915d603e7SGreg Roach        $html = '';
23015d603e7SGreg Roach        foreach ($census->columns() as $column) {
231b6c326d8SGreg 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>';
23299f222b3SGreg Roach        }
23399f222b3SGreg Roach
234c0935879SGreg 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 class="icon-remove" href="#" title="' . I18N::translate('Remove') . '"></a></td></tr>';
23599f222b3SGreg Roach    }
2368c2e8227SGreg Roach}
237