xref: /webtrees/app/Module/ResearchTaskModule.php (revision 147e99aae08229316226c46b32d24d4f8bce04e0)
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;
1964b9f5b2SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
2164b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class ResearchTaskModule
278c2e8227SGreg Roach */
28e2a378d3SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
2964b9f5b2SGreg Roach	const DEFAULT_SHOW_OTHER      = '1';
3064b9f5b2SGreg Roach	const DEFAULT_SHOW_UNASSIGNED = '1';
3164b9f5b2SGreg Roach	const DEFAULT_SHOW_FUTURE     = '1';
3264b9f5b2SGreg Roach	const DEFAULT_BLOCK           = '1';
3364b9f5b2SGreg Roach
348c2e8227SGreg Roach	/** {@inheritdoc} */
358c2e8227SGreg Roach	public function getTitle() {
368c2e8227SGreg Roach		return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
398c2e8227SGreg Roach	/** {@inheritdoc} */
408c2e8227SGreg Roach	public function getDescription() {
418c2e8227SGreg Roach		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
428c2e8227SGreg Roach	}
438c2e8227SGreg Roach
4476692c8bSGreg Roach	/**
4576692c8bSGreg Roach	 * Generate the HTML content of this block.
4676692c8bSGreg Roach	 *
47e490cd80SGreg Roach	 * @param Tree     $tree
4876692c8bSGreg Roach	 * @param int      $block_id
4976692c8bSGreg Roach	 * @param bool     $template
50727f238cSGreg Roach	 * @param string[] $cfg
5176692c8bSGreg Roach	 *
5276692c8bSGreg Roach	 * @return string
5376692c8bSGreg Roach	 */
54e490cd80SGreg Roach	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
55e490cd80SGreg Roach		global $ctype;
568c2e8227SGreg Roach
5764b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
5864b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
5964b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
608c2e8227SGreg Roach
61c385536dSGreg Roach		extract($cfg, EXTR_OVERWRITE);
628c2e8227SGreg Roach
638c2e8227SGreg Roach		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
6464b9f5b2SGreg Roach
6564b9f5b2SGreg Roach		$xrefs = Database::prepare(
66c19940adSGreg Roach			"SELECT DISTINCT d_gid FROM `##dates`" .
67c19940adSGreg Roach			" WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd"
6813abd6f3SGreg Roach		)->execute([
69e490cd80SGreg Roach			'tree_id' => $tree->getTreeId(),
7064b9f5b2SGreg Roach			'jd'      => $end_jd,
7113abd6f3SGreg Roach		])->fetchOneColumn();
7264b9f5b2SGreg Roach
73e490cd80SGreg Roach		$records = array_map(function ($xref) use ($tree) {
74e490cd80SGreg Roach			return GedcomRecord::getInstance($xref, $tree);
75f7f4b984SGreg Roach		}, $xrefs);
76f7f4b984SGreg Roach
77f7f4b984SGreg Roach		$tasks = [];
78f7f4b984SGreg Roach
79f7f4b984SGreg Roach		foreach ($records as $record) {
80f7f4b984SGreg Roach			foreach ($record->getFacts('_TODO') as $task) {
81f7f4b984SGreg Roach				$user_name = $task->getAttribute('_WT_USER');
82f7f4b984SGreg Roach
83f7f4b984SGreg Roach				if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) {
84f7f4b984SGreg Roach					$tasks[] = $task;
85f7f4b984SGreg Roach				}
86f7f4b984SGreg Roach			}
87f7f4b984SGreg Roach		}
88f7f4b984SGreg Roach
89f7f4b984SGreg Roach		if (empty($records)) {
90a078385aSGreg Roach			$content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
91a078385aSGreg Roach		} else {
92*147e99aaSGreg Roach			$content = view('modules/todo/research-tasks', ['tasks' => $tasks]);
938c2e8227SGreg Roach		}
948c2e8227SGreg Roach
958c2e8227SGreg Roach		if ($template) {
96e490cd80SGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
97e490cd80SGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
98397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
99e490cd80SGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
1008cbbfdceSGreg Roach			} else {
1018cbbfdceSGreg Roach				$config_url = '';
1028cbbfdceSGreg Roach			}
1038cbbfdceSGreg Roach
104*147e99aaSGreg Roach			return view('modules/block-template', [
1059c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1069c6524dcSGreg Roach				'id'         => $block_id,
1078cbbfdceSGreg Roach				'config_url' => $config_url,
1089c6524dcSGreg Roach				'title'      => $this->getTitle(),
1099c6524dcSGreg Roach				'content'    => $content,
1109c6524dcSGreg Roach			]);
1118c2e8227SGreg Roach		} else {
1128c2e8227SGreg Roach			return $content;
1138c2e8227SGreg Roach		}
1148c2e8227SGreg Roach	}
1158c2e8227SGreg Roach
1168c2e8227SGreg Roach	/** {@inheritdoc} */
117a9430be8SGreg Roach	public function loadAjax(): bool {
1188c2e8227SGreg Roach		return false;
1198c2e8227SGreg Roach	}
1208c2e8227SGreg Roach
1218c2e8227SGreg Roach	/** {@inheritdoc} */
122a9430be8SGreg Roach	public function isUserBlock(): bool {
1238c2e8227SGreg Roach		return true;
1248c2e8227SGreg Roach	}
1258c2e8227SGreg Roach
1268c2e8227SGreg Roach	/** {@inheritdoc} */
127a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1288c2e8227SGreg Roach		return true;
1298c2e8227SGreg Roach	}
1308c2e8227SGreg Roach
13176692c8bSGreg Roach	/**
13276692c8bSGreg Roach	 * An HTML form to edit block settings
13376692c8bSGreg Roach	 *
134e490cd80SGreg Roach	 * @param Tree $tree
13576692c8bSGreg Roach	 * @param int  $block_id
136a9430be8SGreg Roach	 *
137a9430be8SGreg Roach	 * @return void
13876692c8bSGreg Roach	 */
139e490cd80SGreg Roach	public function configureBlock(Tree $tree, int $block_id) {
140c385536dSGreg Roach		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
141e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other'));
142e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned'));
143e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future'));
144c385536dSGreg Roach
145c385536dSGreg Roach			return;
1468c2e8227SGreg Roach		}
1478c2e8227SGreg Roach
14864b9f5b2SGreg Roach		$show_other      = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER);
14964b9f5b2SGreg Roach		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED);
15064b9f5b2SGreg Roach		$show_future     = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE);
1518c2e8227SGreg Roach
152*147e99aaSGreg Roach		echo view('modules/todo/config', [
153c385536dSGreg Roach			'show_future'     => $show_future,
154c385536dSGreg Roach			'show_other'      => $show_other,
155c385536dSGreg Roach			'show_unassigned' => $show_unassigned,
156c385536dSGreg Roach		]);
1578c2e8227SGreg Roach
1588c2e8227SGreg Roach	}
1598c2e8227SGreg Roach}
160