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