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