xref: /webtrees/app/Module/CensusAssistantModule.php (revision a45f98897789fc9ff88705eb09ae5f037bf49c10)
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 */
1615d603e7SGreg Roach
1776692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1876692c8bSGreg Roach
19ad51e0bbSGreg Roachuse Fisharebest\Webtrees\Census\CensusInterface;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2199f222b3SGreg Roachuse Fisharebest\Webtrees\Individual;
229001c0b3SGreg Roachuse Fisharebest\Webtrees\Tree;
239001c0b3SGreg Roachuse Symfony\Component\HttpFoundation\Request;
249001c0b3SGreg Roachuse Symfony\Component\HttpFoundation\Response;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class CensusAssistantModule
288c2e8227SGreg Roach */
29c1010edaSGreg Roachclass CensusAssistantModule extends AbstractModule
30c1010edaSGreg Roach{
318c2e8227SGreg Roach    /** {@inheritdoc} */
32c1010edaSGreg Roach    public function getTitle()
33c1010edaSGreg Roach    {
34bbb76c12SGreg Roach        /* I18N: Name of a module */
35bbb76c12SGreg Roach        return I18N::translate('Census assistant');
368c2e8227SGreg Roach    }
378c2e8227SGreg Roach
388c2e8227SGreg Roach    /** {@inheritdoc} */
39c1010edaSGreg Roach    public function getDescription()
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Description of the “Census assistant” module */
42bbb76c12SGreg Roach        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
4576692c8bSGreg Roach    /**
469001c0b3SGreg Roach     * @param Request $request
479001c0b3SGreg Roach     *
489001c0b3SGreg Roach     * @return Response
499001c0b3SGreg Roach     */
50c1010edaSGreg Roach    public function getCensusHeaderAction(Request $request): Response
51c1010edaSGreg Roach    {
529001c0b3SGreg Roach        $census = $request->get('census');
539001c0b3SGreg Roach
5459f2f229SGreg Roach        $html = $this->censusTableHeader(new $census());
559001c0b3SGreg Roach
569001c0b3SGreg Roach        return new Response($html);
579001c0b3SGreg Roach    }
589001c0b3SGreg Roach
599001c0b3SGreg Roach    /**
609001c0b3SGreg Roach     * @param Request $request
61b6db7c1fSGreg Roach     * @param Tree    $tree
629001c0b3SGreg Roach     *
639001c0b3SGreg Roach     * @return Response
649001c0b3SGreg Roach     */
65b6db7c1fSGreg Roach    public function getCensusIndividualAction(Request $request, Tree $tree): Response
66c1010edaSGreg Roach    {
679001c0b3SGreg Roach        $census = $request->get('census');
689001c0b3SGreg Roach
699001c0b3SGreg Roach        $individual = Individual::getInstance($request->get('xref'), $tree);
709001c0b3SGreg Roach        $head       = Individual::getInstance($request->get('head'), $tree);
7159f2f229SGreg Roach        $html       = $this->censusTableRow(new $census(), $individual, $head);
729001c0b3SGreg Roach
739001c0b3SGreg Roach        return new Response($html);
749001c0b3SGreg Roach    }
759001c0b3SGreg Roach
769001c0b3SGreg Roach    /**
7715d603e7SGreg Roach     * @param Individual $individual
789001c0b3SGreg Roach     *
799001c0b3SGreg Roach     * @return string
808c2e8227SGreg Roach     */
81c1010edaSGreg Roach    public function createCensusAssistant(Individual $individual)
82c1010edaSGreg Roach    {
83225e381fSGreg Roach        return view('modules/census-assistant', [
8434cd602eSGreg Roach            'individual' => $individual,
8534cd602eSGreg Roach        ]);
8615d603e7SGreg Roach    }
8715d603e7SGreg Roach
8815d603e7SGreg Roach    /**
89*a45f9889SGreg Roach     * @param Request    $request
9015d603e7SGreg Roach     * @param Individual $individual
9160bc3e3fSGreg Roach     * @param string     $fact_id
9215d603e7SGreg Roach     * @param string     $newged
9360bc3e3fSGreg Roach     * @param bool       $keep_chan
9415d603e7SGreg Roach     *
9515d603e7SGreg Roach     * @return string
9615d603e7SGreg Roach     */
97*a45f9889SGreg Roach    public function updateCensusAssistant(Request $request, Individual $individual, $fact_id, $newged, $keep_chan)
98c1010edaSGreg Roach    {
99*a45f9889SGreg Roach        $ca_title       = $request->get('ca_title', '');
100*a45f9889SGreg Roach        $ca_place       = $request->get('ca_place', '');
101*a45f9889SGreg Roach        $ca_citation    = $request->get('ca_citation', '');
102*a45f9889SGreg Roach        $ca_individuals = (array) $request->get('ca_individuals');
103*a45f9889SGreg Roach        $ca_notes       = $request->get('ca_notes', '');
104*a45f9889SGreg Roach        $ca_census      = $request->get('ca_census', '');
10515d603e7SGreg Roach
10615d603e7SGreg Roach        if ($ca_census !== '' && !empty($ca_individuals)) {
10759f2f229SGreg Roach            $census = new $ca_census();
10815d603e7SGreg Roach
10915d603e7SGreg Roach            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
11015d603e7SGreg Roach            $note_gedcom = '0 @new@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
11115d603e7SGreg Roach            $note        = $individual->getTree()->createRecord($note_gedcom);
11215d603e7SGreg Roach
11315d603e7SGreg Roach            $newged .= "\n2 NOTE @" . $note->getXref() . '@';
11415d603e7SGreg Roach
11515d603e7SGreg Roach            // Add the census fact to the rest of the household
11615d603e7SGreg Roach            foreach (array_keys($ca_individuals) as $xref) {
11715d603e7SGreg Roach                if ($xref !== $individual->getXref()) {
11815d603e7SGreg Roach                    Individual::getInstance($xref, $individual->getTree())
11915d603e7SGreg Roach                        ->updateFact($fact_id, $newged, !$keep_chan);
12015d603e7SGreg Roach                }
12115d603e7SGreg Roach            }
12215d603e7SGreg Roach        }
12315d603e7SGreg Roach
12415d603e7SGreg Roach        return $newged;
12515d603e7SGreg Roach    }
12615d603e7SGreg Roach
12715d603e7SGreg Roach    /**
12815d603e7SGreg Roach     * @param CensusInterface $census
12915d603e7SGreg Roach     * @param string          $ca_title
13015d603e7SGreg Roach     * @param string          $ca_place
13115d603e7SGreg Roach     * @param string          $ca_citation
13215d603e7SGreg Roach     * @param string[][]      $ca_individuals
13315d603e7SGreg Roach     * @param string          $ca_notes
13415d603e7SGreg Roach     *
13515d603e7SGreg Roach     * @return string
13615d603e7SGreg Roach     */
137c1010edaSGreg Roach    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes)
138c1010edaSGreg Roach    {
1390d46ec71SGreg Roach        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
14015d603e7SGreg Roach
14115d603e7SGreg Roach        foreach ($census->columns() as $n => $column) {
1420d46ec71SGreg Roach            if ($n === 0) {
1430d46ec71SGreg Roach                $text .= "\n";
1440d46ec71SGreg Roach            } else {
14515d603e7SGreg Roach                $text .= ' | ';
14615d603e7SGreg Roach            }
1470d46ec71SGreg Roach            $text .= $column->abbreviation();
1480d46ec71SGreg Roach        }
1490d46ec71SGreg Roach
1500d46ec71SGreg Roach        foreach ($census->columns() as $n => $column) {
1510d46ec71SGreg Roach            if ($n === 0) {
1520d46ec71SGreg Roach                $text .= "\n";
1530d46ec71SGreg Roach            } else {
1540d46ec71SGreg Roach                $text .= ' | ';
1550d46ec71SGreg Roach            }
1560d46ec71SGreg Roach            $text .= '-----';
15715d603e7SGreg Roach        }
15815d603e7SGreg Roach
15915d603e7SGreg Roach        foreach ($ca_individuals as $xref => $columns) {
16015d603e7SGreg Roach            $text .= "\n" . implode(' | ', $columns);
16115d603e7SGreg Roach        }
16215d603e7SGreg Roach
1630d46ec71SGreg Roach        return $text . "\n\n" . $ca_notes;
16440990b78SGreg Roach    }
16540990b78SGreg Roach
16640990b78SGreg Roach    /**
167ad51e0bbSGreg Roach     * Generate an HTML row of data for the census header
16852bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
16952bc9faeSGreg Roach     * Add suffix cell (delete button)
17052bc9faeSGreg Roach     *
171ad51e0bbSGreg Roach     * @param CensusInterface $census
17299f222b3SGreg Roach     *
173ad51e0bbSGreg Roach     * @return string
17499f222b3SGreg Roach     */
175c1010edaSGreg Roach    public static function censusTableHeader(CensusInterface $census)
176c1010edaSGreg Roach    {
17752bc9faeSGreg Roach        $html = '';
178ad51e0bbSGreg Roach        foreach ($census->columns() as $column) {
17915d603e7SGreg Roach            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
18099f222b3SGreg Roach        }
18199f222b3SGreg Roach
18215d603e7SGreg Roach        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
183ad51e0bbSGreg Roach    }
18499f222b3SGreg Roach
185ad51e0bbSGreg Roach    /**
186ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
18752bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
18852bc9faeSGreg Roach     * Add suffix cell (delete button)
18952bc9faeSGreg Roach     *
190ad51e0bbSGreg Roach     * @param CensusInterface $census
191ad51e0bbSGreg Roach     *
192ad51e0bbSGreg Roach     * @return string
193ad51e0bbSGreg Roach     */
194c1010edaSGreg Roach    public static function censusTableEmptyRow(CensusInterface $census)
195c1010edaSGreg Roach    {
19615d603e7SGreg 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>';
197ad51e0bbSGreg Roach    }
19899f222b3SGreg Roach
199ad51e0bbSGreg Roach    /**
200ad51e0bbSGreg Roach     * Generate an HTML row of data for the census
20152bc9faeSGreg Roach     * Add prefix cell (store XREF and drag/drop)
20252bc9faeSGreg Roach     * Add suffix cell (delete button)
20352bc9faeSGreg Roach     *
204ad51e0bbSGreg Roach     * @param CensusInterface $census
205ad51e0bbSGreg Roach     * @param Individual      $individual
206ad51e0bbSGreg Roach     * @param Individual      $head
207ad51e0bbSGreg Roach     *
208ad51e0bbSGreg Roach     * @return string
209ad51e0bbSGreg Roach     */
210c1010edaSGreg Roach    public static function censusTableRow(CensusInterface $census, Individual $individual, Individual $head)
211c1010edaSGreg Roach    {
21215d603e7SGreg Roach        $html = '';
21315d603e7SGreg Roach        foreach ($census->columns() as $column) {
21415d603e7SGreg 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>';
21599f222b3SGreg Roach        }
21699f222b3SGreg Roach
21715d603e7SGreg 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>';
21899f222b3SGreg Roach    }
2198c2e8227SGreg Roach}
220