xref: /webtrees/app/Module/CensusAssistantModule.php (revision b6db7c1f82484c632c75d5225ce767d398ddc27f)
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\Filter;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2299f222b3SGreg Roachuse Fisharebest\Webtrees\Individual;
239001c0b3SGreg Roachuse Fisharebest\Webtrees\Tree;
249001c0b3SGreg Roachuse Symfony\Component\HttpFoundation\Request;
259001c0b3SGreg Roachuse Symfony\Component\HttpFoundation\Response;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class CensusAssistantModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass CensusAssistantModule extends AbstractModule
31c1010edaSGreg Roach{
328c2e8227SGreg Roach    /** {@inheritdoc} */
33c1010edaSGreg Roach    public function getTitle()
34c1010edaSGreg Roach    {
3515d603e7SGreg Roach        return /* I18N: Name of a module */
3615d603e7SGreg Roach            I18N::translate('Census assistant');
378c2e8227SGreg Roach    }
388c2e8227SGreg Roach
398c2e8227SGreg Roach    /** {@inheritdoc} */
40c1010edaSGreg Roach    public function getDescription()
41c1010edaSGreg Roach    {
4215d603e7SGreg Roach        return /* I18N: Description of the “Census assistant” module */
4315d603e7SGreg Roach            I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
4676692c8bSGreg Roach    /**
479001c0b3SGreg Roach     * @param Request $request
489001c0b3SGreg Roach     *
499001c0b3SGreg Roach     * @return Response
509001c0b3SGreg Roach     */
51c1010edaSGreg Roach    public function getCensusHeaderAction(Request $request): Response
52c1010edaSGreg Roach    {
539001c0b3SGreg Roach        $census = $request->get('census');
549001c0b3SGreg Roach
559001c0b3SGreg Roach        $html = $this->censusTableHeader(new $census);
569001c0b3SGreg Roach
579001c0b3SGreg Roach        return new Response($html);
589001c0b3SGreg Roach    }
599001c0b3SGreg Roach
609001c0b3SGreg Roach    /**
619001c0b3SGreg Roach     * @param Request $request
62*b6db7c1fSGreg Roach     * @param Tree    $tree
639001c0b3SGreg Roach     *
649001c0b3SGreg Roach     * @return Response
659001c0b3SGreg Roach     */
66*b6db7c1fSGreg Roach    public function getCensusIndividualAction(Request $request, Tree $tree): Response
67c1010edaSGreg Roach    {
689001c0b3SGreg Roach        $census = $request->get('census');
699001c0b3SGreg Roach
709001c0b3SGreg Roach        $individual = Individual::getInstance($request->get('xref'), $tree);
719001c0b3SGreg Roach        $head       = Individual::getInstance($request->get('head'), $tree);
729001c0b3SGreg Roach        $html       = $this->censusTableRow(new $census, $individual, $head);
739001c0b3SGreg Roach
749001c0b3SGreg Roach        return new Response($html);
759001c0b3SGreg Roach    }
769001c0b3SGreg Roach
779001c0b3SGreg Roach    /**
7815d603e7SGreg Roach     * @param Individual $individual
799001c0b3SGreg Roach     *
809001c0b3SGreg Roach     * @return string
818c2e8227SGreg Roach     */
82c1010edaSGreg Roach    public function createCensusAssistant(Individual $individual)
83c1010edaSGreg Roach    {
84225e381fSGreg Roach        return view('modules/census-assistant', [
8534cd602eSGreg Roach            'individual' => $individual,
8634cd602eSGreg Roach        ]);
8715d603e7SGreg Roach    }
8815d603e7SGreg Roach
8915d603e7SGreg Roach    /**
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     */
97c1010edaSGreg Roach    public function updateCensusAssistant(Individual $individual, $fact_id, $newged, $keep_chan)
98c1010edaSGreg Roach    {
9915d603e7SGreg Roach        $ca_title       = Filter::post('ca_title');
10015d603e7SGreg Roach        $ca_place       = Filter::post('ca_place');
10115d603e7SGreg Roach        $ca_citation    = Filter::post('ca_citation');
10215d603e7SGreg Roach        $ca_individuals = Filter::postArray('ca_individuals');
10315d603e7SGreg Roach        $ca_notes       = Filter::post('ca_notes');
10415d603e7SGreg Roach        $ca_census      = Filter::post('ca_census', 'Fisharebest\\\\Webtrees\\\\Census\\\\CensusOf[A-Za-z0-9]+');
10515d603e7SGreg Roach
10615d603e7SGreg Roach        if ($ca_census !== '' && !empty($ca_individuals)) {
10715d603e7SGreg 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