1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Elements\DateValueToday; 24use Fisharebest\Webtrees\Elements\ResearchTask; 25use Fisharebest\Webtrees\Elements\WebtreesUser; 26use Fisharebest\Webtrees\Family; 27use Fisharebest\Webtrees\GedcomRecord; 28use Fisharebest\Webtrees\I18N; 29use Fisharebest\Webtrees\Individual; 30use Fisharebest\Webtrees\Registry; 31use Fisharebest\Webtrees\Tree; 32use Illuminate\Database\Capsule\Manager as DB; 33use Illuminate\Database\Query\JoinClause; 34use Illuminate\Support\Collection; 35use Illuminate\Support\Str; 36use Psr\Http\Message\ServerRequestInterface; 37 38/** 39 * Class ResearchTaskModule 40 */ 41class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface 42{ 43 use ModuleBlockTrait; 44 45 private const DEFAULT_SHOW_OTHER = '1'; 46 private const DEFAULT_SHOW_UNASSIGNED = '1'; 47 private const DEFAULT_SHOW_FUTURE = '1'; 48 49 // 31 DEC 9999 50 private const MAXIMUM_JULIAN_DAY = 5373484; 51 52 // Pagination 53 private const LIMIT_LOW = 10; 54 private const LIMIT_HIGH = 20; 55 56 /** 57 * Early initialisation. Called before most of the middleware. 58 */ 59 public function boot(): void 60 { 61 Registry::elementFactory()->registerTags([ 62 'FAM:_TODO' => new ResearchTask(I18N::translate('Research task')), 63 'FAM:_TODO:DATE' => new DateValueToday(I18N::translate('Date')), 64 'FAM:_TODO:_WT_USER' => new WebtreesUser(I18N::translate('User')), 65 'INDI:_TODO' => new ResearchTask(I18N::translate('Research task')), 66 'INDI:_TODO:DATE' => new DateValueToday(I18N::translate('Date')), 67 'INDI:_TODO:_WT_USER' => new WebtreesUser(I18N::translate('User')), 68 ]); 69 70 Registry::elementFactory()->make('FAM')->subtag('_TODO', '0:M'); 71 Registry::elementFactory()->make('INDI')->subtag('_TODO', '0:M'); 72 } 73 74 /** 75 * A sentence describing what this module does. 76 * 77 * @return string 78 */ 79 public function description(): string 80 { 81 /* I18N: Description of “Research tasks” module */ 82 return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 83 } 84 85 /** 86 * Generate the HTML content of this block. 87 * 88 * @param Tree $tree 89 * @param int $block_id 90 * @param string $context 91 * @param array<string,string> $config 92 * 93 * @return string 94 */ 95 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 96 { 97 $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 98 $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 99 $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 100 101 extract($config, EXTR_OVERWRITE); 102 103 $end_jd = $show_future ? self::MAXIMUM_JULIAN_DAY : Registry::timestampFactory()->now()->julianDay(); 104 $individuals = $this->individualsWithTasks($tree, $end_jd); 105 $families = $this->familiesWithTasks($tree, $end_jd); 106 107 $records = $individuals->merge($families); 108 109 $tasks = new Collection(); 110 111 foreach ($records as $record) { 112 foreach ($record->facts(['_TODO']) as $task) { 113 $user_name = $task->attribute('_WT_USER'); 114 115 if ($user_name === Auth::user()->userName()) { 116 // Tasks belonging to us. 117 $tasks->add($task); 118 } elseif ($user_name === '' && $show_unassigned) { 119 // Tasks belonging to nobody. 120 $tasks->add($task); 121 } elseif ($user_name !== '' && $show_other) { 122 // Tasks belonging to others. 123 $tasks->add($task); 124 } 125 } 126 } 127 128 if ($records->isEmpty()) { 129 $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 130 } else { 131 $content = view('modules/todo/research-tasks', [ 132 'limit_low' => self::LIMIT_LOW, 133 'limit_high' => self::LIMIT_HIGH, 134 'tasks' => $tasks, 135 ]); 136 } 137 138 if ($context !== self::CONTEXT_EMBED) { 139 return view('modules/block-template', [ 140 'block' => Str::kebab($this->name()), 141 'id' => $block_id, 142 'config_url' => $this->configUrl($tree, $context, $block_id), 143 'title' => $this->title(), 144 'content' => $content, 145 ]); 146 } 147 148 return $content; 149 } 150 151 /** 152 * @param Tree $tree 153 * @param int $max_julian_day 154 * 155 * @return Collection<int,Individual> 156 */ 157 private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 158 { 159 return DB::table('individuals') 160 ->join('dates', static function (JoinClause $join): void { 161 $join 162 ->on('i_file', '=', 'd_file') 163 ->on('i_id', '=', 'd_gid'); 164 }) 165 ->where('i_file', '=', $tree->id()) 166 ->where('d_fact', '=', '_TODO') 167 ->where('d_julianday1', '<', $max_julian_day) 168 ->select(['individuals.*']) 169 ->distinct() 170 ->get() 171 ->map(Registry::individualFactory()->mapper($tree)) 172 ->filter(GedcomRecord::accessFilter()); 173 } 174 175 /** 176 * @param Tree $tree 177 * @param int $max_julian_day 178 * 179 * @return Collection<int,Family> 180 */ 181 private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 182 { 183 return DB::table('families') 184 ->join('dates', static function (JoinClause $join): void { 185 $join 186 ->on('f_file', '=', 'd_file') 187 ->on('f_id', '=', 'd_gid'); 188 }) 189 ->where('f_file', '=', $tree->id()) 190 ->where('d_fact', '=', '_TODO') 191 ->where('d_julianday1', '<', $max_julian_day) 192 ->select(['families.*']) 193 ->distinct() 194 ->get() 195 ->map(Registry::familyFactory()->mapper($tree)) 196 ->filter(GedcomRecord::accessFilter()); 197 } 198 199 /** 200 * How should this module be identified in the control panel, etc.? 201 * 202 * @return string 203 */ 204 public function title(): string 205 { 206 /* I18N: Name of a module. Tasks that need further research. */ 207 return I18N::translate('Research tasks'); 208 } 209 210 /** 211 * Should this block load asynchronously using AJAX? 212 * 213 * Simple blocks are faster in-line, more complex ones can be loaded later. 214 * 215 * @return bool 216 */ 217 public function loadAjax(): bool 218 { 219 return false; 220 } 221 222 /** 223 * Can this block be shown on the user’s home page? 224 * 225 * @return bool 226 */ 227 public function isUserBlock(): bool 228 { 229 return true; 230 } 231 232 /** 233 * Can this block be shown on the tree’s home page? 234 * 235 * @return bool 236 */ 237 public function isTreeBlock(): bool 238 { 239 return true; 240 } 241 242 /** 243 * Update the configuration for a block. 244 * 245 * @param ServerRequestInterface $request 246 * @param int $block_id 247 * 248 * @return void 249 */ 250 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 251 { 252 $params = (array) $request->getParsedBody(); 253 254 $this->setBlockSetting($block_id, 'show_other', $params['show_other']); 255 $this->setBlockSetting($block_id, 'show_unassigned', $params['show_unassigned']); 256 $this->setBlockSetting($block_id, 'show_future', $params['show_future']); 257 } 258 259 /** 260 * An HTML form to edit block settings 261 * 262 * @param Tree $tree 263 * @param int $block_id 264 * 265 * @return string 266 */ 267 public function editBlockConfiguration(Tree $tree, int $block_id): string 268 { 269 $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 270 $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 271 $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 272 273 return view('modules/todo/config', [ 274 'show_future' => $show_future, 275 'show_other' => $show_other, 276 'show_unassigned' => $show_unassigned, 277 ]); 278 } 279} 280