xref: /webtrees/app/Module/ResearchTaskModule.php (revision c19940ad5f18dfbd2e52c2738e4c6c25f5212a55)
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\FunctionsDb;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
2364b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
278c2e8227SGreg Roachuse Rhumsaa\Uuid\Uuid;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class ResearchTaskModule
318c2e8227SGreg Roach */
32e2a378d3SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
3364b9f5b2SGreg Roach	const DEFAULT_SHOW_OTHER      = '1';
3464b9f5b2SGreg Roach	const DEFAULT_SHOW_UNASSIGNED = '1';
3564b9f5b2SGreg Roach	const DEFAULT_SHOW_FUTURE     = '1';
3664b9f5b2SGreg Roach	const DEFAULT_BLOCK           = '1';
3764b9f5b2SGreg Roach
388c2e8227SGreg Roach	/** {@inheritdoc} */
398c2e8227SGreg Roach	public function getTitle() {
408c2e8227SGreg Roach		return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks');
418c2e8227SGreg Roach	}
428c2e8227SGreg Roach
438c2e8227SGreg Roach	/** {@inheritdoc} */
448c2e8227SGreg Roach	public function getDescription() {
458c2e8227SGreg Roach		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * Generate the HTML content of this block.
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @param int      $block_id
5276692c8bSGreg Roach	 * @param bool     $template
53727f238cSGreg Roach	 * @param string[] $cfg
5476692c8bSGreg Roach	 *
5576692c8bSGreg Roach	 * @return string
5676692c8bSGreg Roach	 */
5776692c8bSGreg Roach	public function getBlock($block_id, $template = true, $cfg = array()) {
584b9ff166SGreg Roach		global $ctype, $controller, $WT_TREE;
598c2e8227SGreg Roach
6064b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
6164b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
6264b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
6364b9f5b2SGreg Roach		$block           = $this->getBlockSetting($block_id, 'block', self::DEFAULT_BLOCK);
648c2e8227SGreg Roach
658c2e8227SGreg Roach		foreach (array('show_unassigned', 'show_other', 'show_future', 'block') as $name) {
668c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
678c2e8227SGreg Roach				$$name = $cfg[$name];
688c2e8227SGreg Roach			}
698c2e8227SGreg Roach		}
708c2e8227SGreg Roach
718c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
728c2e8227SGreg Roach		$class = $this->getName() . '_block';
734b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
749353052eSGreg Roach			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
758c2e8227SGreg Roach		} else {
768c2e8227SGreg Roach			$title = '';
778c2e8227SGreg Roach		}
788c2e8227SGreg Roach		$title .= $this->getTitle();
798c2e8227SGreg Roach
808c2e8227SGreg Roach		$table_id = Uuid::uuid4(); // create a unique ID
818c2e8227SGreg Roach
828c2e8227SGreg Roach		$controller
838c2e8227SGreg Roach			->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
848c2e8227SGreg Roach			->addInlineJavascript('
858c2e8227SGreg Roach			jQuery("#' . $table_id . '").dataTable({
868c2e8227SGreg Roach				dom: \'t\',
878c2e8227SGreg Roach				' . I18N::datatablesI18N() . ',
888c2e8227SGreg Roach				autoWidth: false,
898c2e8227SGreg Roach				paginate: false,
908c2e8227SGreg Roach				lengthChange: false,
918c2e8227SGreg Roach				filter: false,
928c2e8227SGreg Roach				info: true,
938c2e8227SGreg Roach				jQueryUI: true,
948c2e8227SGreg Roach				columns: [
9564b9f5b2SGreg Roach					/* 0-DATE */     null,
968c2e8227SGreg Roach					/* 1-Date */     { dataSort: 0 },
9764b9f5b2SGreg Roach					/* 2-Record */   null,
9864b9f5b2SGreg Roach					/* 3-Username */ null,
9964b9f5b2SGreg Roach					/* 4-Text */     null
1008c2e8227SGreg Roach				]
1018c2e8227SGreg Roach			});
1028c2e8227SGreg Roach			jQuery("#' . $table_id . '").css("visibility", "visible");
1038c2e8227SGreg Roach			jQuery(".loading-image").css("display", "none");
1048c2e8227SGreg Roach		');
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach		$content = '';
1078c2e8227SGreg Roach		$content .= '<div class="loading-image">&nbsp;</div>';
1088c2e8227SGreg Roach		$content .= '<table id="' . $table_id . '" style="visibility:hidden;">';
1098c2e8227SGreg Roach		$content .= '<thead><tr>';
11064b9f5b2SGreg Roach		$content .= '<th hidden>DATE</th>';
111764a01d9SGreg Roach		$content .= '<th>' . GedcomTag::getLabel('DATE') . '</th>';
1128c2e8227SGreg Roach		$content .= '<th>' . I18N::translate('Record') . '</th>';
1138c2e8227SGreg Roach		$content .= '<th>' . I18N::translate('Username') . '</th>';
114764a01d9SGreg Roach		$content .= '<th>' . GedcomTag::getLabel('TEXT') . '</th>';
1158c2e8227SGreg Roach		$content .= '</tr></thead><tbody>';
1168c2e8227SGreg Roach
1178c2e8227SGreg Roach		$found  = false;
1188c2e8227SGreg Roach		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
11964b9f5b2SGreg Roach
12064b9f5b2SGreg Roach		$xrefs = Database::prepare(
121*c19940adSGreg Roach			"SELECT DISTINCT d_gid FROM `##dates`" .
122*c19940adSGreg Roach			" WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
12364b9f5b2SGreg Roach		)->execute(array(
124*c19940adSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
12564b9f5b2SGreg Roach			'jd'      => $end_jd,
12664b9f5b2SGreg Roach		))->fetchOneColumn();
12764b9f5b2SGreg Roach
12864b9f5b2SGreg Roach		$facts = array();
12964b9f5b2SGreg Roach		foreach ($xrefs as $xref) {
13064b9f5b2SGreg Roach			$record = GedcomRecord::getInstance($xref, $WT_TREE);
13164b9f5b2SGreg Roach			if ($record->canShow()) {
13264b9f5b2SGreg Roach				foreach ($record->getFacts('_TODO') as $fact) {
13364b9f5b2SGreg Roach					$facts[] = $fact;
13464b9f5b2SGreg Roach				}
13564b9f5b2SGreg Roach			}
13664b9f5b2SGreg Roach		}
13764b9f5b2SGreg Roach
13864b9f5b2SGreg Roach		foreach ($facts as $fact) {
1398c2e8227SGreg Roach			$record    = $fact->getParent();
1408c2e8227SGreg Roach			$user_name = $fact->getAttribute('_WT_USER');
1418c2e8227SGreg Roach			if ($user_name === Auth::user()->getUserName() || !$user_name && $show_unassigned || $user_name && $show_other) {
1428c2e8227SGreg Roach				$content .= '<tr>';
14364b9f5b2SGreg Roach				$content .= '<td hidden>' . $fact->getDate()->julianDay() . '</td>';
1448c2e8227SGreg Roach				$content .= '<td class="wrap">' . $fact->getDate()->display() . '</td>';
1458c2e8227SGreg Roach				$content .= '<td class="wrap"><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
1468c2e8227SGreg Roach				$content .= '<td class="wrap">' . $user_name . '</td>';
14764b9f5b2SGreg Roach				$content .= '<td class="wrap">' . $fact->getValue() . '</td>';
1488c2e8227SGreg Roach				$content .= '</tr>';
1498c2e8227SGreg Roach				$found = true;
1508c2e8227SGreg Roach			}
1518c2e8227SGreg Roach		}
1528c2e8227SGreg Roach
1538c2e8227SGreg Roach		$content .= '</tbody></table>';
1548c2e8227SGreg Roach		if (!$found) {
1558c2e8227SGreg Roach			$content .= '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
1568c2e8227SGreg Roach		}
1578c2e8227SGreg Roach
1588c2e8227SGreg Roach		if ($template) {
1598c2e8227SGreg Roach			if ($block) {
1608c2e8227SGreg Roach				$class .= ' small_inner_block';
1618c2e8227SGreg Roach			}
162cbc1590aSGreg Roach
1638c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1648c2e8227SGreg Roach		} else {
1658c2e8227SGreg Roach			return $content;
1668c2e8227SGreg Roach		}
1678c2e8227SGreg Roach	}
1688c2e8227SGreg Roach
1698c2e8227SGreg Roach	/** {@inheritdoc} */
1708c2e8227SGreg Roach	public function loadAjax() {
1718c2e8227SGreg Roach		return false;
1728c2e8227SGreg Roach	}
1738c2e8227SGreg Roach
1748c2e8227SGreg Roach	/** {@inheritdoc} */
1758c2e8227SGreg Roach	public function isUserBlock() {
1768c2e8227SGreg Roach		return true;
1778c2e8227SGreg Roach	}
1788c2e8227SGreg Roach
1798c2e8227SGreg Roach	/** {@inheritdoc} */
1808c2e8227SGreg Roach	public function isGedcomBlock() {
1818c2e8227SGreg Roach		return true;
1828c2e8227SGreg Roach	}
1838c2e8227SGreg Roach
18476692c8bSGreg Roach	/**
18576692c8bSGreg Roach	 * An HTML form to edit block settings
18676692c8bSGreg Roach	 *
18776692c8bSGreg Roach	 * @param int $block_id
18876692c8bSGreg Roach	 */
1898c2e8227SGreg Roach	public function configureBlock($block_id) {
1908c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
191e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other'));
192e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned'));
193e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future'));
194e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
1958c2e8227SGreg Roach		}
1968c2e8227SGreg Roach
19764b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
19864b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
19964b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
20064b9f5b2SGreg Roach		$block           = $this->getBlockSetting($block_id, 'block', self::DEFAULT_BLOCK);
2018c2e8227SGreg Roach
2028c2e8227SGreg Roach		?>
2038c2e8227SGreg Roach		<tr>
2048c2e8227SGreg Roach			<td colspan="2">
2058c2e8227SGreg 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.'); ?>
2068c2e8227SGreg 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.'); ?>
2078c2e8227SGreg Roach				<?php echo I18N::translate('Research tasks are stored using the custom GEDCOM tag “_TODO”. Other genealogy applications may not recognize this tag.'); ?>
2088c2e8227SGreg Roach			</td>
2098c2e8227SGreg Roach		</tr>
2108c2e8227SGreg Roach		<?php
2118c2e8227SGreg Roach
2128c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2138c2e8227SGreg Roach		echo I18N::translate('Show research tasks that are assigned to other users');
2148c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2153d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('show_other', $show_other);
2168c2e8227SGreg Roach		echo '</td></tr>';
2178c2e8227SGreg Roach
2188c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2198c2e8227SGreg Roach		echo I18N::translate('Show research tasks that are not assigned to any user');
2208c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2213d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('show_unassigned', $show_unassigned);
2228c2e8227SGreg Roach		echo '</td></tr>';
2238c2e8227SGreg Roach
2248c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2258c2e8227SGreg Roach		echo I18N::translate('Show research tasks that have a date in the future');
2268c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2273d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('show_future', $show_future);
2288c2e8227SGreg Roach		echo '</td></tr>';
2298c2e8227SGreg Roach
2308c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2318c2e8227SGreg Roach		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
2328c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2333d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('block', $block);
2348c2e8227SGreg Roach		echo '</td></tr>';
2358c2e8227SGreg Roach	}
2368c2e8227SGreg Roach}
237