xref: /webtrees/app/Module/CensusAssistantModule.php (revision 8b9cfadb614ce7d48a20883286cecb608bc36fbc)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg 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{
34961ec755SGreg Roach    /**
35961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
36961ec755SGreg Roach     *
37961ec755SGreg Roach     * @return string
38961ec755SGreg Roach     */
3949a243cbSGreg Roach    public function title(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Name of a module */
42bbb76c12SGreg Roach        return I18N::translate('Census assistant');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
45961ec755SGreg Roach    /**
46961ec755SGreg Roach     * A sentence describing what this module does.
47961ec755SGreg Roach     *
48961ec755SGreg Roach     * @return string
49961ec755SGreg Roach     */
5049a243cbSGreg Roach    public function description(): string
51c1010edaSGreg Roach    {
52bbb76c12SGreg Roach        /* I18N: Description of the “Census assistant” module */
53bbb76c12SGreg Roach        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
548c2e8227SGreg Roach    }
558c2e8227SGreg Roach
5676692c8bSGreg Roach    /**
579001c0b3SGreg Roach     * @param Request $request
589001c0b3SGreg Roach     *
599001c0b3SGreg Roach     * @return Response
609001c0b3SGreg Roach     */
61c1010edaSGreg Roach    public function getCensusHeaderAction(Request $request): Response
62c1010edaSGreg Roach    {
639001c0b3SGreg Roach        $census = $request->get('census');
649001c0b3SGreg Roach
6559f2f229SGreg Roach        $html = $this->censusTableHeader(new $census());
669001c0b3SGreg Roach
679001c0b3SGreg Roach        return new Response($html);
689001c0b3SGreg Roach    }
699001c0b3SGreg Roach
709001c0b3SGreg Roach    /**
719001c0b3SGreg Roach     * @param Request $request
72b6db7c1fSGreg Roach     * @param Tree    $tree
739001c0b3SGreg Roach     *
749001c0b3SGreg Roach     * @return Response
759001c0b3SGreg Roach     */
76b6db7c1fSGreg Roach    public function getCensusIndividualAction(Request $request, Tree $tree): Response
77c1010edaSGreg Roach    {
785b05d6adSGreg Roach        $census = $request->get('census', '');
799001c0b3SGreg Roach
805b05d6adSGreg Roach        $individual = Individual::getInstance($request->get('xref', ''), $tree);
815b05d6adSGreg Roach        $head       = Individual::getInstance($request->get('head', ''), $tree);
82d45cb9d3SGreg Roach
83d45cb9d3SGreg Roach        if ($individual instanceof Individual && $head instanceof Individual) {
8459f2f229SGreg Roach            $html = $this->censusTableRow(new $census(), $individual, $head);
859001c0b3SGreg Roach
869001c0b3SGreg Roach            return new Response($html);
87d45cb9d3SGreg Roach        } else {
88d45cb9d3SGreg Roach            throw new NotFoundHttpException();
89d45cb9d3SGreg Roach        }
909001c0b3SGreg Roach    }
919001c0b3SGreg Roach
929001c0b3SGreg Roach    /**
9315d603e7SGreg Roach     * @param Individual $individual
949001c0b3SGreg Roach     *
959001c0b3SGreg Roach     * @return string
968c2e8227SGreg Roach     */
978f53f488SRico Sonntag    public function createCensusAssistant(Individual $individual): string
98c1010edaSGreg Roach    {
99225e381fSGreg Roach        return view('modules/census-assistant', [
10034cd602eSGreg Roach            'individual' => $individual,
10134cd602eSGreg Roach        ]);
10215d603e7SGreg Roach    }
10315d603e7SGreg Roach
10415d603e7SGreg Roach    /**
105a45f9889SGreg Roach     * @param Request    $request
10615d603e7SGreg Roach     * @param Individual $individual
10760bc3e3fSGreg Roach     * @param string     $fact_id
10815d603e7SGreg Roach     * @param string     $newged
10960bc3e3fSGreg Roach     * @param bool       $keep_chan
11015d603e7SGreg Roach     *
11115d603e7SGreg Roach     * @return string
11215d603e7SGreg Roach     */
1138f53f488SRico Sonntag    public function updateCensusAssistant(Request $request, Individual $individual, $fact_id, $newged, $keep_chan): string
114c1010edaSGreg Roach    {
115a45f9889SGreg Roach        $ca_title       = $request->get('ca_title', '');
116a45f9889SGreg Roach        $ca_place       = $request->get('ca_place', '');
117a45f9889SGreg Roach        $ca_citation    = $request->get('ca_citation', '');
118a45f9889SGreg Roach        $ca_individuals = (array) $request->get('ca_individuals');
119a45f9889SGreg Roach        $ca_notes       = $request->get('ca_notes', '');
120a45f9889SGreg Roach        $ca_census      = $request->get('ca_census', '');
12115d603e7SGreg Roach
12215d603e7SGreg Roach        if ($ca_census !== '' && !empty($ca_individuals)) {
12359f2f229SGreg Roach            $census = new $ca_census();
12415d603e7SGreg Roach
12515d603e7SGreg Roach            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
126afb591d7SGreg Roach            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
127f4afa648SGreg Roach            $note        = $individual->tree()->createRecord($note_gedcom);
12815d603e7SGreg Roach
129c0935879SGreg Roach            $newged .= "\n2 NOTE @" . $note->xref() . '@';
13015d603e7SGreg Roach
13115d603e7SGreg Roach            // Add the census fact to the rest of the household
13215d603e7SGreg Roach            foreach (array_keys($ca_individuals) as $xref) {
133c0935879SGreg Roach                if ($xref !== $individual->xref()) {
134f4afa648SGreg Roach                    Individual::getInstance($xref, $individual->tree())
13515d603e7SGreg Roach                        ->updateFact($fact_id, $newged, !$keep_chan);
13615d603e7SGreg Roach                }
13715d603e7SGreg Roach            }
13815d603e7SGreg Roach        }
13915d603e7SGreg Roach
14015d603e7SGreg Roach        return $newged;
14115d603e7SGreg Roach    }
14215d603e7SGreg Roach
14315d603e7SGreg Roach    /**
14415d603e7SGreg Roach     * @param CensusInterface $census
14515d603e7SGreg Roach     * @param string          $ca_title
14615d603e7SGreg Roach     * @param string          $ca_place
14715d603e7SGreg Roach     * @param string          $ca_citation
14815d603e7SGreg Roach     * @param string[][]      $ca_individuals
14915d603e7SGreg Roach     * @param string          $ca_notes
15015d603e7SGreg Roach     *
15115d603e7SGreg Roach     * @return string
15215d603e7SGreg Roach     */
1538f53f488SRico Sonntag    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
154c1010edaSGreg Roach    {
1550d46ec71SGreg Roach        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
15615d603e7SGreg Roach
15715d603e7SGreg Roach        foreach ($census->columns() as $n => $column) {
1580d46ec71SGreg Roach            if ($n === 0) {
1590d46ec71SGreg Roach                $text .= "\n";
1600d46ec71SGreg Roach            } else {
16115d603e7SGreg Roach                $text .= ' | ';
16215d603e7SGreg Roach            }
1630d46ec71SGreg Roach            $text .= $column->abbreviation();
1640d46ec71SGreg Roach        }
1650d46ec71SGreg Roach
1660d46ec71SGreg Roach        foreach ($census->columns() as $n => $column) {
1670d46ec71SGreg Roach            if ($n === 0) {
1680d46ec71SGreg Roach                $text .= "\n";
1690d46ec71SGreg Roach            } else {
1700d46ec71SGreg Roach                $text .= ' | ';
1710d46ec71SGreg Roach            }
1720d46ec71SGreg Roach            $text .= '-----';
17315d603e7SGreg Roach        }
17415d603e7SGreg Roach
17515d603e7SGreg Roach        foreach ($ca_individuals as $xref => $columns) {
17615d603e7SGreg Roach            $text .= "\n" . implode(' | ', $columns);
17715d603e7SGreg Roach        }
17815d603e7SGreg Roach
1790d46ec71SGreg Roach        return $text . "\n\n" . $ca_notes;
18040990b78SGreg Roach    }
18140990b78SGreg Roach
18240990b78SGreg Roach    /**
183ad51e0bbSGreg Roach     * Generate an HTML row of data for the census header
18452bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
18552bc9faeSGreg Roach     * Add suffix cell (delete button)
18652bc9faeSGreg Roach     *
187ad51e0bbSGreg Roach     * @param CensusInterface $census
18899f222b3SGreg Roach     *
189ad51e0bbSGreg Roach     * @return string
19099f222b3SGreg Roach     */
1915a62e0a6SGreg Roach    protected function censusTableHeader(CensusInterface $census): string
192c1010edaSGreg Roach    {
19352bc9faeSGreg Roach        $html = '';
194ad51e0bbSGreg Roach        foreach ($census->columns() as $column) {
19515d603e7SGreg Roach            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
19699f222b3SGreg Roach        }
19799f222b3SGreg Roach
19815d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></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     *
208ad51e0bbSGreg Roach     * @return string
209ad51e0bbSGreg Roach     */
210*8b9cfadbSGreg Roach    public function censusTableEmptyRow(CensusInterface $census): string
211c1010edaSGreg Roach    {
21215d603e7SGreg 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>';
213ad51e0bbSGreg Roach    }
21499f222b3SGreg Roach
215ad51e0bbSGreg Roach    /**
216ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
21752bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
21852bc9faeSGreg Roach     * Add suffix cell (delete button)
21952bc9faeSGreg Roach     *
220ad51e0bbSGreg Roach     * @param CensusInterface $census
221ad51e0bbSGreg Roach     * @param Individual      $individual
222ad51e0bbSGreg Roach     * @param Individual      $head
223ad51e0bbSGreg Roach     *
224ad51e0bbSGreg Roach     * @return string
225ad51e0bbSGreg Roach     */
226*8b9cfadbSGreg Roach    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
227c1010edaSGreg Roach    {
22815d603e7SGreg Roach        $html = '';
22915d603e7SGreg Roach        foreach ($census->columns() as $column) {
230c0935879SGreg 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>';
23199f222b3SGreg Roach        }
23299f222b3SGreg Roach
233c0935879SGreg 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>';
23499f222b3SGreg Roach    }
2358c2e8227SGreg Roach}
236