xref: /webtrees/app/Module/ResearchTaskModule.php (revision 9c6524dcf4555157077b94309ec75cc0781acd78)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 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;
2115d603e7SGreg Roachuse Fisharebest\Webtrees\Datatables;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
2315d603e7SGreg Roachuse Fisharebest\Webtrees\FontAwesome;
243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
2564b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
26cc5ab399SGreg Roachuse Fisharebest\Webtrees\Html;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
29*9c6524dcSGreg Roachuse Fisharebest\Webtrees\View;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class ResearchTaskModule
338c2e8227SGreg Roach */
34e2a378d3SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
3564b9f5b2SGreg Roach	const DEFAULT_SHOW_OTHER      = '1';
3664b9f5b2SGreg Roach	const DEFAULT_SHOW_UNASSIGNED = '1';
3764b9f5b2SGreg Roach	const DEFAULT_SHOW_FUTURE     = '1';
3864b9f5b2SGreg Roach	const DEFAULT_BLOCK           = '1';
3964b9f5b2SGreg Roach
408c2e8227SGreg Roach	/** {@inheritdoc} */
418c2e8227SGreg Roach	public function getTitle() {
428c2e8227SGreg Roach		return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks');
438c2e8227SGreg Roach	}
448c2e8227SGreg Roach
458c2e8227SGreg Roach	/** {@inheritdoc} */
468c2e8227SGreg Roach	public function getDescription() {
478c2e8227SGreg Roach		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
488c2e8227SGreg Roach	}
498c2e8227SGreg Roach
5076692c8bSGreg Roach	/**
5176692c8bSGreg Roach	 * Generate the HTML content of this block.
5276692c8bSGreg Roach	 *
5376692c8bSGreg Roach	 * @param int      $block_id
5476692c8bSGreg Roach	 * @param bool     $template
55727f238cSGreg Roach	 * @param string[] $cfg
5676692c8bSGreg Roach	 *
5776692c8bSGreg Roach	 * @return string
5876692c8bSGreg Roach	 */
5913abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
6015d603e7SGreg Roach		global $ctype, $WT_TREE;
618c2e8227SGreg Roach
6264b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
6364b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
6464b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
658c2e8227SGreg Roach
6615d603e7SGreg Roach		foreach (['show_unassigned', 'show_other', 'show_future'] as $name) {
678c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
688c2e8227SGreg Roach				$$name = $cfg[$name];
698c2e8227SGreg Roach			}
708c2e8227SGreg Roach		}
718c2e8227SGreg Roach
728c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
738c2e8227SGreg Roach		$class = $this->getName() . '_block';
744b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
7515d603e7SGreg Roach			$title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' ';
768c2e8227SGreg Roach		} else {
778c2e8227SGreg Roach			$title = '';
788c2e8227SGreg Roach		}
798c2e8227SGreg Roach		$title .= $this->getTitle();
808c2e8227SGreg Roach
818c2e8227SGreg Roach		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
8264b9f5b2SGreg Roach
8364b9f5b2SGreg Roach		$xrefs = Database::prepare(
84c19940adSGreg Roach			"SELECT DISTINCT d_gid FROM `##dates`" .
85c19940adSGreg Roach			" WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
8613abd6f3SGreg Roach		)->execute([
87c19940adSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
8864b9f5b2SGreg Roach			'jd'      => $end_jd,
8913abd6f3SGreg Roach		])->fetchOneColumn();
9064b9f5b2SGreg Roach
9115d603e7SGreg Roach		$content = '';
9215d603e7SGreg Roach		$content .= '<table ' . Datatables::researchTaskTableAttributes() . '>';
9315d603e7SGreg Roach		$content .= '<thead><tr>';
9415d603e7SGreg Roach		$content .= '<th>' . I18N::translate('Date') . '</th>';
9515d603e7SGreg Roach		$content .= '<th>' . I18N::translate('Record') . '</th>';
9615d603e7SGreg Roach		$content .= '<th>' . I18N::translate('Username') . '</th>';
9715d603e7SGreg Roach		$content .= '<th>' . I18N::translate('Research task') . '</th>';
9815d603e7SGreg Roach		$content .= '</tr></thead><tbody>';
9915d603e7SGreg Roach
10064b9f5b2SGreg Roach		foreach ($xrefs as $xref) {
10164b9f5b2SGreg Roach			$record = GedcomRecord::getInstance($xref, $WT_TREE);
10264b9f5b2SGreg Roach			if ($record->canShow()) {
10364b9f5b2SGreg Roach				foreach ($record->getFacts('_TODO') as $fact) {
1048c2e8227SGreg Roach					$user_name = $fact->getAttribute('_WT_USER');
1058c2e8227SGreg Roach					if ($user_name === Auth::user()->getUserName() || !$user_name && $show_unassigned || $user_name && $show_other) {
1068c2e8227SGreg Roach						$content .= '<tr>';
1070c597aa6SGreg Roach						$content .= '<td data-sort="' . $fact->getDate()->julianDay() . '">' . $fact->getDate()->display() . '</td>';
108cc5ab399SGreg Roach						$content .= '<td data-sort="' . Html::escape($record->getSortName()) . '"><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
1090c597aa6SGreg Roach						$content .= '<td>' . $user_name . '</td>';
1100c597aa6SGreg Roach						$content .= '<td dir="auto">' . $fact->getValue() . '</td>';
1118c2e8227SGreg Roach						$content .= '</tr>';
11215d603e7SGreg Roach					}
11315d603e7SGreg Roach				}
1148c2e8227SGreg Roach			}
1158c2e8227SGreg Roach		}
1168c2e8227SGreg Roach
1178c2e8227SGreg Roach		$content .= '</tbody></table>';
11815d603e7SGreg Roach		if (empty($xrefs)) {
1198c2e8227SGreg Roach			$content .= '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
1208c2e8227SGreg Roach		}
1218c2e8227SGreg Roach
1228c2e8227SGreg Roach		if ($template) {
123*9c6524dcSGreg Roach			return View::make('blocks/template', [
124*9c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
125*9c6524dcSGreg Roach				'id'         => $block_id,
126*9c6524dcSGreg Roach				'config_url' => '',
127*9c6524dcSGreg Roach				'title'      => $this->getTitle(),
128*9c6524dcSGreg Roach				'content'    => $content,
129*9c6524dcSGreg Roach			]);
1308c2e8227SGreg Roach		} else {
1318c2e8227SGreg Roach			return $content;
1328c2e8227SGreg Roach		}
1338c2e8227SGreg Roach	}
1348c2e8227SGreg Roach
1358c2e8227SGreg Roach	/** {@inheritdoc} */
1368c2e8227SGreg Roach	public function loadAjax() {
1378c2e8227SGreg Roach		return false;
1388c2e8227SGreg Roach	}
1398c2e8227SGreg Roach
1408c2e8227SGreg Roach	/** {@inheritdoc} */
1418c2e8227SGreg Roach	public function isUserBlock() {
1428c2e8227SGreg Roach		return true;
1438c2e8227SGreg Roach	}
1448c2e8227SGreg Roach
1458c2e8227SGreg Roach	/** {@inheritdoc} */
1468c2e8227SGreg Roach	public function isGedcomBlock() {
1478c2e8227SGreg Roach		return true;
1488c2e8227SGreg Roach	}
1498c2e8227SGreg Roach
15076692c8bSGreg Roach	/**
15176692c8bSGreg Roach	 * An HTML form to edit block settings
15276692c8bSGreg Roach	 *
15376692c8bSGreg Roach	 * @param int $block_id
15476692c8bSGreg Roach	 */
1558c2e8227SGreg Roach	public function configureBlock($block_id) {
1568c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
157e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other'));
158e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned'));
159e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future'));
1608c2e8227SGreg Roach		}
1618c2e8227SGreg Roach
16264b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
16364b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
16464b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1658c2e8227SGreg Roach
1668c2e8227SGreg Roach		?>
16715d603e7SGreg Roach		<p>
16815d603e7SGreg 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.') ?>
16915d603e7SGreg 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.') ?>
17015d603e7SGreg Roach			<?= I18N::translate('Research tasks are stored using the custom GEDCOM tag “_TODO”. Other genealogy applications may not recognize this tag.') ?>
17115d603e7SGreg Roach		</p>
1728c2e8227SGreg Roach		<?php
1738c2e8227SGreg Roach
17415d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_other">';
1758c2e8227SGreg Roach		echo I18N::translate('Show research tasks that are assigned to other users');
17615d603e7SGreg Roach		echo '</div><div class="col-sm-9">';
17715d603e7SGreg Roach		echo Bootstrap4::radioButtons('show_other', FunctionsEdit::optionsNoYes(), $show_other, true);
17815d603e7SGreg Roach		echo '</div></div>';
1798c2e8227SGreg Roach
18015d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_unassigned">';
1818c2e8227SGreg Roach		echo I18N::translate('Show research tasks that are not assigned to any user');
18215d603e7SGreg Roach		echo '</div><div class="col-sm-9">';
18315d603e7SGreg Roach		echo Bootstrap4::radioButtons('show_unassigned', FunctionsEdit::optionsNoYes(), $show_unassigned, true);
18415d603e7SGreg Roach		echo '</div></div>';
1858c2e8227SGreg Roach
18615d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_future">';
1878c2e8227SGreg Roach		echo I18N::translate('Show research tasks that have a date in the future');
18815d603e7SGreg Roach		echo '</div><div class="col-sm-9">';
18915d603e7SGreg Roach		echo Bootstrap4::radioButtons('show_future', FunctionsEdit::optionsNoYes(), $show_future, true);
19015d603e7SGreg Roach		echo '</div></div>';
1918c2e8227SGreg Roach	}
1928c2e8227SGreg Roach}
193