xref: /webtrees/app/Module/ResearchTaskModule.php (revision 13abd6f3a37322f885d85df150e105d27ad81f8d)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4369c0ce6SGreg Roach * Copyright (C) 2016 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;
1964b9f5b2SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
2264b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
268c2e8227SGreg Roachuse Rhumsaa\Uuid\Uuid;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class ResearchTaskModule
308c2e8227SGreg Roach */
31e2a378d3SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
3264b9f5b2SGreg Roach	const DEFAULT_SHOW_OTHER      = '1';
3364b9f5b2SGreg Roach	const DEFAULT_SHOW_UNASSIGNED = '1';
3464b9f5b2SGreg Roach	const DEFAULT_SHOW_FUTURE     = '1';
3564b9f5b2SGreg Roach	const DEFAULT_BLOCK           = '1';
3664b9f5b2SGreg Roach
378c2e8227SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function getTitle() {
398c2e8227SGreg Roach		return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
428c2e8227SGreg Roach	/** {@inheritdoc} */
438c2e8227SGreg Roach	public function getDescription() {
448c2e8227SGreg Roach		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
458c2e8227SGreg Roach	}
468c2e8227SGreg Roach
4776692c8bSGreg Roach	/**
4876692c8bSGreg Roach	 * Generate the HTML content of this block.
4976692c8bSGreg Roach	 *
5076692c8bSGreg Roach	 * @param int      $block_id
5176692c8bSGreg Roach	 * @param bool     $template
52727f238cSGreg Roach	 * @param string[] $cfg
5376692c8bSGreg Roach	 *
5476692c8bSGreg Roach	 * @return string
5576692c8bSGreg Roach	 */
56*13abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
574b9ff166SGreg Roach		global $ctype, $controller, $WT_TREE;
588c2e8227SGreg Roach
5964b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
6064b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
6164b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
6264b9f5b2SGreg Roach		$block           = $this->getBlockSetting($block_id, 'block', self::DEFAULT_BLOCK);
638c2e8227SGreg Roach
64*13abd6f3SGreg Roach		foreach (['show_unassigned', 'show_other', 'show_future', 'block'] as $name) {
658c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
668c2e8227SGreg Roach				$$name = $cfg[$name];
678c2e8227SGreg Roach			}
688c2e8227SGreg Roach		}
698c2e8227SGreg Roach
708c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
718c2e8227SGreg Roach		$class = $this->getName() . '_block';
724b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
736d1c8039SGreg Roach			$title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
748c2e8227SGreg Roach		} else {
758c2e8227SGreg Roach			$title = '';
768c2e8227SGreg Roach		}
778c2e8227SGreg Roach		$title .= $this->getTitle();
788c2e8227SGreg Roach
798c2e8227SGreg Roach		$table_id = Uuid::uuid4(); // create a unique ID
808c2e8227SGreg Roach
818c2e8227SGreg Roach		$controller
828c2e8227SGreg Roach			->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
838c2e8227SGreg Roach			->addInlineJavascript('
848c2e8227SGreg Roach			jQuery("#' . $table_id . '").dataTable({
858c2e8227SGreg Roach				dom: \'t\',
868c2e8227SGreg Roach				' . I18N::datatablesI18N() . ',
878c2e8227SGreg Roach				autoWidth: false,
888c2e8227SGreg Roach				paginate: false,
898c2e8227SGreg Roach				lengthChange: false,
908c2e8227SGreg Roach				filter: false,
918c2e8227SGreg Roach				info: true,
928c2e8227SGreg Roach				jQueryUI: true,
938c2e8227SGreg Roach				columns: [
940c597aa6SGreg Roach					null,
950c597aa6SGreg Roach					null,
960c597aa6SGreg Roach					null,
970c597aa6SGreg Roach					null
988c2e8227SGreg Roach				]
998c2e8227SGreg Roach			});
1008c2e8227SGreg Roach			jQuery("#' . $table_id . '").css("visibility", "visible");
1018c2e8227SGreg Roach			jQuery(".loading-image").css("display", "none");
1028c2e8227SGreg Roach		');
1038c2e8227SGreg Roach
1048c2e8227SGreg Roach		$content = '';
1058c2e8227SGreg Roach		$content .= '<div class="loading-image">&nbsp;</div>';
1068c2e8227SGreg Roach		$content .= '<table id="' . $table_id . '" style="visibility:hidden;">';
1078c2e8227SGreg Roach		$content .= '<thead><tr>';
108764a01d9SGreg Roach		$content .= '<th>' . GedcomTag::getLabel('DATE') . '</th>';
1098c2e8227SGreg Roach		$content .= '<th>' . I18N::translate('Record') . '</th>';
1108c2e8227SGreg Roach		$content .= '<th>' . I18N::translate('Username') . '</th>';
111764a01d9SGreg Roach		$content .= '<th>' . GedcomTag::getLabel('TEXT') . '</th>';
1128c2e8227SGreg Roach		$content .= '</tr></thead><tbody>';
1138c2e8227SGreg Roach
1148c2e8227SGreg Roach		$found  = false;
1158c2e8227SGreg Roach		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
11664b9f5b2SGreg Roach
11764b9f5b2SGreg Roach		$xrefs = Database::prepare(
118c19940adSGreg Roach			"SELECT DISTINCT d_gid FROM `##dates`" .
119c19940adSGreg Roach			" WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
120*13abd6f3SGreg Roach		)->execute([
121c19940adSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
12264b9f5b2SGreg Roach			'jd'      => $end_jd,
123*13abd6f3SGreg Roach		])->fetchOneColumn();
12464b9f5b2SGreg Roach
125*13abd6f3SGreg Roach		$facts = [];
12664b9f5b2SGreg Roach		foreach ($xrefs as $xref) {
12764b9f5b2SGreg Roach			$record = GedcomRecord::getInstance($xref, $WT_TREE);
12864b9f5b2SGreg Roach			if ($record->canShow()) {
12964b9f5b2SGreg Roach				foreach ($record->getFacts('_TODO') as $fact) {
13064b9f5b2SGreg Roach					$facts[] = $fact;
13164b9f5b2SGreg Roach				}
13264b9f5b2SGreg Roach			}
13364b9f5b2SGreg Roach		}
13464b9f5b2SGreg Roach
13564b9f5b2SGreg Roach		foreach ($facts as $fact) {
1368c2e8227SGreg Roach			$record    = $fact->getParent();
1378c2e8227SGreg Roach			$user_name = $fact->getAttribute('_WT_USER');
1388c2e8227SGreg Roach			if ($user_name === Auth::user()->getUserName() || !$user_name && $show_unassigned || $user_name && $show_other) {
1398c2e8227SGreg Roach				$content .= '<tr>';
1400c597aa6SGreg Roach				$content .= '<td data-sort="' . $fact->getDate()->julianDay() . '">' . $fact->getDate()->display() . '</td>';
1410c597aa6SGreg Roach				$content .= '<td data-sort="' . Filter::escapeHtml($record->getSortName()) . '"><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
1420c597aa6SGreg Roach				$content .= '<td>' . $user_name . '</td>';
1430c597aa6SGreg Roach				$content .= '<td dir="auto">' . $fact->getValue() . '</td>';
1448c2e8227SGreg Roach				$content .= '</tr>';
1458c2e8227SGreg Roach				$found = true;
1468c2e8227SGreg Roach			}
1478c2e8227SGreg Roach		}
1488c2e8227SGreg Roach
1498c2e8227SGreg Roach		$content .= '</tbody></table>';
1508c2e8227SGreg Roach		if (!$found) {
1518c2e8227SGreg Roach			$content .= '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
1528c2e8227SGreg Roach		}
1538c2e8227SGreg Roach
1548c2e8227SGreg Roach		if ($template) {
1558c2e8227SGreg Roach			if ($block) {
1568c2e8227SGreg Roach				$class .= ' small_inner_block';
1578c2e8227SGreg Roach			}
158cbc1590aSGreg Roach
1598c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1608c2e8227SGreg Roach		} else {
1618c2e8227SGreg Roach			return $content;
1628c2e8227SGreg Roach		}
1638c2e8227SGreg Roach	}
1648c2e8227SGreg Roach
1658c2e8227SGreg Roach	/** {@inheritdoc} */
1668c2e8227SGreg Roach	public function loadAjax() {
1678c2e8227SGreg Roach		return false;
1688c2e8227SGreg Roach	}
1698c2e8227SGreg Roach
1708c2e8227SGreg Roach	/** {@inheritdoc} */
1718c2e8227SGreg Roach	public function isUserBlock() {
1728c2e8227SGreg Roach		return true;
1738c2e8227SGreg Roach	}
1748c2e8227SGreg Roach
1758c2e8227SGreg Roach	/** {@inheritdoc} */
1768c2e8227SGreg Roach	public function isGedcomBlock() {
1778c2e8227SGreg Roach		return true;
1788c2e8227SGreg Roach	}
1798c2e8227SGreg Roach
18076692c8bSGreg Roach	/**
18176692c8bSGreg Roach	 * An HTML form to edit block settings
18276692c8bSGreg Roach	 *
18376692c8bSGreg Roach	 * @param int $block_id
18476692c8bSGreg Roach	 */
1858c2e8227SGreg Roach	public function configureBlock($block_id) {
1868c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
187e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other'));
188e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned'));
189e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future'));
190e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
1918c2e8227SGreg Roach		}
1928c2e8227SGreg Roach
19364b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
19464b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
19564b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
19664b9f5b2SGreg Roach		$block           = $this->getBlockSetting($block_id, 'block', self::DEFAULT_BLOCK);
1978c2e8227SGreg Roach
1988c2e8227SGreg Roach		?>
1998c2e8227SGreg Roach		<tr>
2008c2e8227SGreg Roach			<td colspan="2">
2018c2e8227SGreg Roach				<?php echo 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.'); ?>
2028c2e8227SGreg Roach				<?php echo 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.'); ?>
2038c2e8227SGreg Roach				<?php echo I18N::translate('Research tasks are stored using the custom GEDCOM tag “_TODO”. Other genealogy applications may not recognize this tag.'); ?>
2048c2e8227SGreg Roach			</td>
2058c2e8227SGreg Roach		</tr>
2068c2e8227SGreg Roach		<?php
2078c2e8227SGreg Roach
2088c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2098c2e8227SGreg Roach		echo I18N::translate('Show research tasks that are assigned to other users');
2108c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2113d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('show_other', $show_other);
2128c2e8227SGreg Roach		echo '</td></tr>';
2138c2e8227SGreg Roach
2148c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2158c2e8227SGreg Roach		echo I18N::translate('Show research tasks that are not assigned to any user');
2168c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2173d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('show_unassigned', $show_unassigned);
2188c2e8227SGreg Roach		echo '</td></tr>';
2198c2e8227SGreg Roach
2208c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2218c2e8227SGreg Roach		echo I18N::translate('Show research tasks that have a date in the future');
2228c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2233d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('show_future', $show_future);
2248c2e8227SGreg Roach		echo '</td></tr>';
2258c2e8227SGreg Roach
2268c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2278c2e8227SGreg Roach		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
2288c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2293d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('block', $block);
2308c2e8227SGreg Roach		echo '</td></tr>';
2318c2e8227SGreg Roach	}
2328c2e8227SGreg Roach}
233