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