xref: /webtrees/app/Module/ResearchTaskModule.php (revision 397e599a3131684b8a335ebfddcfe63828b9f51b)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
178c2e8227SGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
2064b9f5b2SGreg Roachuse Fisharebest\Webtrees\Database;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
2364b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
24cc5ab399SGreg Roachuse Fisharebest\Webtrees\Html;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class ResearchTaskModule
298c2e8227SGreg Roach */
30e2a378d3SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
3164b9f5b2SGreg Roach	const DEFAULT_SHOW_OTHER      = '1';
3264b9f5b2SGreg Roach	const DEFAULT_SHOW_UNASSIGNED = '1';
3364b9f5b2SGreg Roach	const DEFAULT_SHOW_FUTURE     = '1';
3464b9f5b2SGreg Roach	const DEFAULT_BLOCK           = '1';
3564b9f5b2SGreg Roach
368c2e8227SGreg Roach	/** {@inheritdoc} */
378c2e8227SGreg Roach	public function getTitle() {
388c2e8227SGreg Roach		return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks');
398c2e8227SGreg Roach	}
408c2e8227SGreg Roach
418c2e8227SGreg Roach	/** {@inheritdoc} */
428c2e8227SGreg Roach	public function getDescription() {
438c2e8227SGreg Roach		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
448c2e8227SGreg Roach	}
458c2e8227SGreg Roach
4676692c8bSGreg Roach	/**
4776692c8bSGreg Roach	 * Generate the HTML content of this block.
4876692c8bSGreg Roach	 *
4976692c8bSGreg Roach	 * @param int      $block_id
5076692c8bSGreg Roach	 * @param bool     $template
51727f238cSGreg Roach	 * @param string[] $cfg
5276692c8bSGreg Roach	 *
5376692c8bSGreg Roach	 * @return string
5476692c8bSGreg Roach	 */
55a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
5615d603e7SGreg Roach		global $ctype, $WT_TREE;
578c2e8227SGreg Roach
5864b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
5964b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
6064b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
618c2e8227SGreg Roach
6215d603e7SGreg Roach		foreach (['show_unassigned', 'show_other', 'show_future'] as $name) {
638c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
648c2e8227SGreg Roach				$$name = $cfg[$name];
658c2e8227SGreg Roach			}
668c2e8227SGreg Roach		}
678c2e8227SGreg Roach
688c2e8227SGreg Roach		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
6964b9f5b2SGreg Roach
7064b9f5b2SGreg Roach		$xrefs = Database::prepare(
71c19940adSGreg Roach			"SELECT DISTINCT d_gid FROM `##dates`" .
72c19940adSGreg Roach			" WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
7313abd6f3SGreg Roach		)->execute([
74c19940adSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
7564b9f5b2SGreg Roach			'jd'      => $end_jd,
7613abd6f3SGreg Roach		])->fetchOneColumn();
7764b9f5b2SGreg Roach
78f7f4b984SGreg Roach		$records = array_map(function ($xref) use ($WT_TREE) {
79f7f4b984SGreg Roach			return GedcomRecord::getInstance($xref, $WT_TREE);
80f7f4b984SGreg Roach		}, $xrefs);
81f7f4b984SGreg Roach
82f7f4b984SGreg Roach		$tasks = [];
83f7f4b984SGreg Roach
84f7f4b984SGreg Roach		foreach ($records as $record) {
85f7f4b984SGreg Roach			foreach ($record->getFacts('_TODO') as $task) {
86f7f4b984SGreg Roach				$user_name = $task->getAttribute('_WT_USER');
87f7f4b984SGreg Roach
88f7f4b984SGreg Roach				if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
89f7f4b984SGreg Roach					$tasks[] = $task;
90f7f4b984SGreg Roach				}
91f7f4b984SGreg Roach			}
92f7f4b984SGreg Roach		}
93f7f4b984SGreg Roach
94f7f4b984SGreg Roach		if (empty($records)) {
95a078385aSGreg Roach			$content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
96a078385aSGreg Roach		} else {
97225e381fSGreg Roach			$content = view('blocks/research-tasks', ['tasks' => $tasks]);
988c2e8227SGreg Roach		}
998c2e8227SGreg Roach
1008c2e8227SGreg Roach		if ($template) {
101*397e599aSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
102*397e599aSGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
103*397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
104*397e599aSGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1058cbbfdceSGreg Roach			} else {
1068cbbfdceSGreg Roach				$config_url = '';
1078cbbfdceSGreg Roach			}
1088cbbfdceSGreg Roach
109225e381fSGreg Roach			return view('blocks/template', [
1109c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1119c6524dcSGreg Roach				'id'         => $block_id,
1128cbbfdceSGreg Roach				'config_url' => $config_url,
1139c6524dcSGreg Roach				'title'      => $this->getTitle(),
1149c6524dcSGreg Roach				'content'    => $content,
1159c6524dcSGreg Roach			]);
1168c2e8227SGreg Roach		} else {
1178c2e8227SGreg Roach			return $content;
1188c2e8227SGreg Roach		}
1198c2e8227SGreg Roach	}
1208c2e8227SGreg Roach
1218c2e8227SGreg Roach	/** {@inheritdoc} */
122a9430be8SGreg Roach	public function loadAjax(): bool {
1238c2e8227SGreg Roach		return false;
1248c2e8227SGreg Roach	}
1258c2e8227SGreg Roach
1268c2e8227SGreg Roach	/** {@inheritdoc} */
127a9430be8SGreg Roach	public function isUserBlock(): bool {
1288c2e8227SGreg Roach		return true;
1298c2e8227SGreg Roach	}
1308c2e8227SGreg Roach
1318c2e8227SGreg Roach	/** {@inheritdoc} */
132a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1338c2e8227SGreg Roach		return true;
1348c2e8227SGreg Roach	}
1358c2e8227SGreg Roach
13676692c8bSGreg Roach	/**
13776692c8bSGreg Roach	 * An HTML form to edit block settings
13876692c8bSGreg Roach	 *
13976692c8bSGreg Roach	 * @param int $block_id
140a9430be8SGreg Roach	 *
141a9430be8SGreg Roach	 * @return void
14276692c8bSGreg Roach	 */
143be9a728cSGreg Roach	public function configureBlock($block_id) {
1448c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
145e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other'));
146e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned'));
147e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future'));
1488c2e8227SGreg Roach		}
1498c2e8227SGreg Roach
15064b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
15164b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
15264b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1538c2e8227SGreg Roach
1548c2e8227SGreg Roach		?>
15515d603e7SGreg Roach		<p>
15615d603e7SGreg Roach			<?= 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.') ?>
15715d603e7SGreg Roach			<?= 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.') ?>
15815d603e7SGreg Roach			<?= I18N::translate('Research tasks are stored using the custom GEDCOM tag “_TODO”. Other genealogy applications may not recognize this tag.') ?>
15915d603e7SGreg Roach		</p>
1608c2e8227SGreg Roach		<?php
1618c2e8227SGreg Roach
16215d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_other">';
1638c2e8227SGreg Roach		echo I18N::translate('Show research tasks that are assigned to other users');
16415d603e7SGreg Roach		echo '</div><div class="col-sm-9">';
16515d603e7SGreg Roach		echo Bootstrap4::radioButtons('show_other', FunctionsEdit::optionsNoYes(), $show_other, true);
16615d603e7SGreg Roach		echo '</div></div>';
1678c2e8227SGreg Roach
16815d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_unassigned">';
1698c2e8227SGreg Roach		echo I18N::translate('Show research tasks that are not assigned to any user');
17015d603e7SGreg Roach		echo '</div><div class="col-sm-9">';
17115d603e7SGreg Roach		echo Bootstrap4::radioButtons('show_unassigned', FunctionsEdit::optionsNoYes(), $show_unassigned, true);
17215d603e7SGreg Roach		echo '</div></div>';
1738c2e8227SGreg Roach
17415d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_future">';
1758c2e8227SGreg Roach		echo I18N::translate('Show research tasks that have a date in the future');
17615d603e7SGreg Roach		echo '</div><div class="col-sm-9">';
17715d603e7SGreg Roach		echo Bootstrap4::radioButtons('show_future', FunctionsEdit::optionsNoYes(), $show_future, true);
17815d603e7SGreg Roach		echo '</div></div>';
1798c2e8227SGreg Roach	}
1808c2e8227SGreg Roach}
181