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