xref: /webtrees/app/Module/CensusAssistantModule.php (revision bd44f43fad3efd8fd7f063363bfcb5a97080fa7d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17namespace Fisharebest\Webtrees\Module;
18
19use Fisharebest\Webtrees\Census\CensusInterface;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Tree;
24use Symfony\Component\HttpFoundation\Request;
25use Symfony\Component\HttpFoundation\Response;
26
27/**
28 * Class CensusAssistantModule
29 */
30class CensusAssistantModule extends AbstractModule
31{
32    /** {@inheritdoc} */
33    public function getTitle()
34    {
35        return /* I18N: Name of a module */
36            I18N::translate('Census assistant');
37    }
38
39    /** {@inheritdoc} */
40    public function getDescription()
41    {
42        return /* I18N: Description of the “Census assistant” module */
43            I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
44    }
45
46    /**
47     * @param Request $request
48     *
49     * @return Response
50     */
51    public function getCensusHeaderAction(Request $request): Response
52    {
53        $census = $request->get('census');
54
55        $html = $this->censusTableHeader(new $census);
56
57        return new Response($html);
58    }
59
60    /**
61     * @param Request $request
62     * @param Tree    $tree
63     *
64     * @return Response
65     */
66    public function getCensusIndividualAction(Request $request, Tree $tree): Response
67    {
68        $census = $request->get('census');
69
70        $individual = Individual::getInstance($request->get('xref'), $tree);
71        $head       = Individual::getInstance($request->get('head'), $tree);
72        $html       = $this->censusTableRow(new $census, $individual, $head);
73
74        return new Response($html);
75    }
76
77    /**
78     * @param Individual $individual
79     *
80     * @return string
81     */
82    public function createCensusAssistant(Individual $individual)
83    {
84        return view('modules/census-assistant', [
85            'individual' => $individual,
86        ]);
87    }
88
89    /**
90     * @param Individual $individual
91     * @param string     $fact_id
92     * @param string     $newged
93     * @param bool       $keep_chan
94     *
95     * @return string
96     */
97    public function updateCensusAssistant(Individual $individual, $fact_id, $newged, $keep_chan)
98    {
99        $ca_title       = Filter::post('ca_title');
100        $ca_place       = Filter::post('ca_place');
101        $ca_citation    = Filter::post('ca_citation');
102        $ca_individuals = Filter::postArray('ca_individuals');
103        $ca_notes       = Filter::post('ca_notes');
104        $ca_census      = Filter::post('ca_census', 'Fisharebest\\\\Webtrees\\\\Census\\\\CensusOf[A-Za-z0-9]+');
105
106        if ($ca_census !== '' && !empty($ca_individuals)) {
107            $census = new $ca_census;
108
109            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
110            $note_gedcom = '0 @new@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
111            $note        = $individual->getTree()->createRecord($note_gedcom);
112
113            $newged .= "\n2 NOTE @" . $note->getXref() . '@';
114
115            // Add the census fact to the rest of the household
116            foreach (array_keys($ca_individuals) as $xref) {
117                if ($xref !== $individual->getXref()) {
118                    Individual::getInstance($xref, $individual->getTree())
119                        ->updateFact($fact_id, $newged, !$keep_chan);
120                }
121            }
122        }
123
124        return $newged;
125    }
126
127    /**
128     * @param CensusInterface $census
129     * @param string          $ca_title
130     * @param string          $ca_place
131     * @param string          $ca_citation
132     * @param string[][]      $ca_individuals
133     * @param string          $ca_notes
134     *
135     * @return string
136     */
137    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes)
138    {
139        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
140
141        foreach ($census->columns() as $n => $column) {
142            if ($n === 0) {
143                $text .= "\n";
144            } else {
145                $text .= ' | ';
146            }
147            $text .= $column->abbreviation();
148        }
149
150        foreach ($census->columns() as $n => $column) {
151            if ($n === 0) {
152                $text .= "\n";
153            } else {
154                $text .= ' | ';
155            }
156            $text .= '-----';
157        }
158
159        foreach ($ca_individuals as $xref => $columns) {
160            $text .= "\n" . implode(' | ', $columns);
161        }
162
163        return $text . "\n\n" . $ca_notes;
164    }
165
166    /**
167     * Generate an HTML row of data for the census header
168     * Add prefix cell (store XREF and drag/drop)
169     * Add suffix cell (delete button)
170     *
171     * @param CensusInterface $census
172     *
173     * @return string
174     */
175    public static function censusTableHeader(CensusInterface $census)
176    {
177        $html = '';
178        foreach ($census->columns() as $column) {
179            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
180        }
181
182        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
183    }
184
185    /**
186     * Generate an HTML row of data for the census
187     * Add prefix cell (store XREF and drag/drop)
188     * Add suffix cell (delete button)
189     *
190     * @param CensusInterface $census
191     *
192     * @return string
193     */
194    public static function censusTableEmptyRow(CensusInterface $census)
195    {
196        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>';
197    }
198
199    /**
200     * Generate an HTML row of data for the census
201     * Add prefix cell (store XREF and drag/drop)
202     * Add suffix cell (delete button)
203     *
204     * @param CensusInterface $census
205     * @param Individual      $individual
206     * @param Individual      $head
207     *
208     * @return string
209     */
210    public static function censusTableRow(CensusInterface $census, Individual $individual, Individual $head)
211    {
212        $html = '';
213        foreach ($census->columns() as $column) {
214            $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>';
215        }
216
217        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>';
218    }
219}
220