xref: /webtrees/app/Module/CensusAssistantModule.php (revision 5b05d6ad6f609ece13806903f6819d1664d0f910)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1815d603e7SGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
21ad51e0bbSGreg Roachuse Fisharebest\Webtrees\Census\CensusInterface;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2399f222b3SGreg Roachuse Fisharebest\Webtrees\Individual;
249001c0b3SGreg Roachuse Fisharebest\Webtrees\Tree;
259001c0b3SGreg Roachuse Symfony\Component\HttpFoundation\Request;
269001c0b3SGreg Roachuse Symfony\Component\HttpFoundation\Response;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class CensusAssistantModule
308c2e8227SGreg Roach */
31027c6af4SGreg Roachclass CensusAssistantModule extends AbstractModule implements ModuleInterface
32c1010edaSGreg Roach{
338c2e8227SGreg Roach    /** {@inheritdoc} */
348f53f488SRico Sonntag    public function getTitle(): string
35c1010edaSGreg Roach    {
36bbb76c12SGreg Roach        /* I18N: Name of a module */
37bbb76c12SGreg Roach        return I18N::translate('Census assistant');
388c2e8227SGreg Roach    }
398c2e8227SGreg Roach
408c2e8227SGreg Roach    /** {@inheritdoc} */
418f53f488SRico Sonntag    public function getDescription(): string
42c1010edaSGreg Roach    {
43bbb76c12SGreg Roach        /* I18N: Description of the “Census assistant” module */
44bbb76c12SGreg Roach        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
458c2e8227SGreg Roach    }
468c2e8227SGreg Roach
4776692c8bSGreg Roach    /**
489001c0b3SGreg Roach     * @param Request $request
499001c0b3SGreg Roach     *
509001c0b3SGreg Roach     * @return Response
519001c0b3SGreg Roach     */
52c1010edaSGreg Roach    public function getCensusHeaderAction(Request $request): Response
53c1010edaSGreg Roach    {
549001c0b3SGreg Roach        $census = $request->get('census');
559001c0b3SGreg Roach
5659f2f229SGreg Roach        $html = $this->censusTableHeader(new $census());
579001c0b3SGreg Roach
589001c0b3SGreg Roach        return new Response($html);
599001c0b3SGreg Roach    }
609001c0b3SGreg Roach
619001c0b3SGreg Roach    /**
629001c0b3SGreg Roach     * @param Request $request
63b6db7c1fSGreg Roach     * @param Tree    $tree
649001c0b3SGreg Roach     *
659001c0b3SGreg Roach     * @return Response
669001c0b3SGreg Roach     */
67b6db7c1fSGreg Roach    public function getCensusIndividualAction(Request $request, Tree $tree): Response
68c1010edaSGreg Roach    {
69*5b05d6adSGreg Roach        $census = $request->get('census', '');
709001c0b3SGreg Roach
71*5b05d6adSGreg Roach        $individual = Individual::getInstance($request->get('xref', ''), $tree);
72*5b05d6adSGreg Roach        $head       = Individual::getInstance($request->get('head', ''), $tree);
7359f2f229SGreg Roach        $html       = $this->censusTableRow(new $census(), $individual, $head);
749001c0b3SGreg Roach
759001c0b3SGreg Roach        return new Response($html);
769001c0b3SGreg Roach    }
779001c0b3SGreg Roach
789001c0b3SGreg Roach    /**
7915d603e7SGreg Roach     * @param Individual $individual
809001c0b3SGreg Roach     *
819001c0b3SGreg Roach     * @return string
828c2e8227SGreg Roach     */
838f53f488SRico Sonntag    public function createCensusAssistant(Individual $individual): string
84c1010edaSGreg Roach    {
85225e381fSGreg Roach        return view('modules/census-assistant', [
8634cd602eSGreg Roach            'individual' => $individual,
8734cd602eSGreg Roach        ]);
8815d603e7SGreg Roach    }
8915d603e7SGreg Roach
9015d603e7SGreg Roach    /**
91a45f9889SGreg Roach     * @param Request    $request
9215d603e7SGreg Roach     * @param Individual $individual
9360bc3e3fSGreg Roach     * @param string     $fact_id
9415d603e7SGreg Roach     * @param string     $newged
9560bc3e3fSGreg Roach     * @param bool       $keep_chan
9615d603e7SGreg Roach     *
9715d603e7SGreg Roach     * @return string
9815d603e7SGreg Roach     */
998f53f488SRico Sonntag    public function updateCensusAssistant(Request $request, Individual $individual, $fact_id, $newged, $keep_chan): string
100c1010edaSGreg Roach    {
101a45f9889SGreg Roach        $ca_title       = $request->get('ca_title', '');
102a45f9889SGreg Roach        $ca_place       = $request->get('ca_place', '');
103a45f9889SGreg Roach        $ca_citation    = $request->get('ca_citation', '');
104a45f9889SGreg Roach        $ca_individuals = (array) $request->get('ca_individuals');
105a45f9889SGreg Roach        $ca_notes       = $request->get('ca_notes', '');
106a45f9889SGreg Roach        $ca_census      = $request->get('ca_census', '');
10715d603e7SGreg Roach
10815d603e7SGreg Roach        if ($ca_census !== '' && !empty($ca_individuals)) {
10959f2f229SGreg Roach            $census = new $ca_census();
11015d603e7SGreg Roach
11115d603e7SGreg Roach            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
112afb591d7SGreg Roach            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
11315d603e7SGreg Roach            $note        = $individual->getTree()->createRecord($note_gedcom);
11415d603e7SGreg Roach
11515d603e7SGreg Roach            $newged .= "\n2 NOTE @" . $note->getXref() . '@';
11615d603e7SGreg Roach
11715d603e7SGreg Roach            // Add the census fact to the rest of the household
11815d603e7SGreg Roach            foreach (array_keys($ca_individuals) as $xref) {
11915d603e7SGreg Roach                if ($xref !== $individual->getXref()) {
12015d603e7SGreg Roach                    Individual::getInstance($xref, $individual->getTree())
12115d603e7SGreg Roach                        ->updateFact($fact_id, $newged, !$keep_chan);
12215d603e7SGreg Roach                }
12315d603e7SGreg Roach            }
12415d603e7SGreg Roach        }
12515d603e7SGreg Roach
12615d603e7SGreg Roach        return $newged;
12715d603e7SGreg Roach    }
12815d603e7SGreg Roach
12915d603e7SGreg Roach    /**
13015d603e7SGreg Roach     * @param CensusInterface $census
13115d603e7SGreg Roach     * @param string          $ca_title
13215d603e7SGreg Roach     * @param string          $ca_place
13315d603e7SGreg Roach     * @param string          $ca_citation
13415d603e7SGreg Roach     * @param string[][]      $ca_individuals
13515d603e7SGreg Roach     * @param string          $ca_notes
13615d603e7SGreg Roach     *
13715d603e7SGreg Roach     * @return string
13815d603e7SGreg Roach     */
1398f53f488SRico Sonntag    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
140c1010edaSGreg Roach    {
1410d46ec71SGreg Roach        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
14215d603e7SGreg Roach
14315d603e7SGreg Roach        foreach ($census->columns() as $n => $column) {
1440d46ec71SGreg Roach            if ($n === 0) {
1450d46ec71SGreg Roach                $text .= "\n";
1460d46ec71SGreg Roach            } else {
14715d603e7SGreg Roach                $text .= ' | ';
14815d603e7SGreg Roach            }
1490d46ec71SGreg Roach            $text .= $column->abbreviation();
1500d46ec71SGreg Roach        }
1510d46ec71SGreg Roach
1520d46ec71SGreg Roach        foreach ($census->columns() as $n => $column) {
1530d46ec71SGreg Roach            if ($n === 0) {
1540d46ec71SGreg Roach                $text .= "\n";
1550d46ec71SGreg Roach            } else {
1560d46ec71SGreg Roach                $text .= ' | ';
1570d46ec71SGreg Roach            }
1580d46ec71SGreg Roach            $text .= '-----';
15915d603e7SGreg Roach        }
16015d603e7SGreg Roach
16115d603e7SGreg Roach        foreach ($ca_individuals as $xref => $columns) {
16215d603e7SGreg Roach            $text .= "\n" . implode(' | ', $columns);
16315d603e7SGreg Roach        }
16415d603e7SGreg Roach
1650d46ec71SGreg Roach        return $text . "\n\n" . $ca_notes;
16640990b78SGreg Roach    }
16740990b78SGreg Roach
16840990b78SGreg Roach    /**
169ad51e0bbSGreg Roach     * Generate an HTML row of data for the census header
17052bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
17152bc9faeSGreg Roach     * Add suffix cell (delete button)
17252bc9faeSGreg Roach     *
173ad51e0bbSGreg Roach     * @param CensusInterface $census
17499f222b3SGreg Roach     *
175ad51e0bbSGreg Roach     * @return string
17699f222b3SGreg Roach     */
1775a62e0a6SGreg Roach    protected function censusTableHeader(CensusInterface $census): string
178c1010edaSGreg Roach    {
17952bc9faeSGreg Roach        $html = '';
180ad51e0bbSGreg Roach        foreach ($census->columns() as $column) {
18115d603e7SGreg Roach            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
18299f222b3SGreg Roach        }
18399f222b3SGreg Roach
18415d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
185ad51e0bbSGreg Roach    }
18699f222b3SGreg Roach
187ad51e0bbSGreg Roach    /**
188ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
18952bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
19052bc9faeSGreg Roach     * Add suffix cell (delete button)
19152bc9faeSGreg Roach     *
192ad51e0bbSGreg Roach     * @param CensusInterface $census
193ad51e0bbSGreg Roach     *
194ad51e0bbSGreg Roach     * @return string
195ad51e0bbSGreg Roach     */
1968f53f488SRico Sonntag    public static function censusTableEmptyRow(CensusInterface $census): string
197c1010edaSGreg Roach    {
19815d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><td hidden></td>' . str_repeat('<td class="wt-census-assistant-field"><input type="text" class="form-control wt-census-assistant-form-control"></td>', count($census->columns())) . '<td><a class="icon-remove" href="#" title="' . I18N::translate('Remove') . '"></a></td></tr>';
199ad51e0bbSGreg Roach    }
20099f222b3SGreg Roach
201ad51e0bbSGreg Roach    /**
202ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
20352bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
20452bc9faeSGreg Roach     * Add suffix cell (delete button)
20552bc9faeSGreg Roach     *
206ad51e0bbSGreg Roach     * @param CensusInterface $census
207ad51e0bbSGreg Roach     * @param Individual      $individual
208ad51e0bbSGreg Roach     * @param Individual      $head
209ad51e0bbSGreg Roach     *
210ad51e0bbSGreg Roach     * @return string
211ad51e0bbSGreg Roach     */
2128f53f488SRico Sonntag    public static function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
213c1010edaSGreg Roach    {
21415d603e7SGreg Roach        $html = '';
21515d603e7SGreg Roach        foreach ($census->columns() as $column) {
21615d603e7SGreg Roach            $html .= '<td class="wt-census-assistant-field"><input class="form-control wt-census-assistant-form-control" type="text" value="' . $column->generate($individual, $head) . '" name="ca_individuals[' . $individual->getXref() . '][]"></td>';
21799f222b3SGreg Roach        }
21899f222b3SGreg Roach
21915d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><td class="wt-census-assistant-field" hidden>' . $individual->getXref() . '</td>' . $html . '<td class="wt-census-assistant-field"><a class="icon-remove" href="#" title="' . I18N::translate('Remove') . '"></a></td></tr>';
22099f222b3SGreg Roach    }
2218c2e8227SGreg Roach}
222