xref: /webtrees/app/Module/ResearchTaskModule.php (revision 2a6fda6001c209d27013f958519efeae6d48e2fb)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Bootstrap4;
20use Fisharebest\Webtrees\Database;
21use Fisharebest\Webtrees\Datatables;
22use Fisharebest\Webtrees\Filter;
23use Fisharebest\Webtrees\FontAwesome;
24use Fisharebest\Webtrees\Functions\FunctionsEdit;
25use Fisharebest\Webtrees\GedcomRecord;
26use Fisharebest\Webtrees\Html;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Theme;
29
30/**
31 * Class ResearchTaskModule
32 */
33class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
34	const DEFAULT_SHOW_OTHER      = '1';
35	const DEFAULT_SHOW_UNASSIGNED = '1';
36	const DEFAULT_SHOW_FUTURE     = '1';
37	const DEFAULT_BLOCK           = '1';
38
39	/** {@inheritdoc} */
40	public function getTitle() {
41		return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks');
42	}
43
44	/** {@inheritdoc} */
45	public function getDescription() {
46		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
47	}
48
49	/**
50	 * Generate the HTML content of this block.
51	 *
52	 * @param int      $block_id
53	 * @param bool     $template
54	 * @param string[] $cfg
55	 *
56	 * @return string
57	 */
58	public function getBlock($block_id, $template = true, $cfg = []) {
59		global $ctype, $WT_TREE;
60
61		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
62		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
63		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
64
65		foreach (['show_unassigned', 'show_other', 'show_future'] as $name) {
66			if (array_key_exists($name, $cfg)) {
67				$$name = $cfg[$name];
68			}
69		}
70
71		$id    = $this->getName() . $block_id;
72		$class = $this->getName() . '_block';
73		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
74			$title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' ';
75		} else {
76			$title = '';
77		}
78		$title .= $this->getTitle();
79
80		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
81
82		$xrefs = Database::prepare(
83			"SELECT DISTINCT d_gid FROM `##dates`" .
84			" WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
85		)->execute([
86			'tree_id' => $WT_TREE->getTreeId(),
87			'jd'      => $end_jd,
88		])->fetchOneColumn();
89
90		$content = '';
91		$content .= '<table ' . Datatables::researchTaskTableAttributes() . '>';
92		$content .= '<thead><tr>';
93		$content .= '<th>' . I18N::translate('Date') . '</th>';
94		$content .= '<th>' . I18N::translate('Record') . '</th>';
95		$content .= '<th>' . I18N::translate('Username') . '</th>';
96		$content .= '<th>' . I18N::translate('Research task') . '</th>';
97		$content .= '</tr></thead><tbody>';
98
99		foreach ($xrefs as $xref) {
100			$record = GedcomRecord::getInstance($xref, $WT_TREE);
101			if ($record->canShow()) {
102				foreach ($record->getFacts('_TODO') as $fact) {
103					$user_name = $fact->getAttribute('_WT_USER');
104					if ($user_name === Auth::user()->getUserName() || !$user_name && $show_unassigned || $user_name && $show_other) {
105						$content .= '<tr>';
106						$content .= '<td data-sort="' . $fact->getDate()->julianDay() . '">' . $fact->getDate()->display() . '</td>';
107						$content .= '<td data-sort="' . Html::escape($record->getSortName()) . '"><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
108						$content .= '<td>' . $user_name . '</td>';
109						$content .= '<td dir="auto">' . $fact->getValue() . '</td>';
110						$content .= '</tr>';
111					}
112				}
113			}
114		}
115
116		$content .= '</tbody></table>';
117		if (empty($xrefs)) {
118			$content .= '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
119		}
120
121		if ($template) {
122			return Theme::theme()->formatBlock($id, $title, $class, $content);
123		} else {
124			return $content;
125		}
126	}
127
128	/** {@inheritdoc} */
129	public function loadAjax() {
130		return false;
131	}
132
133	/** {@inheritdoc} */
134	public function isUserBlock() {
135		return true;
136	}
137
138	/** {@inheritdoc} */
139	public function isGedcomBlock() {
140		return true;
141	}
142
143	/**
144	 * An HTML form to edit block settings
145	 *
146	 * @param int $block_id
147	 */
148	public function configureBlock($block_id) {
149		if (Filter::postBool('save') && Filter::checkCsrf()) {
150			$this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other'));
151			$this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned'));
152			$this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future'));
153		}
154
155		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
156		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
157		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
158
159		?>
160		<p>
161			<?= I18N::translate('Research tasks are special events, added to individuals in your family tree, which identify the need for further research. You can use them as a reminder to check facts against more reliable sources, to obtain documents or photographs, to resolve conflicting information, etc.') ?>
162			<?= I18N::translate('To create new research tasks, you must first add “research task” to the list of facts and events in the family tree’s preferences.') ?>
163			<?= I18N::translate('Research tasks are stored using the custom GEDCOM tag “_TODO”. Other genealogy applications may not recognize this tag.') ?>
164		</p>
165		<?php
166
167		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_other">';
168		echo I18N::translate('Show research tasks that are assigned to other users');
169		echo '</div><div class="col-sm-9">';
170		echo Bootstrap4::radioButtons('show_other', FunctionsEdit::optionsNoYes(), $show_other, true);
171		echo '</div></div>';
172
173		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_unassigned">';
174		echo I18N::translate('Show research tasks that are not assigned to any user');
175		echo '</div><div class="col-sm-9">';
176		echo Bootstrap4::radioButtons('show_unassigned', FunctionsEdit::optionsNoYes(), $show_unassigned, true);
177		echo '</div></div>';
178
179		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_future">';
180		echo I18N::translate('Show research tasks that have a date in the future');
181		echo '</div><div class="col-sm-9">';
182		echo Bootstrap4::radioButtons('show_future', FunctionsEdit::optionsNoYes(), $show_future, true);
183		echo '</div></div>';
184	}
185}
186