xref: /webtrees/app/Module/ResearchTaskModule.php (revision 9f2390a04226d0058d1862402c80d50fe6e79aa1)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\GedcomRecord;
22use Fisharebest\Webtrees\I18N;
23
24/**
25 * Class ResearchTaskModule
26 */
27class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
28	const DEFAULT_SHOW_OTHER      = '1';
29	const DEFAULT_SHOW_UNASSIGNED = '1';
30	const DEFAULT_SHOW_FUTURE     = '1';
31	const DEFAULT_BLOCK           = '1';
32
33	/** {@inheritdoc} */
34	public function getTitle() {
35		return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks');
36	}
37
38	/** {@inheritdoc} */
39	public function getDescription() {
40		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
41	}
42
43	/**
44	 * Generate the HTML content of this block.
45	 *
46	 * @param int      $block_id
47	 * @param bool     $template
48	 * @param string[] $cfg
49	 *
50	 * @return string
51	 */
52	public function getBlock($block_id, $template = true, $cfg = []): string {
53		global $ctype, $WT_TREE;
54
55		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
56		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
57		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
58
59		extract($cfg, EXTR_OVERWRITE);
60
61		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
62
63		$xrefs = Database::prepare(
64			"SELECT DISTINCT d_gid FROM `##dates`" .
65			" WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
66		)->execute([
67			'tree_id' => $WT_TREE->getTreeId(),
68			'jd'      => $end_jd,
69		])->fetchOneColumn();
70
71		$records = array_map(function ($xref) use ($WT_TREE) {
72			return GedcomRecord::getInstance($xref, $WT_TREE);
73		}, $xrefs);
74
75		$tasks = [];
76
77		foreach ($records as $record) {
78			foreach ($record->getFacts('_TODO') as $task) {
79				$user_name = $task->getAttribute('_WT_USER');
80
81				if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
82					$tasks[] = $task;
83				}
84			}
85		}
86
87		if (empty($records)) {
88			$content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
89		} else {
90			$content = view('blocks/research-tasks', ['tasks' => $tasks]);
91		}
92
93		if ($template) {
94			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
95				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
96			} elseif ($ctype === 'user' && Auth::check()) {
97				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
98			} else {
99				$config_url = '';
100			}
101
102			return view('blocks/template', [
103				'block'      => str_replace('_', '-', $this->getName()),
104				'id'         => $block_id,
105				'config_url' => $config_url,
106				'title'      => $this->getTitle(),
107				'content'    => $content,
108			]);
109		} else {
110			return $content;
111		}
112	}
113
114	/** {@inheritdoc} */
115	public function loadAjax(): bool {
116		return false;
117	}
118
119	/** {@inheritdoc} */
120	public function isUserBlock(): bool {
121		return true;
122	}
123
124	/** {@inheritdoc} */
125	public function isGedcomBlock(): bool {
126		return true;
127	}
128
129	/**
130	 * An HTML form to edit block settings
131	 *
132	 * @param int $block_id
133	 *
134	 * @return void
135	 */
136	public function configureBlock($block_id) {
137		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
138			$this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other'));
139			$this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned'));
140			$this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future'));
141
142			return;
143		}
144
145		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
146		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
147		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
148
149		echo view('blocks/research-tasks-config', [
150			'show_future'     => $show_future,
151			'show_other'      => $show_other,
152			'show_unassigned' => $show_unassigned,
153		]);
154
155	}
156}
157