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 */ 3449a243cbSGreg Roachclass ResearchTaskModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 35c1010edaSGreg Roach{ 3649a243cbSGreg Roach use ModuleBlockTrait; 3749a243cbSGreg 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 42961ec755SGreg Roach /** 43961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 44961ec755SGreg Roach * 45961ec755SGreg Roach * @return string 46961ec755SGreg Roach */ 4749a243cbSGreg Roach public function title(): string 48c1010edaSGreg Roach { 49bbb76c12SGreg Roach /* I18N: Name of a module. Tasks that need further research. */ 50bbb76c12SGreg Roach return I18N::translate('Research tasks'); 518c2e8227SGreg Roach } 528c2e8227SGreg Roach 53961ec755SGreg Roach /** 54961ec755SGreg Roach * A sentence describing what this module does. 55961ec755SGreg Roach * 56961ec755SGreg Roach * @return string 57961ec755SGreg Roach */ 5849a243cbSGreg Roach public function description(): string 59c1010edaSGreg Roach { 60bbb76c12SGreg Roach /* I18N: Description of “Research tasks” module */ 61bbb76c12SGreg Roach return I18N::translate('A list of tasks and activities that are linked to the family tree.'); 628c2e8227SGreg Roach } 638c2e8227SGreg Roach 6476692c8bSGreg Roach /** 6576692c8bSGreg Roach * Generate the HTML content of this block. 6676692c8bSGreg Roach * 67e490cd80SGreg Roach * @param Tree $tree 6876692c8bSGreg Roach * @param int $block_id 695f2ae573SGreg Roach * @param string $ctype 70727f238cSGreg Roach * @param string[] $cfg 7176692c8bSGreg Roach * 7276692c8bSGreg Roach * @return string 7376692c8bSGreg Roach */ 745f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 75c1010edaSGreg Roach { 7664b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 7764b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 7864b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 798c2e8227SGreg Roach 80c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 818c2e8227SGreg Roach 828c2e8227SGreg Roach $end_jd = $show_future ? 99999999 : WT_CLIENT_JD; 8364b9f5b2SGreg Roach 848e74ca62SGreg Roach $individuals = $this->individualsWithTasks($tree, $end_jd); 858e74ca62SGreg Roach $families = $this->familiesWithTasks($tree, $end_jd); 8664b9f5b2SGreg Roach 878e74ca62SGreg Roach /** @var GedcomRecord[] $records */ 888e74ca62SGreg Roach $records = $individuals->merge($families); 89f7f4b984SGreg Roach 90f7f4b984SGreg Roach $tasks = []; 91f7f4b984SGreg Roach 92f7f4b984SGreg Roach foreach ($records as $record) { 938d0ebef0SGreg Roach foreach ($record->facts(['_TODO']) as $task) { 944852d81fSGreg Roach $user_name = $task->attribute('_WT_USER'); 95f7f4b984SGreg Roach 96f7f4b984SGreg Roach if ($user_name === Auth::user()->getUserName() || empty($user_name) && $show_unassigned || !empty($user_name) && $show_other) { 97f7f4b984SGreg Roach $tasks[] = $task; 98f7f4b984SGreg Roach } 99f7f4b984SGreg Roach } 100f7f4b984SGreg Roach } 101f7f4b984SGreg Roach 102f7f4b984SGreg Roach if (empty($records)) { 103a078385aSGreg Roach $content = '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 104a078385aSGreg Roach } else { 105147e99aaSGreg Roach $content = view('modules/todo/research-tasks', ['tasks' => $tasks]); 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach 1086a8879feSGreg Roach if ($ctype !== '') { 109e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 110c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 111c1010edaSGreg Roach 'block_id' => $block_id, 112aa6f03bbSGreg Roach 'ged' => $tree->name(), 113c1010edaSGreg Roach ]); 114397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 115c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 116c1010edaSGreg Roach 'block_id' => $block_id, 117aa6f03bbSGreg Roach 'ged' => $tree->name(), 118c1010edaSGreg Roach ]); 1198cbbfdceSGreg Roach } else { 1208cbbfdceSGreg Roach $config_url = ''; 1218cbbfdceSGreg Roach } 1228cbbfdceSGreg Roach 123147e99aaSGreg Roach return view('modules/block-template', [ 124*26684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1259c6524dcSGreg Roach 'id' => $block_id, 1268cbbfdceSGreg Roach 'config_url' => $config_url, 12749a243cbSGreg Roach 'title' => $this->title(), 1289c6524dcSGreg Roach 'content' => $content, 1299c6524dcSGreg Roach ]); 1308c2e8227SGreg Roach } 131b2ce94c6SRico Sonntag 132b2ce94c6SRico Sonntag return $content; 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach 1358c2e8227SGreg Roach /** {@inheritdoc} */ 136c1010edaSGreg Roach public function loadAjax(): bool 137c1010edaSGreg Roach { 1388c2e8227SGreg Roach return false; 1398c2e8227SGreg Roach } 1408c2e8227SGreg Roach 1418c2e8227SGreg Roach /** {@inheritdoc} */ 142c1010edaSGreg Roach public function isUserBlock(): bool 143c1010edaSGreg Roach { 1448c2e8227SGreg Roach return true; 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach 1478c2e8227SGreg Roach /** {@inheritdoc} */ 148c1010edaSGreg Roach public function isGedcomBlock(): bool 149c1010edaSGreg Roach { 1508c2e8227SGreg Roach return true; 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach 15376692c8bSGreg Roach /** 154a45f9889SGreg Roach * Update the configuration for a block. 155a45f9889SGreg Roach * 156a45f9889SGreg Roach * @param Request $request 157a45f9889SGreg Roach * @param int $block_id 158a45f9889SGreg Roach * 159a45f9889SGreg Roach * @return void 160a45f9889SGreg Roach */ 161a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 162a45f9889SGreg Roach { 163a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_other', $request->get('show_other', '')); 164a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_unassigned', $request->get('show_unassigned', '')); 165a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_future', $request->get('show_future', '')); 166a45f9889SGreg Roach } 167a45f9889SGreg Roach 168a45f9889SGreg Roach /** 16976692c8bSGreg Roach * An HTML form to edit block settings 17076692c8bSGreg Roach * 171e490cd80SGreg Roach * @param Tree $tree 17276692c8bSGreg Roach * @param int $block_id 173a9430be8SGreg Roach * 174a9430be8SGreg Roach * @return void 17576692c8bSGreg Roach */ 176a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 177c1010edaSGreg Roach { 17864b9f5b2SGreg Roach $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 17964b9f5b2SGreg Roach $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 18064b9f5b2SGreg Roach $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 1818c2e8227SGreg Roach 182147e99aaSGreg Roach echo view('modules/todo/config', [ 183c385536dSGreg Roach 'show_future' => $show_future, 184c385536dSGreg Roach 'show_other' => $show_other, 185c385536dSGreg Roach 'show_unassigned' => $show_unassigned, 186c385536dSGreg Roach ]); 1878c2e8227SGreg Roach } 1888e74ca62SGreg Roach 1898e74ca62SGreg Roach /** 1908e74ca62SGreg Roach * @param Tree $tree 1918e74ca62SGreg Roach * @param int $max_julian_day 1928e74ca62SGreg Roach * 1938e74ca62SGreg Roach * @return Collection 1948e74ca62SGreg Roach */ 1958e74ca62SGreg Roach private function familiesWithTasks(Tree $tree, int $max_julian_day): Collection 1968e74ca62SGreg Roach { 1978e74ca62SGreg Roach return DB::table('families') 1988e74ca62SGreg Roach ->join('dates', function (JoinClause $join): void { 1998e74ca62SGreg Roach $join 2008e74ca62SGreg Roach ->on('f_file', '=', 'd_file') 2018e74ca62SGreg Roach ->on('f_id', '=', 'd_gid'); 2028e74ca62SGreg Roach }) 2038e74ca62SGreg Roach ->where('f_file', '=', $tree->id()) 2048e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2058e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 206c0804649SGreg Roach ->select(['families.*']) 207c0804649SGreg Roach ->distinct() 2088e74ca62SGreg Roach ->get() 2094146fabcSGreg Roach ->map(Family::rowMapper()) 2104146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2118e74ca62SGreg Roach } 2128e74ca62SGreg Roach 2138e74ca62SGreg Roach /** 2148e74ca62SGreg Roach * @param Tree $tree 2158e74ca62SGreg Roach * @param int $max_julian_day 2168e74ca62SGreg Roach * 2178e74ca62SGreg Roach * @return Collection 2188e74ca62SGreg Roach */ 2198e74ca62SGreg Roach private function individualsWithTasks(Tree $tree, int $max_julian_day): Collection 2208e74ca62SGreg Roach { 2218e74ca62SGreg Roach return DB::table('individuals') 2228e74ca62SGreg Roach ->join('dates', function (JoinClause $join): void { 2238e74ca62SGreg Roach $join 2248e74ca62SGreg Roach ->on('i_file', '=', 'd_file') 2258e74ca62SGreg Roach ->on('i_id', '=', 'd_gid'); 2268e74ca62SGreg Roach }) 2278e74ca62SGreg Roach ->where('i_file', '=', $tree->id()) 2288e74ca62SGreg Roach ->where('d_fact', '=', '_TODO') 2298e74ca62SGreg Roach ->where('d_julianday1', '<', $max_julian_day) 230c0804649SGreg Roach ->select(['individuals.*']) 231c0804649SGreg Roach ->distinct() 2328e74ca62SGreg Roach ->get() 2334146fabcSGreg Roach ->map(Individual::rowMapper()) 2344146fabcSGreg Roach ->filter(GedcomRecord::accessFilter()); 2358e74ca62SGreg Roach } 2368c2e8227SGreg Roach} 237