xref: /webtrees/app/Module/CensusAssistantModule.php (revision 356c61b15f7d84f8732f8bf5d25319719f1bf14b)
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\Census;
20use Fisharebest\Webtrees\Census\CensusInterface;
21use Fisharebest\Webtrees\Family;
22use Fisharebest\Webtrees\Filter;
23use Fisharebest\Webtrees\Functions\FunctionsDb;
24use Fisharebest\Webtrees\GedcomRecord;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Tree;
28use Symfony\Component\HttpFoundation\Request;
29use Symfony\Component\HttpFoundation\Response;
30
31/**
32 * Class CensusAssistantModule
33 */
34class CensusAssistantModule extends AbstractModule {
35	/** {@inheritdoc} */
36	public function getTitle() {
37		return /* I18N: Name of a module */
38			I18N::translate('Census assistant');
39	}
40
41	/** {@inheritdoc} */
42	public function getDescription() {
43		return /* I18N: Description of the “Census assistant” module */
44			I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
45	}
46
47	/**
48	 * @param Request $request
49	 *
50	 * @return Response
51	 */
52	public function getCensusHeaderAction(Request $request): Response {
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	 *
63	 * @return Response
64	 */
65	public function getCensusIndividualAction(Request $request): Response {
66		/** @var Tree $tree */
67		$tree = $request->attributes->get('tree');
68
69		$census = $request->get('census');
70
71		$individual = Individual::getInstance($request->get('xref'), $tree);
72		$head       = Individual::getInstance($request->get('head'), $tree);
73		$html       = $this->censusTableRow(new $census, $individual, $head);
74
75		return new Response($html);
76	}
77
78	/**
79	 * @param Individual $individual
80	 *
81	 * @return string
82	 */
83	public function createCensusAssistant(Individual $individual) {
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		$ca_title       = Filter::post('ca_title');
99		$ca_place       = Filter::post('ca_place');
100		$ca_citation    = Filter::post('ca_citation');
101		$ca_individuals = Filter::postArray('ca_individuals');
102		$ca_notes       = Filter::post('ca_notes');
103		$ca_census      = Filter::post('ca_census', 'Fisharebest\\\\Webtrees\\\\Census\\\\CensusOf[A-Za-z0-9]+');
104
105		if ($ca_census !== '' && !empty($ca_individuals)) {
106			$census = new $ca_census;
107
108			$note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
109			$note_gedcom = '0 @new@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
110			$note        = $individual->getTree()->createRecord($note_gedcom);
111
112			$newged .= "\n2 NOTE @" . $note->getXref() . '@';
113
114			// Add the census fact to the rest of the household
115			foreach (array_keys($ca_individuals) as $xref) {
116				if ($xref !== $individual->getXref()) {
117					Individual::getInstance($xref, $individual->getTree())
118						->updateFact($fact_id, $newged, !$keep_chan);
119				}
120			}
121		}
122
123		return $newged;
124	}
125
126	/**
127	 * @param CensusInterface $census
128	 * @param string          $ca_title
129	 * @param string          $ca_place
130	 * @param string          $ca_citation
131	 * @param string[][]      $ca_individuals
132	 * @param string          $ca_notes
133	 *
134	 * @return string
135	 */
136	private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes) {
137		$text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
138
139		foreach ($census->columns() as $n => $column) {
140			if ($n === 0) {
141				$text .= "\n";
142			} else {
143				$text .= ' | ';
144			}
145			$text .= $column->abbreviation();
146		}
147
148		foreach ($census->columns() as $n => $column) {
149			if ($n === 0) {
150				$text .= "\n";
151			} else {
152				$text .= ' | ';
153			}
154			$text .= '-----';
155		}
156
157		foreach ($ca_individuals as $xref => $columns) {
158			$text .= "\n" . implode(' | ', $columns);
159		}
160
161		return $text . "\n\n" . $ca_notes;
162	}
163
164	/**
165	 * Generate an HTML row of data for the census header
166	 * Add prefix cell (store XREF and drag/drop)
167	 * Add suffix cell (delete button)
168	 *
169	 * @param CensusInterface $census
170	 *
171	 * @return string
172	 */
173	public static function censusTableHeader(CensusInterface $census) {
174		$html = '';
175		foreach ($census->columns() as $column) {
176			$html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
177		}
178
179		return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
180	}
181
182	/**
183	 * Generate an HTML row of data for the census
184	 * Add prefix cell (store XREF and drag/drop)
185	 * Add suffix cell (delete button)
186	 *
187	 * @param CensusInterface $census
188	 *
189	 * @return string
190	 */
191	public static function censusTableEmptyRow(CensusInterface $census) {
192		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>';
193	}
194
195	/**
196	 * Generate an HTML row of data for the census
197	 * Add prefix cell (store XREF and drag/drop)
198	 * Add suffix cell (delete button)
199	 *
200	 * @param CensusInterface $census
201	 * @param Individual      $individual
202	 * @param Individual      $head
203	 *
204	 * @return string
205	 */
206	public static function censusTableRow(CensusInterface $census, Individual $individual, Individual $head) {
207		$html = '';
208		foreach ($census->columns() as $column) {
209			$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>';
210		}
211
212		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>';
213	}
214}
215