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