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