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; 218e74ca62SGreg Roachuse Fisharebest\Webtrees\Family; 2264b9f5b2SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 248e74ca62SGreg Roachuse Fisharebest\Webtrees\Individual; 25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 268e74ca62SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 278e74ca62SGreg Roachuse Illuminate\Database\Query\JoinClause; 288e74ca62SGreg Roachuse Illuminate\Support\Collection; 29a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 308c2e8227SGreg Roach 318c2e8227SGreg Roach/** 328c2e8227SGreg Roach * Class ResearchTaskModule 338c2e8227SGreg Roach */ 34*49a243cbSGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 35c1010edaSGreg Roach{ 36*49a243cbSGreg Roach use ModuleBlockTrait; 37*49a243cbSGreg Roach 3816d6367aSGreg Roach private const DEFAULT_SHOW_OTHER = '1'; 3916d6367aSGreg Roach private const DEFAULT_SHOW_UNASSIGNED = '1'; 4016d6367aSGreg Roach private const DEFAULT_SHOW_FUTURE = '1'; 4164b9f5b2SGreg Roach 428c2e8227SGreg Roach /** {@inheritdoc} */ 43*49a243cbSGreg Roach public function title(): string 44c1010edaSGreg Roach { 45bbb76c12SGreg Roach /* I18N: Name of a module. Tasks that need further research. */ 46bbb76c12SGreg Roach return I18N::translate('Research tasks'); 478c2e8227SGreg Roach } 488c2e8227SGreg Roach 498c2e8227SGreg Roach /** {@inheritdoc} */ 50*49a243cbSGreg Roach public function description(): string 51c1010edaSGreg Roach { 52bbb76c12SGreg Roach /* I18N: Description of “Research tasks” module */ 53bbb76c12SGreg Roach return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 548c2e8227SGreg Roach } 558c2e8227SGreg Roach 5676692c8bSGreg Roach /** 5776692c8bSGreg Roach * Generate the HTML content of this block. 5876692c8bSGreg Roach * 59e490cd80SGreg Roach * @param Tree $tree 6076692c8bSGreg Roach * @param int $block_id 615f2ae573SGreg Roach * @param string $ctype 62727f238cSGreg Roach * @param string[] $cfg 6376692c8bSGreg Roach * 6476692c8bSGreg Roach * @return string 6576692c8bSGreg Roach */ 665f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 67c1010edaSGreg Roach { 6864b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 6964b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 7064b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 718c2e8227SGreg Roach 72c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 738c2e8227SGreg Roach 748c2e8227SGreg Roach $end_jd = $show_future ? 99999999 : WT_CLIENT_JD; 7564b9f5b2SGreg Roach 768e74ca62SGreg Roach $individuals = $this->individualsWithTasks($tree, $end_jd); 778e74ca62SGreg Roach $families = $this->familiesWithTasks($tree, $end_jd); 7864b9f5b2SGreg Roach 798e74ca62SGreg Roach /** @var GedcomRecord[] $records */ 808e74ca62SGreg Roach $records = $individuals->merge($families); 81f7f4b984SGreg Roach 82f7f4b984SGreg Roach $tasks = []; 83f7f4b984SGreg Roach 84f7f4b984SGreg Roach foreach ($records as $record) { 858d0ebef0SGreg Roach foreach ($record->facts(['_TODO']) as $task) { 864852d81fSGreg Roach $user_name = $task->attribute('_WT_USER'); 87f7f4b984SGreg Roach 88f7f4b984SGreg Roach if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) { 89f7f4b984SGreg Roach $tasks[] = $task; 90f7f4b984SGreg Roach } 91f7f4b984SGreg Roach } 92f7f4b984SGreg Roach } 93f7f4b984SGreg Roach 94f7f4b984SGreg Roach if (empty($records)) { 95a078385aSGreg Roach $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 96a078385aSGreg Roach } else { 97147e99aaSGreg Roach $content = view('modules/todo/research-tasks', ['tasks' => $tasks]); 988c2e8227SGreg Roach } 998c2e8227SGreg Roach 1006a8879feSGreg Roach if ($ctype !== '') { 101e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 102c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 103c1010edaSGreg Roach 'block_id' => $block_id, 104aa6f03bbSGreg Roach 'ged' => $tree->name(), 105c1010edaSGreg Roach ]); 106397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 107c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 108c1010edaSGreg Roach 'block_id' => $block_id, 109aa6f03bbSGreg Roach 'ged' => $tree->name(), 110c1010edaSGreg Roach ]); 1118cbbfdceSGreg Roach } else { 1128cbbfdceSGreg Roach $config_url = ''; 1138cbbfdceSGreg Roach } 1148cbbfdceSGreg Roach 115147e99aaSGreg Roach return view('modules/block-template', [ 1169c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1179c6524dcSGreg Roach 'id' => $block_id, 1188cbbfdceSGreg Roach 'config_url' => $config_url, 119*49a243cbSGreg Roach 'title' => $this->title(), 1209c6524dcSGreg Roach 'content' => $content, 1219c6524dcSGreg Roach ]); 1228c2e8227SGreg Roach } 123b2ce94c6SRico Sonntag 124b2ce94c6SRico Sonntag return $content; 1258c2e8227SGreg Roach } 1268c2e8227SGreg Roach 1278c2e8227SGreg Roach /** {@inheritdoc} */ 128c1010edaSGreg Roach public function loadAjax(): bool 129c1010edaSGreg Roach { 1308c2e8227SGreg Roach return false; 1318c2e8227SGreg Roach } 1328c2e8227SGreg Roach 1338c2e8227SGreg Roach /** {@inheritdoc} */ 134c1010edaSGreg Roach public function isUserBlock(): bool 135c1010edaSGreg Roach { 1368c2e8227SGreg Roach return true; 1378c2e8227SGreg Roach } 1388c2e8227SGreg Roach 1398c2e8227SGreg Roach /** {@inheritdoc} */ 140c1010edaSGreg Roach public function isGedcomBlock(): bool 141c1010edaSGreg Roach { 1428c2e8227SGreg Roach return true; 1438c2e8227SGreg Roach } 1448c2e8227SGreg Roach 14576692c8bSGreg Roach /** 146a45f9889SGreg Roach * Update the configuration for a block. 147a45f9889SGreg Roach * 148a45f9889SGreg Roach * @param Request $request 149a45f9889SGreg Roach * @param int $block_id 150a45f9889SGreg Roach * 151a45f9889SGreg Roach * @return void 152a45f9889SGreg Roach */ 153a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 154a45f9889SGreg Roach { 155a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', '')); 156a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', '')); 157a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', '')); 158a45f9889SGreg Roach } 159a45f9889SGreg Roach 160a45f9889SGreg Roach /** 16176692c8bSGreg Roach * An HTML form to edit block settings 16276692c8bSGreg Roach * 163e490cd80SGreg Roach * @param Tree $tree 16476692c8bSGreg Roach * @param int $block_id 165a9430be8SGreg Roach * 166a9430be8SGreg Roach * @return void 16776692c8bSGreg Roach */ 168a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 169c1010edaSGreg Roach { 17064b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 17164b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 17264b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 1738c2e8227SGreg Roach 174147e99aaSGreg Roach echo view('modules/todo/config', [ 175c385536dSGreg Roach 'show_future' => $show_future, 176c385536dSGreg Roach 'show_other' => $show_other, 177c385536dSGreg Roach 'show_unassigned' => $show_unassigned, 178c385536dSGreg Roach ]); 1798c2e8227SGreg Roach } 1808e74ca62SGreg Roach 1818e74ca62SGreg Roach /** 1828e74ca62SGreg Roach * @param Tree $tree 1838e74ca62SGreg Roach * @param int $max_julian_day 1848e74ca62SGreg Roach * 1858e74ca62SGreg Roach * @return Collection 1868e74ca62SGreg Roach */ 1878e74ca62SGreg Roach private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 1888e74ca62SGreg Roach { 1898e74ca62SGreg Roach return DB::table('families') 1908e74ca62SGreg Roach ->join('dates', function (JoinClause $join): void { 1918e74ca62SGreg Roach $join 1928e74ca62SGreg Roach ->on('f_file', '=', 'd_file') 1938e74ca62SGreg Roach ->on('f_id', '=', 'd_gid'); 1948e74ca62SGreg Roach }) 1958e74ca62SGreg Roach ->where('f_file', '=', $tree->id()) 1968e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 1978e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 198c0804649SGreg Roach ->select(['families.*']) 199c0804649SGreg Roach ->distinct() 2008e74ca62SGreg Roach ->get() 2014146fabcSGreg Roach ->map(Family::rowMapper()) 2024146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2038e74ca62SGreg Roach } 2048e74ca62SGreg Roach 2058e74ca62SGreg Roach /** 2068e74ca62SGreg Roach * @param Tree $tree 2078e74ca62SGreg Roach * @param int $max_julian_day 2088e74ca62SGreg Roach * 2098e74ca62SGreg Roach * @return Collection 2108e74ca62SGreg Roach */ 2118e74ca62SGreg Roach private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 2128e74ca62SGreg Roach { 2138e74ca62SGreg Roach return DB::table('individuals') 2148e74ca62SGreg Roach ->join('dates', function (JoinClause $join): void { 2158e74ca62SGreg Roach $join 2168e74ca62SGreg Roach ->on('i_file', '=', 'd_file') 2178e74ca62SGreg Roach ->on('i_id', '=', 'd_gid'); 2188e74ca62SGreg Roach }) 2198e74ca62SGreg Roach ->where('i_file', '=', $tree->id()) 2208e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2218e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 222c0804649SGreg Roach ->select(['individuals.*']) 223c0804649SGreg Roach ->distinct() 2248e74ca62SGreg Roach ->get() 2254146fabcSGreg Roach ->map(Individual::rowMapper()) 2264146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2278e74ca62SGreg Roach } 2288c2e8227SGreg Roach} 229