xref: /webtrees/app/Module/ResearchTaskModule.php (revision a078385ab6e1f346ddf795e4d45f53f87ef9de08)
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\Functions\FunctionsEdit;
24use Fisharebest\Webtrees\GedcomRecord;
25use Fisharebest\Webtrees\Html;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\View;
28
29/**
30 * Class ResearchTaskModule
31 */
32class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
33	const DEFAULT_SHOW_OTHER      = '1';
34	const DEFAULT_SHOW_UNASSIGNED = '1';
35	const DEFAULT_SHOW_FUTURE     = '1';
36	const DEFAULT_BLOCK           = '1';
37
38	/** {@inheritdoc} */
39	public function getTitle() {
40		return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks');
41	}
42
43	/** {@inheritdoc} */
44	public function getDescription() {
45		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
46	}
47
48	/**
49	 * Generate the HTML content of this block.
50	 *
51	 * @param int      $block_id
52	 * @param bool     $template
53	 * @param string[] $cfg
54	 *
55	 * @return string
56	 */
57	public function getBlock($block_id, $template = true, $cfg = []) {
58		global $ctype, $WT_TREE;
59
60		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
61		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
62		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
63
64		foreach (['show_unassigned', 'show_other', 'show_future'] as $name) {
65			if (array_key_exists($name, $cfg)) {
66				$$name = $cfg[$name];
67			}
68		}
69
70		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
71
72		$xrefs = Database::prepare(
73			"SELECT DISTINCT d_gid FROM `##dates`" .
74			" WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
75		)->execute([
76			'tree_id' => $WT_TREE->getTreeId(),
77			'jd'      => $end_jd,
78		])->fetchOneColumn();
79
80		$records = array_map(function($xref) use ($WT_TREE) {
81			return GedcomRecord::getInstance($xref, $WT_TREE);
82		}, $xrefs);
83
84		$tasks = [];
85
86		foreach ($records as $record) {
87			foreach ($record->getFacts('_TODO') as $task) {
88				$user_name = $task->getAttribute('_WT_USER');
89
90				if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
91					$tasks[] = $task;
92				}
93			}
94		}
95
96		if (empty($records)) {
97			$content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
98		} else {
99			$content = View::make('blocks/research-tasks', ['tasks' => $tasks]);
100		}
101
102		if ($template) {
103			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
104				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
105			} else {
106				$config_url = '';
107			}
108
109			return View::make('blocks/template', [
110				'block'      => str_replace('_', '-', $this->getName()),
111				'id'         => $block_id,
112				'config_url' => $config_url,
113				'title'      => $this->getTitle(),
114				'content'    => $content,
115			]);
116		} else {
117			return $content;
118		}
119	}
120
121	/** {@inheritdoc} */
122	public function loadAjax() {
123		return false;
124	}
125
126	/** {@inheritdoc} */
127	public function isUserBlock() {
128		return true;
129	}
130
131	/** {@inheritdoc} */
132	public function isGedcomBlock() {
133		return true;
134	}
135
136	/**
137	 * An HTML form to edit block settings
138	 *
139	 * @param int $block_id
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