xref: /webtrees/app/Module/CensusAssistantModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 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;
27d45cb9d3SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class CensusAssistantModule
318c2e8227SGreg Roach */
32027c6af4SGreg Roachclass CensusAssistantModule extends AbstractModule implements ModuleInterface
33c1010edaSGreg Roach{
348c2e8227SGreg Roach    /** {@inheritdoc} */
358f53f488SRico Sonntag    public function getTitle(): string
36c1010edaSGreg Roach    {
37bbb76c12SGreg Roach        /* I18N: Name of a module */
38bbb76c12SGreg Roach        return I18N::translate('Census assistant');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
428f53f488SRico Sonntag    public function getDescription(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Description of the “Census assistant” module */
45bbb76c12SGreg Roach        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
4876692c8bSGreg Roach    /**
499001c0b3SGreg Roach     * @param Request $request
509001c0b3SGreg Roach     *
519001c0b3SGreg Roach     * @return Response
529001c0b3SGreg Roach     */
53c1010edaSGreg Roach    public function getCensusHeaderAction(Request $request): Response
54c1010edaSGreg Roach    {
559001c0b3SGreg Roach        $census = $request->get('census');
569001c0b3SGreg Roach
5759f2f229SGreg Roach        $html = $this->censusTableHeader(new $census());
589001c0b3SGreg Roach
599001c0b3SGreg Roach        return new Response($html);
609001c0b3SGreg Roach    }
619001c0b3SGreg Roach
629001c0b3SGreg Roach    /**
639001c0b3SGreg Roach     * @param Request $request
64b6db7c1fSGreg Roach     * @param Tree    $tree
659001c0b3SGreg Roach     *
669001c0b3SGreg Roach     * @return Response
679001c0b3SGreg Roach     */
68b6db7c1fSGreg Roach    public function getCensusIndividualAction(Request $request, Tree $tree): Response
69c1010edaSGreg Roach    {
705b05d6adSGreg Roach        $census = $request->get('census', '');
719001c0b3SGreg Roach
725b05d6adSGreg Roach        $individual = Individual::getInstance($request->get('xref', ''), $tree);
735b05d6adSGreg Roach        $head       = Individual::getInstance($request->get('head', ''), $tree);
74d45cb9d3SGreg Roach
75d45cb9d3SGreg Roach        if ($individual instanceof Individual && $head instanceof Individual) {
7659f2f229SGreg Roach            $html = $this->censusTableRow(new $census(), $individual, $head);
779001c0b3SGreg Roach
789001c0b3SGreg Roach            return new Response($html);
79d45cb9d3SGreg Roach        } else {
80d45cb9d3SGreg Roach            throw new NotFoundHttpException();
81d45cb9d3SGreg Roach        }
829001c0b3SGreg Roach    }
839001c0b3SGreg Roach
849001c0b3SGreg Roach    /**
8515d603e7SGreg Roach     * @param Individual $individual
869001c0b3SGreg Roach     *
879001c0b3SGreg Roach     * @return string
888c2e8227SGreg Roach     */
898f53f488SRico Sonntag    public function createCensusAssistant(Individual $individual): string
90c1010edaSGreg Roach    {
91225e381fSGreg Roach        return view('modules/census-assistant', [
9234cd602eSGreg Roach            'individual' => $individual,
9334cd602eSGreg Roach        ]);
9415d603e7SGreg Roach    }
9515d603e7SGreg Roach
9615d603e7SGreg Roach    /**
97a45f9889SGreg Roach     * @param Request    $request
9815d603e7SGreg Roach     * @param Individual $individual
9960bc3e3fSGreg Roach     * @param string     $fact_id
10015d603e7SGreg Roach     * @param string     $newged
10160bc3e3fSGreg Roach     * @param bool       $keep_chan
10215d603e7SGreg Roach     *
10315d603e7SGreg Roach     * @return string
10415d603e7SGreg Roach     */
1058f53f488SRico Sonntag    public function updateCensusAssistant(Request $request, Individual $individual, $fact_id, $newged, $keep_chan): string
106c1010edaSGreg Roach    {
107a45f9889SGreg Roach        $ca_title       = $request->get('ca_title', '');
108a45f9889SGreg Roach        $ca_place       = $request->get('ca_place', '');
109a45f9889SGreg Roach        $ca_citation    = $request->get('ca_citation', '');
110a45f9889SGreg Roach        $ca_individuals = (array) $request->get('ca_individuals');
111a45f9889SGreg Roach        $ca_notes       = $request->get('ca_notes', '');
112a45f9889SGreg Roach        $ca_census      = $request->get('ca_census', '');
11315d603e7SGreg Roach
11415d603e7SGreg Roach        if ($ca_census !== '' && !empty($ca_individuals)) {
11559f2f229SGreg Roach            $census = new $ca_census();
11615d603e7SGreg Roach
11715d603e7SGreg Roach            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
118afb591d7SGreg Roach            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
119f4afa648SGreg Roach            $note        = $individual->tree()->createRecord($note_gedcom);
12015d603e7SGreg Roach
121c0935879SGreg Roach            $newged .= "\n2 NOTE @" . $note->xref() . '@';
12215d603e7SGreg Roach
12315d603e7SGreg Roach            // Add the census fact to the rest of the household
12415d603e7SGreg Roach            foreach (array_keys($ca_individuals) as $xref) {
125c0935879SGreg Roach                if ($xref !== $individual->xref()) {
126f4afa648SGreg Roach                    Individual::getInstance($xref, $individual->tree())
12715d603e7SGreg Roach                        ->updateFact($fact_id, $newged, !$keep_chan);
12815d603e7SGreg Roach                }
12915d603e7SGreg Roach            }
13015d603e7SGreg Roach        }
13115d603e7SGreg Roach
13215d603e7SGreg Roach        return $newged;
13315d603e7SGreg Roach    }
13415d603e7SGreg Roach
13515d603e7SGreg Roach    /**
13615d603e7SGreg Roach     * @param CensusInterface $census
13715d603e7SGreg Roach     * @param string          $ca_title
13815d603e7SGreg Roach     * @param string          $ca_place
13915d603e7SGreg Roach     * @param string          $ca_citation
14015d603e7SGreg Roach     * @param string[][]      $ca_individuals
14115d603e7SGreg Roach     * @param string          $ca_notes
14215d603e7SGreg Roach     *
14315d603e7SGreg Roach     * @return string
14415d603e7SGreg Roach     */
1458f53f488SRico Sonntag    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
146c1010edaSGreg Roach    {
1470d46ec71SGreg Roach        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
14815d603e7SGreg Roach
14915d603e7SGreg Roach        foreach ($census->columns() as $n => $column) {
1500d46ec71SGreg Roach            if ($n === 0) {
1510d46ec71SGreg Roach                $text .= "\n";
1520d46ec71SGreg Roach            } else {
15315d603e7SGreg Roach                $text .= ' | ';
15415d603e7SGreg Roach            }
1550d46ec71SGreg Roach            $text .= $column->abbreviation();
1560d46ec71SGreg Roach        }
1570d46ec71SGreg Roach
1580d46ec71SGreg Roach        foreach ($census->columns() as $n => $column) {
1590d46ec71SGreg Roach            if ($n === 0) {
1600d46ec71SGreg Roach                $text .= "\n";
1610d46ec71SGreg Roach            } else {
1620d46ec71SGreg Roach                $text .= ' | ';
1630d46ec71SGreg Roach            }
1640d46ec71SGreg Roach            $text .= '-----';
16515d603e7SGreg Roach        }
16615d603e7SGreg Roach
16715d603e7SGreg Roach        foreach ($ca_individuals as $xref => $columns) {
16815d603e7SGreg Roach            $text .= "\n" . implode(' | ', $columns);
16915d603e7SGreg Roach        }
17015d603e7SGreg Roach
1710d46ec71SGreg Roach        return $text . "\n\n" . $ca_notes;
17240990b78SGreg Roach    }
17340990b78SGreg Roach
17440990b78SGreg Roach    /**
175ad51e0bbSGreg Roach     * Generate an HTML row of data for the census header
17652bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
17752bc9faeSGreg Roach     * Add suffix cell (delete button)
17852bc9faeSGreg Roach     *
179ad51e0bbSGreg Roach     * @param CensusInterface $census
18099f222b3SGreg Roach     *
181ad51e0bbSGreg Roach     * @return string
18299f222b3SGreg Roach     */
1835a62e0a6SGreg Roach    protected function censusTableHeader(CensusInterface $census): string
184c1010edaSGreg Roach    {
18552bc9faeSGreg Roach        $html = '';
186ad51e0bbSGreg Roach        foreach ($census->columns() as $column) {
18715d603e7SGreg Roach            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
18899f222b3SGreg Roach        }
18999f222b3SGreg Roach
19015d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
191ad51e0bbSGreg Roach    }
19299f222b3SGreg Roach
193ad51e0bbSGreg Roach    /**
194ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
19552bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
19652bc9faeSGreg Roach     * Add suffix cell (delete button)
19752bc9faeSGreg Roach     *
198ad51e0bbSGreg Roach     * @param CensusInterface $census
199ad51e0bbSGreg Roach     *
200ad51e0bbSGreg Roach     * @return string
201ad51e0bbSGreg Roach     */
2028f53f488SRico Sonntag    public static function censusTableEmptyRow(CensusInterface $census): string
203c1010edaSGreg Roach    {
20415d603e7SGreg 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>';
205ad51e0bbSGreg Roach    }
20699f222b3SGreg Roach
207ad51e0bbSGreg Roach    /**
208ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
20952bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
21052bc9faeSGreg Roach     * Add suffix cell (delete button)
21152bc9faeSGreg Roach     *
212ad51e0bbSGreg Roach     * @param CensusInterface $census
213ad51e0bbSGreg Roach     * @param Individual      $individual
214ad51e0bbSGreg Roach     * @param Individual      $head
215ad51e0bbSGreg Roach     *
216ad51e0bbSGreg Roach     * @return string
217ad51e0bbSGreg Roach     */
2188f53f488SRico Sonntag    public static function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
219c1010edaSGreg Roach    {
22015d603e7SGreg Roach        $html = '';
22115d603e7SGreg Roach        foreach ($census->columns() as $column) {
222c0935879SGreg 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->xref() . '][]"></td>';
22399f222b3SGreg Roach        }
22499f222b3SGreg Roach
225c0935879SGreg 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>';
22699f222b3SGreg Roach    }
2278c2e8227SGreg Roach}
228