18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 198c2e8227SGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 21*8e74ca62SGreg Roachuse Fisharebest\Webtrees\Family; 2264b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 24*8e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual; 25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 26*8e74ca62SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 27*8e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause; 28*8e74ca62SGreg Roachuse Illuminate\Support\Collection; 29a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 308c2e8227SGreg Roach 318c2e8227SGreg Roach/** 328c2e8227SGreg Roach * Class ResearchTaskModule 338c2e8227SGreg Roach */ 34c1010edaSGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleBlockInterface 35c1010edaSGreg Roach{ 3616d6367aSGreg Roach private const DEFAULT_SHOW_OTHER = '1'; 3716d6367aSGreg Roach private const DEFAULT_SHOW_UNASSIGNED = '1'; 3816d6367aSGreg Roach private const DEFAULT_SHOW_FUTURE = '1'; 3964b9f5b2SGreg Roach 408c2e8227SGreg Roach /** {@inheritdoc} */ 418f53f488SRico Sonntag public function getTitle(): string 42c1010edaSGreg Roach { 43bbb76c12SGreg Roach /* I18N: Name of a module. Tasks that need further research. */ 44bbb76c12SGreg Roach return I18N::translate('Research tasks'); 458c2e8227SGreg Roach } 468c2e8227SGreg Roach 478c2e8227SGreg Roach /** {@inheritdoc} */ 488f53f488SRico Sonntag public function getDescription(): string 49c1010edaSGreg Roach { 50bbb76c12SGreg Roach /* I18N: Description of “Research tasks” module */ 51bbb76c12SGreg Roach return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 528c2e8227SGreg Roach } 538c2e8227SGreg Roach 5476692c8bSGreg Roach /** 5576692c8bSGreg Roach * Generate the HTML content of this block. 5676692c8bSGreg Roach * 57e490cd80SGreg Roach * @param Tree $tree 5876692c8bSGreg Roach * @param int $block_id 595f2ae573SGreg Roach * @param string $ctype 60727f238cSGreg Roach * @param string[] $cfg 6176692c8bSGreg Roach * 6276692c8bSGreg Roach * @return string 6376692c8bSGreg Roach */ 645f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 65c1010edaSGreg Roach { 6664b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 6764b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 6864b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 698c2e8227SGreg Roach 70c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 718c2e8227SGreg Roach 728c2e8227SGreg Roach $end_jd = $show_future ? 99999999 : WT_CLIENT_JD; 7364b9f5b2SGreg Roach 74*8e74ca62SGreg Roach $individuals = $this->individualsWithTasks($tree, $end_jd); 75*8e74ca62SGreg Roach $families = $this->familiesWithTasks($tree, $end_jd); 7664b9f5b2SGreg Roach 77*8e74ca62SGreg Roach /** @var GedcomRecord[] $records */ 78*8e74ca62SGreg Roach $records = $individuals->merge($families); 79f7f4b984SGreg Roach 80f7f4b984SGreg Roach $tasks = []; 81f7f4b984SGreg Roach 82f7f4b984SGreg Roach foreach ($records as $record) { 838d0ebef0SGreg Roach foreach ($record->facts(['_TODO']) as $task) { 844852d81fSGreg Roach $user_name = $task->attribute('_WT_USER'); 85f7f4b984SGreg Roach 86f7f4b984SGreg Roach if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) { 87f7f4b984SGreg Roach $tasks[] = $task; 88f7f4b984SGreg Roach } 89f7f4b984SGreg Roach } 90f7f4b984SGreg Roach } 91f7f4b984SGreg Roach 92f7f4b984SGreg Roach if (empty($records)) { 93a078385aSGreg Roach $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 94a078385aSGreg Roach } else { 95147e99aaSGreg Roach $content = view('modules/todo/research-tasks', ['tasks' => $tasks]); 968c2e8227SGreg Roach } 978c2e8227SGreg Roach 986a8879feSGreg Roach if ($ctype !== '') { 99e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 100c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 101c1010edaSGreg Roach 'block_id' => $block_id, 102aa6f03bbSGreg Roach 'ged' => $tree->name(), 103c1010edaSGreg Roach ]); 104397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 105c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 106c1010edaSGreg Roach 'block_id' => $block_id, 107aa6f03bbSGreg Roach 'ged' => $tree->name(), 108c1010edaSGreg Roach ]); 1098cbbfdceSGreg Roach } else { 1108cbbfdceSGreg Roach $config_url = ''; 1118cbbfdceSGreg Roach } 1128cbbfdceSGreg Roach 113147e99aaSGreg Roach return view('modules/block-template', [ 1149c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1159c6524dcSGreg Roach 'id' => $block_id, 1168cbbfdceSGreg Roach 'config_url' => $config_url, 1179c6524dcSGreg Roach 'title' => $this->getTitle(), 1189c6524dcSGreg Roach 'content' => $content, 1199c6524dcSGreg Roach ]); 1208c2e8227SGreg Roach } 121b2ce94c6SRico Sonntag 122b2ce94c6SRico Sonntag return $content; 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach 1258c2e8227SGreg Roach /** {@inheritdoc} */ 126c1010edaSGreg Roach public function loadAjax(): bool 127c1010edaSGreg Roach { 1288c2e8227SGreg Roach return false; 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach 1318c2e8227SGreg Roach /** {@inheritdoc} */ 132c1010edaSGreg Roach public function isUserBlock(): bool 133c1010edaSGreg Roach { 1348c2e8227SGreg Roach return true; 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach 1378c2e8227SGreg Roach /** {@inheritdoc} */ 138c1010edaSGreg Roach public function isGedcomBlock(): bool 139c1010edaSGreg Roach { 1408c2e8227SGreg Roach return true; 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach 14376692c8bSGreg Roach /** 144a45f9889SGreg Roach * Update the configuration for a block. 145a45f9889SGreg Roach * 146a45f9889SGreg Roach * @param Request $request 147a45f9889SGreg Roach * @param int $block_id 148a45f9889SGreg Roach * 149a45f9889SGreg Roach * @return void 150a45f9889SGreg Roach */ 151a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 152a45f9889SGreg Roach { 153a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', '')); 154a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', '')); 155a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', '')); 156a45f9889SGreg Roach } 157a45f9889SGreg Roach 158a45f9889SGreg Roach /** 15976692c8bSGreg Roach * An HTML form to edit block settings 16076692c8bSGreg Roach * 161e490cd80SGreg Roach * @param Tree $tree 16276692c8bSGreg Roach * @param int $block_id 163a9430be8SGreg Roach * 164a9430be8SGreg Roach * @return void 16576692c8bSGreg Roach */ 166a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 167c1010edaSGreg Roach { 16864b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 16964b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 17064b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 1718c2e8227SGreg Roach 172147e99aaSGreg Roach echo view('modules/todo/config', [ 173c385536dSGreg Roach 'show_future' => $show_future, 174c385536dSGreg Roach 'show_other' => $show_other, 175c385536dSGreg Roach 'show_unassigned' => $show_unassigned, 176c385536dSGreg Roach ]); 1778c2e8227SGreg Roach } 178*8e74ca62SGreg Roach 179*8e74ca62SGreg Roach /** 180*8e74ca62SGreg Roach * @param Tree $tree 181*8e74ca62SGreg Roach * @param int $max_julian_day 182*8e74ca62SGreg Roach * 183*8e74ca62SGreg Roach * @return Collection 184*8e74ca62SGreg Roach */ 185*8e74ca62SGreg Roach private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 186*8e74ca62SGreg Roach { 187*8e74ca62SGreg Roach return DB::table('families') 188*8e74ca62SGreg Roach ->join('dates', function (JoinClause $join): void { 189*8e74ca62SGreg Roach $join 190*8e74ca62SGreg Roach ->on('f_file', '=', 'd_file') 191*8e74ca62SGreg Roach ->on('f_id', '=', 'd_gid'); 192*8e74ca62SGreg Roach }) 193*8e74ca62SGreg Roach ->where('f_file', '=', $tree->id()) 194*8e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 195*8e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 196*8e74ca62SGreg Roach ->select(['f_id', 'f_gedcom']) 197*8e74ca62SGreg Roach ->get() 198*8e74ca62SGreg Roach ->map(Family::rowMapper($tree)); 199*8e74ca62SGreg Roach } 200*8e74ca62SGreg Roach 201*8e74ca62SGreg Roach /** 202*8e74ca62SGreg Roach * @param Tree $tree 203*8e74ca62SGreg Roach * @param int $max_julian_day 204*8e74ca62SGreg Roach * 205*8e74ca62SGreg Roach * @return Collection 206*8e74ca62SGreg Roach */ 207*8e74ca62SGreg Roach private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 208*8e74ca62SGreg Roach { 209*8e74ca62SGreg Roach return DB::table('individuals') 210*8e74ca62SGreg Roach ->join('dates', function (JoinClause $join): void { 211*8e74ca62SGreg Roach $join 212*8e74ca62SGreg Roach ->on('i_file', '=', 'd_file') 213*8e74ca62SGreg Roach ->on('i_id', '=', 'd_gid'); 214*8e74ca62SGreg Roach }) 215*8e74ca62SGreg Roach ->where('i_file', '=', $tree->id()) 216*8e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 217*8e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 218*8e74ca62SGreg Roach ->select(['i_id', 'i_gedcom']) 219*8e74ca62SGreg Roach ->get() 220*8e74ca62SGreg Roach ->map(Individual::rowMapper($tree)); 221*8e74ca62SGreg Roach } 2228c2e8227SGreg Roach} 223