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; 238c2e8227SGreg Roach 248c2e8227SGreg Roach/** 258c2e8227SGreg Roach * Class ResearchTaskModule 268c2e8227SGreg Roach */ 27e2a378d3SGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface { 2864b9f5b2SGreg Roach const DEFAULT_SHOW_OTHER = '1'; 2964b9f5b2SGreg Roach const DEFAULT_SHOW_UNASSIGNED = '1'; 3064b9f5b2SGreg Roach const DEFAULT_SHOW_FUTURE = '1'; 3164b9f5b2SGreg Roach const DEFAULT_BLOCK = '1'; 3264b9f5b2SGreg Roach 338c2e8227SGreg Roach /** {@inheritdoc} */ 348c2e8227SGreg Roach public function getTitle() { 358c2e8227SGreg Roach return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks'); 368c2e8227SGreg Roach } 378c2e8227SGreg Roach 388c2e8227SGreg Roach /** {@inheritdoc} */ 398c2e8227SGreg Roach public function getDescription() { 408c2e8227SGreg Roach return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.'); 418c2e8227SGreg Roach } 428c2e8227SGreg Roach 4376692c8bSGreg Roach /** 4476692c8bSGreg Roach * Generate the HTML content of this block. 4576692c8bSGreg Roach * 4676692c8bSGreg Roach * @param int $block_id 4776692c8bSGreg Roach * @param bool $template 48727f238cSGreg Roach * @param string[] $cfg 4976692c8bSGreg Roach * 5076692c8bSGreg Roach * @return string 5176692c8bSGreg Roach */ 52a9430be8SGreg Roach public function getBlock($block_id, $template = true, $cfg = []): string { 5315d603e7SGreg Roach global $ctype, $WT_TREE; 548c2e8227SGreg Roach 5564b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 5664b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 5764b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 588c2e8227SGreg Roach 59*c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 608c2e8227SGreg Roach 618c2e8227SGreg Roach $end_jd = $show_future ? 99999999 : WT_CLIENT_JD; 6264b9f5b2SGreg Roach 6364b9f5b2SGreg Roach $xrefs = Database::prepare( 64c19940adSGreg Roach "SELECT DISTINCT d_gid FROM `##dates`" . 65c19940adSGreg Roach " WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd" 6613abd6f3SGreg Roach )->execute([ 67c19940adSGreg Roach 'tree_id' => $WT_TREE->getTreeId(), 6864b9f5b2SGreg Roach 'jd' => $end_jd, 6913abd6f3SGreg Roach ])->fetchOneColumn(); 7064b9f5b2SGreg Roach 71f7f4b984SGreg Roach $records = array_map(function ($xref) use ($WT_TREE) { 72f7f4b984SGreg Roach return GedcomRecord::getInstance($xref, $WT_TREE); 73f7f4b984SGreg Roach }, $xrefs); 74f7f4b984SGreg Roach 75f7f4b984SGreg Roach $tasks = []; 76f7f4b984SGreg Roach 77f7f4b984SGreg Roach foreach ($records as $record) { 78f7f4b984SGreg Roach foreach ($record->getFacts('_TODO') as $task) { 79f7f4b984SGreg Roach $user_name = $task->getAttribute('_WT_USER'); 80f7f4b984SGreg Roach 81f7f4b984SGreg Roach if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) { 82f7f4b984SGreg Roach $tasks[] = $task; 83f7f4b984SGreg Roach } 84f7f4b984SGreg Roach } 85f7f4b984SGreg Roach } 86f7f4b984SGreg Roach 87f7f4b984SGreg Roach if (empty($records)) { 88a078385aSGreg Roach $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 89a078385aSGreg Roach } else { 90225e381fSGreg Roach $content = view('blocks/research-tasks', ['tasks' => $tasks]); 918c2e8227SGreg Roach } 928c2e8227SGreg Roach 938c2e8227SGreg Roach if ($template) { 94397e599aSGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) { 95397e599aSGreg Roach $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 96397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 97397e599aSGreg Roach $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 988cbbfdceSGreg Roach } else { 998cbbfdceSGreg Roach $config_url = ''; 1008cbbfdceSGreg Roach } 1018cbbfdceSGreg Roach 102225e381fSGreg Roach return view('blocks/template', [ 1039c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1049c6524dcSGreg Roach 'id' => $block_id, 1058cbbfdceSGreg Roach 'config_url' => $config_url, 1069c6524dcSGreg Roach 'title' => $this->getTitle(), 1079c6524dcSGreg Roach 'content' => $content, 1089c6524dcSGreg Roach ]); 1098c2e8227SGreg Roach } else { 1108c2e8227SGreg Roach return $content; 1118c2e8227SGreg Roach } 1128c2e8227SGreg Roach } 1138c2e8227SGreg Roach 1148c2e8227SGreg Roach /** {@inheritdoc} */ 115a9430be8SGreg Roach public function loadAjax(): bool { 1168c2e8227SGreg Roach return false; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach 1198c2e8227SGreg Roach /** {@inheritdoc} */ 120a9430be8SGreg Roach public function isUserBlock(): bool { 1218c2e8227SGreg Roach return true; 1228c2e8227SGreg Roach } 1238c2e8227SGreg Roach 1248c2e8227SGreg Roach /** {@inheritdoc} */ 125a9430be8SGreg Roach public function isGedcomBlock(): bool { 1268c2e8227SGreg Roach return true; 1278c2e8227SGreg Roach } 1288c2e8227SGreg Roach 12976692c8bSGreg Roach /** 13076692c8bSGreg Roach * An HTML form to edit block settings 13176692c8bSGreg Roach * 13276692c8bSGreg Roach * @param int $block_id 133a9430be8SGreg Roach * 134a9430be8SGreg Roach * @return void 13576692c8bSGreg Roach */ 136be9a728cSGreg Roach public function configureBlock($block_id) { 137*c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 138e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other')); 139e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned')); 140e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future')); 141*c385536dSGreg Roach 142*c385536dSGreg Roach return; 1438c2e8227SGreg Roach } 1448c2e8227SGreg Roach 14564b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 14664b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 14764b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 1488c2e8227SGreg Roach 149*c385536dSGreg Roach echo view('blocks/research-tasks-config', [ 150*c385536dSGreg Roach 'show_future' => $show_future, 151*c385536dSGreg Roach 'show_other' => $show_other, 152*c385536dSGreg Roach 'show_unassigned' => $show_unassigned, 153*c385536dSGreg Roach ]); 1548c2e8227SGreg Roach 1558c2e8227SGreg Roach } 1568c2e8227SGreg Roach} 157