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\Family; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Capsule\Manager as DB; 27use Illuminate\Database\Query\JoinClause; 28use Illuminate\Support\Collection; 29use Symfony\Component\HttpFoundation\Request; 30 31/** 32 * Class ResearchTaskModule 33 */ 34class ResearchTaskModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 35{ 36 use ModuleBlockTrait; 37 38 private const DEFAULT_SHOW_OTHER = '1'; 39 private const DEFAULT_SHOW_UNASSIGNED = '1'; 40 private const DEFAULT_SHOW_FUTURE = '1'; 41 42 /** {@inheritdoc} */ 43 public function title(): string 44 { 45 /* I18N: Name of a module. Tasks that need further research. */ 46 return I18N::translate('Research tasks'); 47 } 48 49 /** {@inheritdoc} */ 50 public function description(): string 51 { 52 /* I18N: Description of “Research tasks” module */ 53 return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 54 } 55 56 /** 57 * Generate the HTML content of this block. 58 * 59 * @param Tree $tree 60 * @param int $block_id 61 * @param string $ctype 62 * @param string[] $cfg 63 * 64 * @return string 65 */ 66 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 67 { 68 $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 69 $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 70 $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 71 72 extract($cfg, EXTR_OVERWRITE); 73 74 $end_jd = $show_future ? 99999999 : WT_CLIENT_JD; 75 76 $individuals = $this->individualsWithTasks($tree, $end_jd); 77 $families = $this->familiesWithTasks($tree, $end_jd); 78 79 /** @var GedcomRecord[] $records */ 80 $records = $individuals->merge($families); 81 82 $tasks = []; 83 84 foreach ($records as $record) { 85 foreach ($record->facts(['_TODO']) as $task) { 86 $user_name = $task->attribute('_WT_USER'); 87 88 if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) { 89 $tasks[] = $task; 90 } 91 } 92 } 93 94 if (empty($records)) { 95 $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 96 } else { 97 $content = view('modules/todo/research-tasks', ['tasks' => $tasks]); 98 } 99 100 if ($ctype !== '') { 101 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 102 $config_url = route('tree-page-block-edit', [ 103 'block_id' => $block_id, 104 'ged' => $tree->name(), 105 ]); 106 } elseif ($ctype === 'user' && Auth::check()) { 107 $config_url = route('user-page-block-edit', [ 108 'block_id' => $block_id, 109 'ged' => $tree->name(), 110 ]); 111 } else { 112 $config_url = ''; 113 } 114 115 return view('modules/block-template', [ 116 'block' => str_replace('_', '-', $this->getName()), 117 'id' => $block_id, 118 'config_url' => $config_url, 119 'title' => $this->title(), 120 'content' => $content, 121 ]); 122 } 123 124 return $content; 125 } 126 127 /** {@inheritdoc} */ 128 public function loadAjax(): bool 129 { 130 return false; 131 } 132 133 /** {@inheritdoc} */ 134 public function isUserBlock(): bool 135 { 136 return true; 137 } 138 139 /** {@inheritdoc} */ 140 public function isGedcomBlock(): bool 141 { 142 return true; 143 } 144 145 /** 146 * Update the configuration for a block. 147 * 148 * @param Request $request 149 * @param int $block_id 150 * 151 * @return void 152 */ 153 public function saveBlockConfiguration(Request $request, int $block_id) 154 { 155 $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', '')); 156 $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', '')); 157 $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', '')); 158 } 159 160 /** 161 * An HTML form to edit block settings 162 * 163 * @param Tree $tree 164 * @param int $block_id 165 * 166 * @return void 167 */ 168 public function editBlockConfiguration(Tree $tree, int $block_id) 169 { 170 $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 171 $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 172 $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 173 174 echo view('modules/todo/config', [ 175 'show_future' => $show_future, 176 'show_other' => $show_other, 177 'show_unassigned' => $show_unassigned, 178 ]); 179 } 180 181 /** 182 * @param Tree $tree 183 * @param int $max_julian_day 184 * 185 * @return Collection 186 */ 187 private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 188 { 189 return DB::table('families') 190 ->join('dates', function (JoinClause $join): void { 191 $join 192 ->on('f_file', '=', 'd_file') 193 ->on('f_id', '=', 'd_gid'); 194 }) 195 ->where('f_file', '=', $tree->id()) 196 ->where('d_fact', '=', '_TODO') 197 ->where('d_julianday1', '<', $max_julian_day) 198 ->select(['families.*']) 199 ->distinct() 200 ->get() 201 ->map(Family::rowMapper()) 202 ->filter(GedcomRecord::accessFilter()); 203 } 204 205 /** 206 * @param Tree $tree 207 * @param int $max_julian_day 208 * 209 * @return Collection 210 */ 211 private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 212 { 213 return DB::table('individuals') 214 ->join('dates', function (JoinClause $join): void { 215 $join 216 ->on('i_file', '=', 'd_file') 217 ->on('i_id', '=', 'd_gid'); 218 }) 219 ->where('i_file', '=', $tree->id()) 220 ->where('d_fact', '=', '_TODO') 221 ->where('d_julianday1', '<', $max_julian_day) 222 ->select(['individuals.*']) 223 ->distinct() 224 ->get() 225 ->map(Individual::rowMapper()) 226 ->filter(GedcomRecord::accessFilter()); 227 } 228} 229